android顯示RGB565資料映像

來源:互聯網
上載者:User

標籤:android   bmp   rgb565   

本人近期做了一個項目,是關於類似於一個視頻監控,主要的是用了一個開發板,把一幀一幀的映像資料通過socket傳出來,然後在Android用戶端進行監聽資料;當擷取到資料之後,在imageview上顯示出來,最核心的部分是看了老外的一篇文章才解決的問題。

文章是這樣的,老外遇到了和我一樣的問題:大概就是怎麼讓imageview顯示RGB565的映像,他嘗試了兩種方法,就是下面的代碼部分,當然這兩種方法都不可以!


6down votefavorite3

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


6down voteaccepted

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



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


當然解決問題的就是第二種方法,先建立一個rgb565格式的bitmap對象,然後建立一個位元組buffer,用它的wrap方法封裝,然後把像素資料從裡面拷貝出來;這就是核心實現部分,當然你們要做的就是怎麼擷取到一幀的RGB565的位元組資料。

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

另一個好的文章:http://www.iteye.com/problems/79561




android顯示RGB565資料映像

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.