ImageUtil.java
11.6 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
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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
package com.metroapp.forum;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.graphics.Bitmap.Config;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.media.ExifInterface;
import android.text.TextUtils;
import com.douwan.peacemetro.R;
import com.metroapp.MainApplication;
import com.metroapp.constant.StorageType;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
public class ImageUtil {
public static class ImageSize {
public int width = 0;
public int height = 0;
public ImageSize(int width, int height) {
this.width = width;
this.height = height;
}
}
public final static float MAX_IMAGE_RATIO = 5f;
public static final Bitmap getBitmapImmutableCopy(Resources res, int id) {
return getBitmap(res.getDrawable(id)).copy(Config.RGB_565, false);
}
public static final Bitmap getBitmap(Drawable dr) {
if (dr == null) {
return null;
}
if (dr instanceof BitmapDrawable) {
return ((BitmapDrawable) dr).getBitmap();
}
return null;
}
public static Bitmap rotateBitmapInNeeded(String path, Bitmap srcBitmap) {
if (TextUtils.isEmpty(path) || srcBitmap == null) {
return null;
}
ExifInterface localExifInterface;
try {
localExifInterface = new ExifInterface(path);
int rotateInt = localExifInterface.getAttributeInt(
ExifInterface.TAG_ORIENTATION,
ExifInterface.ORIENTATION_NORMAL);
float rotate = getImageRotate(rotateInt);
if (rotate != 0) {
Matrix matrix = new Matrix();
matrix.postRotate(rotate);
Bitmap dstBitmap = Bitmap.createBitmap(srcBitmap, 0, 0,
srcBitmap.getWidth(), srcBitmap.getHeight(), matrix,
false);
if (dstBitmap == null) {
return srcBitmap;
} else {
if (srcBitmap != null && !srcBitmap.isRecycled()) {
srcBitmap.recycle();
}
return dstBitmap;
}
} else {
return srcBitmap;
}
} catch (IOException e) {
e.printStackTrace();
return srcBitmap;
}
}
/**
* 获得旋转角度
*
* @param rotate
* @return
*/
public static float getImageRotate(int rotate) {
float f;
if (rotate == 6) {
f = 90.0F;
} else if (rotate == 3) {
f = 180.0F;
} else if (rotate == 8) {
f = 270.0F;
} else {
f = 0.0F;
}
return f;
}
public static Boolean scaleThumbnail(File srcFile, File dstFile, int dstMaxWH, int dstMinWH, CompressFormat compressFormat, int quality) {
Boolean bRet = false;
Bitmap srcBitmap = null;
Bitmap dstBitmap = null;
BufferedOutputStream bos = null;
try {
int[] bound =BitmapDecoder.decodeBound(srcFile);
ImageSize size = getThumbnailDisplaySize(bound[0], bound[1], dstMaxWH, dstMinWH);
srcBitmap =BitmapDecoder.decodeSampled(srcFile.getPath(), size.width, size.height);
// 旋转
ExifInterface localExifInterface = new ExifInterface(srcFile.getAbsolutePath());
int rotateInt = localExifInterface.getAttributeInt(
ExifInterface.TAG_ORIENTATION,
ExifInterface.ORIENTATION_NORMAL);
float rotate = getImageRotate(rotateInt);
Matrix matrix = new Matrix();
matrix.postRotate(rotate);
float inSampleSize = 1;
if (srcBitmap.getWidth() >= dstMinWH && srcBitmap.getHeight() <= dstMaxWH
&& srcBitmap.getWidth() >= dstMinWH && srcBitmap.getHeight() <= dstMaxWH) {
//如果第一轮拿到的srcBitmap尺寸都符合要求,不需要再做缩放
} else {
if (srcBitmap.getWidth() != size.width || srcBitmap.getHeight() != size.height) {
float widthScale = (float) size.width / (float) srcBitmap.getWidth();
float heightScale = (float) size.height / (float) srcBitmap.getHeight();
if (widthScale >= heightScale) {
size.width = srcBitmap.getWidth();
size.height /= widthScale;//必定小于srcBitmap.getHeight()
inSampleSize = widthScale;
} else {
size.width /= heightScale;//必定小于srcBitmap.getWidth()
size.height = srcBitmap.getHeight();
inSampleSize = heightScale;
}
}
}
matrix.postScale(inSampleSize, inSampleSize);
if (rotate == 0 && inSampleSize == 1) {
dstBitmap = srcBitmap;
} else {
dstBitmap = Bitmap.createBitmap(srcBitmap, 0, 0, size.width, size.height, matrix, true);
}
bos = new BufferedOutputStream(new FileOutputStream(dstFile));
dstBitmap.compress(compressFormat, quality, bos);
bos.flush();
bRet = true;
} catch (OutOfMemoryError e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (bos != null) {
try {
bos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (srcBitmap != null && !srcBitmap.isRecycled()) {
srcBitmap.recycle();
srcBitmap = null;
}
if (dstBitmap != null && !dstBitmap.isRecycled()) {
dstBitmap.recycle();
dstBitmap = null;
}
}
return bRet;
}
public static ImageSize getThumbnailDisplaySize(float srcWidth, float srcHeight, float dstMaxWH, float dstMinWH) {
if (srcWidth <= 0 || srcHeight <= 0) { // bounds check
return new ImageSize((int)dstMinWH, (int)dstMinWH);
}
float shorter;
float longer;
boolean widthIsShorter;
//store
if (srcHeight < srcWidth) {
shorter = srcHeight;
longer = srcWidth;
widthIsShorter = false;
} else {
shorter = srcWidth;
longer = srcHeight;
widthIsShorter = true;
}
if (shorter < dstMinWH) {
float scale = dstMinWH / shorter;
shorter = dstMinWH;
if (longer * scale > dstMaxWH) {
longer = dstMaxWH;
} else {
longer *= scale;
}
}
else if (longer > dstMaxWH) {
float scale = dstMaxWH / longer;
longer = dstMaxWH;
if (shorter * scale < dstMinWH) {
shorter = dstMinWH;
} else {
shorter *= scale;
}
}
//restore
if (widthIsShorter) {
srcWidth = shorter;
srcHeight = longer;
} else {
srcWidth = longer;
srcHeight = shorter;
}
return new ImageSize((int) srcWidth, (int) srcHeight);
}
private static String getTempFilePath(String extension) {
return StorageUtil.getWritePath(
MainApplication._this,
"temp_image_" + StringUtil.get36UUID() + "." + extension,
StorageType.TYPE_TEMP);
}
public static Boolean scaleImage(File srcFile, File dstFile, int dstMaxWH, CompressFormat compressFormat, int quality) {
Boolean success = false;
try {
int inSampleSize = SampleSizeUtil.calculateSampleSize(srcFile.getAbsolutePath(), dstMaxWH * dstMaxWH);
Bitmap srcBitmap = BitmapDecoder.decodeSampled(srcFile.getPath(), inSampleSize);
if (srcBitmap == null) {
return success;
}
// 旋转
ExifInterface localExifInterface = new ExifInterface(srcFile.getAbsolutePath());
int rotateInt = localExifInterface.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
float rotate = getImageRotate(rotateInt);
Bitmap dstBitmap;
float scale = (float) Math.sqrt(((float) dstMaxWH * (float) dstMaxWH) / ((float) srcBitmap.getWidth() * (float) srcBitmap.getHeight()));
if (rotate == 0f && scale >= 1) {
dstBitmap = srcBitmap;
} else {
try {
Matrix matrix = new Matrix();
if (rotate != 0) {
matrix.postRotate(rotate);
}
if (scale < 1) {
matrix.postScale(scale, scale);
}
dstBitmap = Bitmap.createBitmap(srcBitmap, 0, 0, srcBitmap.getWidth(), srcBitmap.getHeight(), matrix, true);
} catch (OutOfMemoryError e) {
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(dstFile));
srcBitmap.compress(compressFormat, quality, bos);
bos.flush();
bos.close();
success = true;
if (!srcBitmap.isRecycled())
srcBitmap.recycle();
srcBitmap = null;
return success;
}
}
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(dstFile));
dstBitmap.compress(compressFormat, quality, bos);
bos.flush();
bos.close();
success = true;
if (!srcBitmap.isRecycled())
srcBitmap.recycle();
srcBitmap = null;
if (!dstBitmap.isRecycled())
dstBitmap.recycle();
dstBitmap = null;
} catch (Exception e) {
e.printStackTrace();
} catch (OutOfMemoryError e) {
e.printStackTrace();
}
return success;
}
public static Bitmap getDefaultBitmapWhenGetFail() {
try {
return getBitmapImmutableCopy(MainApplication._this.getResources(), R.drawable.nim_image_download_failed);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
public static ImageSize getThumbnailDisplaySize(int maxSide, int minSide, String imagePath) {
int[] bound = BitmapDecoder.decodeBound(imagePath);
ImageSize imageSize = getThumbnailDisplaySize(bound[0], bound[1], maxSide, minSide);
return imageSize;
}
public static boolean isInvalidPictureFile(String mimeType) {
String lowerCaseFilepath = mimeType.toLowerCase();
return (lowerCaseFilepath.contains("jpg") || lowerCaseFilepath.contains("jpeg")
|| lowerCaseFilepath.toLowerCase().contains("png") || lowerCaseFilepath.toLowerCase().contains("bmp") || lowerCaseFilepath
.toLowerCase().contains("gif"));
}
/**
* return a bitmap from service
* @param url
* @return bitmap type
*/
public final static Bitmap returnBitMap(String url) {
URL myFileUrl = null;
Bitmap bitmap = null;
try {
myFileUrl = new URL(url);
HttpURLConnection conn;
conn = (HttpURLConnection) myFileUrl.openConnection();
conn.setDoInput(true);
conn.connect();
InputStream is = conn.getInputStream();
bitmap = BitmapFactory.decodeStream(is);
} catch (IOException e) {
e.printStackTrace();
}
return bitmap;
}
public static boolean isGif(String extension) {
return !TextUtils.isEmpty(extension) && extension.toLowerCase().equals("gif");
}
}