我正在尝试裁剪一张图片,但我想将裁剪区域设置为 640 像素 x 640 像素。我希望防止用户将裁剪区域缩小到非常小的范围。因此,基本上我更喜欢将裁剪区域的高度和宽度固定下来。我已经研究了几个第三方库,但没有解决这个问题。我该怎么做?
我建议使用以下两种解决方案之一:
这两个方案似乎都很适合解决你的问题,而且一定可以涵盖更多的边缘情况、设备和其他 Android 东西,以使其更加稳定可靠。
编辑说明:
我对 android-crop
进行了一些更改,现在您可以使用 withFixedSize(int width, int height)
来设置固定的像素裁剪区域。
看起来是这样的:
private void beginCrop(Uri source) {
Uri outputUri = Uri.fromFile(new File(getCacheDir(), "cropped"));
new Crop(source).output(outputUri).withFixedSize(640, 640).start(this);
}
这是一个拉取请求。
完整的代码请查看我的GitHub:https://github.com/mklimek/android-crop/tree/newfeature_fied_size_crop.
您可以克隆、构建并将其添加到您的项目中。
希望它能帮助到您。
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(selectedImage, "image/*");
//set crop properties
cropIntent.putExtra("crop", "true");
//indicate aspect of desired crop
cropIntent.putExtra("aspectX", 1);
cropIntent.putExtra("aspectY", 1);
//indicate output X and Y
cropIntent.putExtra("outputX", 640);
cropIntent.putExtra("outputY", 640);
//retrieve data on return
cropIntent.putExtra("return-data", true);
//start the activity - we handle returning in onActivityResult
startActivityForResult(cropIntent, PIC_CROP);
}
catch(ActivityNotFoundException anfe){
//display an error message
String errorMessage = "Whoops - your device doesn't support the crop action!";
Toast toast = Toast.makeText(this, errorMessage, Toast.LENGTH_SHORT);
toast.show();
}
}
您感兴趣的是这部分代码:
cropIntent.putExtra("outputX", 640);
cropIntent.putExtra("outputY", 640);
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CAMERA && resultCode == RESULT_OK) {
selectedImage = data.getData();
performCrop();
}
if (requestCode == UPLOAD && resultCode == RESULT_OK) {
selectedImage = data.getData();
performCrop();
}
if (requestCode == PIC_CROP && resultCode == RESULT_OK){
Bundle extras = data.getExtras();
//get the cropped bitmap
Bitmap thePic = extras.getParcelable("data");
// Do what you want to do with the pic here
}
}
CROP
Intent
: http://commonsware.com/blog/2013/01/23/no-android-does-not-have-crop-intent.html - CommonsWare