Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to load RGB565 buffer to ImageView

I have file "Image_RGB565.raw" which contains image buffer in RGB565 format. I want this image to be displayed in ImageView. is it possible without extra code of converting to RGB888?

I have tried to

BitmapFactory.Options opt = new BitmapFactory.Options();
opt.inPreferredConfig = Bitmap.Config.RGB_565;
Bitmap bitmap = BitmapFactory.decodeFile("Image_001_RGB565.raw");

but bitmap is null.

then i also tried to load using bytearray

BitmapFactory.Options opt = new BitmapFactory.Options();
opt.inPreferredConfig = Bitmap.Config.RGB_565;
Bitmap bitmap = BitmapFactory.decodeFile(decodeByteArray(data, 0, data.length, opt);

Please guide me to correct direction. My image dimension is 160x160.

like image 332
JRC Avatar asked Oct 18 '10 05:10

JRC


2 Answers

First of all, you should avoid storing or transferring raw images to your phone; its always better to convert them to a compressed format such as PNG or JPG on your PC, and deploy that artwork to the device.

However, if for some unusual reason you really want to load raw images, here is an approach:

1) create an Bitmap.Config.RGB_565 bitmap to contain your image. You must know the height and width of your raw image.

2) create a ByteBuffer that is sufficient size to contain all the pixels in the bitmap; each scanline of the image takes a stride amount of pixels, which may be more than the image's width. This extra padding on each line is necessary. (Sometimes by happy coincidence the stride is the same as the width - there is no padding; this cannot be relied upon, always do your offsets taking account for stride.)

With ByteBuffers, its important to understand the read and write offsets. After you've written to a ByteBuffer, you flip it to read those bytes.

3) read the raw pixels from the file into your ByteBuffer, one scanline at a time, with the appropriate stride spacing between lines.

4) use Bitmap.copyPixelsFromBuffer().

5) discard the ByteBuffer

like image 95
Will Avatar answered Nov 12 '22 02:11

Will


I did it like this, and it works.

Bitmap bitmap = Bitmap.createBitmap(captureWidth, captureHeight, Bitmap.Config.RGB_565);

ByteBuffer buffer = ByteBuffer.wrap(data);

bitmap.copyPixelsFromBuffer(buffer);

like image 40
Out Of Office Avatar answered Nov 12 '22 03:11

Out Of Office