|
| 1 | +package com.blankj.utilcode.utils; |
| 2 | + |
| 3 | +import android.app.Activity; |
| 4 | +import android.content.ContentResolver; |
| 5 | +import android.content.Intent; |
| 6 | +import android.database.Cursor; |
| 7 | +import android.graphics.Bitmap; |
| 8 | +import android.net.Uri; |
| 9 | +import android.os.Bundle; |
| 10 | +import android.provider.MediaStore; |
| 11 | +import android.util.Log; |
| 12 | + |
| 13 | +import java.io.File; |
| 14 | +import java.io.FileOutputStream; |
| 15 | +import java.io.IOException; |
| 16 | + |
| 17 | +/** |
| 18 | + * <pre> |
| 19 | + * author: Blankj |
| 20 | + * blog : http://blankj.com |
| 21 | + * time : 2016/9/19 |
| 22 | + * desc : 相机相关工具类 |
| 23 | + * </pre> |
| 24 | + */ |
| 25 | +public class CameraUtils { |
| 26 | + |
| 27 | + private CameraUtils() { |
| 28 | + throw new UnsupportedOperationException("u can't fuck me..."); |
| 29 | + } |
| 30 | + |
| 31 | + /** |
| 32 | + * 获取打开照程序界面的Intent |
| 33 | + */ |
| 34 | + public static Intent getOpenCameraIntent() { |
| 35 | + return new Intent(MediaStore.ACTION_IMAGE_CAPTURE); |
| 36 | + } |
| 37 | + |
| 38 | + /** |
| 39 | + * 获取跳转至相册选择界面的Intent |
| 40 | + */ |
| 41 | + public static Intent getImagePickerIntent() { |
| 42 | + Intent intent = new Intent(Intent.ACTION_PICK, null); |
| 43 | + return intent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*"); |
| 44 | + } |
| 45 | + |
| 46 | + /** |
| 47 | + * 获取[跳转至相册选择界面,并跳转至裁剪界面,默认可缩放裁剪区域]的Intent |
| 48 | + */ |
| 49 | + public static Intent getImagePickerIntent(int outputX, int outputY, Uri fromFileURI, |
| 50 | + Uri saveFileURI) { |
| 51 | + return getImagePickerIntent(1, 1, outputX, outputY, true, fromFileURI, saveFileURI); |
| 52 | + } |
| 53 | + |
| 54 | + /** |
| 55 | + * 获取[跳转至相册选择界面,并跳转至裁剪界面,默认可缩放裁剪区域]的Intent |
| 56 | + */ |
| 57 | + public static Intent getImagePickerIntent(int aspectX, int aspectY, int outputX, int outputY, Uri fromFileURI, |
| 58 | + Uri saveFileURI) { |
| 59 | + return getImagePickerIntent(aspectX, aspectY, outputX, outputY, true, fromFileURI, saveFileURI); |
| 60 | + } |
| 61 | + |
| 62 | + /** |
| 63 | + * 获取[跳转至相册选择界面,并跳转至裁剪界面,可以指定是否缩放裁剪区域]的Intent |
| 64 | + * |
| 65 | + * @param aspectX 裁剪框尺寸比例X |
| 66 | + * @param aspectY 裁剪框尺寸比例Y |
| 67 | + * @param outputX 输出尺寸宽度 |
| 68 | + * @param outputY 输出尺寸高度 |
| 69 | + * @param canScale 是否可缩放 |
| 70 | + * @param fromFileURI 文件来源路径URI |
| 71 | + * @param saveFileURI 输出文件路径URI |
| 72 | + */ |
| 73 | + public static Intent getImagePickerIntent(int aspectX, int aspectY, int outputX, int outputY, boolean canScale, |
| 74 | + Uri fromFileURI, Uri saveFileURI) { |
| 75 | + Intent intent = new Intent(Intent.ACTION_PICK); |
| 76 | + intent.setDataAndType(fromFileURI, "image/*"); |
| 77 | + intent.putExtra("crop", "true"); |
| 78 | + intent.putExtra("aspectX", aspectX <= 0 ? 1 : aspectX); |
| 79 | + intent.putExtra("aspectY", aspectY <= 0 ? 1 : aspectY); |
| 80 | + intent.putExtra("outputX", outputX); |
| 81 | + intent.putExtra("outputY", outputY); |
| 82 | + intent.putExtra("scale", canScale); |
| 83 | + // 图片剪裁不足黑边解决 |
| 84 | + intent.putExtra("scaleUpIfNeeded", true); |
| 85 | + intent.putExtra("return-data", false); |
| 86 | + intent.putExtra(MediaStore.EXTRA_OUTPUT, saveFileURI); |
| 87 | + intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString()); |
| 88 | + // 去除人脸识别 |
| 89 | + return intent.putExtra("noFaceDetection", true); |
| 90 | + } |
| 91 | + |
| 92 | + /** |
| 93 | + * 获取[跳转至相册选择界面,并跳转至裁剪界面,默认可缩放裁剪区域]的Intent |
| 94 | + */ |
| 95 | + public static Intent getCameraIntent(Uri saveFileURI) { |
| 96 | + Intent mIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); |
| 97 | + return mIntent.putExtra(MediaStore.EXTRA_OUTPUT, saveFileURI); |
| 98 | + } |
| 99 | + |
| 100 | + /** |
| 101 | + * 获取[跳转至裁剪界面,默认可缩放]的Intent |
| 102 | + */ |
| 103 | + public static Intent getCropImageIntent(int outputX, int outputY, Uri fromFileURI, |
| 104 | + Uri saveFileURI) { |
| 105 | + return getCropImageIntent(1, 1, outputX, outputY, true, fromFileURI, saveFileURI); |
| 106 | + } |
| 107 | + |
| 108 | + /** |
| 109 | + * 获取[跳转至裁剪界面,默认可缩放]的Intent |
| 110 | + */ |
| 111 | + public static Intent getCropImageIntent(int aspectX, int aspectY, int outputX, int outputY, Uri fromFileURI, |
| 112 | + Uri saveFileURI) { |
| 113 | + return getCropImageIntent(aspectX, aspectY, outputX, outputY, true, fromFileURI, saveFileURI); |
| 114 | + } |
| 115 | + |
| 116 | + |
| 117 | + /** |
| 118 | + * 获取[跳转至裁剪界面]的Intent |
| 119 | + */ |
| 120 | + public static Intent getCropImageIntent(int aspectX, int aspectY, int outputX, int outputY, boolean canScale, |
| 121 | + Uri fromFileURI, Uri saveFileURI) { |
| 122 | + Intent intent = new Intent("com.android.camera.action.CROP"); |
| 123 | + intent.setDataAndType(fromFileURI, "image/*"); |
| 124 | + intent.putExtra("crop", "true"); |
| 125 | + // X方向上的比例 |
| 126 | + intent.putExtra("aspectX", aspectX <= 0 ? 1 : aspectX); |
| 127 | + // Y方向上的比例 |
| 128 | + intent.putExtra("aspectY", aspectY <= 0 ? 1 : aspectY); |
| 129 | + intent.putExtra("outputX", outputX); |
| 130 | + intent.putExtra("outputY", outputY); |
| 131 | + intent.putExtra("scale", canScale); |
| 132 | + // 图片剪裁不足黑边解决 |
| 133 | + intent.putExtra("scaleUpIfNeeded", true); |
| 134 | + intent.putExtra("return-data", false); |
| 135 | + // 需要将读取的文件路径和裁剪写入的路径区分,否则会造成文件0byte |
| 136 | + intent.putExtra(MediaStore.EXTRA_OUTPUT, saveFileURI); |
| 137 | + // true-->返回数据类型可以设置为Bitmap,但是不能传输太大,截大图用URI,小图用Bitmap或者全部使用URI |
| 138 | + intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString()); |
| 139 | + // 取消人脸识别功能 |
| 140 | + intent.putExtra("noFaceDetection", true); |
| 141 | + return intent; |
| 142 | + } |
| 143 | + |
| 144 | + /** |
| 145 | + * 获得选中相册的图片 |
| 146 | + * |
| 147 | + * @param context 上下文 |
| 148 | + * @param data onActivityResult返回的Intent |
| 149 | + * @return bitmap |
| 150 | + */ |
| 151 | + public static Bitmap getChoosedImage(Activity context, Intent data) { |
| 152 | + if (data == null) return null; |
| 153 | + Bitmap bm = null; |
| 154 | + ContentResolver cr = context.getContentResolver(); |
| 155 | + Uri originalUri = data.getData(); |
| 156 | + try { |
| 157 | + bm = MediaStore.Images.Media.getBitmap(cr, originalUri); |
| 158 | + } catch (IOException e) { |
| 159 | + e.printStackTrace(); |
| 160 | + } |
| 161 | + return bm; |
| 162 | + } |
| 163 | + |
| 164 | + /** |
| 165 | + * 获得选中相册的图片路径 |
| 166 | + * |
| 167 | + * @param context 上下文 |
| 168 | + * @param data onActivityResult返回的Intent |
| 169 | + * @return |
| 170 | + */ |
| 171 | + public static String getChoosedImagePath(Activity context, Intent data) { |
| 172 | + if (data == null) return null; |
| 173 | + String path = ""; |
| 174 | + ContentResolver resolver = context.getContentResolver(); |
| 175 | + Uri originalUri = data.getData(); |
| 176 | + if (null == originalUri) return null; |
| 177 | + String[] projection = {MediaStore.Images.Media.DATA}; |
| 178 | + Cursor cursor = resolver.query(originalUri, projection, null, null, null); |
| 179 | + if (null != cursor) { |
| 180 | + try { |
| 181 | + int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA); |
| 182 | + cursor.moveToFirst(); |
| 183 | + path = cursor.getString(column_index); |
| 184 | + } catch (IllegalArgumentException e) { |
| 185 | + e.printStackTrace(); |
| 186 | + } finally { |
| 187 | + try { |
| 188 | + if (!cursor.isClosed()) { |
| 189 | + cursor.close(); |
| 190 | + } |
| 191 | + } catch (Exception e) { |
| 192 | + e.printStackTrace(); |
| 193 | + } |
| 194 | + } |
| 195 | + } |
| 196 | + return StringUtils.isEmpty(path) ? originalUri.getPath() : null; |
| 197 | + } |
| 198 | + |
| 199 | + /** |
| 200 | + * 获取拍照之后的照片文件(JPG格式) |
| 201 | + * |
| 202 | + * @param data onActivityResult回调返回的数据 |
| 203 | + * @param filePath 文件路径 |
| 204 | + * @return 文件 |
| 205 | + */ |
| 206 | + public static File getTakePictureFile(Intent data, String filePath) { |
| 207 | + if (data == null) return null; |
| 208 | + Bundle extras = data.getExtras(); |
| 209 | + if (extras == null) return null; |
| 210 | + Bitmap photo = extras.getParcelable("data"); |
| 211 | + File file = new File(filePath); |
| 212 | + if (ImageUtils.save(photo, file, Bitmap.CompressFormat.JPEG)) return file; |
| 213 | + return null; |
| 214 | + } |
| 215 | +} |
0 commit comments