【问题标题】:Crop a fixed size image in Android在 Android 中裁剪固定大小的图像
【发布时间】:2015-02-15 06:08:54
【问题描述】:

我正在尝试裁剪图像,但我希望能够将裁剪区域设置为 640 像素 x 640 像素。我想防止用户裁剪到一个非常小的区域。所以基本上我宁愿为裁剪区域设置一个固定的高度和宽度。我已经研究了几个第三方库,但没有看到解决这个问题。我怎样才能做到这一点?

【问题讨论】:

  • “我该怎么做?” -- 从其中一个库开始并对其进行修改以包含您想要的功能。
  • @CommonsWare 我做到了。检查我的答案并投票是否是您的意思:)

标签: android crop


【解决方案1】:

我会使用以下解决方案之一:

  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);
    }

这是pull request.

在我的 github https://github.com/mklimek/android-crop/tree/newfeature_fied_size_crop 上查看完整代码。

您可以克隆构建,然后将其添加到您的项目中。

希望对你有所帮助。

【讨论】:

  • 我也试过这两个库,但都没有固定的裁剪窗口。
  • @toobsco42 我编辑了我的答案,我认为它会解决你的问题。
  • 我尝试使用您的代码,但您的 github 存储库中似乎不存在“withFixedSize”方法。你知道我怎么称呼这个方法吗?
  • @Shargotth android-crop 所有者没有添加我的拉取请求,您必须使用我的 repo 来构建自己的 android-crop 并在您的项目中放入最终的 .aar github.com/mklimek/android-crop/tree/newfeature_fied_size_crop
  • 非常使用完整答案
【解决方案2】:

有一种方法可以使用Æ

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
    }



}

【讨论】:

  • 但这并不能修复裁剪窗口。它仍然可以调整大小。
  • 如果你玩弄 .putExtra("scale", true) .putExtra("scaleUpIfNeeded", true) 试试真/假变体
  • Android 没有CROP Intent: commonsware.com/blog/2013/01/23/…
  • @CommonsWare:您是否在链接中阅读了这些库的源代码,Thay 还指出了 Android 上的默认意图裁剪: com.android.camera.action.CROP 。那么如果我们直接使用它,这些库作为帮助器,而不是差异。
  • @KhangTran:“您是否阅读了链接中这些库的源代码”——是的,我阅读了。你没有。 “Thay 还指出了 Android 上的默认意图裁剪:com.android.camera.action.CROP”——第一个提供了自己的裁剪活动。第二个提供了自己的裁剪视图。第三个是第二个的叉子。最后一个已经不存在了,但现在有plenty of other candidates
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-01-01
  • 2013-11-11
  • 2012-03-12
  • 2021-09-07
  • 2011-06-02
相关资源
最近更新 更多