首页 > 系统 > Android > 正文

Android中3种图片压缩处理方法

2019-10-24 21:12:35
字体:
来源:转载
供稿:网友

这篇文章主要介绍了Android中3种图片压缩处理方法,本文讲解了质量压缩方法、获得缩略图、图片缩放三种方法并分别给出示例代码,需要的朋友可以参考下

Android中图片的存在形式:

1:文件形式:二进制形式存在与硬盘中。

2:流的形式:二进制形式存在与内存中。

3:Bitmap的形式

三种形式的区别:

文件形式和流的形式:对图片体积大小并没有影响。也就是说,如果你手机SD卡上的图片通过流的形式读到内存中,在内存中的大小也是原图的大小。

注意:不是Bitmap的形式。

Bitmap的形式:图片占用的内存会瞬间变大。

以下是代码的形式:

 

 
  1. /** 
  2. * 图片压缩的方法总结 
  3. */ 
  4.  
  5. /* 
  6. * 图片压缩的方法01:质量压缩方法 
  7. */ 
  8. private Bitmap compressImage(Bitmap beforBitmap) { 
  9.  
  10. // 可以捕获内存缓冲区的数据,转换成字节数组。 
  11. ByteArrayOutputStream bos = new ByteArrayOutputStream(); 
  12. if (beforBitmap != null) { 
  13. // 第一个参数:图片压缩的格式;第二个参数:压缩的比率;第三个参数:压缩的数据存放到bos中 
  14. beforBitmap.compress(CompressFormat.JPEG, 100, bos); 
  15. int options = 100; 
  16. // 循环判断压缩后的图片是否是大于100kb,如果大于,就继续压缩,否则就不压缩 
  17. while (bos.toByteArray().length / 1024 > 100) { 
  18. bos.reset();// 置为空 
  19. // 压缩options% 
  20. beforBitmap.compress(CompressFormat.JPEG, options, bos); 
  21. // 每次都减少10 
  22. options -= 10; 
  23.  
  24. // 从bos中将数据读出来 存放到ByteArrayInputStream中 
  25. ByteArrayInputStream bis = new ByteArrayInputStream( 
  26. bos.toByteArray()); 
  27. // 将数据转换成图片 
  28. Bitmap afterBitmap = BitmapFactory.decodeStream(bis); 
  29. return afterBitmap; 
  30. return null
  31.  
  32. /* 
  33. * 图片压缩方法02:获得缩略图 
  34. */ 
  35. public Bitmap getThumbnail(int id) { 
  36. // 获得原图 
  37. Bitmap beforeBitmap = BitmapFactory.decodeResource( 
  38. mContext.getResources(), id); 
  39. // 宽 
  40. int w = mContext.getResources() 
  41. .getDimensionPixelOffset(R.dimen.image_w); 
  42. // 高 
  43. int h = mContext.getResources().getDimensionPixelSize(R.dimen.image_h); 
  44.  
  45. // 获得缩略图 
  46. Bitmap afterBitmap = ThumbnailUtils 
  47. .extractThumbnail(beforeBitmap, w, h); 
  48. return afterBitmap; 
  49.  
  50.  
  51. /** 
  52. * 图片压缩03 
  53.  
  54. * @param id 
  55. * 要操作的图片的大小 
  56. * @param newWidth 
  57. * 图片指定的宽度 
  58. * @param newHeight 
  59. * 图片指定的高度 
  60. * @return 
  61. */ 
  62. public Bitmap compressBitmap(int id, double newWidth, double newHeight) { 
  63. // 获得原图 
  64. Bitmap beforeBitmap = BitmapFactory.decodeResource( 
  65. mContext.getResources(), id); 
  66. // 图片原有的宽度和高度 
  67. float beforeWidth = beforeBitmap.getWidth(); 
  68. float beforeHeight = beforeBitmap.getHeight(); 
  69.  
  70. // 计算宽高缩放率 
  71. float scaleWidth = 0; 
  72. float scaleHeight = 0; 
  73. if (beforeWidth > beforeHeight) { 
  74. scaleWidth = ((float) newWidth) / beforeWidth; 
  75. scaleHeight = ((float) newHeight) / beforeHeight; 
  76. else { 
  77. scaleWidth = ((float) newWidth) / beforeHeight; 
  78. scaleHeight = ((float) newHeight) / beforeWidth; 
  79.  
  80. // 矩阵对象 
  81. Matrix matrix = new Matrix(); 
  82. // 缩放图片动作 缩放比例 
  83. matrix.postScale(scaleWidth, scaleHeight); 
  84. // 创建一个新的Bitmap 从原始图像剪切图像 
  85. Bitmap afterBitmap = Bitmap.createBitmap(beforeBitmap, 0, 0, 
  86. (int) beforeWidth, (int) beforeHeight, matrix, true); 
  87. return afterBitmap; 
  88.  

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表