【问题标题】:Working with an uncompressed bitmap byte array使用未压缩的位图字节数组
【发布时间】:2014-05-22 09:31:29
【问题描述】:

目前我正在尝试将未压缩的位图字节数组发送到第三方库,当我得到这个字节数组时,我想将其转换回位图。

目前一种可行的解决方案是循环遍历字节数组并将逐个像素绘制到位图中,但显然这样做的性能很糟糕。

问题是:如何以最快的方式将未压缩的字节数组转换为位图?

【问题讨论】:

  • 尝试带 int[] 颜色参数的 Bitmap.create()

标签: java android arrays bitmap xamarin


【解决方案1】:

使用这个:

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

【讨论】:

  • 不需要其他软件包。请考虑投票。
【解决方案2】:

您是否尝试过使用 BitmapFactory.decodeByteArray()?

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

【讨论】:

    【解决方案3】:

    我用过这个方法;成功了!

    public static Drawable byteToDrawable(byte[] data) {
    
        if (data == null)
            return null;
        else
            return new BitmapDrawable(BitmapFactory.decodeByteArray(data, 0,data.length));
    }
    

    它返回一个drawable...

    我想它会对你有所帮助。

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-12-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-28
    • 2011-07-11
    • 1970-01-01
    相关资源
    最近更新 更多