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

时间:2015-04-15 11:59:55

标签: 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];
            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! 我的错误是什么?我怎么能避免这个问题?有更好的方法吗?

2 个答案:

答案 0 :(得分:1)

SCIFIO捆绑在一起的ImageJ库可以轻松完成此操作:

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

该方法的源代码为here(调用here,调用here)。

但实际上,如果您使用SCIFIO和/或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>

答案 1 :(得分:0)

您无法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));
    }
}
相关问题