[英]Android image cropping is reducing the size of the image
我需要從Android手機畫廊裁剪一張照片,然后進行編輯。 照片的原始尺寸為3264 * 2448,在我的尺寸為1280 * 720的屏幕上顯示為縮小的圖像。 當我裁剪它時,我得到的圖像大小為185 * 139。
我用於裁剪的代碼是
Intent cropIntent = new Intent("com.android.camera.action.CROP");
cropIntent.setDataAndType(picUri, "image/*");
cropIntent.putExtra("crop", "true");
cropIntent.putExtra("return-data", true);
startActivityForResult(cropIntent, PIC_CROP);
當我在imageView中顯示裁剪的圖像時,它顯示為更小的圖像。
我應該裁剪原始大小的圖像而不是縮小版本。 你們有沒有人可以指導我如何做到這一點?
請試試這個,我知道為時已晚,但可能會對某人有所幫助。
private void performCrop() {
try {
//call the standard crop action intent (the user device may not support it)
Intent cropIntent = new Intent("com.android.camera.action.CROP");
//indicate image type and Uri
cropIntent.setDataAndType(mImageCaptureUri, "image/*");
//set crop properties
cropIntent.putExtra("crop", "true");
//indicate aspect of desired crop
cropIntent.putExtra("aspectX", 4);
cropIntent.putExtra("aspectY", 3);
//indicate output X and Y
cropIntent.putExtra("outputX", 800);
cropIntent.putExtra("outputY", 800);
File f = new File(Environment.getExternalStorageDirectory(),
"/temporary_holder.jpg");
try {
f.createNewFile();
} catch (IOException ex) {
Log.e("io", ex.getMessage());
}
uri = Uri.fromFile(f);
cropIntent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
startActivityForResult(cropIntent, PIC_CROP);
} //respond to users whose devices do not support the crop action
catch (ActivityNotFoundException anfe) {
//display an error message
String errorMessage = "Your device doesn't support the crop action!";
Toast toast = Toast.makeText(this, errorMessage, Toast.LENGTH_SHORT);
toast.show();
}
}
讓你的onActivityResult像這樣: -
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == PIC_CROP) {
String filePath = Environment.getExternalStorageDirectory()
+ "/temporary_holder.jpg";
thumbnail = BitmapFactory.decodeFile(filePath);
//thumbnail = BitmapFactory.decodeFile(filePath);
// Log.i("",String.valueOf(thumbnail.getHeight()));
ImageView image = (ImageView) findViewById(R.id.pestImage);
image.setImageBitmap(thumbnail);
}
}
加:
intent.putExtra(MediaStore.EXTRA_OUTPUT, tempUri);
然后你可以得到一個沒有減少的圖像文件。 並使用此保存的文件而不是:
Intent.getExtras().getParcelable("data")
只需忽略返回的位圖。
首先,您應該知道,不能依靠作物意圖來實現跨設備兼容性。 (任何OEM都可以用自己的版本替換庫存相機/圖庫應用程序)。 除此之外,我認為你錯過了一些額外的東西應該進入意圖並給你你想要的效果。
// width & height are your desired width/height for the cropped result
cropIntent.putExtra("outputX", width);
cropIntent.putExtra("outputY", height);
cropIntent.putExtra("aspectX", width);
cropIntent.putExtra("aspectY", height);
cropIntent.putExtra("scaleUpIfNeeded", true);
嘗試將這些添加到您的意圖中,看看結果如何。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.