如何裁剪 Android 中已知 URI 的图像?
我希望在 imageview 中显示的图像被选择为特定部分(并且只有选定的部分需要突出显示,其他部分为半透明),并且该部分也可以根据需要调整大小或由用户在触摸事件上完成。
现在,需要裁剪图像的选定部分,然后显示并保存裁剪后的图像。
编辑:
我使用 Intent 打开图像并使用 intent.putExtra("crop","true"); 裁剪它。
但是在传递意图时,我想打开 URI 已知的图像,而不是打开整个图像库相册。
任何人都可以建议,如何通过意图传递来打开特定的 URI 以打开图像。 提前致谢。
I want image being shown in imageview to be selected with particular portion(and only selected portion needs to highlighted and other portion to be semi transparent) and that portion can also be resized as needed or done done by user on touch event.
Now, the selected portion of image is needed to be croped and then show and save that cropped image.
EDIT:
I used Intent to open the image and crop it using intent.putExtra("crop","true");
But while passing intent I want to open image whose URI is already known instead of opening the whole album of image gallery.
Can anyone suggest, how can I open particular URI through intent passing for opening image.
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
关于问题的最后一部分,如果您使用的是最新的 Gingerbread(2.3.3,API 级别 10),则可以使用
BitmapRegionDecoder
裁剪图像。它很有用,因为在这个 API 出现之前,您必须将整个图像加载到内存中才能进行裁剪。对于 5mpix 和 8mpix 相机,如果不进行二次采样,这通常是不可能的(即裁剪后的图像会损失很多分辨率)。
Regarding the last part of your question, if you're on the very latest Gingerbread (2.3.3, API level 10), you can use
BitmapRegionDecoder
to crop an image.It's useful because, until this API existed, you had to load the entire image into memory before you could crop. With 5mpix and 8mpix cameras this is usually impossible without subsampling (i.e. the cropped image loses lots of resolution).
更新:在编辑问题并更准确地描述后更改了我的答案。
您面临的问题有着悠久的历史,也在SO:
无法在android中找到com.android.camera.CropImage活动
一个答案已经描述了您需要什么。请注意,您使用的 Intent 不属于官方 SDK,因此您可能会遇到不同类型的问题。我遇到的问题是相机拍摄图像后立即使用裁剪。此外,它在不同的 Android 版本之间不兼容,所以如果你让它在 1.5 上运行,它可能不会在 2.3 上运行。其他有用的链接:
http://groups.google.com/group/android-developers/browse_thread/thread/2dd647523926192c/569f36b5b28f2661?lnk=gst&q=Crop+image+intent#569f36b5b28f2661
http://groups .google.com/group/android-developers/browse_thread/thread/2dd647523926192c/dcbe5aef29eddad6?lnk=gst&q=Crop+image+intent#dcbe5aef29eddad6
http://groups.google.com/group/android-开发人员/browse_thread/thread/d7b6a133c164aa17/184bf3b85da2ce58?lnk=gst&q=Crop+image+intent#184bf3b85da2ce58
UPDATE: Changed my answer after question was edited and more precisely described.
Issue you are facing with has long history, also at SO:
unable to find com.android.camera.CropImage activity in android
One answer has described what you need. Please note that you are using intent that is not part of the official SDK, and you may encounter different kind of issues. Issue I experienced was using crop immediatelly after image was taken by the camera. Also, it is not compatible through different Android versions, so if you get it working for 1.5 maybe it will not work for 2.3. Other useful links:
http://groups.google.com/group/android-developers/browse_thread/thread/2dd647523926192c/569f36b5b28f2661?lnk=gst&q=Crop+image+intent#569f36b5b28f2661
http://groups.google.com/group/android-developers/browse_thread/thread/2dd647523926192c/dcbe5aef29eddad6?lnk=gst&q=Crop+image+intent#dcbe5aef29eddad6
http://groups.google.com/group/android-developers/browse_thread/thread/d7b6a133c164aa17/184bf3b85da2ce58?lnk=gst&q=Crop+image+intent#184bf3b85da2ce58
查看我对这个问题的回答。它不处理问题的触摸调整大小方面,而是处理在原始图像上绘制图像的部分。
最重要的是,您不想使用 ImageView,因为它主要用于显示具有各种缩放属性的静态图像。您最好使用带有重写的 draw() 方法的自定义视图。
Check out my answer to this question. It doesn't deal with the touch-to-resize aspect of your question, but handles drawing parts of an image over the original image.
Bottom line is, you don't want to use an ImageView, since that's mainly for displaying a static image with various scaling properties. You're better off using a custom view with an overridden draw() method.