本人近期做了一个项目,是关于类似于一个视频监控,主要的是用了一个开发板,把一帧一帧的图像数据通过socket传出来,然后在Android客户端进行监听数据;当获取到数据之后,在imageview上显示出来,最核心的部分是看了老外的一篇帖子才解决的问题。

帖子是这样的,老外遇到了和我一样的问题:大概就是怎么让imageview显示RGB565的图像,他尝试了两种方法,就是下面的代码部分,当然这两种方法都不可以!


6
down vote favorite 3

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);

最终出现了两个ANSWERS


6
down vote accepted

Firstof 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 anBitmap.Config.RGB_565bitmapto contain your image. You must know the height and width of your raw image.

2) create aByteBufferthat is sufficient size to contain all the pixels in the bitmap; eachscanlineof the image takes astrideamount 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, youflipit to read those bytes.

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

4) useBitmap.copyPixelsFromBuffer().

5) discard theByteBuffer



2
down 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);


当然解决问题的就是第二种方法,先创建一个rgb565格式的bitmap对象,然后创建一个字节buffer,用它的wrap方法封装,然后把像素数据从里面拷贝出来;这就是核心实现部分,当然你们要做的就是怎么获取到一帧的RGB565的字节数据。

贴出原帖地址:http://stackoverflow.com/questions/3956770/how-to-load-rgb565-buffer-to-imageview

另一个好的帖子:http://www.iteye.com/problems/79561


更多相关文章

  1. 一句话锁定MySQL数据占用元凶
  2. Android(安卓)ProgressBar详解以及自定义
  3. 实现Android监控任意控件或按键双击事件方法
  4. Android工程中R类访问不到工程中的资源文件的解决方法
  5. Android(安卓)不得不说的VideoView的一些坑及其解决方案
  6. Android手势源码浅析----手势识别
  7. 在android里做一个竖着的seekbar
  8. android-从音频数据库获取音乐数据
  9. View那些事儿(1) -- View绘制的整体流程

随机推荐

  1. android 退出整个程序
  2. Android之Activity--Loaders
  3. android中操纵sqlite数据库
  4. android 学习视频
  5. android start new process
  6. HttpUrlConnect 响应为空的问题分析
  7. Android(安卓)聊天界面对话
  8. Android璧勬簮鏂囦欢 - 浣跨敤璧勬簮瀛樺
  9. Android电源管理
  10. android SQLite数据库使用实例