How to decode a bitmap with a sample size not equal to power 2?

A simple example:

BitmapFactory.Options opts = new BitmapFactory.Options(); opts.inSampleSize = scale; Bitmap bmp = BitmapFactory.decodeStream(is, null, opts); 

When I pass a scale value not equal to the power of two, the bitmap still scales to the nearest power of 2 values. For example, if scale = 3 , then for some reason the actual scale becomes 2 . Maybe because I use hardware acceleration?

Anyway, how can I scale a bitmap to a value other than level 2 without memory allocation for a full bitmap?

PS I know that using the power of the two is much faster, but in my case the time is not so critical, and I need to scale the image exactly to the scale provided (otherwise it will become too large or too small) - I'm "woking with image processing, so big the image is not such a problem ( ImageView scales it to the required size), but to apply some filter, for example, additional time is required.

+4
source share
1 answer

If you read the documentation for inSampleSize :

Note: the decoder will try to fulfill this request, but the resulting raster map may have different sizes that were exactly requested. In addition, credentials 2 are often faster / easier for a decoder.

You are not guaranteed the exact size. Since the memory sounds like it is a concern, I would use your current method to get the image more than you need, but something that works better for your memory needs than the original image. Then use another method like Bitmap.createScaledBitmap to get it according to your exact sizes.

+2
source

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


All Articles