-
Notifications
You must be signed in to change notification settings - Fork 77
/
Copy pathImageTakerHelper.java
373 lines (344 loc) · 12.4 KB
/
ImageTakerHelper.java
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
366
367
368
369
370
371
372
373
/*
* Copyright 2017 [AllenCoder]
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.allen.mediautil;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.media.ExifInterface;
import android.net.Uri;
import android.os.Build;
import android.provider.MediaStore;
import android.support.v4.app.Fragment;
import android.support.v4.content.CursorLoader;
import android.support.v4.content.FileProvider;
import android.text.TextUtils;
import android.util.Log;
import android.widget.Toast;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
/**
* 从相机或者相册取图片辅助类
* <p>
* 需要 READ_EXTERNAL_STORAGE 和 WRITE_EXTERNAL_STORAGE 权限
*/
public class ImageTakerHelper {
/**
* 最大图片宽高
*/
public static final int MAX_IMAGE_SIZE = 2560;
/**
* 缩略图最大宽高
**/
public static final int MAX_THUMB_IMAGE_SIZE = 120;
/**
* 保存的图片质量
*/
public static final int IMG_QUALITY = 80;
/**
* 请求相册图片类型
**/
public static final String MEDIA_TYPE_IMAGE = "image/*";
public static final String THUMB_IMG_SUFFIX = "-thumb";
/**
* 图片临时存储后缀
**/
public static final String IMG_TEMP_STORE_SUFFIX = ".jpg";
public static final String IMG_STORE_SUFFIX = ".webp";
public static final int REQUEST_CAMERA = 0x10;
public static final int REQUEST_ALBUM = 0x11;
/**
* 图片临时保存格式
*/
public static final Bitmap.CompressFormat IMG_TEMP_FORMAT = Bitmap.CompressFormat.JPEG;
/**
* 图片最终保存格式
*/
public static final Bitmap.CompressFormat IMG_FINAL_FORMAT = Bitmap.CompressFormat.WEBP;
/**
* 根据图片名称获取其缩略图名称
*
* @param imageName 图片名称
* @return 缩略图名称
*/
public static String getAccountThumbNameFromImgName(String imageName) {
if (TextUtils.isEmpty(imageName)) {
return null;
}
int idx = imageName.lastIndexOf(".");
if (idx == -1) {
return imageName + THUMB_IMG_SUFFIX;
}
return imageName.substring(0, idx) + THUMB_IMG_SUFFIX + imageName.substring(idx);
}
/**
* 根据原始文件名获取保存的文件名(添加后缀)
*
* @param name 文件名
* @param isThumbnail true 缩略图文件名,false原图文件名
* @return 文件名
*/
public static String getImageSaveName(String name, Bitmap.CompressFormat format, boolean isThumbnail) {
return name + (isThumbnail ? THUMB_IMG_SUFFIX : "") + (format == Bitmap.CompressFormat.WEBP ? ".webp"
: (format == Bitmap.CompressFormat.PNG ? ".png" : ".jpg"));
}
/**
* 打开相册获取图片 调用onActivityResult()获取图片
*/
public static void openAlbum(Activity activity) {
Intent intentAlbum = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
intentAlbum.setType("image/*");
activity.startActivityForResult(Intent.createChooser(intentAlbum, "选择图片"), REQUEST_ALBUM);
}
/**
* 打开相册获取图片 调用onActivityResult()获取图片
*/
public static void openAlbum(Fragment fragment) {
Intent intentAlbum = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
intentAlbum.setType(MEDIA_TYPE_IMAGE);
fragment.startActivityForResult(Intent.createChooser(intentAlbum, "选择图片"), REQUEST_ALBUM);
}
/**
* 打开相机获取图片
* <p>
* 调用onActivityResult()获取图片
*/
public static void openCamera(Activity activity,String authority ) {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
intent.putExtra(MediaStore.EXTRA_OUTPUT, getOutputPictureUri(activity.getApplicationContext(),authority));
activity.startActivityForResult(intent, REQUEST_CAMERA);
}
/**
* 获取相机拍照存储的图片位置
* 为了保证第三方相机也可以保存图片,因此设置为存储卡指定文件夹
*
* @param context Context
* @return 文件位置
*/
public static File getTmpSaveFilePath(Context context) {
File f = new File(context.getExternalCacheDir(), "/camera/tmp.jpg");
if (!f.getParentFile().exists()) {
f.getParentFile().mkdirs();
}
return f;
}
/**
* 获取相机拍照存储的图片位置Uri
*
* @param context
* @param authority fileProvider 名字
* @return
*/
public static Uri getOutputPictureUri(Context context, String authority) {
// String authority = context.getString(R.string.provider_authority);
File saveFile = getTmpSaveFilePath(context);
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.M) {
try {
return FileProvider.getUriForFile(context, authority, saveFile);
} catch (Exception e) {
return Uri.fromFile(saveFile);
}
} else {
return Uri.fromFile(saveFile);
}
}
/**
* 获取指定位置的fileuri
*
* @param context
* @param file
* @param authority fileProvider name
* @return
*/
public static Uri getOutputPictureUri(Context context, File file, String authority) {
// String authority = context.getString(R.string.provider_authority);
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.M) {
try {
return FileProvider.getUriForFile(context, authority, file);
} catch (Exception e) {
return Uri.fromFile(file);
}
} else {
return Uri.fromFile(file);
}
}
/**
* 从相册返回结果中取图片
*
* @param data 相册返回结果
* @return 相册的图片位置
*/
public static String readBitmapFromAlbumResult(Context context, Intent data) {
Uri imageUri = data.getData();
if (imageUri == null) {
return null;
} else if (imageUri.toString().startsWith("file:///")) {
try {
// 尝试读取图片
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(imageUri.getPath(), options);
return imageUri.getPath();
} catch (Exception ex) {
Toast.makeText(context.getApplicationContext(), "无法读取该文件夹图片,请选择其他图片", Toast.LENGTH_SHORT).show();
return null;
}
} else {
String[] projection = {MediaStore.MediaColumns.DATA};
CursorLoader cursorLoader = new CursorLoader(context, imageUri, projection, null, null, null);
Cursor cursor = cursorLoader.loadInBackground();
int column_index = cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.DATA);
cursor.moveToFirst();
String selectedImagePath = cursor.getString(column_index);
cursor.close();
return selectedImagePath;
}
}
/**
* 从相机结果取照片
*
* @param data 相机返回结果
*/
public static String readBitmapFromCameraResult(Context context, Intent data) {
return String.valueOf(getTmpSaveFilePath(context).getPath());
}
/**
* 压缩裁剪旋转图片
* <p>
* 保存图片到指定文件夹,并保存缩略图
*
* @param originalImagePath 图片路径
* @param destDir 保存到的文件夹
* @param saveName 保存文件名(不带后缀) 缩略图最大宽高
* @return 压缩裁剪后的图片位置
*/
public static String saveAccountImg(String originalImagePath, File destDir, String saveName) {
Bitmap.CompressFormat format = IMG_TEMP_FORMAT;
int maxSize = MAX_IMAGE_SIZE;
int thumbnailMaxSize = MAX_THUMB_IMAGE_SIZE;
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(originalImagePath, options);
int scale = 1;
int size = Math.max(options.outWidth, options.outHeight);
while (size / scale > maxSize) {
scale *= 2;
}
int degree = readPictureDegree(originalImagePath);
options.inSampleSize = scale;
options.inJustDecodeBounds = false;
Bitmap bm = BitmapFactory.decodeFile(originalImagePath, options);
if (degree != 0) {
Bitmap bp = rotateImageView(degree, bm);
if (bp != bm) {
bm.recycle();
}
bm = bp;
}
// 保存缩放后的图片
File imageSavePos = new File(destDir, getImageSaveName(saveName, format, false));
FileOutputStream fos = null;
try {
fos = new FileOutputStream(imageSavePos);
bm.compress(format, 100, fos);
fos.flush();
} catch (IOException e) {
e.printStackTrace();
} finally {
closeSilent(fos);
}
bm.recycle();
// 保存缩略图
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(originalImagePath, options);
scale = 1;
size = Math.max(options.outWidth, options.outHeight);
while (size / scale > thumbnailMaxSize) {
scale *= 2;
}
options.inJustDecodeBounds = false;
options.inSampleSize = scale;
Bitmap thumb = BitmapFactory.decodeFile(originalImagePath, options);
try {
fos = new FileOutputStream(new File(destDir, getImageSaveName(saveName, format, true)));
thumb.compress(format, 100, fos);
fos.flush();
} catch (IOException e) {
e.printStackTrace();
} finally {
closeSilent(fos);
}
thumb.recycle();
return imageSavePos.getAbsolutePath();
}
/**
* 旋转图片
*
* @param angle angle
* @param bitmap bitmap
* @return Bitmap
*/
private static Bitmap rotateImageView(int angle, Bitmap bitmap) {
Matrix matrix = new Matrix();
matrix.postRotate(angle);
return Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
}
/**
* 读取图片属性:旋转的角度
*
* @param path 图片绝对路径
* @return degree旋转的角度
*/
private static int readPictureDegree(String path) {
int degree = 0;
try {
ExifInterface exifInterface = new ExifInterface(path);
int orientation = exifInterface.getAttributeInt(ExifInterface.TAG_ORIENTATION,
ExifInterface.ORIENTATION_NORMAL);
switch (orientation) {
case ExifInterface.ORIENTATION_ROTATE_90:
degree = 90;// SUPPRESS CHECKSTYLE
break;
case ExifInterface.ORIENTATION_ROTATE_180:
degree = 180;// SUPPRESS CHECKSTYLE
break;
case ExifInterface.ORIENTATION_ROTATE_270:
degree = 270;// SUPPRESS CHECKSTYLE
break;
default:
break;
}
} catch (IOException e) {
Log.e("readPictureDegree", "readPictureDegree failed", e);
}
return degree;
}
private static void closeSilent(OutputStream outputStream) {
if (outputStream != null) {
try {
outputStream.close();
} catch (IOException e) {
// null
}
}
}
}