系统相机 系统相册 裁剪【示例】

时间:2021-04-24 20:51:49

Activity 解耦版

   
   
  1. public class MainActivity2 extends ListActivity {
  2. public static final String fileSavePath = Environment.getExternalStorageDirectory().getPath() + File.separator;
  3. public static final String tempFileSavePath = fileSavePath + "temp.png";
  4. public static final String tempCropFileSavePath = fileSavePath + "temp_crop.png";
  5. public static final int REQUEST_CODE_0 = 1100;
  6. public static final int REQUEST_CODE_1 = 1101;
  7. public static final int REQUEST_CODE_2 = 1102;
  8. public static final int REQUEST_CODE_3 = 1103;
  9. public static final int REQUEST_CODE_4 = 1104;
  10. public static final int REQUEST_CODE_5 = 1105;
  11. public static final int REQUEST_CODE_6 = 1106;
  12. public static final int REQUEST_CODE_11 = 1111;
  13. public static final int REQUEST_CODE_12 = 1112;
  14. private TextView tv;
  15. private ImageView iv;
  16. protected void onCreate(Bundle savedInstanceState) {
  17. super.onCreate(savedInstanceState);
  18. int size = ((ActivityManager) this.getSystemService(Context.ACTIVITY_SERVICE)).getMemoryClass();
  19. Toast.makeText(this, "内存 " + size, Toast.LENGTH_SHORT).show();
  20. String[] array = {"使用相机拍照并获取返回的照片\n注意返回的并非原图,照片会自动被压缩", //
  21. "使用相机拍照并将照片保存在指定的位置\n注意这时在指定位置保存的是原图",//
  22. "调用系统图库APP的裁剪图片功能,将图片裁剪为指定大小\n将裁剪后的图片通过onActivityResult返回",//
  23. "调用系统图库APP的裁剪图片功能,将图片裁剪为指定大小\n将裁剪后的照片保存在指定的位置",//
  24. "从系统图库APP中获取图片,这里获取的是原图",//
  25. "从系统图库APP中获取图片,并将照片裁剪为指定大小\n将裁剪后的图片通过onActivityResult返回",//
  26. "从系统图库APP中获取图片,并将照片裁剪为指定大小\n将裁剪后的照片保存在指定的位置",};
  27. for (int i = 0; i < array.length; i++) {
  28. array[i] = i + "、" + array[i];
  29. }
  30. iv = new ImageView(this);
  31. //iv.setScaleType(ImageView.ScaleType.CENTER_CROP);
  32. getListView().addFooterView(iv);
  33. tv = new TextView(this);
  34. tv.setTextColor(Color.BLUE);
  35. getListView().addFooterView(tv);
  36. setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, new ArrayList<String>(Arrays.asList(array))));
  37. }
  38. @Override
  39. protected void onListItemClick(ListView l, View v, int position, long id) {
  40. switch (position) {
  41. case 0:
  42. PhotoUtils.startCamera(this, REQUEST_CODE_0);
  43. break;
  44. case 1:
  45. PhotoUtils.startCameraAndCrop(this, REQUEST_CODE_1);
  46. break;
  47. case 2:
  48. PhotoUtils.startCameraAndCrop(this, REQUEST_CODE_2);
  49. break;
  50. case 3:
  51. PhotoUtils.startCameraAndCrop(this, REQUEST_CODE_3);
  52. break;
  53. case 4:
  54. PhotoUtils.startAlbum(this, REQUEST_CODE_4);
  55. break;
  56. case 5:
  57. PhotoUtils.startAlbum(this, REQUEST_CODE_5);
  58. break;
  59. case 6:
  60. PhotoUtils.startAlbum(this, REQUEST_CODE_6);
  61. break;
  62. }
  63. }
  64. @Override
  65. protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  66. super.onActivityResult(requestCode, resultCode, data);
  67. if (resultCode == RESULT_CANCELED) {//0
  68. Toast.makeText(this, "结果码:" + requestCode + " RESULT_CANCELED", Toast.LENGTH_SHORT).show();
  69. return;
  70. } else if (resultCode == RESULT_OK) {//-1
  71. Toast.makeText(this, "结果码:" + requestCode + " RESULT_OK", Toast.LENGTH_SHORT).show();
  72. }
  73. //*******************************************************************************************************************
  74. switch (requestCode) {
  75. case REQUEST_CODE_0:// 注意,返回的并非原图,照片会自动被压缩!
  76. Bitmap bitmap = (Bitmap) data.getExtras().get("data");
  77. if (bitmap != null) {
  78. tv.append("返回的Bitmap的尺寸为" + bitmap.getWidth() + " * " + bitmap.getHeight() + "\n");
  79. iv.setImageBitmap(bitmap);
  80. }
  81. break;
  82. case REQUEST_CODE_1://注意,这里返回的data为null,这时在指定位置保存的是原图
  83. Bitmap bitmap1 = BitmapFactory.decodeFile(tempFileSavePath);
  84. if (bitmap1 != null) {
  85. iv.setImageBitmap(bitmap1);
  86. tv.append("保存的Bitmap的尺寸为" + bitmap1.getWidth() + " * " + bitmap1.getHeight() + "\n");
  87. }
  88. break;
  89. case REQUEST_CODE_2://
  90. case REQUEST_CODE_3:
  91. Bitmap bitmap2 = BitmapFactory.decodeFile(tempFileSavePath);
  92. if (bitmap2 != null) {
  93. tv.append("裁剪前的Bitmap的尺寸为" + bitmap2.getWidth() + " * " + bitmap2.getHeight() + "\n");
  94. Uri fromUri = Uri.fromFile(new File(tempFileSavePath));
  95. if (requestCode == REQUEST_CODE_2) PhotoUtils.cropImage(this, REQUEST_CODE_11, fromUri);
  96. else {
  97. Uri toUri = Uri.fromFile(new File(tempCropFileSavePath));
  98. PhotoUtils.cropImage(this, REQUEST_CODE_12, fromUri, toUri);
  99. }
  100. }
  101. break;
  102. //
  103. //*******************************************************************************************************************
  104. //
  105. case REQUEST_CODE_4:
  106. try {
  107. Uri fromUri = data.getData();
  108. Bitmap bitmap4 = MediaStore.Images.Media.getBitmap(getContentResolver(), fromUri);
  109. if (bitmap4 != null) {
  110. tv.append("返回的Bitmap的尺寸为" + bitmap4.getWidth() + " * " + bitmap4.getHeight() + "\n");
  111. iv.setImageBitmap(bitmap4);
  112. }
  113. } catch (IOException e) {
  114. e.printStackTrace();
  115. }
  116. break;
  117. case REQUEST_CODE_5:
  118. case REQUEST_CODE_6:
  119. try {
  120. Bitmap bitmap5 = MediaStore.Images.Media.getBitmap(getContentResolver(), data.getData());
  121. if (bitmap5 != null) {
  122. tv.append("裁剪前的Bitmap的尺寸为" + bitmap5.getWidth() + " * " + bitmap5.getHeight() + "\n");
  123. if (requestCode == REQUEST_CODE_5) PhotoUtils.cropImage(this, REQUEST_CODE_11, data.getData());
  124. else {
  125. Uri toUri = Uri.fromFile(new File(tempCropFileSavePath));
  126. PhotoUtils.cropImage(this, REQUEST_CODE_12, data.getData(), toUri);
  127. }
  128. }
  129. } catch (IOException e) {
  130. e.printStackTrace();
  131. }
  132. break;
  133. //
  134. //*******************************************************************************************************************
  135. //
  136. case REQUEST_CODE_11: // 将裁剪后的图片通过onActivityResult中的Intent返回
  137. Bitmap bitmap11 = (Bitmap) data.getParcelableExtra("data");
  138. if (bitmap11 != null) {
  139. tv.append("裁剪后的Bitmap的尺寸为" + bitmap11.getWidth() + " * " + bitmap11.getHeight() + "\n");
  140. iv.setImageBitmap(bitmap11);
  141. }
  142. break;
  143. case REQUEST_CODE_12: //将裁剪后的图片通过指定的Uri返回
  144. try {
  145. Uri tempCropFileUri = Uri.fromFile(new File(tempCropFileSavePath));
  146. Bitmap bitmap12 = MediaStore.Images.Media.getBitmap(getContentResolver(), tempCropFileUri);
  147. tv.append("指定Uri后裁剪后的尺寸为" + bitmap12.getWidth() + " * " + bitmap12.getHeight() + "\n");
  148. iv.setImageBitmap(bitmap12);
  149. } catch (IOException e) {
  150. e.printStackTrace();
  151. }
  152. break;
  153. }
  154. }
  155. }

工具类

   
   
  1. public class PhotoUtils {
  2. public static final String tempFileSavePath = Environment.getExternalStorageDirectory().getPath() + File.separator + "temp.png";
  3. //**************************************************************************************************************************
  4. // 使用相机拍照
  5. //**************************************************************************************************************************
  6. /**
  7. * 使用相机拍照并获取返回的照片
  8. * 注意返回的并非原图,照片会自动被压缩
  9. */
  10. public static void startCamera(Activity activity, int requestCode) {
  11. Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);//调用系统相机拍照
  12. activity.startActivityForResult(intent, requestCode);
  13. }
  14. /**
  15. * 使用相机拍照并将照片保存在指定的位置
  16. * 注意这时在指定位置保存的是原图
  17. */
  18. public static void startCameraAndCrop(Activity activity, int requestCode) {
  19. Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);//调用系统相机拍照
  20. intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(tempFileSavePath)));//将返回的数据保存到指定位置
  21. activity.startActivityForResult(intent, requestCode);
  22. }
  23. //**************************************************************************************************************************
  24. // 从相册中获取图片
  25. //**************************************************************************************************************************
  26. public static void startAlbum(Activity activity, int requestCode) {
  27. Intent intent = new Intent(Intent.ACTION_PICK);
  28. //intent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*");
  29. intent.setType("image/*");//视频【"video/*"】
  30. activity.startActivityForResult(intent, requestCode);
  31. }
  32. //**************************************************************************************************************************
  33. // 裁剪图片
  34. //**************************************************************************************************************************
  35. /**
  36. * 调用系统图库APP的裁剪图片功能,将指定Uri中的图片裁剪为指定大小
  37. */
  38. public static void cropImage(Activity activity, int requestCode, Uri fromUri) {
  39. cropImage(activity, requestCode, fromUri, 288, 288, null);
  40. }
  41. /**
  42. * 将裁剪后的照片保存在指定的位置
  43. * 注意一个大bug,当指定toUri时,设置的裁剪大小是无效的。
  44. */
  45. public static void cropImage(Activity activity, int requestCode, Uri fromUri, Uri toUri) {
  46. cropImage(activity, requestCode, fromUri, 288, 288, toUri);
  47. }
  48. /**
  49. * 调用系统图库APP的裁剪图片功能,将指定图片裁剪为指定大小
  50. */
  51. public static void cropImage(Activity activity, int requestCode, Uri fromUri, int desWidth, int desHeight, Uri toUri) {
  52. if (fromUri == null) return;
  53. //指定action是使用系统图库裁剪图片
  54. Intent intent = new Intent("com.android.camera.action.CROP");
  55. intent.setDataAndType(fromUri, "image/*");
  56. //设置在开启的Intent中,显示的VIEW可裁剪
  57. intent.putExtra("crop", "true");
  58. //设置裁剪比例及裁剪图片的具体宽高
  59. intent.putExtra("aspectX", 1);
  60. intent.putExtra("aspectY", 1);
  61. intent.putExtra("outputX", desWidth);
  62. intent.putExtra("outputY", desHeight);
  63. //设置是否允许拉伸
  64. intent.putExtra("scale", true);
  65. intent.putExtra("scaleUpIfNeeded", true);
  66. //设置输出格式
  67. intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());
  68. //设置是否需要人脸识别,默认不需要设置
  69. intent.putExtra("noFaceDetection", true);
  70. //设置是否返回数据
  71. if (toUri == null) {
  72. //如果设置为true,那么便不会在给定的uri中获取到裁剪的图片,这里是将数据返回到了onActivityResult中的intent中了
  73. intent.putExtra("return-data", true);
  74. } else {
  75. //如果要在给定的uri中返回图片,则必须设置为false。注意:要防止裁减前图片的uri与保存时图片的uri相同
  76. intent.putExtra("return-data", false);
  77. intent.putExtra(MediaStore.EXTRA_OUTPUT, toUri);
  78. }
  79. activity.startActivityForResult(intent, requestCode);
  80. }
  81. }

Activity 耦合版

   
   
  1. public class MainActivity extends ListActivity {
  2. public static final String fileSavePath = Environment.getExternalStorageDirectory().getPath() + File.separator;
  3. public static final String tempFileSavePath = fileSavePath + "temp.png";
  4. public static final String tempCropFileSavePath = fileSavePath + "temp_crop.png";
  5. public static final int REQUEST_CODE_0 = 1100;
  6. public static final int REQUEST_CODE_1 = 1101;
  7. public static final int REQUEST_CODE_11 = 1111;
  8. public static final int REQUEST_CODE_12 = 1112;
  9. public static final int REQUEST_CODE_4 = 1104;
  10. public static final int REQUEST_CODE_5 = 1105;
  11. private TextView tv;
  12. private ImageView iv;
  13. private boolean isCrop;
  14. private boolean isSaveToUri;
  15. protected void onCreate(Bundle savedInstanceState) {
  16. super.onCreate(savedInstanceState);
  17. int size=((ActivityManager)this.getSystemService(Context.ACTIVITY_SERVICE)).getMemoryClass();
  18. Toast.makeText(this, "内存 "+size, Toast.LENGTH_SHORT).show();
  19. String[] array = {"使用相机拍照并获取返回的照片\n注意返回的并非原图,照片会自动被压缩", //
  20. "使用相机拍照并将照片保存在指定的位置\n注意这时在指定位置保存的是原图",//
  21. "调用系统图库APP的裁剪图片功能,将图片裁剪为指定大小\n将裁剪后的图片通过onActivityResult返回",//
  22. "调用系统图库APP的裁剪图片功能,将图片裁剪为指定大小\n将裁剪后的照片保存在指定的位置",//
  23. "从系统图库APP中获取图片,这里获取的是原图",//
  24. "从系统图库APP中获取图片,并将照片裁剪为指定大小\n将裁剪后的图片通过onActivityResult返回",//
  25. "从系统图库APP中获取图片,并将照片裁剪为指定大小\n将裁剪后的照片保存在指定的位置",};
  26. for (int i = 0; i < array.length; i++) {
  27. array[i] = i + "、" + array[i];
  28. }
  29. iv = new ImageView(this);
  30. //iv.setScaleType(ImageView.ScaleType.CENTER_CROP);
  31. getListView().addFooterView(iv);
  32. tv = new TextView(this);
  33. tv.setTextColor(Color.BLUE);
  34. getListView().addFooterView(tv);
  35. setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, new ArrayList<String>(Arrays.asList(array))));
  36. }
  37. @Override
  38. protected void onListItemClick(ListView l, View v, int position, long id) {
  39. switch (position) {
  40. case 0:
  41. case 1:
  42. case 2:
  43. case 3:
  44. Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);//调用系统相机拍照
  45. if (position == 0) startActivityForResult(intent, REQUEST_CODE_0);
  46. else {//将返回的数据保存到指定位置
  47. isCrop = (position >= 2);//裁剪
  48. isSaveToUri = (position == 3);
  49. intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(tempFileSavePath)));
  50. startActivityForResult(intent, REQUEST_CODE_1);
  51. }
  52. break;
  53. case 4:
  54. case 5:
  55. case 6:
  56. isCrop = (position >= 5);//裁剪
  57. Intent intent2 = new Intent(Intent.ACTION_PICK);
  58. intent2.setType("image/*");
  59. if (position == 4) startActivityForResult(intent2, REQUEST_CODE_4);
  60. else {
  61. isSaveToUri = (position == 6);
  62. startActivityForResult(intent2, REQUEST_CODE_5);
  63. }
  64. break;
  65. }
  66. }
  67. @Override
  68. protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  69. super.onActivityResult(requestCode, resultCode, data);
  70. if (resultCode == RESULT_CANCELED) {//0
  71. Toast.makeText(this, "结果码:" + requestCode + " RESULT_CANCELED", Toast.LENGTH_SHORT).show();
  72. return;
  73. } else if (resultCode == RESULT_OK) {//-1
  74. Toast.makeText(this, "结果码:" + requestCode + " RESULT_OK", Toast.LENGTH_SHORT).show();
  75. }
  76. //
  77. //*******************************************************************************************************************
  78. //
  79. switch (requestCode) {
  80. case REQUEST_CODE_0:// 注意,返回的并非原图,照片会自动被压缩!
  81. Bitmap bitmap = (Bitmap) data.getExtras().get("data");
  82. if (bitmap != null) {
  83. tv.append("返回的Bitmap的尺寸为" + bitmap.getWidth() + " * " + bitmap.getHeight() + "\n");
  84. iv.setImageBitmap(bitmap);
  85. }
  86. break;
  87. case REQUEST_CODE_1://注意,这里返回的data为null,这时在指定位置保存的是原图
  88. tv.append("已将Bitmap保存到指定位置,此时 data == null : " + (data == null) + "\n");
  89. Bitmap bitmap1 = BitmapFactory.decodeFile(tempFileSavePath);
  90. if (bitmap1 != null) {
  91. iv.setImageBitmap(bitmap1);
  92. tv.append("裁剪前的Bitmap的尺寸为" + bitmap1.getWidth() + " * " + bitmap1.getHeight() + "\n");
  93. if (isCrop) {
  94. Uri fromUri = Uri.fromFile(new File(tempFileSavePath));
  95. if (!isSaveToUri) cropImage(this, REQUEST_CODE_11, fromUri);
  96. else {
  97. Uri toUri = Uri.fromFile(new File(tempCropFileSavePath));
  98. cropImage(this, REQUEST_CODE_12, fromUri, toUri);
  99. }
  100. }
  101. }
  102. break;
  103. //
  104. //*******************************************************************************************************************
  105. //
  106. case REQUEST_CODE_4:
  107. case REQUEST_CODE_5:
  108. try {
  109. Uri fromUri = data.getData();
  110. Bitmap bitmap4 = MediaStore.Images.Media.getBitmap(getContentResolver(), fromUri);
  111. if (bitmap4 != null) {
  112. tv.append("返回的Bitmap的尺寸为" + bitmap4.getWidth() + " * " + bitmap4.getHeight() + "\n");
  113. if (!isCrop) iv.setImageBitmap(bitmap4);
  114. else {
  115. if (!isSaveToUri) cropImage(this, REQUEST_CODE_11, fromUri);
  116. else {
  117. Uri toUri = Uri.fromFile(new File(tempCropFileSavePath));
  118. cropImage(this, REQUEST_CODE_12, fromUri, toUri);
  119. }
  120. }
  121. }
  122. } catch (IOException e) {
  123. e.printStackTrace();
  124. }
  125. break;
  126. //
  127. //*******************************************************************************************************************
  128. //
  129. case REQUEST_CODE_11: // 将裁剪后的图片通过onActivityResult中的Intent返回
  130. Bitmap bitmap5 = (Bitmap) data.getParcelableExtra("data");
  131. if (bitmap5 != null) {
  132. tv.append("裁剪后的Bitmap的尺寸为" + bitmap5.getWidth() + " * " + bitmap5.getHeight() + "\n");
  133. iv.setImageBitmap(bitmap5);
  134. }
  135. break;
  136. case REQUEST_CODE_12: //将裁剪后的图片通过指定的Uri返回
  137. try {
  138. Uri tempCropFileUri = Uri.fromFile(new File(tempCropFileSavePath));
  139. Bitmap bitmap6 = MediaStore.Images.Media.getBitmap(getContentResolver(), tempCropFileUri);
  140. tv.append("指定Uri后裁剪后的尺寸为" + bitmap6.getWidth() + " * " + bitmap6.getHeight() + "\n");
  141. iv.setImageBitmap(bitmap6);
  142. } catch (IOException e) {
  143. e.printStackTrace();
  144. }
  145. break;
  146. }
  147. }
  148. /**
  149. * 调用系统图库APP的裁剪图片功能,将指定Uri中的图片裁剪为指定大小
  150. */
  151. public static void cropImage(Activity activity, int requestCode, Uri fromUri) {
  152. cropImage(activity, requestCode, fromUri, 144, 144, null);
  153. }
  154. /**
  155. * 将裁剪后的照片保存在指定的位置
  156. */
  157. public static void cropImage(Activity activity, int requestCode, Uri fromUri, Uri toUri) {
  158. cropImage(activity, requestCode, fromUri, 144, 144, toUri);
  159. }
  160. /**
  161. * 调用系统图库APP的裁剪图片功能,将指定图片裁剪为指定大小
  162. */
  163. public static void cropImage(Activity activity, int requestCode, Uri fromUri, int desWidth, int desHeight, Uri toUri) {
  164. if (fromUri == null) return;
  165. //指定action是使用系统图库裁剪图片
  166. Intent intent = new Intent("com.android.camera.action.CROP");
  167. intent.setDataAndType(fromUri, "image/*");
  168. //设置在开启的Intent中,显示的VIEW可裁剪
  169. intent.putExtra("crop", "true");
  170. //设置裁剪比例及裁剪图片的具体宽高
  171. intent.putExtra("aspectX", 1);
  172. intent.putExtra("aspectY", 1);
  173. intent.putExtra("outputX", desWidth);
  174. intent.putExtra("outputY", desHeight);
  175. //设置是否允许拉伸
  176. intent.putExtra("scale", true);
  177. intent.putExtra("scaleUpIfNeeded", true);
  178. //设置输出格式
  179. intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());
  180. //设置是否需要人脸识别,默认不需要设置
  181. intent.putExtra("noFaceDetection", true);
  182. //设置是否返回数据
  183. if (toUri == null) {
  184. //如果设置为true,那么便不会在给定的uri中获取到裁剪的图片,这里是将数据返回到了onActivityResult中的intent中了
  185. intent.putExtra("return-data", true);
  186. } else {
  187. //如果要在给定的uri中返回图片,则必须设置为false。注意:要防止裁减前图片的uri与保存时图片的uri相同
  188. intent.putExtra("return-data", false);
  189. intent.putExtra(MediaStore.EXTRA_OUTPUT, toUri);
  190. }
  191. activity.startActivityForResult(intent, requestCode);
  192. }
  193. }



null