.java
1 package com.jerry.crop; 2 3 import java.io.File; 4 5 import android.app.Activity; 6 import android.content.Intent; 7 import android.graphics.Bitmap; 8 import android.net.Uri; 9 import android.os.Bundle; 10 import android.os.Environment; 11 import android.provider.MediaStore; 12 import android.view.View; 13 import android.widget.ImageView; 14 import android.widget.Toast; 15 16 public class MainActivity extends Activity { 17 18 private static final int PHOTO_REQUEST_CAREMA = 1;// 拍照 19 private static final int PHOTO_REQUEST_GALLERY = 2;// 從相冊中選擇 20 private static final int PHOTO_REQUEST_CUT = 3;// 結果 21 22 private ImageView iv_image; 23 24 /* 頭像名稱 */ 25 private static final String PHOTO_FILE_NAME = "temp_photo.jpg"; 26 private File tempFile; 27 28 @Override 29 protected void onCreate(Bundle savedInstanceState) { 30 super.onCreate(savedInstanceState); 31 setContentView(R.layout.activity_main); 32 this.iv_image = (ImageView) this.findViewById(R.id.iv_image); 33 } 34 35 /* 36 * 從相冊獲取 37 */ 38 public void gallery(View view) { 39 // 激活系統圖庫,選擇一張圖片 40 Intent intent = new Intent(Intent.ACTION_PICK); 41 intent.setType("image/*"); 42 // 開啟一個帶有返回值的Activity,請求碼為PHOTO_REQUEST_GALLERY 43 startActivityForResult(intent, PHOTO_REQUEST_GALLERY); 44 } 45 46 /* 47 * 從相機獲取 48 */ 49 public void camera(View view) { 50 // 激活相機 51 Intent intent = new Intent("android.media.action.IMAGE_CAPTURE"); 52 // 判斷存儲卡是否可以用,可用進行存儲 53 if (hasSdcard()) { 54 tempFile = new File(Environment.getExternalStorageDirectory(), 55 PHOTO_FILE_NAME); 56 // 從文件中創建uri 57 Uri uri = Uri.fromFile(tempFile); 58 intent.putExtra(MediaStore.EXTRA_OUTPUT, uri); 59 } 60 // 開啟一個帶有返回值的Activity,請求碼為PHOTO_REQUEST_CAREMA 61 startActivityForResult(intent, PHOTO_REQUEST_CAREMA); 62 } 63 64 /* 65 * 剪切圖片 66 */ 67 private void crop(Uri uri) { 68 // 裁剪圖片意圖 69 Intent intent = new Intent("com.android.camera.action.CROP"); 70 intent.setDataAndType(uri, "image/*"); 71 intent.putExtra("crop", "true"); 72 // 裁剪框的比例,1:1 73 intent.putExtra("aspectX", 1); 74 intent.putExtra("aspectY", 1); 75 // 裁剪后輸出圖片的尺寸大小 76 intent.putExtra("outputX", 250); 77 intent.putExtra("outputY", 250); 78 79 intent.putExtra("outputFormat", "JPEG");// 圖片格式 80 intent.putExtra("noFaceDetection", true);// 取消人臉識別 81 intent.putExtra("return-data", true); 82 // 開啟一個帶有返回值的Activity,請求碼為PHOTO_REQUEST_CUT 83 startActivityForResult(intent, PHOTO_REQUEST_CUT); 84 } 85 86 /* 87 * 判斷sdcard是否被掛載 88 */ 89 private boolean hasSdcard() { 90 if (Environment.getExternalStorageState().equals( 91 Environment.MEDIA_MOUNTED)) { 92 return true; 93 } else { 94 return false; 95 } 96 } 97 98 @Override 99 protected void onActivityResult(int requestCode, int resultCode, Intent data) { 100 if (requestCode == PHOTO_REQUEST_GALLERY) { 101 // 從相冊返回的數據 102 if (data != null) { 103 // 得到圖片的全路徑 104 Uri uri = data.getData(); 105 crop(uri); 106 } 107 108 } else if (requestCode == PHOTO_REQUEST_CAREMA) { 109 // 從相機返回的數據 110 if (hasSdcard()) { 111 crop(Uri.fromFile(tempFile)); 112 } else { 113 Toast.makeText(MainActivity.this, "未找到存儲卡,無法存儲照片!", 0).show(); 114 } 115 116 } else if (requestCode == PHOTO_REQUEST_CUT) { 117 // 從剪切圖片返回的數據 118 if (data != null) { 119 Bitmap bitmap = data.getParcelableExtra("data"); 120 this.iv_image.setImageBitmap(bitmap); 121 } 122 try { 123 // 將臨時文件刪除 124 tempFile.delete(); 125 } catch (Exception e) { 126 e.printStackTrace(); 127 } 128 129 } 130 131 super.onActivityResult(requestCode, resultCode, data); 132 } 133 }
.xml
1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 xmlns:tools="http://schemas.android.com/tools" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:orientation="vertical" 6 tools:context=".MainActivity" > 7 8 <Button 9 android:layout_width="wrap_content" 10 android:layout_height="wrap_content" 11 android:onClick="gallery" 12 android:text="獲取圖庫圖片" /> 13 <Button 14 android:layout_width="wrap_content" 15 android:layout_height="wrap_content" 16 android:onClick="camera" 17 android:text="拍照獲取圖片" /> 18 19 <ImageView 20 android:id="@+id/iv_image" 21 android:layout_width="wrap_content" 22 android:layout_height="wrap_content" /> 23 24 </LinearLayout>
效果圖: