【问题标题】:How to rotate image during crop intent如何在裁剪意图期间旋转图像
【发布时间】:2026-02-23 06:45:02
【问题描述】:

这里有一些关于这个的旧帖子,但事情似乎已经改变了。昨天我得到了a straightforward way to crop an image using an intent 的答复。问题的第二部分是向预览添加旋转功能。有谁知道我可以如何添加此功能?如果它相当复杂,有人知道那里的例子吗?

【问题讨论】:

  • 裁剪意图不公开,在某些设备上可能会丢失。幸运的是,您可以使用实现它的the library。它的代码本质上是从 AOSP 复制的,没有处理旋转的逻辑。因此,您必须在裁剪图像之前或之后自己编排图像。最大的区别是旋转不一定是交互式的,而裁剪肯定是交互式的。

标签: android camera rotation surfaceview preview


【解决方案1】:

我遇到了同样的问题并使用了这个变通方法。调用裁剪的 Intent 使用 URI,而大多数旋转解决方案使用 Bitmap。所以我所做的是:

  1. 从 URI 中检索位图

    Bitmap bmCameraCapture = BitmapFactory.decodeFile(Uri.fromFile(photo).getPath());

    其中 photo 是你在触发相机 Intent 时定义的新文件。

  2. 旋转位图

  3. 获取旋转位图的URI,我使用了here的代码
public Uri getImageUri(Context inContext, Bitmap inImage) {
    ByteArrayOutputStream bytes = new ByteArrayOutputStream();
    inImage.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
    String path = Images.Media.insertImage(inContext.getContentResolver(), inImage, "Title",null);
    return Uri.parse(path);
}

4 .根据这个新的 URI 触发裁剪意图

我假设您缺少第 1 步和第 3 步,并且知道如何执行其他两个步骤。

【讨论】:

    【解决方案2】:

    试试这个实现轮换

    Bitmap bMap = BitmapFactory.decodeResource(getResources(),R.drawable.test);
    Matrix mat = new Matrix();
    mat.postRotate(90);
    Bitmap bMapRotate = Bitmap.createBitmap(bMap, 0, 0,
    bMap.getWidth(), bMap.getHeight(), mat, true);
    BitmapDrawable bmd = new BitmapDrawable(bMapRotate);
    image.setImageBitmap(bMapRotate);
    image.setImageDrawable(bmd);
    

    希望对你有帮助

    【讨论】:

    • 感谢您的回复,但问题不在于如何旋转图像。问题是:如何向预览添加旋转功能,以便用户可以像用户裁剪图像一样旋转图像。如果您查看链接,您将看到裁剪是如何完成的:通过意图。