• Bitmap


    加载大图片到内存

    public class MainActivity extends Activity {

     private ImageView iv_img;

     private int windowHeight;

     private int windowWidth;

     @Override

     protected void onCreate(Bundle savedInstanceState) {

      super.onCreate(savedInstanceState);

      setContentView(R.layout.activity_main);

      this.iv_img = (ImageView) this.findViewById(R.id.iv_img);

      WindowManager manager = (WindowManager) getSystemService(Context.WINDOW_SERVICE);

      // 第一种获取手机屏幕宽高的方法

      this.windowHeight = manager.getDefaultDisplay().getHeight();

      this.windowWidth = manager.getDefaultDisplay().getWidth();

      System.out.println("手机宽 :" + this.windowWidth);

      System.out.println("手机高 :" + this.windowHeight);

      // 第二种获取手机屏幕宽高的方法,但是getSize()是从 API Level 13才有的方法

      // Point outSize = new Point();

      // manager.getDefaultDisplay().getSize(outSize );

      // this.windowWidth = outSize.x;

      // this.windowHeight = outSize.y;

     }

     public void load1(View view) {

      String src = "mnt/sdcard/DSC.jpg";

      Bitmap bitmap = BitmapFactory.decodeFile(src);

      this.iv_img.setImageBitmap(bitmap);

     }

     public void load2(View view) {

      String src = "mnt/sdcard/DSC.jpg";

      // 图片解析的配置

      Options options = new Options();

      // 不去真正解析图片,只是获取图片的宽高

      options.inJustDecodeBounds = true;

      BitmapFactory.decodeFile(src, options);

      int imageWidth = options.outWidth;

      int imageHeight = options.outHeight;

      System.out.println("图片宽 :" + imageWidth);

      System.out.println("图片高 :" + imageHeight);

      int scaleX = imageWidth / this.windowWidth;

      int scaleY = imageHeight / this.windowHeight;

      int scale = 1;

      if (scaleX >= scaleY && scaleX >= 1) {

       // 水平方向的缩放比例比竖直方向的缩放比例大,同时图片的宽要比手机屏幕要大,就按水平方向比例缩放

       System.out.println("按宽比例缩放");

       scale = scaleX;

      } else if (scaleY >= scaleX && scaleY >= 1) {

       // 竖直方向的缩放比例比水平方向的缩放比例大,同时图片的高要比手机屏幕要大,就按竖直方向比例缩放

       System.out.println("按高比例缩放");

       scale = scaleY;

      }

      System.out.println("缩放比例:" + scale);

      // 真正解析图片

      options.inJustDecodeBounds = false;

      // 设置采样率

      options.inSampleSize = scale;

      Bitmap bitmap = BitmapFactory.decodeFile(src,options);

      this.iv_img.setImageBitmap(bitmap);

     }

    }

    获取图片exif信息

    public class MainActivity extends Activity {

     @Override

     protected void onCreate(Bundle savedInstanceState) {

      super.onCreate(savedInstanceState);

      setContentView(R.layout.activity_main);

     }

     public void read(View view) {

      try {

       /*

        * ExifInterface.TAG_APERTURE:光圈

        * ExifInterface.TAG_DATETIME:拍摄日期

        * ExifInterface.TAG_EXPOSURE_TIME:曝光时间

        * ExifInterface.TAG_FLASH:闪光灯

        * ExifInterface.TAG_FOCAL_LENGTH:焦距

        * ExifInterface.TAG_GPS_ALTITUDE:海拔

        * ExifInterface.TAG_GPS_ALTITUDE_REF:海拔参数

        * ExifInterface.TAG_GPS_DATESTAMP

        */

       String src = "mnt/sdcard/DCIM/Camera/IMG_20130718_164129.jpg";

    // src = "mnt/sdcard/dog.jpg";

       ExifInterface exif = new ExifInterface(src);

    // String make = exif. getAttribute(ExifInterface.TAG_MAKE);

    // String model = exif.getAttribute(ExifInterface.TAG_MODEL);

    // String datetime = exif.getAttribute(ExifInterface.TAG_DATETIME);

    // Toast.makeText(this, "制造商: " + make, 0).show();

    // Toast.makeText(this, "相机类型: " + model, 0).show();

    // Toast.makeText(this, "拍摄日期:" + datetime, 0).show();

    // System.out.println(exif.getAttribute(ExifInterface.TAG_APERTURE));

       System.out.println(exif.getAttribute(ExifInterface.TAG_DATETIME));

    // System.out.println(exif.getAttribute(ExifInterface.TAG_EXPOSURE_TIME));

       System.out.println(exif.getAttribute(ExifInterface.TAG_FLASH));

       System.out.println(exif.getAttribute(ExifInterface.TAG_FOCAL_LENGTH));

    // System.out.println(exif.getAttribute(ExifInterface.TAG_GPS_ALTITUDE));

    // System.out.println(exif.getAttribute(ExifInterface.TAG_GPS_ALTITUDE_REF));

       System.out.println(exif.getAttribute(ExifInterface.TAG_GPS_DATESTAMP));

       System.out.println(exif.getAttribute(ExifInterface.TAG_GPS_LATITUDE));

       System.out.println(exif.getAttribute(ExifInterface.TAG_GPS_LATITUDE_REF));

       System.out.println(exif.getAttribute(ExifInterface.TAG_GPS_LONGITUDE));

       System.out.println(exif.getAttribute(ExifInterface.TAG_GPS_LONGITUDE_REF));

       System.out.println(exif.getAttribute(ExifInterface.TAG_IMAGE_LENGTH));

       System.out.println(exif.getAttribute(ExifInterface.TAG_IMAGE_WIDTH));

    // System.out.println(exif.getAttribute(ExifInterface.TAG_ISO));

       System.out.println(exif.getAttribute(ExifInterface.TAG_MAKE));

       System.out.println(exif.getAttribute(ExifInterface.TAG_MODEL));

       System.out.println(exif.getAttribute(ExifInterface.TAG_ORIENTATION));

       System.out.println(exif.getAttribute(ExifInterface.TAG_WHITE_BALANCE));

      } catch (Exception e) {

       // TODO Auto-generated catch block

       e.printStackTrace();

      }

     }

    }

    从gallery获取图片

    查看系统gallery应用的源码:

    在清单文件中,我们发现可以通过android.intent.action.PICK这个action来从图库选择一张图片

    <activity android:name="com.android.camera.ImageGallery"

                    android:label="@string/gallery_label"

                    android:configChanges="orientation|keyboardHidden"

                    android:icon="@drawable/ic_launcher_gallery">

                .......

                <intent-filter>

                    <action android:name="android.intent.action.PICK" />

                    <category android:name="android.intent.category.DEFAULT" />

                    <data android:mimeType="image/*" />

                    <data android:mimeType="video/*" />

                </intent-filter>

              .......

               

    </activity>

           

    再查看com.android.camera.ImageGallery类发现,图库会返回图片的全路径和图片的缩略图给我们

    Intent result = new Intent(null, img.fullSizeImageUri());

                if (myExtras != null && myExtras.getBoolean("return-data")) {

                    // The size of a transaction should be below 100K.

                    Bitmap bitmap = img.fullSizeBitmap(

                            IImage.UNCONSTRAINED, 100 * 1024);

                    if (bitmap != null) {

                        result.putExtra("data", bitmap);

                    }

    根据上边的信息,我们可以编写一下代码来获取图库的某种图片

    public class MainActivity extends Activity {

     private ImageView iv_image;

     @Override

     protected void onCreate(Bundle savedInstanceState) {

      super.onCreate(savedInstanceState);

      setContentView(R.layout.activity_main);

      this.iv_image = (ImageView) this.findViewById(R.id.iv_image);

     }

     public void load(View view) {

      // 激活系统图库,选择一张图片

      Intent intent = new Intent();

      intent.setAction(Intent.ACTION_PICK);

      intent.setType("image/*");

      startActivityForResult(intent, 0);

     }

     @Override

     protected void onActivityResult(int requestCode, int resultCode, Intent data) {

      if (data != null) {

       // 得到图片的全路径

       Uri uri = data.getData();

       // 通过路径加载图片

       // this.iv_image.setImageURI(uri);

       try {

        InputStream is = getContentResolver().openInputStream(uri);

        Bitmap bitmap = BitmapFactory.decodeStream(is);

        this.iv_image.setImageBitmap(bitmap);

       } catch (FileNotFoundException e) {

        // TODO Auto-generated catch block

        e.printStackTrace();

       }

       // 获取图片的缩略图,可能为空!

       // Bitmap bitmap = data.getParcelableExtra("data");

       // this.iv_image.setImageBitmap(bitmap);

      }

      super.onActivityResult(requestCode, resultCode, data);

     }

    }

  • 相关阅读:
    调戏木马病毒的正确姿势——下
    调戏木马病毒的正确姿势——上
    可恢复的安全rm
    关于System.Convert那些事
    ORA-00600: internal error code, arguments: [kkqtSetOp.1]
    PL/SQL个人学习笔记
    苹果iOS手机后门的”诊断功能论“不攻自破
    畅谈HTML开发
    Triangle
    Java中的equals()和hashCode()
  • 原文地址:https://www.cnblogs.com/freenovo/p/4469820.html
Copyright © 2020-2023  润新知