从字节矩阵创建一组图像文件

Create a set of image files from a byte matrix

正如我在标题中所写,我想了解如何从包含字节的数组创建一些图像。这是目前为止写的

        BufferedImage arrayImage[] = new BufferedImage [depthV];
        int arrayIndex = 0;
        for (int z = 0; z < depthV; z++)
        {
            byte byteToImg[] = new byte [widthV*heightV];
            for (int x = 0; x < widthV; x++) 
            {
                for (int y = 0; y < heightV; y++) 
                {
                     byteToImg[x + y] = data3D[0][z][y][x];
                }
            }
            ByteArrayInputStream byteIn = new ByteArrayInputStream(byteToImg);
            BufferedImage finalImage= null;
            try {
                finalImage = ImageIO.read(byteIn);
            } catch (IOException e) {

                e.printStackTrace();
            }
            arrayImage[arrayIndex]=finalImage;
            arrayIndex++;
        }
        for (int i = 0; i < arrayImage.length; i++) 
        {
            File outputfile = new File("./Resources/tmp/image"+i+".jpg");
            try {
                ImageIO.write(arrayImage[i], "jpg", outputfile);
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

Java 函数以 java.lang.IllegalArgumentException 结束:image == null! 我的错误是什么?我怎样才能避免这个问题?有更好的方法吗?

您不能ImageIO.read 从字节数组创建图像。 有效的是:

public class ByteRasterImage extends BufferedImage {

    private static final ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_GRAY);
    private static int[] nBits = {8};
    private static final ColorModel colorModel = new ComponentColorModel(cs, nBits, false, false,
                                         Transparency.OPAQUE,
                                         DataBuffer.TYPE_BYTE);

    private static WritableRaster createWritableRaster(byte[] bytes, final int w, final int h) {
        final DataBufferByte db = new DataBufferByte(new byte[][] {bytes}, bytes.length);
        final ComponentSampleModel sm = new ComponentSampleModel(DataBuffer.TYPE_BYTE, w, h, 1, w, new int[] {0});
        return Raster.createWritableRaster(sm, db, new Point(0, 0));
    }

    private ByteRasterImage (WritableRaster raster) {
        super (colorModel, raster, false, null);
    }

    /**
     * Create a ByteRasterImage from the given data
     * @param bytes the data content to wrap into an image, size w * h
     * @param w the width of the image
     * @param h the height of the image
     */
    public ByteRasterImage (byte[] bytes, int w, int h) {
        this(createWritableRaster(bytes, w, h));
    }
}

SCIFIO library, bundled with ImageJ,可以轻松做到:

import io.scif.gui.AWTImageTools;
...
byte[] bytes = new byte[width * height];
...
boolean signed = false;
BufferedImage bi = AWTImageTools.makeImage(bytes, width, height, signed);

该方法的源代码是 here (which calls here, which calls here)。

但实际上,如果您使用 SCIFIO and/or ImageJ,则根本不需要使用 BufferedImage。有关如何写出图像平面的示例,请参阅 this tutorial

SCIFIO 可从 ImageJ Maven repository 获得。相关的 pom.xml 片段是:

<repositories>
    <repository>
        <id>imagej.public</id>
        <url>http://maven.imagej.net/content/groups/public</url>
    </repository>
</repositories>
...
<dependency>
    <groupId>io.scif</groupId>
    <artifactId>scifio</artifactId>
    <version>0.22.0</version>
</dependency>