0

什么原因javax.imageio.IIOException: Sample size must be <= 8。我尝试像这样写 IIOImage

public static byte[] compressImage(BufferedImage imageSource, float quality) throws IOException {
    Iterator i = ImageIO.getImageWritersByFormatName("jpeg");

    ImageWriter writer = (ImageWriter) i.next();
    ImageWriteParam iwp = writer.getDefaultWriteParam();
    iwp.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
    iwp.setCompressionQuality(quality);

    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    ImageOutputStream output = ImageIO.createImageOutputStream(bos);
    writer.setOutput(output);
    IIOImage image = new IIOImage(imageSource, null, null); 
    writer.write(null, image, iwp);
    writer.dispose();

    return bos.toByteArray();
}

但是当 ImageWriter 尝试写入 IIOImage 时,出现了异常。我不知道出了什么问题,但是如果图像尺寸很大,则显示并运行完美也不例外,但是如果图像尺寸很小,则javax.imageio.IIOException: Sample size must be <= 8显示。对不起,我的英语不好,感谢您对我的问题的任何答复。哦,这是完整的痕迹

Caused by: javax.imageio.IIOException: Sample size must be <= 8
at com.sun.imageio.plugins.jpeg.JPEGImageWriter.writeOnThread(JPEGImageWriter.java:494) ~[na:1.7.0_21]
at com.sun.imageio.plugins.jpeg.JPEGImageWriter.write(JPEGImageWriter.java:360) ~[na:1.7.0_21]
at com.daksa.cms.util.ImageCompressor.compressImage(ImageCompressor.java:37) ~[CmsServer-ejb_jar/:na]
at com.daksa.cms.util.ImageCompressor.compressImageToSize(ImageCompressor.java:47) ~[CmsServer-ejb_jar/:na]
at com.daksa.cms.bean.PersoEventBean.resizeImage(PersoEventBean.java:292) ~[CmsServer-ejb_jar/:na]
... 85 common frames omitted
4

2 回答 2

1

可能是你应该使用

com.sun.media.imageioimpl.plugins.jpeg.CLibJPEGImageWriter

用于渲染图像的类。请参阅链接。您还可以查看以下内容:

  1. http://www.codingtiger.com/questions/java/ImageIO-not-able-to-write-a-JPEG-file.html
  2. ImageIO 无法写入 JPEG 文件
于 2013-10-01T05:24:06.270 回答
0

知道我知道问题,对不起图片格式错误,应该是.jpg

于 2013-10-02T08:41:48.457 回答