图像的字节数组抛出 IllegalArgumentException
byte array to Image throw IllegalArgumentException
我有一个图像作为 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;
}
}
我在这里做错了什么?感谢您的支持。
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
我有一个图像作为 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;
}
}
我在这里做错了什么?感谢您的支持。
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