![](/img/trans.png)
[英]Java: Is there a way to get the expected uncompressed length of a zipped byte array?
[英]Working with an uncompressed bitmap byte array
目前,我正在嘗試將未壓縮的位圖字節數組發送到第三方庫,當我取回該字節數組時,我想將其轉換回位圖。
當前一種可行的解決方案是遍歷字節數組,並將像素逐像素繪制到位圖中,但是顯然這樣做的性能很糟糕。
問題是:如何以最快的方式將未壓縮的字節數組轉換為位圖?
您是否嘗試過使用BitmapFactory.decodeByteArray()?
http://developer.android.com/reference/android/graphics/BitmapFactory.html
我用這種方法; 有效!
public static Drawable byteToDrawable(byte[] data) {
if (data == null)
return null;
else
return new BitmapDrawable(BitmapFactory.decodeByteArray(data, 0,data.length));
}
它返回一個可繪制的...
我認為它將為您提供幫助。
用這個:
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;
}
根據字節順序和alpha通道,您將需要其他圖像類型,例如BufferedImage.TYPE_4BYTE_ABGR
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.