我是靠谱客的博主 过时大叔,这篇文章主要介绍Android 图片压缩的几种方法,现在分享给大家,希望可以做个参考。

当图片太大不满足需求时,需要对图片进行裁剪压缩处理,常用的压缩处理接口主要有三种:质量压缩法,尺寸压缩法,缩略图压缩法和等比例压缩法,具体代码如下:

复制代码
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Matrix; import android.media.ThumbnailUtils; import android.util.Log; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; /** * 图片压缩的方法 */ public class BitmapUtils { /** * 图片压缩:质量压缩方法 * @param beforBitmap 要压缩的图片 * @return 压缩后的图片 */ static private Bitmap compressImage(Bitmap beforeBitmap) { // 可以捕获内存缓冲区的数据,转换成字节数组。 ByteArrayOutputStream bos = new ByteArrayOutputStream(); if (beforeBitmap != null) { // 第一个参数:图片压缩的格式;第二个参数:压缩的比率;第三个参数:压缩的数据存放到bos中 beforeBitmap.compress(Bitmap.CompressFormat.JPEG, 100, bos); // 循环判断压缩后的图片大小是否满足要求,这里限制100kb,若不满足则继续压缩,每次递减10%压缩 int options = 100; while (bos.toByteArray().length / 1024 > 100) { bos.reset();// 置为空 beforeBitmap.compress(Bitmap.CompressFormat.JPEG, options, bos); options -= 10; } // 从bos中将数据读出来 转换成图片 ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray()); Bitmap afterBitmap = BitmapFactory.decodeStream(bis); return afterBitmap; } return null; } /** * 图片压缩:获得缩略图 * @param beforeBitmap 要压缩的图片 * @param width 缩略图宽度 * @param height 缩略图高度 * @return 压缩后的图片 */ static public Bitmap getThumbnail(Bitmap beforeBitmap, int width, int height) { return ThumbnailUtils.extractThumbnail(beforeBitmap, width, height); } /** * 图片压缩: 按尺寸压缩 * @param beforeBitmap 要压缩的图片 * @param newWidth 压缩后的宽度 * @param newHeight 压缩后的高度 * @return 压缩后的图片 */ static public Bitmap compressBitmap1(Bitmap beforeBitmap, double newWidth, double newHeight) { // 图片原有的宽度和高度 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; } /** * 图片压缩: 规定尺寸等比例压缩,宽高不能超过限制要求 * @param beforBitmap 要压缩的图片 * @param maxWidth 最大宽度限制 * @param maxHeight 最大高度限制 * @return 压缩后的图片 */ static public Bitmap compressBitmap(Bitmap beforBitmap, double maxWidth, double maxHeight) { // 图片原有的宽度和高度 float beforeWidth = beforBitmap.getWidth(); float beforeHeight = beforBitmap.getHeight(); if (beforeWidth <= maxWidth && beforeHeight <= maxHeight) { return beforBitmap; } // 计算宽高缩放率,等比例缩放 float scaleWidth = ((float) maxWidth) / beforeWidth; float scaleHeight = ((float)maxHeight) / beforeHeight; float scale = scaleWidth; if (scaleWidth > scaleHeight) { scale = scaleHeight; } Log.d("BitmapUtils", "before[" + beforeWidth + ", " + beforeHeight + "] max[" + maxWidth + ", " + maxHeight + "] scale:" + scale); // 矩阵对象 Matrix matrix = new Matrix(); // 缩放图片动作 缩放比例 matrix.postScale(scale, scale); // 创建一个新的Bitmap 从原始图像剪切图像 Bitmap afterBitmap = Bitmap.createBitmap(beforBitmap, 0, 0, (int) beforeWidth, (int) beforeHeight, matrix, true); return afterBitmap; } }

最后

以上就是过时大叔最近收集整理的关于Android 图片压缩的几种方法的全部内容,更多相关Android内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(54)

评论列表共有 0 条评论

立即
投稿
返回
顶部