我是靠谱客的博主 结实乌冬面,这篇文章主要介绍Android BitmapUtils工具类使用详解,现在分享给大家,希望可以做个参考。

本文实例为大家分享了Android BitmapUtils工具类的具体代码,供大家参考,具体内容如下

复制代码
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
public final class BitmapUtils { public static final String TAG = "BitmapUtil"; private static int sShotScreenWidth = 480; private static int sShotScreenHeight = 720; private static int sShotScreenSize = sShotScreenWidth * sShotScreenHeight; @SuppressLint("StaticFieldLeak") private static Context mContext; @SuppressLint("StaticFieldLeak") private static Activity mActivity; public void init(Context context,Activity ac) { mContext=context; mActivity=ac; DisplayMetrics dm = new DisplayMetrics(); ac.getWindowManager().getDefaultDisplay().getMetrics(dm); //获取屏幕分辨率 sShotScreenWidth = dm.widthPixels; sShotScreenHeight = dm.heightPixels; sShotScreenSize = sShotScreenWidth * sShotScreenHeight; } /** * 图片合成 * * @param bitmap 位图1 * @param mark 位图2 * @return Bitmap */ public static Bitmap createBitmap(Bitmap bitmap, Bitmap mark) { int w = bitmap.getWidth(); int h = bitmap.getHeight(); int mW = mark.getWidth(); int mH = mark.getHeight(); Bitmap newbitmap = Bitmap.createBitmap(w, h, Config.ARGB_8888);// 创建一个长宽一样的位图 Canvas cv = new Canvas(newbitmap); cv.drawBitmap(bitmap, 0, 0, null);// 在 0,0坐标开始画入bitmap cv.drawBitmap(mark, w - mW , h - mH , null);// 在右下角画入水印mark cv.save(Canvas.ALL_SAVE_FLAG);// 保存 cv.restore();// 存储 return newbitmap; } /** * 放大缩小图片 * @param bitmap 位图 * @param w 新的宽度 * @param h 新的高度 * @return Bitmap */ public static Bitmap zoomBitmap(Bitmap bitmap, int w, int h) { int width = bitmap.getWidth(); int height = bitmap.getHeight(); Matrix matrix = new Matrix(); float scaleWidht = ((float) w / width); float scaleHeight = ((float) h / height); matrix.postScale(scaleWidht, scaleHeight); return Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, true); } /** * 旋转图片 * @param bitmap 要旋转的图片 * @param angle 旋转角度 * @return bitmap */ public static Bitmap rotate(Bitmap bitmap,int angle) { Matrix matrix = new Matrix(); matrix.postRotate(angle); return Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true); } /** * 圆形图片 *@param source 位图 * @param strokeWidth 裁剪范围 0表示最大 * @param bl 是否需要描边 * @param bl 画笔粗细 * @param bl 颜色代码 * @return bitmap */ public static Bitmap createCircleBitmap(Bitmap source, int strokeWidth, boolean bl,int edge,int color) { int diameter = source.getWidth() < source.getHeight() ? source.getWidth() : source.getHeight(); Bitmap target = Bitmap.createBitmap(diameter, diameter, Config.ARGB_8888); Canvas canvas = new Canvas(target);//创建画布 Paint paint = new Paint(); paint.setAntiAlias(true); canvas.drawCircle(diameter / 2, diameter / 2, diameter / 2, paint);//绘制圆形 paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));//取相交裁剪 canvas.drawBitmap(source, strokeWidth, strokeWidth, paint); if(bl) { if (color == 0) color = 0xFFFEA248;//默认橘黄色 paint.setColor(color); paint.setStyle(Paint.Style.STROKE);//描边 paint.setStrokeWidth(edge); canvas.drawCircle(diameter / 2, diameter / 2, diameter / 2, paint); } return target; } /** * 圆角图片 * @param bitmap 位图 * @param rx x方向上的圆角半径 * @param ry y方向上的圆角半径 * @param bl 是否需要描边 * @param bl 画笔粗细 * @param bl 颜色代码 * @return bitmap */ public static Bitmap createCornerBitmap(Bitmap bitmap,int rx,int ry,boolean bl,int edge,int color) { Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Config.ARGB_8888); Canvas canvas = new Canvas(output);//创建画布 Paint paint = new Paint(); paint.setAntiAlias(true); Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight()); RectF rectF = new RectF(rect); canvas.drawRoundRect(rectF, rx, ry, paint);//绘制圆角矩形 paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));//取相交裁剪 canvas.drawBitmap(bitmap, rect, rect, paint); if(bl) { if (color == 0) color = 0xFFFEA248;//默认橘黄色 paint.setColor(color); paint.setColor(color); paint.setStyle(Paint.Style.STROKE);//描边 paint.setStrokeWidth(edge); canvas.drawRoundRect(rectF, rx, ry, paint); } return output; } /** * 按比例裁剪图片 * @param bitmap 位图 * @param wScale 裁剪宽 0~100% * @param hScale 裁剪高 0~100% * @return bitmap */ public static Bitmap cropBitmap(Bitmap bitmap, float wScale, float hScale) { int w = bitmap.getWidth(); int h = bitmap.getHeight(); int wh = (int) (w * wScale); int hw = (int) (h * hScale); int retX = (int) (w * (1 - wScale) / 2); int retY = (int) (h * (1 - hScale) / 2); return Bitmap.createBitmap(bitmap, retX, retY, wh, hw, null, false); } /** * 获得带倒影的图片方法 * @param bitmap 位图 * @param region 倒影区域 0.1~1 * @return bitmap */ public static Bitmap createReflectionBitmap(Bitmap bitmap,float region) { int width = bitmap.getWidth(); int height = bitmap.getHeight(); Matrix matrix = new Matrix(); matrix.preScale(1, -1);//镜像缩放 Bitmap reflectionBitmap = Bitmap.createBitmap( bitmap,0 , (int)(height*(1-region))//从哪个点开始绘制 , width ,(int) (height*region)//绘制多高 , matrix, false); Bitmap reflectionWithBitmap = Bitmap.createBitmap(width,height+ (int) (height*region), Config.ARGB_8888); Canvas canvas = new Canvas(reflectionWithBitmap); canvas.drawBitmap(bitmap, 0, 0, null); canvas.drawBitmap(reflectionBitmap, 0, height , null); LinearGradient shader = new LinearGradient(0, bitmap.getHeight(), 0, reflectionWithBitmap.getHeight() , 0x70ffffff, 0x00ffffff, TileMode.CLAMP); Paint paint = new Paint(); paint.setShader(shader); paint.setXfermode(new PorterDuffXfermode(Mode.DST_IN));//取两层绘制交集。显示下层。 canvas.drawRect(0, height, width, reflectionWithBitmap.getHeight() , paint); return reflectionWithBitmap; } /** * 图片质量压缩 * @param bitmap * @param many 百分比 * @return */ public static Bitmap compressBitmap(Bitmap bitmap, float many){ ByteArrayOutputStream baos = new ByteArrayOutputStream(); bitmap.compress(Bitmap.CompressFormat.JPEG, (int)many*100, baos); ByteArrayInputStream isBm = new ByteArrayInputStream(baos.toByteArray()); return BitmapFactory.decodeStream(isBm, null, null); } /** * 高级图片质量压缩 *@param bitmap 位图 * @param maxSize 压缩后的大小,单位kb */ public static Bitmap imageZoom(Bitmap bitmap, double maxSize) { // 将bitmap放至数组中,意在获得bitmap的大小(与实际读取的原文件要大) ByteArrayOutputStream baos = new ByteArrayOutputStream(); // 格式、质量、输出流 bitmap.compress(Bitmap.CompressFormat.PNG, 70, baos); byte[] b = baos.toByteArray(); // 将字节换成KB double mid = b.length / 1024; // 获取bitmap大小 是允许最大大小的多少倍 double i = mid / maxSize; // 判断bitmap占用空间是否大于允许最大空间 如果大于则压缩 小于则不压缩 doRecycledIfNot(bitmap); if (i > 1) { // 缩放图片 此处用到平方根 将宽带和高度压缩掉对应的平方根倍 // (保持宽高不变,缩放后也达到了最大占用空间的大小) return scaleWithWH(bitmap,bitmap.getWidth() / Math.sqrt(i), bitmap.getHeight() / Math.sqrt(i)); } return null; } /*** * 图片缩放 *@param bitmap 位图 * @param w 新的宽度 * @param h 新的高度 * @return Bitmap */ public static Bitmap scaleWithWH(Bitmap bitmap, double w, double h) { if (w == 0 || h == 0 || bitmap == null) { return bitmap; } else { int width = bitmap.getWidth(); int height = bitmap.getHeight(); Matrix matrix = new Matrix(); float scaleWidth = (float) (w / width); float scaleHeight = (float) (h / height); matrix.postScale(scaleWidth, scaleHeight); return Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, true); } } /** * YUV视频流格式转bitmap * @param data YUV视频流格式 * @return width 设置宽度 * @return width 设置高度 */ public static Bitmap getBitmap(byte[] data, int width, int height) { Bitmap bitmap; YuvImage yuvimage = new YuvImage(data, ImageFormat.NV21, width, height, null); //data是onPreviewFrame参数提供 ByteArrayOutputStream baos = new ByteArrayOutputStream(); yuvimage.compressToJpeg(new Rect(0, 0, yuvimage.getWidth(), yuvimage.getHeight()), 100, baos);// // 80--JPG图片的质量[0-100],100最高 byte[] rawImage = baos.toByteArray(); BitmapFactory.Options options = new BitmapFactory.Options(); SoftReference<Bitmap> softRef = new SoftReference<Bitmap>(BitmapFactory.decodeByteArray(rawImage, 0, rawImage .length, options)); bitmap = softRef.get(); return bitmap; } /** * 图片路径转bitmap * @param file 图片的绝对路径 * @return bitmap */ public static Bitmap getAssetImage(String file) { Bitmap bitmap = null; AssetManager am = mActivity.getAssets(); try { InputStream is = am.open(file); bitmap = BitmapFactory.decodeStream(is); is.close(); } catch (IOException e) { e.printStackTrace(); } return bitmap; } /** * bitmap保存到指定路径 * @param file 图片的绝对路径 * @param file 位图 * @return bitmap */ public static boolean saveFile(String file, Bitmap bmp) { if(TextUtils.isEmpty(file) || bmp == null) return false; File f = new File(file); if (f.exists()) { f.delete(); }else { File p = f.getParentFile(); if(!p.exists()) { p.mkdirs(); } } try { FileOutputStream out = new FileOutputStream(f); bmp.compress(Bitmap.CompressFormat.JPEG, 100, out); out.flush(); out.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); return false; } return true; } /** * 回收一个未被回收的Bitmap *@param bitmap */ public static void doRecycledIfNot(Bitmap bitmap) { if (!bitmap.isRecycled()) { bitmap.recycle(); } } }

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持靠谱客。

最后

以上就是结实乌冬面最近收集整理的关于Android BitmapUtils工具类使用详解的全部内容,更多相关Android内容请搜索靠谱客的其他文章。

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

评论列表共有 0 条评论

立即
投稿
返回
顶部