簡體   English   中英

在Android中將字節[]渲染為位圖

[英]Render a byte[] as Bitmap in Android

我從JNI調用獲取一個字節數組,並試圖用它構造一個Bitmap對象。

我的問題是,以下代碼返回null。

    byte[] image = services.getImageBuffer(1024, 600);
    Bitmap bmp = BitmapFactory.decodeByteArray(image, 0, image.length);

關於它的任何提示?

PS:像素布局是BGR,而不是RGB。

該文檔說,如果圖像無法解碼,該方法將返回“null”。 你可以試試:

byte[] image = services.getImageBuffer(1024, 600);
InputStream is = new ByteArrayInputStream(image);
Bitmap bmp = BitmapFactory.decodeStream(is);

即使我不認為它會改變任何東西..嘗試看看android.graphics.BitmapFactory.Options以及

decodeByteArray確實不適用於這種格式。 我手動從BGR更改為RGB。

    byte[] image = services.getImageBuffer(1024, 600);

    Bitmap bmp = Bitmap.createBitmap(1024, 600, Bitmap.Config.RGB_565);
    int row = 0, col = 0;
    for (int i = 0; i < image.length; i += 3) {
        bmp.setPixel(col++, row, image[i + 2] & image[i + 1] & image[i]);

        if (col == 1024) {
            col = 0;
            row++;
        }

然而,

for (i < image.length) 。。。bmp.setPixel(image[i + 2] & image[i + 1] & image[i]); 

可以引起:

08-29 14:34:23.460:ERROR / AndroidRuntime(8638):java.lang.ArrayIndexOutOfBoundsException

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM