Android图片处理之Bitmap类

来源:互联网 发布:局域网网络扫描仪 编辑:程序博客网 时间:2024/06/01 21:06

介绍

Bitmap是Android系统中的图像处理的最重要类之一。用它可以获取图像文件信息,进行图像剪切、旋转、缩放等操作,并可以指定格式保存图像文件。本文从应用的角度,着重介绍怎么用Bitmap来实现这些功能。


Bitmap的存储可以说包括两个部分,像素以及长,宽,颜色等描述信息。像素是Bitmap最占用内存的地方,长宽和像素位数是用来描述图片的。 
Bitmap官方API文档:

enum  Bitmap.CompressFormat

Specifies the known formats a bitmap can be compressed into 
enum Bitmap.Config

Possible bitmap configurations.

其中CompressFormat有JPE、GPN、GWEBP;Config如下:

Bitmap.Config ALPHA_8

Each pixel is stored as a single translucency (alpha) channel.  
Bitmap.Config ARGB_4444

This field was deprecated in API level 13. Because of the poor quality 
of this configuration, it is advised to use ARGB_8888 instead. 
Bitmap.Config ARGB_8888

Each pixel is stored on 4 bytes. 
Bitmap.Config RGB_565 
Each pixel is stored on 2 bytes and only the RGB channels are encoded: 
red is stored with 5 bits of precision (32 possible values), green is 
stored with 6 bits of precision (64 possible values) and blue is 
stored with 5 bits of precision.

ARGB—Alpha,Red,Green,Blue 
一种色彩模式,也就是RGB色彩模式附加上Alpha(透明度)通道

图片缩放

在展示高分辨率图片的时候,最好先将图片进行压缩,从而防止OOM的出现。主要有两种方法:

  • inSampleSize(采样率) 
    优点:效率较高,解析速度快 
    缺点:采样率inSampleSize的取值只能是2的次方数(如:inSampleSize=15,实际取值为8),因此该方法不能精确的指定图片的大小

    int inSampleSize

    If set to a value > 1, requests the decoder to subsample the original 
    image, returning a smaller image to save memory. The sample size is 
    the number of pixels in either dimension that correspond to a single 
    pixel in the decoded bitmap. For example, inSampleSize == 4 returns an 
    image that is 1/4 the width/height of the original, and 1/16 the 
    number of pixels. Any value <= 1 is treated the same as 1. Note: the 
    decoder uses a final value based on powers of 2, any other value will 
    be rounded down to the nearest power of 2.

  • Matrix 
    优点:可以精确地指定图片的缩放大小 
    缺点:是在原bitmap的基础之上生成的,占内存,效率低.


设置采样率缩放


  1. public static Bitmap decodeSampledBitmapFromResource(Resources res, int resId,
  2. int reqWidth, int reqHeight) {
  3. // 第一次解析将inJustDecodeBounds设置为true,来获取图片大小
  4. final BitmapFactory.Options options = new BitmapFactory.Options();
  5. options.inJustDecodeBounds = true;
  6. //解析资源文件中的图片
  7. BitmapFactory.decodeResource(res, resId, options);
  8. // 调用方法计算inSampleSize值
  9. options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
  10. // 使用获取到的inSampleSize值再次解析图片
  11. options.inJustDecodeBounds = false;
  12. return BitmapFactory.decodeResource(res, resId, options);
  13. }
  14. public static int calculateInSampleSize(BitmapFactory.Options options,
  15. int reqWidth, int reqHeight) {
  16. if (reqWidth == 0 || reqHeight == 0) {
  17. return 1;
  18. }
  19. final int height = options.outHeight;
  20. final int width = options.outWidth;
  21. Log.d(TAG, "origin, w= " + width + " h=" + height);
  22. int inSampleSize = 1;
  23. //保证最终图片的宽和高一定都会大于等于目标的宽和高
  24. if (height > reqHeight || width > reqWidth) {
  25. final int halfHeight = height / 2;
  26. final int halfWidth = width / 2;
  27. while ((halfHeight / inSampleSize) >= reqHeight
  28. && (halfWidth / inSampleSize) >= reqWidth) {
  29. inSampleSize *= 2;
  30. }
  31. }
  32. Log.d(TAG, "sampleSize:" + inSampleSize);
  33. return inSampleSize;
  34. }
  35. //主程序调用示例
  36. mImageView.setImageBitmap(decodeSampledBitmapFromResource(getResources(), R.id.myimage, 100, 100));

Matrix缩放方法

  1. public static Bitmap zoomImage(Bitmap bgimage, double newWidth,
  2. double newHeight) {
  3. // 获取这个图片的宽和高
  4. float width = bgimage.getWidth();
  5. float height = bgimage.getHeight();
  6. // 创建操作图片用的matrix对象
  7. Matrix matrix = new Matrix();
  8. // 计算宽高缩放率
  9. float scaleWidth = ((float) newWidth) / width;
  10. float scaleHeight = ((float) newHeight) / height;
  11. // 缩放图片动作
  12. matrix.postScale(scaleWidth, scaleHeight);
  13. Bitmap bitmap = Bitmap.createBitmap(bgimage, 0, 0, (int) width,
  14. (int) height, matrix, true);
  15. Log.v("缩放后的bitmap占用内存:", bitmap.getByteCount() / 1024 + "k");
  16. return bitmap;
  17. }

图片缓存

使用内存缓存技术来对图片进行缓存,从而在加载很多图片的时候可以提高响应速度和流畅性。 
LruCach适合用来缓存图片,它的主要算法原理是把最近使用的对象用强引用存储在 LinkedHashMap中,并且把最近最少使用的对象在缓存值达到预设定值之前从内存中移除。

不推荐使用软引用或弱引用方式来缓存,因为从 Android 2.3 (API Level 9)开始,垃圾回收器会更倾向于回收持有软引用或弱引用的对象,这让软引用和弱引用变得不再可靠

  1. private LruCache<String, Bitmap> mMemoryCache;
  2. @Override
  3. protected void onCreate(Bundle savedInstanceState) {
  4. // 获取到可用内存的最大值,使用内存超出这个值会引起OutOfMemory异常。
  5. // LruCache通过构造函数传入缓存值,以KB为单位。
  6. int maxMemory = (int) (Runtime.getRuntime().maxMemory() / 1024);
  7. // 使用最大可用内存值的1/8作为缓存的大小。
  8. int cacheSize = maxMemory / 8;
  9. mMemoryCache = new LruCache<String, Bitmap>(cacheSize) {
  10. @Override
  11. protected int sizeOf(String key, Bitmap bitmap) {
  12. // 重写此方法来衡量每张图片的大小,默认返回图片数量。
  13. return bitmap.getByteCount() / 1024;
  14. }
  15. };
  16. }
  17. public void addBitmapToMemoryCache(String key, Bitmap bitmap) {
  18. if (getBitmapFromMemCache(key) == null) {
  19. mMemoryCache.put(key, bitmap);
  20. }
  21. }
  22. public Bitmap getBitmapFromMemCache(String key) {
  23. return mMemoryCache.get(key);
  24. }
  25. public void loadBitmap(int resId, ImageView imageView) {
  26. final String imageKey = String.valueOf(resId);
  27. final Bitmap bitmap = getBitmapFromMemCache(imageKey);
  28. if (bitmap != null) {
  29. imageView.setImageBitmap(bitmap);
  30. } else {
  31. imageView.setImageResource(R.drawable.image_placeholder);
  32. BitmapWorkerTask task = new BitmapWorkerTask(imageView);
  33. task.execute(resId);
  34. }
  35. }
  36. class BitmapWorkerTask extends AsyncTask<Integer, Void, Bitmap> {
  37. // 在后台加载图片
  38. @Override
  39. protected Bitmap doInBackground(Integer... params) {
  40. final Bitmap bitmap = decodeSampledBitmapFromResource(
  41. getResources(), params[0], 100, 100);
  42. addBitmapToMemoryCache(String.valueOf(params[0]), bitmap);
  43. return bitmap;
  44. }
  45. }

参考

官方文档 
https://developer.android.com/training/displaying-bitmaps/load-bitmap.html 
https://developer.android.com/reference/android/graphics/Bitmap.html