服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - Android - Android中3种图片压缩处理方法

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

2021-03-25 14:12Android开发网 Android

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

Android中图片的存在形式:

1:文件形式:二进制形式存在与硬盘中。
2:流的形式:二进制形式存在与内存中。
3:Bitmap的形式

三种形式的区别:
文件形式和流的形式:对图片体积大小并没有影响。也就是说,如果你手机SD卡上的图片通过流的形式读到内存中,在内存中的大小也是原图的大小。
注意:不是Bitmap的形式。
Bitmap的形式:图片占用的内存会瞬间变大。
以下是代码的形式:

    

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

延伸 · 阅读

精彩推荐