參考:
- http://www.cnblogs.com/rossoneri/p/3976530.html
- https://my.oschina.net/ryanhoo/blog/86842
- https://my.oschina.net/ryanhoo/blog/86865
一. 原理
- 通過(guò)Intent打開系統(tǒng)自帶的剪裁Activity辖试,通過(guò)Intent設(shè)置Extra參數(shù),進(jìn)行剪裁參數(shù)的設(shè)定。剪裁結(jié)果的縮略圖會(huì)保存在返回的data中贤徒,而大圖要通過(guò)Uri進(jìn)行保存(其實(shí)就是保存到自己建的文件里面)懒豹。
二. 剪裁時(shí)候可以設(shè)置的參數(shù)及簡(jiǎn)介
三. 返回縮略圖的使用步驟
- 獲取要剪裁的圖片(可以通過(guò)相冊(cè)選擇,也可以通過(guò)相機(jī)拍照)PS:此處演示從相冊(cè)選擇(就是從相冊(cè)選取圖片袜蚕,然后返回一個(gè)圖片路徑的Uri)
//打開相冊(cè) Intent intent = new Intent(Intent.ACTION_GET_CONTENT); intent.setType("image/*"); startActivityForResult(intent, REQ_PIC);
//onActivityResult()里面獲取選擇的圖片 if (requestCode == REQ_PIC) { mImageUri = data.getData(); crop();//獲取到之后開始剪裁 }
- 設(shè)置剪裁Intent及參數(shù)
private void cropAndThumbnail(){ Intent intent = new Intent("com.android.camera.action.CROP"); intent.setDataAndType(mImageUri, "image/*");//設(shè)置要縮放的圖片Uri和類型 intent.putExtra("aspectX", 768);//寬度比 intent.putExtra("aspectY", 1024);//高度比 intent.putExtra("outputX", 768);//輸出圖片的寬度 intent.putExtra("outputY", 1024);//輸出圖片的高度 intent.putExtra("scale", true);//縮放 intent.putExtra("return-data", false);//當(dāng)為true的時(shí)候就返回縮略圖八毯,false就不返回,需要通過(guò)Uri intent.putExtra("noFaceDetection", false);//前置攝像頭 startActivityForResult(intent, REQ_CROP);//打開剪裁Activity }
- startActivityForResult打開剪裁Activity
- 代碼即為上面代碼最后一句
- onActivityResult回調(diào)里面進(jìn)行數(shù)據(jù)處理(縮略圖保存在返回的Intent的Bundle里面焙蚓,然后通過(guò)data鍵可以取出)
if (requestCode == REQ_CROP) { /*縮略圖*/ if(data!=null){ Bundle bundle = data.getExtras(); Bitmap bitmap = bundle.getParcelable("data"); mImageView.setImageBitmap(bitmap); Log.i(TAG, "onActivityResult: smallUri "+mSmallUri); } }
四. 返回原圖的使用步驟
原圖太大朝氓,Android不會(huì)保存到內(nèi)存里面去,所以也不會(huì)返回到調(diào)用的Activity中主届,因此需要通過(guò)Uri保存到外部文件中赵哲。
- 獲取要剪裁的圖片(可以通過(guò)相冊(cè)選擇,也可以通過(guò)相機(jī)拍照)
- 代碼同上1
- 設(shè)置剪裁Intent及參數(shù)
private void crop() { /*新建用于存剪裁后圖片的文件君丁,并轉(zhuǎn)化為Uri*/ File imageFile = createImageFile(); mSmallUri = Uri.fromFile(imageFile); Log.i(TAG, "crop: " + mSmallUri); Intent intent = new Intent("com.android.camera.action.CROP"); intent.setDataAndType(mImageUri, "image/*"); intent.putExtra("aspectX", 768); intent.putExtra("aspectY", 1024); intent.putExtra("outputX", 768); intent.putExtra("outputY", 1024); intent.putExtra("scale", true); intent.putExtra("return-data", false);//設(shè)置為不返回縮略圖 intent.putExtra(MediaStore.EXTRA_OUTPUT, mSmallUri);//設(shè)置大圖保存到文件 intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());//保存的圖片格式 intent.putExtra("noFaceDetection", false); startActivityForResult(intent, REQ_CROP); }
- startActivityForResult打開剪裁Activity
- 代碼即為上面代碼最后一句
- onActivityResult回調(diào)里面進(jìn)行數(shù)據(jù)處理(文件對(duì)應(yīng)的Uri)
try { if (mSmallUri != null) { Bitmap bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(mSmallUri)); mImageView.setImageBitmap(bitmap); } else { Log.i(TAG, "onActivityResult: Uri is null"); } } catch (FileNotFoundException e) { e.printStackTrace(); }
五.源碼
public class MainActivity extends AppCompatActivity {
private Button mButton;
private static final int REQ_CROP = 873;
private static final int REQ_PIC = 434;
private Uri mImageUri;
private ImageView mImageView;
private Uri mSmallUri;
private final String TAG = getClass().getSimpleName();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mButton = findViewById(R.id.button);//點(diǎn)擊從相冊(cè)選取照片
mImageView = findViewById(R.id.imageView);//顯示剪裁后的圖片
mButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("image/*");
startActivityForResult(intent, REQ_PIC);
}
});
}
private void crop() {
/*新建用于存剪裁后圖片的文件枫夺,并轉(zhuǎn)化為Uri*/
File imageFile = createImageFile();
mSmallUri = Uri.fromFile(imageFile);
Log.i(TAG, "crop: " + mSmallUri);
/*File image = new File(getExternalCacheDir() + "/demo.jpg");
Log.i(TAG, "crop: path " + image.getAbsolutePath());
mSmallUri = Uri.fromFile(image);*/
Intent intent = new Intent("com.android.camera.action.CROP");
intent.setDataAndType(mImageUri, "image/*");
intent.putExtra("aspectX", 768);
intent.putExtra("aspectY", 1024);
intent.putExtra("outputX", 768);
intent.putExtra("outputY", 1024);
intent.putExtra("scale", true);
intent.putExtra("return-data", false);//設(shè)置為不返回縮略圖
intent.putExtra(MediaStore.EXTRA_OUTPUT, mSmallUri);//設(shè)置大圖保存到文件
intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());//保存的圖片格式
intent.putExtra("noFaceDetection", false);
startActivityForResult(intent, REQ_CROP);
}
/*縮略圖*/
private void cropAndThumbnail() {
Intent intent = new Intent("com.android.camera.action.CROP");
intent.setDataAndType(mImageUri, "image/*");//設(shè)置要縮放的圖片Uri和類型
intent.putExtra("aspectX", 768);//寬度比
intent.putExtra("aspectY", 1024);//高度比
intent.putExtra("outputX", 768);//輸出圖片的寬度
intent.putExtra("outputY", 1024);//輸出圖片的高度
intent.putExtra("scale", true);//縮放
intent.putExtra("return-data", false);//當(dāng)為true的時(shí)候就返回縮略圖,false就不返回绘闷,需要通過(guò)Uri
intent.putExtra("noFaceDetection", false);//前置攝像頭
startActivityForResult(intent, REQ_CROP);
}
/*用時(shí)間創(chuàng)建圖片文件橡庞,防重名*/
private File createImageFile() {
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String imageFileName = "JPEG_" + timeStamp + "_";
File storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES);
File imageFile = null;
try {
imageFile = File.createTempFile(imageFileName, ".jpg", storageDir);
} catch (IOException e) {
e.printStackTrace();
}
return imageFile;
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode != RESULT_OK) {
return;
}
if (requestCode == REQ_PIC) {//選取圖片
mImageUri = data.getData();
crop();
} else if (requestCode == REQ_CROP) {//剪裁
/*縮略圖*/
/*if(data!=null){
Bundle bundle = data.getExtras();
Bitmap bitmap = bundle.getParcelable("data");
mImageView.setImageBitmap(bitmap);
Log.i(TAG, "onActivityResult: smallUri "+mSmallUri);
}*/
/*原圖*/
try {
if (mSmallUri != null) {
Bitmap bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(mSmallUri));
mImageView.setImageBitmap(bitmap);
} else {
Log.i(TAG, "onActivityResult: Uri is null");
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
}