Java 在android中裁剪后保存图像
我正在尝试保存裁剪后的图像,该图像已成功保存,但问题是保存的图像太小。我想得到裁剪图像的原始尺寸。这是我保存裁剪图像的代码Java 在android中裁剪后保存图像,java,android,image,android-camera,Java,Android,Image,Android Camera,我正在尝试保存裁剪后的图像,该图像已成功保存,但问题是保存的图像太小。我想得到裁剪图像的原始尺寸。这是我保存裁剪图像的代码 Bundle extras = data.getExtras(); if (extras != null) { Bitmap photo = extras.getParcelable("data"); ByteArrayOutputStream bytes = new ByteArrayOutputStream(); photo.compress(Bitmap.Compr
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();
}
谁能帮我得到高质量的裁剪图像。任何帮助都将不胜感激。谢谢:)发送长宽比和尺寸的裁剪意向
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);
}
快乐编码:)这一切都取决于原始图像的大小和裁剪图像的大小 裁剪的一种方法是:
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)的正方形图像(纵横比=1)。
所以,如果你选择一个800px的正方形图像,它将被缩小,失去质量。
相反,选择一个50像素的正方形也会得到400像素的结果
现在,如果你去掉尺寸限制(就在虚线处)。您将返回最初选择的区域(示例为800和50),并获得尽可能高的质量。这是我的代码
intent.setData(mImageCaptureUri);
意图。额外(“作物”、“真实”);
意向。额外(“输出”,320);
意向。额外(“输出”,470);
intent.putExtra(“aspectX”,320);
意图.putExtra(“aspectY”,470);
意图。putExtra(“比例”,真实);
intent.putExtra(“返回数据”,true)代码>您正在准确地保存您得到的内容;具有该格式的最高质量。这方面没有太多改进。我见过许多具有高分辨率裁剪图像的应用程序,应该有办法。atline BitmapDrawable drawable=(BitmapDrawable)i1.getDrawable();什么是i1?i1是imageview,我在编码中使用了它。这只是为了得到想法,如何去做。工作完美,不知道为什么你没有被提升。谢谢Kathir我怎么能去掉尺寸限制?它们介于两个“剪贴”评论之间。extras“outputX”和“outputY”为此目的设置输出大小。extras“outputX”和“outputY”的大小应该是多少以消除大小限制。感谢您的回复,非常感谢:)我想完全省略这两行代码。这样,您就可以准确地获得在裁剪过程中选择的内容。