什么原因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