在Android中,調(diào)用系統(tǒng)相機(jī)拍照時(shí),將會接收到返回的圖像數(shù)據(jù),但是這些圖片并不是全尺寸的圖像,而是系統(tǒng)給的縮略圖,當(dāng)對拍照的圖片進(jìn)行裁切后顯示時(shí),得到的卻是模糊的圖片。下面針對這個(gè)問題提出解決的方法。
首先,我們知道調(diào)用系統(tǒng)的裁切是通過Intent intent = new Intent(“com.android.camera.action.CROP”);
但是intent到底能夠攜帶哪些數(shù)據(jù)呢,都有什么含義呢,我們可以看到如下:

上面包含了所有可選的操作,其中有一些非常重要的參數(shù)。
intent.putExtra(“return-data”, true):表示裁剪后返回的數(shù)據(jù)為Bitmap,是存在內(nèi)存中的縮略圖,效果模糊。獲取的方式為,在Activity中的onActivityResult方法中:
Bundle bundle = data.getExtras();Bitmap bitmap = bundle.getParcelable("data");為了獲取到裁切后的原圖,我們選擇將剪切的圖片保存在本地,然后調(diào)用本地的圖片,并不直接返回Bitmap.
intent.putExtra("return-data", false);intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);下面代碼實(shí)現(xiàn)拍照/剪切并進(jìn)行顯示的
public static int TAKE_PHOTO_REQUEST_CODE = 1; //拍照 public static int PHOTO_REQUEST_CUT = 2; //裁切 public static int PHOTO_REQUEST_GALLERY = 3; //相冊 public Uri imageUri; /** * 打開相機(jī)拍照 */ private void takePhotos() { imageUri = Uri.fromFile(getImageStoragePath(this)); Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); //指定照片存儲路徑 intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri); startActivityForResult(intent,TAKE_PHOTO_REQUEST_CODE); } /** * 打開相冊選擇圖片 */ private void choicePicFromAlbum() { Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI); intent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*"); startActivityForResult(intent, PHOTO_REQUEST_GALLERY); } /** * 設(shè)置圖片保存路徑 * @return */ private File getImageStoragePath(Context context){ if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){ File file = new File(context.getExternalFilesDir(Environment.DIRECTORY_PICTURES),"temp.jpg"); return file; } return null; } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (requestCode == TAKE_PHOTO_REQUEST_CODE){ if (imageUri != null){ startPhotoZoom(imageUri); } }else if (requestCode == PHOTO_REQUEST_CUT){ if (imageUri != null) { Bitmap bitmap = decodeUriBitmap(imageUri); imageView.setImageBitmap(bitmap); } }else if (requestCode == PHOTO_REQUEST_GALLERY){ if (data != null) { imageUri = data.getData(); Bitmap bitmap = decodeUriBitmap(imageUri); imageView.setImageBitmap(bitmap); } } } private Bitmap decodeUriBitmap(Uri uri) { Bitmap bitmap = null; try { bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(uri)); } catch (FileNotFoundException e) { e.printStackTrace(); return null; } return bitmap; } /** * 調(diào)用系統(tǒng)裁剪 * * @param uri */ public void startPhotoZoom(Uri uri) { Intent intent = new Intent("com.android.camera.action.CROP"); intent.setDataAndType(uri, "image/*"); // crop為true是設(shè)置在開啟的intent中設(shè)置顯示的view可以剪裁 intent.putExtra("crop", "true"); intent.putExtra("scale", true); // aspectX aspectY 是寬高的比例 intent.putExtra("aspectX", 1); intent.putExtra("aspectY", 1); // outputX,outputY 是剪裁圖片的寬高 intent.putExtra("outputX", 800); intent.putExtra("outputY", 800); //設(shè)置了true的話直接返回bitmap,可能會很占內(nèi)存 intent.putExtra("return-data", false); //設(shè)置輸出的格式 intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString()); //設(shè)置輸出的地址 intent.putExtra(MediaStore.EXTRA_OUTPUT, uri); //不啟用人臉識別 intent.putExtra("noFaceDetection", true); startActivityForResult(intent, PHOTO_REQUEST_CUT); }以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持武林網(wǎng)。
新聞熱點(diǎn)
疑難解答
圖片精選