在Android中裁剪固定尺寸的图片

16

我正在尝试裁剪一张图片,但我想将裁剪区域设置为 640 像素 x 640 像素。我希望防止用户将裁剪区域缩小到非常小的范围。因此,基本上我更喜欢将裁剪区域的高度和宽度固定下来。我已经研究了几个第三方库,但没有解决这个问题。我该怎么做?

输入图像描述


4
“我该怎么做?”-- 从一个库开始并修改它以包含您想要的功能。 - CommonsWare
@CommonsWare 我做到了。检查一下我的答案,如果符合你的意思,请投票支持 :) - klimat
2个回答

18

我建议使用以下两种解决方案之一:

  1. https://github.com/jdamcd/android-crop
  2. https://github.com/edmodo/cropper

这两个方案似乎都很适合解决你的问题,而且一定可以涵盖更多的边缘情况、设备和其他 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.

您可以克隆、构建并将其添加到您的项目中。

希望它能帮助到您。


我也尝试了这两个库,但是都没有固定的裁剪窗口。 - Etienne Lawlor
3
我修改了我的答案,我认为它将解决你的问题。 - klimat
我尝试使用你的代码,但是在你的Github仓库中似乎不存在“withFixedSize”方法。你知道我该如何调用这个方法吗? - MHogge
1
@Shargotth android-crop的所有者尚未添加我的拉取请求,您必须使用我的存储库构建自己的android-crop,并将最终的.aar放入您的项目中。https://github.com/mklimek/android-crop/tree/newfeature_fied_size_crop - klimat
非常有用的答案 - Aboobakkar P S
使用此函数后,图片缩放功能被禁用。请告知如何启用该功能。 - Muhammad Zubair Ashraf

4
有一种方法可以使用。
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);

你应该这样调用crop方法:
    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
    }



}

但这并不能修复裁剪窗口,它仍然可以调整大小。 - Etienne Lawlor
如果您尝试使用.putExtra("scale", true)和.putExtra("scaleUpIfNeeded", true)进行调试,可以尝试使用true/false变量。 - SteBra
1
Android没有CROP Intent: http://commonsware.com/blog/2013/01/23/no-android-does-not-have-crop-intent.html - CommonsWare
@CommonsWare:您是否阅读了这些库的源代码?它们也指向 Android 的默认意图裁剪: com.android.camera.action.CROP 。所以,如果我们直接使用它,这些库只是帮手,没有什么不同。 - Khang Tran
@KhangTran:“你读了这些库的源代码吗?” - 是的,我读了。你没有。“他们还指向Android上默认意图裁剪:com.android.camera.action.CROP” - 第一个提供自己的裁剪活动。第二个提供自己的裁剪视图。第三个是第二个的分支。最后一个不再存在,但现在有很多其他选择。 - CommonsWare

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接