Android中图片Bitmap的缩放

转载地址http://blog.csdn.net/stoppig/article/details/23198809

一、什么是Android中的Bitmap

Bitmap是Android系统中的图像处理的最重要类之一。用它可以获取图像文件信息,进行图像剪切、旋转、缩放等操作,并可以指定格式保存图像文件。

二、Bitmap的缩放基本操作

在Android开发中,经常会遇到图片的操作问题,由于Android屏幕尺寸繁多,最经常的是图片的缩放问题。下面为一段Android操作的基本代码
    
  1. // 获得图片的宽高  
  2.     int width = bm.getWidth();  
  3.     int height = bm.getHeight();  
  4.     // 设置想要的大小  
  5.     int newWidth = 320;  
  6.     int newHeight = 480;  
  7.     // 计算缩放比例  
  8.     float scaleWidth = ((float) newWidth) / width;  
  9.     float scaleHeight = ((float) newHeight) / height;  
  10.     // 取得想要缩放的matrix参数  
  11.     Matrix matrix = new Matrix();  
  12.     matrix.postScale(scaleWidth, scaleHeight);  
  13.     // 得到新的图片  
  14.     Bitmap newbm = Bitmap.createBitmap(bm, 00, width, height, matrix,  
  15.       true);  

三、Bitmap缩放方法的封装

为了使用方便,我们对bitmap方法进行封装

  1. import java.io.ByteArrayInputStream;  
  2. import java.io.ByteArrayOutputStream;  
  3. import java.io.IOException;  
  4. import java.io.InputStream;  
  5. import android.content.Context;  
  6. import android.content.Intent;  
  7. import android.graphics.Bitmap;  
  8. import android.graphics.Bitmap.Config;  
  9. import android.graphics.BitmapFactory;  
  10. import android.graphics.Canvas;  
  11. import android.graphics.Matrix;  
  12. import android.net.Uri;  
  13. import android.util.Log;  
  14. public class Bitmaptest  
  15. {  
  16.  private static final String TAG = "BitmapUtil";  
  17.  /** 
  18.   * 通过资源id转化成Bitmap 
  19.   * 
  20.   * @param context 
  21.   * @param resId 
  22.   * @return 
  23.   */  
  24.  public static Bitmap ReadBitmapById(Context context, int resId)  
  25.  {  
  26.   BitmapFactory.Options opt = new BitmapFactory.Options();  
  27.   opt.inPreferredConfig = Bitmap.Config.RGB_565;  
  28.   opt.inPurgeable = true;  
  29.   opt.inInputShareable = true;  
  30.   InputStream is = context.getResources().openRawResource(resId);  
  31.   return BitmapFactory.decodeStream(is, null, opt);  
  32.  }  
  33.  /** 
  34.   * 缩放Bitmap满屏 
  35.   * 
  36.   * @param bitmap 
  37.   * @param screenWidth 
  38.   * @param screenHight 
  39.   * @return 
  40.   */  
  41.  public static Bitmap getBitmap(Bitmap bitmap, int screenWidth,  
  42.    int screenHight)  
  43.  {  
  44.   int w = bitmap.getWidth();  
  45.   int h = bitmap.getHeight();  
  46.   Matrix matrix = new Matrix();  
  47.   float scale = (float) screenWidth / w;  
  48.   float scale2 = (float) screenHight / h;  
  49.   // scale = scale < scale2 ? scale : scale2;  
  50.   matrix.postScale(scale, scale);  
  51.   Bitmap bmp = Bitmap.createBitmap(bitmap, 00, w, h, matrix, true);  
  52.   if (bitmap != null && !bitmap.equals(bmp) && !bitmap.isRecycled())  
  53.   {  
  54.    bitmap.recycle();  
  55.    bitmap = null;  
  56.   }  
  57.   return bmp;// Bitmap.createBitmap(bitmap, 0, 0, w, h, matrix, true);  
  58.  }  
  59.    
  60.  /** 
  61.   * 按最大边按一定大小缩放图片 
  62.   * */  
  63.  public static Bitmap scaleImage(byte[] buffer, float size)  
  64.  {  
  65.   // 获取原图宽度  
  66.   BitmapFactory.Options options = new BitmapFactory.Options();  
  67.   options.inJustDecodeBounds = true;  
  68.   options.inPurgeable = true;  
  69.   options.inInputShareable = true;  
  70.   Bitmap bm = BitmapFactory.decodeByteArray(buffer, 0, buffer.length,  
  71.     options);  
  72.   // 计算缩放比例  
  73.   float reSize = options.outWidth / size;  
  74.   if (options.outWidth < options.outHeight)  
  75.   {  
  76.    reSize = options.outHeight / size;  
  77.   }  
  78.   // 如果是小图则放大  
  79.   if (reSize <= 1)  
  80.   {  
  81.    int newWidth = 0;  
  82.    int newHeight = 0;  
  83.    if (options.outWidth > options.outHeight)  
  84.    {  
  85.     newWidth = (int) size;  
  86.     newHeight = options.outHeight * (int) size / options.outWidth;  
  87.    } else  
  88.    {  
  89.     newHeight = (int) size;  
  90.     newWidth = options.outWidth * (int) size / options.outHeight;  
  91.    }  
  92.    bm = BitmapFactory.decodeByteArray(buffer, 0, buffer.length);  
  93.    bm = scaleImage(bm, newWidth, newHeight);  
  94.    if (bm == null)  
  95.    {  
  96.     Log.e(TAG, "convertToThumb, decode fail:" + null);  
  97.     return null;  
  98.    }  
  99.    return bm;  
  100.   }  
  101.   // 缩放  
  102.   options.inJustDecodeBounds = false;  
  103.   options.inSampleSize = (int) reSize;  
  104.   bm = BitmapFactory.decodeByteArray(buffer, 0, buffer.length, options);  
  105.   if (bm == null)  
  106.   {  
  107.    Log.e(TAG, "convertToThumb, decode fail:" + null);  
  108.    return null;  
  109.   }  
  110.   return bm;  
  111.  }  
  112.  /** 
  113.   * 检查图片是否超过一定值,是则缩小 
  114.   * 
  115.   * @param view 
  116.   * @param strFileName 
  117.   */  
  118.  public static Bitmap convertToThumb(byte[] buffer, float size)  
  119.  {  
  120.   // 获取原图宽度  
  121.   BitmapFactory.Options options = new BitmapFactory.Options();  
  122.   options.inJustDecodeBounds = true;  
  123.   options.inPurgeable = true;  
  124.   options.inInputShareable = true;  
  125.   Bitmap bm = BitmapFactory.decodeByteArray(buffer, 0, buffer.length,  
  126.     options);  
  127.   // 计算缩放比例  
  128.   float reSize = options.outWidth / size;  
  129.   if (options.outWidth > options.outHeight)  
  130.   {  
  131.    reSize = options.outHeight / size;  
  132.   }  
  133.   if (reSize <= 0)  
  134.   {  
  135.    reSize = 1;  
  136.   }  
  137.   Log.d(TAG, "convertToThumb, reSize:" + reSize);  
  138.   // 缩放  
  139.   options.inJustDecodeBounds = false;  
  140.   options.inSampleSize = (int) reSize;  
  141.   if (bm != null && !bm.isRecycled())  
  142.   {  
  143.    bm.recycle();  
  144.    bm = null;  
  145.    Log.e(TAG, "convertToThumb, recyle");  
  146.   }  
  147.   bm = BitmapFactory.decodeByteArray(buffer, 0, buffer.length, options);  
  148.   if (bm == null)  
  149.   {  
  150.    Log.e(TAG, "convertToThumb, decode fail:" + null);  
  151.    return null;  
  152.   }  
  153.   return bm;  
  154.  }  
  155.  /** 
  156.   * Bitmap --> byte[] 
  157.   * 
  158.   * @param bmp 
  159.   * @return 
  160.   */  
  161.  private static byte[] readBitmap(Bitmap bmp)  
  162.  {  
  163.   ByteArrayOutputStream baos = new ByteArrayOutputStream();  
  164.   bmp.compress(Bitmap.CompressFormat.JPEG, 60, baos);  
  165.   try  
  166.   {  
  167.    baos.flush();  
  168.    baos.close();  
  169.   } catch (IOException e)  
  170.   {  
  171.    e.printStackTrace();  
  172.   }  
  173.   return baos.toByteArray();  
  174.  }  
  175.  /** 
  176.   * Bitmap --> byte[] 
  177.   * 
  178.   * @param bmp 
  179.   * @return 
  180.   */  
  181.  public static byte[] readBitmapFromBuffer(byte[] buffer, float size)  
  182.  {  
  183.   return readBitmap(convertToThumb(buffer, size));  
  184.  }  
  185.  /** 
  186.   * 以屏幕宽度为基准,显示图片 
  187.   * 
  188.   * @param iv 
  189.   * @param path 
  190.   * @param screenW 
  191.   * @return 
  192.   */  
  193.  public static Bitmap decodeStream(Context context, Intent data, float size)  
  194.  {  
  195.   Bitmap image = null;  
  196.   try  
  197.   {  
  198.    Uri dataUri = data.getData();  
  199.    // 获取原图宽度  
  200.    BitmapFactory.Options options = new BitmapFactory.Options();  
  201.    options.inJustDecodeBounds = true;  
  202.    options.inPurgeable = true;  
  203.    options.inInputShareable = true;  
  204.    BitmapFactory.decodeStream(context.getContentResolver()  
  205.      .openInputStream(dataUri), null, options);  
  206.    // 计算缩放比例  
  207.    float reSize = (int) (options.outWidth / size);  
  208.    if (reSize <= 0)  
  209.    {  
  210.     reSize = 1;  
  211.    }  
  212.    Log.d(TAG, "old-w:" + options.outWidth + ", llyt-w:" + size  
  213.      + ", resize:" + reSize);  
  214.    // 缩放  
  215.    options.inJustDecodeBounds = false;  
  216.    options.inSampleSize = (int) reSize;  
  217.    image = BitmapFactory.decodeStream(context.getContentResolver()  
  218.      .openInputStream(dataUri), null, options);  
  219.   } catch (Exception e)  
  220.   {  
  221.    e.printStackTrace();  
  222.   }  
  223.   return image;  
  224.  }  
  225.    
  226.  /** 
  227.   * 按新的宽高缩放图片 
  228.   * 
  229.   * @param bm 
  230.   * @param newWidth 
  231.   * @param newHeight 
  232.   * @return 
  233.   */  
  234.  public static Bitmap scaleImage(Bitmap bm, int newWidth, int newHeight)  
  235.  {  
  236.   if (bm == null)  
  237.   {  
  238.    return null;  
  239.   }  
  240.   int width = bm.getWidth();  
  241.   int height = bm.getHeight();  
  242.   float scaleWidth = ((float) newWidth) / width;  
  243.   float scaleHeight = ((float) newHeight) / height;  
  244.   Matrix matrix = new Matrix();  
  245.   matrix.postScale(scaleWidth, scaleHeight);  
  246.   Bitmap newbm = Bitmap.createBitmap(bm, 00, width, height, matrix,  
  247.     true);  
  248.   if (bm != null & !bm.isRecycled())  
  249.   {  
  250.    bm.recycle();  
  251.    bm = null;  
  252.   }  
  253.   return newbm;  
  254.  }  
  255.  /** 
  256.   * fuction: 设置固定的宽度,高度随之变化,使图片不会变形 
  257.   * 
  258.   * @param target 
  259.   * 需要转化bitmap参数 
  260.   * @param newWidth 
  261.   * 设置新的宽度 
  262.   * @return 
  263.   */  
  264.  public static Bitmap fitBitmap(Bitmap target, int newWidth)  
  265.  {  
  266.   int width = target.getWidth();  
  267.   int height = target.getHeight();  
  268.   Matrix matrix = new Matrix();  
  269.   float scaleWidth = ((float) newWidth) / width;  
  270.   // float scaleHeight = ((float)newHeight) / height;  
  271.   int newHeight = (int) (scaleWidth * height);  
  272.   matrix.postScale(scaleWidth, scaleWidth);  
  273.   // Bitmap result = Bitmap.createBitmap(target,0,0,width,height,  
  274.   // matrix,true);  
  275.   Bitmap bmp = Bitmap.createBitmap(target, 00, width, height, matrix,  
  276.     true);  
  277.   if (target != null && !target.equals(bmp) && !target.isRecycled())  
  278.   {  
  279.    target.recycle();  
  280.    target = null;  
  281.   }  
  282.   return bmp;// Bitmap.createBitmap(target, 0, 0, width, height, matrix,  
  283.      // true);  
  284.  }  
  285.    
  286.  /** 
  287.   * 根据指定的宽度平铺图像 
  288.   * 
  289.   * @param width 
  290.   * @param src 
  291.   * @return 
  292.   */  
  293.  public static Bitmap createRepeater(int width, Bitmap src)  
  294.  {  
  295.   int count = (width + src.getWidth() - 1) / src.getWidth();  
  296.   Bitmap bitmap = Bitmap.createBitmap(width, src.getHeight(),  
  297.     Config.ARGB_8888);  
  298.   Canvas canvas = new Canvas(bitmap);  
  299.   for (int idx = 0; idx < count; ++idx)  
  300.   {  
  301.    canvas.drawBitmap(src, idx * src.getWidth(), 0null);  
  302.   }  
  303.   return bitmap;  
  304.  }  
  305.    
  306.  /** 
  307.   * 图片的质量压缩方法 
  308.   * 
  309.   * @param image 
  310.   * @return 
  311.   */  
  312.  public static Bitmap compressImage(Bitmap image)  
  313.  {  
  314.   ByteArrayOutputStream baos = new ByteArrayOutputStream();  
  315.   image.compress(Bitmap.CompressFormat.JPEG, 100, baos);// 质量压缩方法,这里100表示不压缩,把压缩后的数据存放到baos中  
  316.   int options = 100;  
  317.   while (baos.toByteArray().length / 1024 > 100)  
  318.   { // 循环判断如果压缩后图片是否大于100kb,大于继续压缩  
  319.    baos.reset();// 重置baos即清空baos  
  320.    image.compress(Bitmap.CompressFormat.JPEG, options, baos);// 这里压缩options%,把压缩后的数据存放到baos中  
  321.    options -= 10;// 每次都减少10  
  322.   }  
  323.   ByteArrayInputStream isBm = new ByteArrayInputStream(baos.toByteArray());// 把压缩后的数据baos存放到ByteArrayInputStream中  
  324.   Bitmap bitmap = BitmapFactory.decodeStream(isBm, nullnull);// 把ByteArrayInputStream数据生成图片  
  325.   if (baos != null)  
  326.   {  
  327.    try  
  328.    {  
  329.     baos.close();  
  330.    } catch (IOException e)  
  331.    {  
  332.     // TODO Auto-generated catch block  
  333.     e.printStackTrace();  
  334.    }  
  335.   }  
  336.   if (isBm != null)  
  337.   {  
  338.    try  
  339.    {  
  340.     isBm.close();  
  341.    } catch (IOException e)  
  342.    {  
  343.     // TODO Auto-generated catch block  
  344.     e.printStackTrace();  
  345.    }  
  346.   }  
  347.   if (image != null && !image.isRecycled())  
  348.   {  
  349.    image.recycle();  
  350.    image = null;  
  351.   }  
  352.   return bitmap;  
  353.  }  
  354.    
  355.  /** 
  356.   * 图片按比例大小压缩方法(根据Bitmap图片压缩) 
  357.   * 
  358.   * @param image 
  359.   * @return 
  360.   */  
  361.  public static Bitmap getImage(Bitmap image)  
  362.  {  
  363.   ByteArrayOutputStream baos = new ByteArrayOutputStream();  
  364.   image.compress(Bitmap.CompressFormat.JPEG, 100, baos);  
  365.   if (baos.toByteArray().length / 1024 > 1024)  
  366.   {// 判断如果图片大于1M,进行压缩避免在生成图片(BitmapFactory.decodeStream)时溢出  
  367.    baos.reset();// 重置baos即清空baos  
  368.    image.compress(Bitmap.CompressFormat.JPEG, 50, baos);// 这里压缩50%,把压缩后的数据存放到baos中  
  369.   }  
  370.   ByteArrayInputStream isBm = new ByteArrayInputStream(baos.toByteArray());  
  371.   BitmapFactory.Options newOpts = new BitmapFactory.Options();  
  372.   // 开始读入图片,此时把options.inJustDecodeBounds 设回true了  
  373.   newOpts.inJustDecodeBounds = true;  
  374.   Bitmap bitmap = BitmapFactory.decodeStream(isBm, null, newOpts);  
  375.   newOpts.inJustDecodeBounds = false;  
  376.   int w = newOpts.outWidth;  
  377.   int h = newOpts.outHeight;  
  378.   // 现在主流手机比较多是800*480分辨率,所以高和宽我们设置为  
  379.   float hh = 800f;// 这里设置高度为800f  
  380.   float ww = 480f;// 这里设置宽度为480f  
  381.   // 缩放比。由于是固定比例缩放,只用高或者宽其中一个数据进行计算即可  
  382.   int be = 1;// be=1表示不缩放  
  383.   if (w > h && w > ww)  
  384.   {// 如果宽度大的话根据宽度固定大小缩放  
  385.    be = (int) (newOpts.outWidth / ww);  
  386.   } else if (w < h && h > hh)  
  387.   {// 如果高度高的话根据宽度固定大小缩放  
  388.    be = (int) (newOpts.outHeight / hh);  
  389.   }  
  390.   if (be <= 0)  
  391.    be = 1;  
  392.   newOpts.inSampleSize = be;// 设置缩放比例  
  393.   // 重新读入图片,注意此时已经把options.inJustDecodeBounds 设回false了  
  394.   isBm = new ByteArrayInputStream(baos.toByteArray());  
  395.   bitmap = BitmapFactory.decodeStream(isBm, null, newOpts);  
  396.   if (isBm != null)  
  397.   {  
  398.    try  
  399.    {  
  400.     isBm.close();  
  401.    } catch (IOException e)  
  402.    {  
  403.     // TODO Auto-generated catch block  
  404.     e.printStackTrace();  
  405.    }  
  406.   }  
  407.   if (image != null && !image.isRecycled())  
  408.   {  
  409.    image.recycle();  
  410.    image = null;  
  411.   }  
  412.   return compressImage(bitmap);// 压缩好比例大小后再进行质量压缩  
  413.  }  
  414.  /** 
  415.   * 通过资源id转化成Bitmap 全屏显示 
  416.   * 
  417.   * @param context 
  418.   * @param drawableId 
  419.   * @param screenWidth 
  420.   * @param screenHight 
  421.   * @return 
  422.   */  
  423.  public static Bitmap ReadBitmapById(Context context, int drawableId,  
  424.    int screenWidth, int screenHight)  
  425.  {  
  426.   BitmapFactory.Options options = new BitmapFactory.Options();  
  427.   options.inPreferredConfig = Config.ARGB_8888;  
  428.   options.inInputShareable = true;  
  429.   options.inPurgeable = true;  
  430.   InputStream stream = context.getResources().openRawResource(drawableId);  
  431.   Bitmap bitmap = BitmapFactory.decodeStream(stream, null, options);  
  432.   return getBitmap(bitmap, screenWidth, screenHight);  
  433.  }  
  434.    

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值