簡體   English   中英

字節數組到 bitmap 返回 null

[英]byte array to bitmap returns null

我有一個小程序,它采用 Chair.jpg 並將其轉換為 bitmap。 這樣做的原因是將像素的顏色類型更改為 BGR_888 的格式(我從這個堆棧溢出帖子中得到。)

然而 bitmap 是 null。 我相信是因為這個原因D/skia: --- Failed to create image decoder with message 'unimplemented' 在網上看,可能是因為我需要壓縮這個? 我不確定。 有人可以幫我理解嗎?

public class MainActivity extends AppCompatActivity {

    TextView textView;
    ImageView imageView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        imageView = findViewById(R.id.imageView);
        textView = findViewById(R.id.textView);

        Bitmap b = BitmapFactory.decodeResource(getResources(), R.drawable.chair);
        imageView.setImageBitmap(b);

        int width = b.getWidth();
        int height = b.getHeight();

        Log.d("bitmap height", String.valueOf(height));
        Log.d("bitmap width", String.valueOf(width));
        Bitmap.Config config = b.getConfig();
        Log.d("b color format", String.valueOf(config));

        byte[] newImage = getImagePixels(b);
        Log.d("getImagePixels Result", String.valueOf(newImage));

        boolean b2 = isNewBitmapNull(newImage);
        Log.d("is my new bitmap null:",String.valueOf(b2));


    }

    /// <summary>
    /// function getImagePixels
    /// Purpose: Given the bitmap image return the converted 4 byte ARGB to 3 byte BGR
    /// </summary>
    /// <param name="image"> Bitmap image object </param>

    public byte[] getImagePixels(Bitmap image) {
        // calculate how many bytes our image consists of
        int bytes = image.getByteCount();
        // Create a new buffer
        ByteBuffer buffer = ByteBuffer.allocate(bytes);
        // Move the byte data to the buffer
        image.copyPixelsToBuffer(buffer);

        // Get the underlying array containing the data.
        byte[] temp = buffer.array();

        // Allocate for 3 byte BGR
        byte[] pixels = new byte[(temp.length / 4) * 3];

        // Copy pixels into place
        for (int i = 0; i < (temp.length / 4); i++) {
            pixels[i * 3] = temp[i * 4 + 3];     // B
            pixels[i * 3 + 1] = temp[i * 4 + 2]; // G
            pixels[i * 3 + 2] = temp[i * 4 + 1]; // R

            // Alpha is discarded
        }
        Log.d("check if it is array", String.valueOf(pixels.getClass().isArray()));
        Log.d("array object type", String.valueOf(pixels.getClass()));
        Log.d("array length", String.valueOf(pixels.length));
        return pixels;
    }

    public static byte[] convertBitmapToByteArrayUncompressed(Bitmap bitmap){
        ByteBuffer byteBuffer = ByteBuffer.allocate(bitmap.getByteCount());
        bitmap.copyPixelsToBuffer(byteBuffer);
        byteBuffer.rewind();
        return byteBuffer.array();
    }

    /// <summary>
    /// function copyPixelsToBitmap
    /// Purpose: Given the pixel data return a bitmap of size [?,?],PixelFormat=24BGR
    /// </summary>
    /// <param name="pixels"> Byte array with pixel data </param>
    public Bitmap copyPixelsToBitmap(byte[] pixels){
        //Here create the Bitmap to the know height, width and format
        Bitmap bmp = BitmapFactory.decodeByteArray(pixels, 0, pixels.length);

        //Return the bitmap
        return bmp;
    }

    /// <summary>
    /// function isNewBitmapNull
    /// Purpose: Given the pixel data return T/F is the bitmap was created
    /// </summary>
    /// <param name="pixels"> Byte array with pixel data </param>
    public boolean isNewBitmapNull(byte[] pixels){
        //BitmapFactory.Options options = new BitmapFactory.Options();
        //options.inMutable = true;
        Bitmap bmp = BitmapFactory.decodeByteArray(pixels, 0, pixels.length);
        if (bmp == null)
            return true;


        return false;
    }
}

對於 isNewBitMapNull 方法,我也嘗試添加 BitMapFactory 選項,但仍然得到 null bitmap:

//BitmapFactory.Options options = new BitmapFactory.Options();
//options.inMutable = true;
Bitmap bmp = BitmapFactory.decodeByteArray(pixels, 0, pixels.length, options);

這是 output:

D/bitmap height: 1260 
D/bitmap width: 1260
D/b color format: ARGB_8888
D/check if it is array: true
D/array object type: class [B
D/array length: 4762800
D/getImagePixels Result: [B@5a36e09
D/skia: --- Failed to create image decoder with message 'unimplemented'
D/is my new bitmap null:: true

您似乎正在嘗試創建一個每個像素有 3 個字節的 bitmap,但是在 Android 中是不可能的,因為 Bitmap 支持每像素 1、2、4 或 8 個字節

這就是為什么您需要使用受支持的像素格式創建新的 bitmap。 但是使用BitmapFactory.decodeByteArray不起作用,因為它從壓縮的圖像數據(例如存儲在 .jpg 文件中的數據)創建 bitmap。 但是你有一組原始像素值(未壓縮)。

雖然可以選擇壓縮圖像然后使用decodeByteArray讀取它,但您需要自己實現BGR_888的壓縮或使用第 3 方庫。 更簡單的方法是將BGR_888轉換回ARGB_8888並從此數據創建 bitmap:

val temp = IntArray(width * height)

for (i in 0 until width * height) {
    val red = pixels[i * 3 + 2].toInt() and 0xff
    val green = pixels[i * 3 + 1].toInt() and 0xff
    val blue = pixels[i * 3].toInt() and 0xff
    temp[i] = Color.rgb(red, green, blue)
}
return Bitmap.createBitmap(temp, width, height, Bitmap.Config.ARGB_8888)

Java版本:

int[] temp = new int[width * height];

for (int i = 0; i < width * height; ++i) {
    int red = (int)pixels[i * 3 + 2] & 0xff;
    int green = (int)pixels[i * 3 + 1] & 0xff;
    int blue = (int)pixels[i * 3] & 0xff;
    temp[i] = Color.rgb(red, green, blue);
}
return Bitmap.createBitmap(temp, width, height, Bitmap.Config.ARGB_8888);

暫無
暫無

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

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