在Java中使用WritableRaster和DataBufferByte将图像转换为字节[]时出错。
我正在尝试使用代码将图像转换为字节[]在Java中使用WritableRaster和DataBufferByte将图像转换为字节[]时出错。,java,image,bufferedimage,javax.imageio,Java,Image,Bufferedimage,Javax.imageio,我正在尝试使用代码将图像转换为字节[] public static byte[] extractBytes(String ImageName) throws IOException { // open image File imgPath = new File(ImageName); BufferedImage bufferedImage = ImageIO.read(imgPath); // get DataBufferBytes from Raster
public static byte[] extractBytes(String ImageName) throws IOException {
// open image
File imgPath = new File(ImageName);
BufferedImage bufferedImage = ImageIO.read(imgPath);
// get DataBufferBytes from Raster
WritableRaster raster = bufferedImage.getRaster();
DataBufferByte data = (DataBufferByte) raster.getDataBuffer();
return (data.getData());
}
当我使用代码测试它时
public static void main(String[] args) throws IOException {
String filepath = "image_old.jpg";
byte[] data = extractBytes(filepath);
System.out.println(data.length);
BufferedImage img = ImageIO.read(new ByteArrayInputStream(data));
File outputfile = new File("image_new.jpg");
ImageIO.write(img, "jpeg", outputfile);
}
我正在获取data.length=4665600并获取错误信息
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.medianet.hello.HbaseUtil.main(HbaseUtil.java:138)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:134)
但当我将extractBytes代码更改为
public static byte[] extractBytes (String ImageName) throws IOException {
ByteArrayOutputStream baos=new ByteArrayOutputStream();
BufferedImage img=ImageIO.read(new File(ImageName));
ImageIO.write(img, "jpg", baos);
baos.flush();
return baos.toByteArray();
}
我正在获取data.length=120905并获得成功(image.jpg在所需位置创建)问题是,第一个版本的
extractBytes
读取图像,并将图像的像素作为字节数组返回(假设它使用DataBufferByte
)。这些字节不是文件格式,如果没有额外的信息,例如宽度、高度、颜色空间等,这些字节是无用的。ImageIO
无法读回这些字节,因此,null
被返回(并分配给img
,随后导致ImageIO.write(…)
中的IllegalArgumentException
).
第二个版本对图像进行解码,然后再次以JPEG格式进行编码。这是一种ImageIO
能够读取的格式,您将获得一张您期望的图像(分配给img
)
然而,您的代码似乎只是复制图像的一种非常非常耗费CPU的方式(您解码图像,然后编码,然后再次解码,最后编码之前)。。。对于JPEG文件,此解码/编码周期也会降低图像质量。除非您计划将图像数据用于任何用途,并且只想将图像从一个位置复制到另一个位置,否则不要使用ImageIO
和BufferedImage
s。这些类型用于图像处理
以下是主方法的修改版本:
public static void main(String[] args) throws IOException {
byte[] buffer = new byte[1024];
File inFile = new File("image_old.jpg");
File outFile = new File("image_new.jpg");
InputStream in = new FileInputStream(inFile);
try {
OutputStream out = new FileOutputStream(outFile);
try {
int len;
while ((len = in.read(buffer)) > 0) {
out.write(buffer, 0, len);
}
}
finally {
out.close();
}
}
finally {
in.close();
}
}
(使用Java 7中的try-with-resources或NIO2文件可以编写更好/更优雅的代码。在Java 8中复制
,但我将此留给您。:-)