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

Java 从字节矩阵创建一组图像文件,java,image,file,bufferedimage,imagej,Java,Image,File,Bufferedimage,Imagej,正如我在标题中所写,我想了解如何从包含字节的数组中创建一些图像。这就是迄今为止所写的 BufferedImage arrayImage[] = new BufferedImage [depthV]; int arrayIndex = 0; for (int z = 0; z < depthV; z++) { byte byteToImg[] = new byte [widthV*heightV];

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

        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();
            }
        }
BufferedImage arrayImage[]=新的BufferedImage[depthV];
int-arrayIndex=0;
对于(intz=0;z
Java函数以Java.lang.IllegalArgumentException结束:image==null!
我的错误是什么?我怎样才能避免这个问题?有更好的方法吗?

您不能执行
ImageIO。读取
以从字节数组创建图像。
有效的方法是:

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));
    }
}
与捆绑的库可以轻松做到这一点:

import io.scif.gui.AWTImageTools;
...
byte[] bytes = new byte[width * height];
...
boolean signed = false;
BufferedImage bi = AWTImageTools.makeImage(bytes, width, height, signed);
该方法的源代码是(which调用,which调用)

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

SCIFIO可从以下网址获得:。相关的
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>

图像J.公共
http://maven.imagej.net/content/groups/public
...
io.scif
西菲奥
0.22.0

请发布完整的例外情况。这里特别重要的是抛出异常的行。我不能发布所有异常,它太长了。有一行感兴趣的代码:ImageIO.write(arrayImage[i],“jpg”,outputfile);正如您所说,我已经编辑了代码,这是一个新的代码片段:ByteRasterImage finalImage=newbyterasterimage(byteToImg,widthV,heightV);arrayImage[arrayIndex]=最终图像;arrayIndex++;}对于(inti=0;ibyteToImg[x+y]=data3D[0][z][y][x]中的索引错误:必须将
y
与图像的宽度相乘。