Crop particular part of image in android

心不动则不痛 提交于 2019-11-26 22:55:18

问题


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

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.

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


回答1:


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.

For more detail you can refer this blog




回答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




回答3:


1- Change your imageview for bitmap

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

2-use your bitmap to crop what you want

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

3-Take care x,y from Top and left

4- to preview your bitmap again in your imageview

imageView.setImageBitmap(croppedBmp);



回答4:


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



来源:https://stackoverflow.com/questions/9747295/crop-particular-part-of-image-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!