I recently made a project, is about similar to a video surveillance, the main is to use a development board, a frame of the image data through the socket, and then the Android client to listen to data, when the data is obtained, the ImageView on the display, The most important part is to see a foreigner's post to solve the problem.
The post is this, the foreigner met with me the same problem: probably is how to let ImageView display RGB565 image, he tried two methods, is the following code part, of course, neither of these methods can!
6down Votefavorite3 |
I have the file "Image_rgb565.raw" which contains Image buffer in RGB565 format. I want this image to being 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);
|
Eventually there were two answers
6down voteaccepted |
First of all, your should avoid storing or transferring raw images to your phone; Its all better to convert them to a compressed format such as PNG or JPG on your PC, and deploy this artwork to the Dev Ice. However, if for some unusual reason the really want to the load raw images, here are 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's sufficient size to contain all the pixels in the bitmap; each scanline Of the image takes a Stride Amount of pixels, which may is more than the image ' s width. This extra padding on each line is necessary. (sometimes by happy coincidence the stride are the same as the width-there is no padding; this cannot be relied upon, ALW Ays do your offsets taking account for stride.) With bytebuffers, it important to understand the read and write offsets. After your ' ve written to a bytebuffer, you have 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 theByteBuffer |
And
2down vote |
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);
Of course, the second way to solve the problem is to create a bitmap object in rgb565 format, then create a byte buffer, encapsulate it with its wrap method, and then copy the pixel data from it; This is the core implementation part, Of course, all you have to do is get a frame of RGB565 byte data. |
Post the original address: Http://stackoverflow.com/questions/3956770/how-to-load-rgb565-buffer-to-imageview
Another good post: http://www.iteye.com/problems/79561
Android Display RGB565 data image