[英]Using ImageIO.write() to create a JPEG creates a 0 byte file
I am trying to write a method that takes an image, and saves a 100 by 100 thumbnail of that image.我正在尝试编写一种获取图像并保存该图像的 100 x 100 缩略图的方法。 However, when I save the file, it comes out as an unreadable 0 byte image (with the error "Error interpreting JPEG image file (Improper call to JPEG library in state 200)") in Ubuntu's ImageViewer.但是,当我保存文件时,它在 Ubuntu 的 ImageViewer 中显示为不可读的 0 字节图像(错误为“错误解释 JPEG 图像文件(错误调用状态 200 中的 JPEG 库)”)。 My code is as follows:我的代码如下:
public boolean scale(){
String file = filename.substring(filename.lastIndexOf(File.separator)+1);
File out = new File("data"+File.separator+"thumbnails"+File.separator+file);
if( out.exists() ) return false;
BufferedImage bi;
try{
bi = ImageIO.read(new File(filename));
}
catch(IOException e){
return false;
}
Dimension imgSize = new Dimension(bi.getWidth(), bi.getHeight());
Dimension bounds = new Dimension(100, 100);
int newHeight = imgSize.height;
int newWidth = imgSize.width;
if( imgSize.width > bounds.width ){
newWidth = bounds.width;
newHeight = (newWidth*imgSize.height)/imgSize.width;
}
if( imgSize.height > bounds.width ){
newHeight = bounds.height;
newWidth = (newHeight*imgSize.width)/imgSize.height;
}
Image img = bi.getScaledInstance(newWidth, newHeight, BufferedImage.SCALE_SMOOTH);
BufferedImage thumb = new BufferedImage(newWidth, newHeight, BufferedImage.TYPE_4BYTE_ABGR);
Graphics2D g2d = thumb.createGraphics();
g2d.drawImage(img, 0, 0, null);
g2d.dispose();
try{
ImageIO.write(thumb, "jpg", out);
}
catch(IOException e){
return false;
}
return true;
}
Where "filename" is a global variable for the class housing this method, representing the path to the original image.其中“filename”是包含此方法的类的全局变量,表示原始图像的路径。 My main issue is that I do not see why I'm creating a 0 byte image.我的主要问题是我不明白为什么要创建 0 字节图像。
So, the issue was this.所以,问题是这个。 I'm working in OpenJDK.我在 OpenJDK 中工作。 OpenJDK doesn't have a JPEG encoder, apparently, so while the file was being created by显然,OpenJDK 没有 JPEG 编码器,因此在创建文件时
ImageIO.write(thumb, "jpg", out);
it wasn't actually creating anything for the file to save;它实际上并没有为要保存的文件创建任何内容; hence the empty 0 byte unreadable file.因此空的 0 字节不可读文件。 Changing the ImageIO argument to "png" (and adjusting the new File() extension, appropriately) successfully created the desired image with the above code.将 ImageIO 参数更改为“png”(并适当调整新的 File() 扩展名)使用上述代码成功创建了所需的图像。
I experienced the same issue even though the JVM had a JPG encoder and it was caused by using an unsupported type of BufferImage underneath.即使 JVM 有一个 JPG 编码器,我也遇到了同样的问题,这是由于在下面使用了不受支持的 BufferImage 类型造成的。 I had used BufferedImage.TYPE_USHORT_GRAY
which is evidently not supported and gave no error messages but produced a 0 byte file.我使用过BufferedImage.TYPE_USHORT_GRAY
,它显然不受支持并且没有给出错误消息,但产生了一个 0 字节的文件。 When I switched to BufferedImage.TYPE_BYTE_GRAY
it worked perfectly.当我切换到BufferedImage.TYPE_BYTE_GRAY
它工作得很好。
JPG have no alpha channel, ImageIO.write() will fail silently (just return false) one way is to copy your image into another one JPG 没有 alpha 通道,ImageIO.write() 将无声地失败(只返回 false)一种方法是将您的图像复制到另一个
BufferedImage newBufferedImage = new BufferedImage(bufferedImage.getWidth(),
bufferedImage.getHeight(), BufferedImage.TYPE_INT_RGB);
newBufferedImage.getGraphics().drawImage(bufferedImage, 0, 0, null);
boolean write = ImageIO.write(newBufferedImage, extension, outputfile);
if (!write) {
// do something
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.