Converting Bitmap in memory to Bitmap with Bitmap.Config.RGB_565
Asked Answered
A

3

18

I have a loaded Bitmap which I would like to convert to set the config to Bitmap.Config.RGB_565. Is there a simple way of converting a Bitmap to this configuration after the Bitmap is already loaded into memory? For example, below I have a bitmap being decoded from the application resources, however, how would I convert an already loaded Bitmap to RGB_565? I'm sure it's something simple, however, I'm fairly new to working with Bitmaps and after a few hours of looking online, unfortunately I couldn't find what I needed specifically.

BitmapFactory.Options options = new BitmapFactory.Options();
options.inPreferredConfig=Bitmap.Config.RGB_565
bitmap=BitmapFactory.decodeResource(getResources(), R.drawable.myphoto ,options);
Artel answered 25/2, 2013 at 1:43 Comment(0)
P
30

I haven't tested this but it should work:

private Bitmap convert(Bitmap bitmap, Bitmap.Config config) {
    Bitmap convertedBitmap = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), config);
    Canvas canvas = new Canvas(convertedBitmap);
    Paint paint = new Paint();
    paint.setColor(Color.BLACK);
    canvas.drawBitmap(bitmap, 0, 0, paint);
    return convertedBitmap;
}

call the methods like this:

Bitmap convertedBitmap = convert(bitmap, Bitmap.Config.RGB_565);

You can do all kinds of additional transformations like rotating, stretching etc. if you use the drawBitmap with a Matrix.

Pseudo answered 25/2, 2013 at 3:29 Comment(3)
@JasonStack not sure I understand your question. Do you want to know what can be done with matrices applied to bitmap or how it can be done?Pseudo
Google it, there are tons of examples like: programcreek.com/java-api-examples/android.graphics.MatrixPseudo
do all bitmap operations use matrices under the hood?Lardon
T
36

You can also try this:

Bitmap converted = original.copy(Config.RGB_565, false);

From the documentation of Bitmap.copy():

Tries to make a new bitmap based on the dimensions of this bitmap, setting the new bitmap's config to the one specified, and then copying this bitmap's pixels into the new bitmap. If the conversion is not supported, or the allocator fails, then this returns NULL.

Looking through the native source code, you should be fine converting between any values of Bitmap.Config.

Taskmaster answered 15/8, 2014 at 18:29 Comment(1)
If you want to retain transparency be sure to use Config.ARGB_8888 insteadWeirdie
P
30

I haven't tested this but it should work:

private Bitmap convert(Bitmap bitmap, Bitmap.Config config) {
    Bitmap convertedBitmap = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), config);
    Canvas canvas = new Canvas(convertedBitmap);
    Paint paint = new Paint();
    paint.setColor(Color.BLACK);
    canvas.drawBitmap(bitmap, 0, 0, paint);
    return convertedBitmap;
}

call the methods like this:

Bitmap convertedBitmap = convert(bitmap, Bitmap.Config.RGB_565);

You can do all kinds of additional transformations like rotating, stretching etc. if you use the drawBitmap with a Matrix.

Pseudo answered 25/2, 2013 at 3:29 Comment(3)
@JasonStack not sure I understand your question. Do you want to know what can be done with matrices applied to bitmap or how it can be done?Pseudo
Google it, there are tons of examples like: programcreek.com/java-api-examples/android.graphics.MatrixPseudo
do all bitmap operations use matrices under the hood?Lardon
A
0

Found the answer here https://mcmap.net/q/673148/-loading-bitmap-with-rgb565-config, thanks to siliconeagle.

The solution is to create a new bitmap with the required encoding as per link above example.

Artel answered 25/2, 2013 at 3:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.