coobird / thumbnailator

Thumbnailator - a thumbnail generation library for Java
MIT License
5.08k stars 780 forks source link

Does the thumbnailator support generating thumbnails for png images? #214

Closed lhfwudi closed 2 months ago

lhfwudi commented 7 months ago

Why do png thumbnails report exceptions?

  1. My code is as follows public static void main(String[] args) throws Exception { String srcFile = "C:\Users\admin\Desktop\2222.png"; String desFile = "C:\data1\share\biztable\mini\task_fill\333.png"; BufferedImage originalImage = ImageIO.read(new File(srcFile)); BufferedImage thumbnail = Thumbnails.of(originalImage) .size(200, 200) .keepAspectRatio(false) .asBufferedImage(); Thumbnails.of(thumbnail) .scale(0.3) .outputQuality(0.25f) .toFile(desFile); }
  2. The abnormal information is as follows Exception in thread "main" javax.imageio.IIOException: Unsupported Image Type at com.sun.imageio.plugins.jpeg.JPEGImageReader.readInternal(JPEGImageReader.java:1079) at com.sun.imageio.plugins.jpeg.JPEGImageReader.read(JPEGImageReader.java:1050) at javax.imageio.ImageIO.read(ImageIO.java:1448) at javax.imageio.ImageIO.read(ImageIO.java:1308) at zsoft.gov.datacenter.biztable.common.utils.UploadUtil.main(UploadUtil.java:266)

May I ask what causes it? Or is the thumbnailator not supported?

coobird commented 7 months ago

@lhfwudi, the stacktrace shows that Thumbnailator is not being called. The exception is happening on this line:

BufferedImage originalImage = ImageIO.read(new File(srcFile));

And inspecting closer, Java thinks it's a JPEG file even though it has a PNG extension. Therefore, it seems like your image file is mislabeled with a wrong extension. This is not an issue with Thumbnailator as it's not being invoked.