Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting Bitmap in memory to Bitmap with Bitmap.Config.RGB_565

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);
like image 597
Euthyphro Avatar asked Feb 25 '13 01:02

Euthyphro


1 Answers

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.

like image 149
sa.shadow Avatar answered Sep 24 '22 07:09

sa.shadow