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

泪湿孤枕 提交于 2019-11-30 03:26:28

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.

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.

Euthyphro

Found the answer here https://stackoverflow.com/a/12148450/1364673, thanks to siliconeagle.

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

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