繁体   English   中英

如何将2d byte [] []数组(二进制图像数组)转换为图像java

[英]how to convert 2d byte[][] array (binary image array) to image java

您好,我想将2D byte [] []数组转换为图像。 我已经将黑白图像转换为2D byte [] []数组,并想将2d字节数组转换为图像。

将2D数组转换为1D数组

要将byte数组写入BufferedImage ,就我所知,它必须是一维数组。 您可以使用以下简单循环将二维字节数组( byte[][] )转换为一维字节数组( byte[] ):

/*
 * Create a new 1-dimensional byte array which will hold the result
 * Set its size to the item count in the pixelData array
 */
byte[] oneDimArray = new byte[pixelData.length * pixelData[0].length];

/*
 * Loop through the "horizontal" row in the pixelData array
 */
for(int x = 0; x < pixelData.length; x++) {
    /*
     * Loop through each item in the current vertical row
     */
    for(int y = 0; y < pixelData[x].length; y++) {
        /*
         * Set each item in the 1-dimensional array to the corresponding
         * item in the 2-dimensional array
         */
        oneDimArray[x + y * pixelData.length] = twoDimArray[x][y];
    }
}

将字节数组转换为BufferedImage

现在,您可以使用以下简单代码将byte数组写入新的BufferedImage

ByteArrayInputStream byteIn = new ByteArrayInputStream(oneDimArray);
BufferedImage finalImage = ImageIO.read(byteIn);

从这里拍摄

现在,您可以将BufferedImage用于所需的任何对象,希望它看起来像转换之前一样。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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