在android中裁剪图像的特定部分 [英] Crop particular part of image in android

查看:40
本文介绍了在android中裁剪图像的特定部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从下面的图片中裁剪红色部分,android中有没有简单的方法可以裁剪下面的图片.

I want to crop Red part from following image, Is there any simple method available in android that can crop following image.

我发现了很多 SO 问题,但都建议使用以下代码:

I have found many SO questions but all are suggesting to used following code:

Bitmap croppedBitmap = Bitmap.createBitmap(bitmapOriginal, 100, 100,100, 100); 

如果 width &高度大约为 2MP 分辨率,但如果裁剪部分的分辨率超过 3MP,则应用程序会因 OOM 错误而崩溃.

This code work well if width & height are around 2MP resolution, but if that cropped part is more than 3MP resolution than application got crashed with OOM error.

有什么方法可以在裁剪过程中处理超过 3MP 的图像?

Is there any way that handle image more than 3MP during cropping?

推荐答案

您可以使用以下代码来解决您的问题.

You can used following code that can solve your problem.

Matrix matrix = new Matrix();
matrix.postScale(0.5f, 0.5f);
Bitmap croppedBitmap = Bitmap.createBitmap(bitmapOriginal, 100, 100,100, 100, matrix, true);

以上方法在裁剪前对图像进行 postScalling,因此您可以在裁剪后的图像中获得最佳效果,而不会出现 OOM 错误.

Above method do postScalling of image before cropping, so you can get best result with cropped image without getting OOM error.

有关更多详细信息,您可以参考此博客

For more detail you can refer this blog

这篇关于在android中裁剪图像的特定部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆