在android中裁剪图像的特定部分

时间:2022-08-27 10:42:08

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

我想从下图中裁剪红色的部分,android中有没有简单的方法可以裁剪如下的图像。

在android中裁剪图像的特定部分

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

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

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

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.

如果宽度和高度在2百万像素左右,但是如果裁剪的部分超过3百万像素,那么这个代码就可以正常工作。

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

在裁剪过程中是否有任何方法可以处理超过3MP的图像?

4 个解决方案

#1


6  

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

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

上面的方法在裁剪前进行图像的设置,这样你就可以得到最好的裁剪图像而不会产生OOM错误。

For more detail you can refer this blog

有关更多细节,请参阅本博客

#2


2  

Refer this link http://www.londatiga.net/featured-articles/how-to-select-and-crop-image-on-android/ and the source code is in this link https://github.com/lorensiuswlt/AndroidImageCrop

参考这个链接http://www.londatiga.net/featured-articles/how- and-crop-image-on-android/,源代码在这个链接中,https://github.com/lorensiuswlt/AndroidImageCrop

#3


1  

1- Change your imageview for bitmap

1-更改位图的imageview

final Bitmap bitmap=BitmapFactory.decodeResource(getResources(), R.drawable.img);

2-use your bitmap to crop what you want

2 .用你的位图裁剪你想要的

Bitmap croppedBmp = Bitmap.createBitmap(bitmap, x, y , width , height);

3-Take care x,y from Top and left

3-小心x,y从上到左。

4- to preview your bitmap again in your imageview

4-在imageview中再次预览位图

imageView.setImageBitmap(croppedBmp);

#4


0  

If you want to crop image in any shape OR only selected part then you can use ready made open source library

如果您想裁剪任意形状的图像或只选择部分,那么您可以使用现成的开源库

#1


6  

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

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

上面的方法在裁剪前进行图像的设置,这样你就可以得到最好的裁剪图像而不会产生OOM错误。

For more detail you can refer this blog

有关更多细节,请参阅本博客

#2


2  

Refer this link http://www.londatiga.net/featured-articles/how-to-select-and-crop-image-on-android/ and the source code is in this link https://github.com/lorensiuswlt/AndroidImageCrop

参考这个链接http://www.londatiga.net/featured-articles/how- and-crop-image-on-android/,源代码在这个链接中,https://github.com/lorensiuswlt/AndroidImageCrop

#3


1  

1- Change your imageview for bitmap

1-更改位图的imageview

final Bitmap bitmap=BitmapFactory.decodeResource(getResources(), R.drawable.img);

2-use your bitmap to crop what you want

2 .用你的位图裁剪你想要的

Bitmap croppedBmp = Bitmap.createBitmap(bitmap, x, y , width , height);

3-Take care x,y from Top and left

3-小心x,y从上到左。

4- to preview your bitmap again in your imageview

4-在imageview中再次预览位图

imageView.setImageBitmap(croppedBmp);

#4


0  

If you want to crop image in any shape OR only selected part then you can use ready made open source library

如果您想裁剪任意形状的图像或只选择部分,那么您可以使用现成的开源库