字节数组到Image抛出IllegalArgumentException

时间:2015-01-02 09:28:49

标签: java bufferedimage javax.imageio

我将图像作为byte []加载,我需要将其保存为jpeg图像。我试图保存它,但我给了一个IllegalArgumentException。

 private String saveProcessedImage(byte[] imageInBytes, String surface) throws IOException {

    String savePath = imageSavePath+"\\"+surface+"-"+ UUID.randomUUID()+".jpg";

    InputStream in = new ByteArrayInputStream(imageInBytes);
    BufferedImage img = ImageIO.read(in);
    ImageIO.write(img,"jpg",new File(savePath));
    img.flush();
    in.close();
    return savePath;
}

堆栈追踪:

Exception in thread "main" java.lang.IllegalArgumentException: image == null!
    at javax.imageio.ImageTypeSpecifier.createFromRenderedImage(ImageTypeSpecifier.java:925)
    at javax.imageio.ImageIO.getWriter(ImageIO.java:1591)
    at javax.imageio.ImageIO.write(ImageIO.java:1520)
    at com.abc.def.agent.imaging.ImageProcessJobBroker.saveProcessedImage(ImageProcessJobBroker.java:80)
    at com.abc.def.agent.imaging.ImageProcessJobBroker.processFrontAndBack(ImageProcessJobBroker.java:48)
    at com.abc.def.agent.imaging.TestBroker.main(TestBroker.java:9)

我在这里注意到BuffredImage img在被称为ImageIO.write()时为null,即使byte []和InputStream in存在。

调用上面的saveProcessedImage()方法:

frontImagePath = saveProcessedImage(jniImageProcAgent.getProcessedBinaryImage(frontImageFileName), "front");

以下是该方法的JNI签名:

public native int GetProcessedBinaryImage(ZvImage srcImage, ZvImage dest, String[] errorMsg, boolean isFront);

执行jin调用的方法:

public byte[] getProcessedBinaryImage(String filePath) throws IOException {
        File imagePath = new File(filePath);
        BufferedImage bufImg = ImageIO.read(imagePath);
        WritableRaster raster = bufImg.getRaster();
        DataBufferByte imgdata = (DataBufferByte) raster.getDataBuffer();
        ComponentSampleModel csm = (ComponentSampleModel) raster.getSampleModel();

        ZvImage zvSrcImg = new ZvImage();
        ZvImage zvDestImg = new ZvImage();
        zvSrcImg.width = bufImg.getWidth();
        zvSrcImg.height = bufImg.getHeight();
        zvSrcImg.bpp = 24;
        zvSrcImg.stride = csm.getScanlineStride();
        zvSrcImg.data = imgdata.getData();
        String[] msg = new String[1];

        int status = GetProcessedBinaryImage(zvSrcImg, zvDestImg, msg, true);


        if ((zvDestImg.nativeData != null) && (0 == status)) return zvDestImg.nativeData;
        return null;
    }

ZvImage dto用于传输数据:

public class ZvImage {
        public int width;
        public int height;
        public int bpp;
        public int stride;
        public byte[] data;
        public byte[] nativeData;

        public void setNativeData(byte[] nativeData) {
            this.nativeData = nativeData;
        }

        public byte[] getData() {
            return data;
        }
    }

我在这里做错了什么?感谢您的支持。

1 个答案:

答案 0 :(得分:0)

byte []没有问题,问题在于我创建图像的方式,我更改了参数并从调用JNI方法的方法返回它。

//region -- Code stub to create the buffered image --
        BufferedImage result = new BufferedImage(zvDestImg.width, zvDestImg.height, BufferedImage.TYPE_BYTE_GRAY);
        DataBuffer buffer = new DataBufferByte(zvDestImg.nativeData, zvDestImg.nativeData.length);
        SampleModel sampleModel = new ComponentSampleModel(DataBuffer.TYPE_BYTE, zvDestImg.width, zvDestImg.height, 1, zvDestImg.stride, new int[]{0});
        Raster raster2 = Raster.createRaster(sampleModel, buffer, null);
        result.setData(raster2);
        //endregion