BitmapUtil.java
9.11 KB
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
package com.glsx.commonres.util;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.BitmapFactory;
import android.graphics.BitmapFactory.Options;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PixelFormat;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.util.TypedValue;
import androidx.annotation.RequiresApi;
import com.blankj.utilcode.util.LogUtils;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.concurrent.ExecutionException;
import static android.os.FileUtils.copy;
@SuppressWarnings("deprecation")
public class BitmapUtil {
private static final String TAG = BitmapUtil.class.getSimpleName();
public static Bitmap sNullBitmap = Bitmap.createBitmap(1, 1, Config.RGB_565);
public static Bitmap makeBitmap(int minSideLength, int maxNumOfPixels, String filepath) {
if (filepath != null && !filepath.startsWith("/")) {
return makeNetBitmap(minSideLength, maxNumOfPixels, filepath);
}
try {
Options options = new Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(filepath, options);
if (options.mCancel || options.outWidth == -1 || options.outHeight == -1) {
return null;
}
options.inSampleSize = computeSampleSize(options, minSideLength, maxNumOfPixels);
options.inJustDecodeBounds = false;
options.inDither = false;
options.inPreferredConfig = Config.ARGB_8888;
return BitmapFactory.decodeFile(filepath, options);
} catch (OutOfMemoryError ex) {
LogUtils.e(TAG, "Got oom exception ", ex);
return null;
} finally {
}
}
public static Bitmap makeNetBitmap(int minSideLength, int maxNumOfPixels, String uri) {
Bitmap bitmap = null;
try {
Options opt = new Options();
opt.inJustDecodeBounds = true;
opt.inDither = true;
opt.inPreferredConfig = Config.RGB_565;
URL p_url = new URL(uri);
InputStream inStream = p_url.openStream();
BitmapFactory.decodeStream(inStream, null, opt);
inStream.close();
opt.inSampleSize = computeSampleSize(opt, minSideLength, maxNumOfPixels);
opt.inJustDecodeBounds = false;
inStream = p_url.openStream();
bitmap = BitmapFactory.decodeStream(inStream, null, opt);
inStream.close();
} catch (Exception e) {
e.printStackTrace();
}
return bitmap;
}
public static int computeSampleSize(Options options, int minSideLength, int maxNumOfPixels) {
int initialSize = computeInitialSampleSize(options, minSideLength, maxNumOfPixels);
int roundedSize;
if (initialSize <= 8) {
roundedSize = 1;
while (roundedSize < initialSize) {
roundedSize <<= 1;
}
} else {
roundedSize = (initialSize + 7) / 8 * 8;
}
return roundedSize;
}
private static int computeInitialSampleSize(Options options, int minSideLength, int maxNumOfPixels) {
double w = options.outWidth;
double h = options.outHeight;
int lowerBound = (maxNumOfPixels == -1) ? 1 : (int) Math.ceil(Math.sqrt(w * h / maxNumOfPixels));
int upperBound = (minSideLength == -1) ? 128 : (int) Math.min(Math.floor(w / minSideLength),
Math.floor(h / minSideLength));
if (upperBound < lowerBound) {
// return the larger one when there is no overlapping zone.
return lowerBound;
}
if ((maxNumOfPixels == -1) && (minSideLength == -1)) {
return 1;
} else if (minSideLength == -1) {
return lowerBound;
} else {
return upperBound;
}
}
public static Bitmap getLocalBitmap(String url) {
try {
FileInputStream fis = new FileInputStream(url);
return BitmapFactory.decodeStream(fis); // /把流转化为Bitmap图片
} catch (FileNotFoundException e) {
e.printStackTrace();
return null;
}
}
public static Bitmap drawWXMiniBitmap(Bitmap bitmap, int width, int height) {
Bitmap mBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
// 用这个Bitmap生成一个Canvas,然后canvas就会把内容绘制到上面这个bitmap中
Canvas mCanvas = new Canvas(mBitmap);
// 绘制画笔
Paint mPicturePaint = new Paint();
// 绘制背景图片
mCanvas.drawBitmap(mBitmap, 0.0f, 0.0f, mPicturePaint);
// 绘制图片的宽、高
int width_head = bitmap.getWidth();
int height_head = bitmap.getHeight();
// 绘制图片--保证其在水平方向居中
mCanvas.drawBitmap(bitmap, (width - width_head) / 2, (height - height_head) / 2,
mPicturePaint);
// 保存绘图为本地图片
mCanvas.save();
mCanvas.restore();
return mBitmap;
}
/**
* Bitmap转换成byte[]并且进行压缩,压缩到不大于maxkb
*
* @param bitmap
* @param maxKb
* @return
*/
public static byte[] createBitmapThumbnail(Bitmap bitmap, int maxKb) {
ByteArrayOutputStream output = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, output);
int options = 100;
while (output.toByteArray().length > maxKb && options != 10) {
output.reset();
bitmap.compress(Bitmap.CompressFormat.JPEG, options, output);
options -= 10;
}
return output.toByteArray();
}
public static Bitmap drawableToBitmap(Drawable drawable) {
if (drawable == null) {
return null;
}
// 获取 drawable 长宽
int width = drawable.getIntrinsicWidth();
int heigh = drawable.getIntrinsicHeight();
drawable.setBounds(0, 0, width, heigh);
// 获取drawable的颜色格式
Bitmap.Config config = drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888
: Bitmap.Config.RGB_565;
// 创建bitmap
Bitmap bitmap = Bitmap.createBitmap(width, heigh, config);
// 创建bitmap画布
Canvas canvas = new Canvas(bitmap);
// 将drawable 内容画到画布中
drawable.draw(canvas);
return bitmap;
}
public static Bitmap getBitmap(Context context, int resId) {
BitmapFactory.Options options = new BitmapFactory.Options();
TypedValue value = new TypedValue();
context.getResources().openRawResource(resId, value);
options.inTargetDensity = value.density;
options.inScaled = false;
return BitmapFactory.decodeResource(context.getResources(), resId, options);
}
@RequiresApi(api = Build.VERSION_CODES.Q)
public static Bitmap GetLocalOrNetBitmap(String url) {
Bitmap bitmap = null;
InputStream in = null;
BufferedOutputStream out = null;
try {
in = new BufferedInputStream(new URL(url).openStream(), 2048);
final ByteArrayOutputStream dataStream = new ByteArrayOutputStream();
out = new BufferedOutputStream(dataStream, 2048);
copy(in, out);
out.flush();
byte[] data = dataStream.toByteArray();
bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
data = null;
return bitmap;
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
public static byte[] bmpToByteArray(final Bitmap bmp, final boolean needRecycle) {
int i;
int j;
if (bmp.getHeight() > bmp.getWidth()) {
i = bmp.getWidth();
j = bmp.getWidth();
} else {
i = bmp.getHeight();
j = bmp.getHeight();
}
Bitmap localBitmap = Bitmap.createBitmap(i, j, Bitmap.Config.RGB_565);
Canvas localCanvas = new Canvas(localBitmap);
while (true) {
localCanvas.drawBitmap(bmp, new Rect(0, 0, i, j), new Rect(0, 0,i, j), null);
if (needRecycle)
bmp.recycle();
ByteArrayOutputStream localByteArrayOutputStream = new ByteArrayOutputStream();
localBitmap.compress(Bitmap.CompressFormat.JPEG, 100,
localByteArrayOutputStream);
localBitmap.recycle();
byte[] arrayOfByte = localByteArrayOutputStream.toByteArray();
try {
localByteArrayOutputStream.close();
return arrayOfByte;
} catch (Exception e) {
//F.out(e);
}
i = bmp.getHeight();
j = bmp.getHeight();
}
}
}