简体   繁体   中英

Working with an uncompressed bitmap byte array

Currently I am trying sending an uncompressed bitmap byte array to a third party library, when I get this byte array back I would like to convert it back to a Bitmap.

Currently a working solution is to loop over the byte array and draw pixel by pixel into a bitmap but obviously the performance for this is terrible.

The question is: how can I convert an uncompressed byte array to bitmap in the quickest way possible?

Have you tried to use BitmapFactory.decodeByteArray()?

http://developer.android.com/reference/android/graphics/BitmapFactory.html

I used this method ; it worked!

public static Drawable byteToDrawable(byte[] data) {

    if (data == null)
        return null;
    else
        return new BitmapDrawable(BitmapFactory.decodeByteArray(data, 0,data.length));
}

it returns a drawable...

I think it will help you.

Use this:

public static BufferedImage toBufferedImage(byte[] pixels, int width, int height) throws IllegalArgumentException {
    BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_3BYTE_BGR);
    byte[] imgData = ((DataBufferByte)image.getRaster().getDataBuffer()).getData();
    System.arraycopy(pixels, 0, imgData, 0, pixels.length);
    return image;
}

Depending on your byte order and alpha channel you will need another image type, eg BufferedImage.TYPE_4BYTE_ABGR

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM