简体   繁体   English

如何在裁剪意图期间旋转图像

[英]How to rotate image during crop intent

There are some old posts here concerning this, but things seem to have changed. 这里有一些关于此的旧帖子,但事情似乎发生了变化。 Yesterday I got an answer for a straightforward way to crop an image using an intent . 昨天我得到了一个答案, 可以直接使用意图裁剪图像 The second part of the question is to add rotation functionality to the preview. 问题的第二部分是向预览添加旋转功能。 Does anyone know how I might add this functionality? 有谁知道我如何添加此功能? If it's rather complicated, does anyone know of an example out there? 如果它相当复杂,有没有人知道那里有一个例子?

I came across the same problem and used this work-around hack. 我遇到了同样的问题并使用了这种解决方法。 The Intent called to crop uses a URI, while most solutions for rotations use the Bitmap. 调用的Intent使用URI,而大多数旋转解决方案使用Bitmap。 So what I did was: 所以我做的是:

  1. Retrieve the bitmap from the URI 从URI中检索位图

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

    Where photo is the new file you defined when triggering the camera Intent. 照片是您在触发相机Intent时定义的新文件。

  2. Rotate the bitmap 旋转位图

  3. Get the URI of the rotated bitmap, I used the code from here 获取旋转位图的URI,我使用了这里的代码
 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 . 4。 Trigger the crop intent based on this new URI 根据此新URI触发裁剪意图

I'm assuming you are missing steps 1 & 3 and know how to do the other two. 我假设你错过了第1步和第3步,并知道如何做其他两个。

Try this for implementing rotation 试试这个来实现旋转

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

Hope it helps 希望能帮助到你

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM