Java read different type of image formats jpg,tif,gif,png

旧巷老猫 提交于 2020-01-12 10:46:39

问题


I am trying to read some image files jpg, tif, gif, png and need to save files and create icons. And i am getting UnsupportedTypeException.

ImageIO.read(file);

If i use following line, as earlier discuss in form.

BufferedImage img = JPEGCodec.createJPEGDecoder(inputStream).decodeAsBufferedImage();

I get JPEGCodec cannot found symbol.

I am using netbean 7.0.1. I have also added jai-imageio.jar.


回答1:


By default, ImageIO can only read JPG, GIF and PNG file formats, if I remember right. To add new formats like TIFF, you need to add a plugin, which is a jar file, to your classpath, and to add an ImageIO.scanForPlugins() to you code before you try to read a file.

Example of plugin:

http://ij-plugins.sourceforge.net/plugins/imageio/

Try "ImageIO plugins" in Google.




回答2:


JAI-ImageIO does include plugins for file formats like TIFF, so in principal what you are trying to do should work. However, to install JAI-ImageIO it's not enough to add it to your classpath. See the complete installation instructions here: http://java.sun.com/products/java-media/jai/INSTALL-jai_imageio_1_0_01.html




回答3:


Fr detail we can see the like http://www.randelshofer.ch/blog/2011/08/reading-cmyk-jpeg-images-with-java-imageio/

Image img = null;
ImageInputStream iis = new FileImageInputStream(file);
try {
    for (Iterator<ImageReader> i = ImageIO.getImageReaders(iis);
         img == null && i.hasNext(); ) {
        ImageReader r = i.next();
        try {
            r.setInput(iis);
            img = r.read(0);
        } catch (IOException e) {}
    }
} finally {
    iis.close();
}
return img;

Java advance image io also solve the problem, but its hard to maintain to install on all plateform.



来源:https://stackoverflow.com/questions/9111177/java-read-different-type-of-image-formats-jpg-tif-gif-png

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!