我正在尝试保存裁剪的图像并且已成功保存,但问题是保存的图像太小。我想获得裁剪图像的原始尺寸。这是我保存裁剪图像的代码。
Bundle extras = data.getExtras();
if (extras != null) {
Bitmap photo = extras.getParcelable("data");
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
photo.compress(Bitmap.CompressFormat.PNG, 100, bytes);
File f = new File(fileUri.getPath());
try {
f.createNewFile();
FileOutputStream fo = new FileOutputStream(f);
fo.write(bytes.toByteArray());
fo.close();
} catch (IOException e) {
e.printStackTrace();
}
任何人都可以帮助我获得高质量的裁剪图像。任何帮助将非常感激。谢谢:))
答案 0 :(得分:2)
这一切都取决于原始图像的大小和裁剪图像的大小。
裁剪的一种方法是:
Intent photoPickerIntent = new Intent(
Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
photoPickerIntent.setType("image/*");
photoPickerIntent.putExtra("scaleType", "centerCrop");
photoPickerIntent.putExtra("crop", "true");
photoPickerIntent.putExtra("aspectX", 1);
photoPickerIntent.putExtra("aspectY", 1);
//....snip....
photoPickerIntent.putExtra("outputX", 400);
photoPickerIntent.putExtra("outputY", 400);
//....snip....
photoPickerIntent.putExtra(MediaStore.EXTRA_OUTPUT, getBackgroundUri());
photoPickerIntent.putExtra("outputFormat",Bitmap.CompressFormat.JPEG.toString());
startActivityForResult(photoPickerIntent, ActivityCrop);
有了这个,你总能得到400px(输出= 400)大小的方形图像(aspect = 1)。 因此,如果您选择800px的图像正方形,它将缩小,从而降低质量。 相反,选择一个50px的正方形也可以得到400px的结果。
现在,如果你削减了尺寸限制(就在虚线处)。您将获得最初选择的区域(示例中为800和50)并且具有最佳质量。
答案 1 :(得分:1)
使用宽高比和大小发送裁剪意图
Intent cropIntent = new Intent("com.android.camera.action.CROP");
cropIntent.setDataAndType(picUri, "image/*");
cropIntent.putExtra("crop", "true");
cropIntent.putExtra("aspectX", 1);
cropIntent.putExtra("aspectY", 1);
cropIntent.putExtra("outputX", 256);
cropIntent.putExtra("outputY", 256);
cropIntent.putExtra("return-data", true);
startActivityForResult(cropIntent, PIC_CROP);
关于活动结果
if (requestCode == PIC_CROP) {
Bundle extras = data.getExtras();
Bitmap thePic = extras.getParcelable("data");
BitmapDrawable drawable = (BitmapDrawable) i1.getDrawable();
Bitmap bb11 = drawable.getBitmap();
File sdCardDirectory = new File(
Environment
.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),
"MFA - AFTER - CROP");
if (!sdCardDirectory.exists()) {
if (!sdCardDirectory.mkdirs()) {
Log.d("MySnaps", "failed to create directory");
}
}
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss")
.format(new Date());
String nw = "MFA_CROP_" + timeStamp + ".jpeg";
File image = new File(sdCardDirectory, nw);
boolean success = false;
// Encode the file as a PNG image.
FileOutputStream outStream;
try {
outStream = new FileOutputStream(image);
bb11.compress(Bitmap.CompressFormat.JPEG, 100, outStream);
/* 100 to keep full quality of the image */
outStream.flush();
outStream.close();
success = true;
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
b7.setEnabled(true);
}
HappY CodinG:)
答案 2 :(得分:0)
这是我的代码
intent.setData(mImageCaptureUri);
intent.putExtra("crop", "true");
intent.putExtra("outputX", 320);
intent.putExtra("outputY", 470);
intent.putExtra("aspectX", 320);
intent.putExtra("aspectY", 470);
intent.putExtra("scale", true);
intent.putExtra("return-data", true);
从本网站引用