Bitmap.createBitmap (bitmap source, int x, int y, int width, int height) returns an invalid bitmap

I have to crop the bitmap. For this I use

Bitmap bitmap = Bitmap.createBitmap(imgView.getWidth(),imgView.getHeight(), Bitmap.Config.RGB_565); Bitmap result =Bitmap.createBitmap(bitmap,imgView.getLeft()+10, imgView.getTop()+50, imgView.getWidth()-20, imgView.getHeight()-100); bitmap.recycle(); Canvas canvas = new Canvas(result); imgView.draw(canvas); 

But it crop the bottom and right of the bitmap. The top and left parts of the bitmap exist in the output. This means that the position of x and y has no effect.

I am looking for good documentation. But I could not.

Thanks in advance

What is the problem here and how to solve it?

+7
source share
1 answer

Mostly your problem arises from the fact that you are creating a bitmap image. You don’t invest anything in it. Then you create a smaller raster map, and then render the imageView with a smaller raster image.

This cuts off the bottom 100 pixels and 20 pixels.

You need to create a big bitmap. Add image data to this bitmap. Then resize it.

The following code should work:

 Bitmap bitmap = Bitmap.createBitmap(imgView.getWidth(),imgView.getHeight(), Bitmap.Config.RGB_565); Canvas canvas = new Canvas(bitmap); imgView.draw(canvas); Bitmap result =Bitmap.createBitmap(bitmap,imgView.getLeft()+10, imgView.getTop()+50, imgView.getWidth()-20, imgView.getHeight()-100); bitmap.recycle(); 
+12
source

Source: https://habr.com/ru/post/927545/


All Articles