Android大圖片裁剪終極解決方案(下:拍照截圖)

聲明:Ryan的博客文章歡迎您的轉載,但在轉載的同時,請註明文章的來源出處,不勝感激! :-)  java

http://my.oschina.net/ryanhoo/blog/86865 android


    上一篇博客中,咱們學習到了如何使用Android相冊截圖。在這篇博客中,我將向你們展現如何拍照截圖。 git

    拍照截圖有點兒特殊,要知道,如今的Android智能手機的攝像頭都是幾百萬的像素,拍出來的圖片都是很是大的。所以,咱們不能像對待相冊截圖同樣使用Bitmap小圖,不管大圖小圖都統一使用Uri進行操做。 github

    1、首先準備好須要使用到的Uri: 學習

private static final String IMAGE_FILE_LOCATION = "file:///sdcard/temp.jpg";//temp file
Uri imageUri = Uri.parse(IMAGE_FILE_LOCATION);//The Uri to store the big bitmap

    2、使用MediaStore.ACTION_IMAGE_CAPTURE能夠輕鬆調用Camera程序進行拍照: spa

Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);//action is capture
intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
startActivityForResult(intent, TAKE_BIG_PICTURE);//or TAKE_SMALL_PICTURE
    3、接下來就能夠在 onActivityResult中拿到返回的數據(Uri),並將Uri傳遞給截圖的程序。
switch (requestCode) {
case TAKE_BIG_PICTURE:
	Log.d(TAG, "TAKE_BIG_PICTURE: data = " + data);//it seems to be null
	//TODO sent to crop
	cropImageUri(imageUri, 800, 400, CROP_BIG_PICTURE);
	
	break;
case TAKE_SMALL_PICTURE:
	Log.i(TAG, "TAKE_SMALL_PICTURE: data = " + data);
	//TODO sent to crop 
	cropImageUri(imageUri, 300, 150, CROP_SMALL_PICTURE);
	
	break;
default:
	break;
}
    能夠看到,不管是拍大圖片仍是小圖片,都是使用的Uri,只是尺寸不一樣而已。咱們將這個操做封裝在一個方法裏面。
private void cropImageUri(Uri uri, int outputX, int outputY, int requestCode){
	Intent intent = new Intent("com.android.camera.action.CROP");
	intent.setDataAndType(uri, "image/*");
	intent.putExtra("crop", "true");
	intent.putExtra("aspectX", 2);
	intent.putExtra("aspectY", 1);
	intent.putExtra("outputX", outputX);
	intent.putExtra("outputY", outputY);
	intent.putExtra("scale", true);
	intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
	intent.putExtra("return-data", false);
	intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());
	intent.putExtra("noFaceDetection", true); // no face detection
	startActivityForResult(intent, requestCode);
}
    4、最後一步,咱們已經將數據傳入裁剪圖片程序,接下來要作的就是處理返回的數據了:
switch (requestCode) {
case CROP_BIG_PICTURE://from crop_big_picture
	Log.d(TAG, "CROP_BIG_PICTURE: data = " + data);//it seems to be null
	if(imageUri != null){
		Bitmap bitmap = decodeUriAsBitmap(imageUri);
		imageView.setImageBitmap(bitmap);
	}
	break;
case CROP_SMALL_PICTURE:
	if(imageUri != null){
		Bitmap bitmap = decodeUriAsBitmap(imageUri);
		imageView.setImageBitmap(bitmap);
	}else{
		Log.e(TAG, "CROP_SMALL_PICTURE: data = " + data);
	}
	break;
default:
	break;
}

效果圖: .net


代碼託管於GitHub,會不按期更新:https://github.com/ryanhoo/PhotoCropper code


基礎篇: orm

【譯】如何使用Android MediaStore裁剪大圖片

上篇:

Android大圖片裁剪終極解決方案(上:原理分析)

中篇:

Android大圖片裁剪終極解決方案(中:從相冊截圖)

相關文章
相關標籤/搜索