Alternatives libraries for loading PNG images

耗尽温柔 提交于 2019-11-30 18:55:25

问题


My java J2SE application is reading a lot of (png) images from the web and some of them use features such as a transparency color for true-color images (tRNS section) that Sun's/Oracle's PNGImageReader implementation simply ignores.

Therefore the common solution for loading via ImageIO.read(...); does not work for me as it relies on this incomplete PNGImageReader implementation.

Does anybody know a png reader implementation that can read all forms of PNG images correctly - those with color table or true-color and alpha transparency or transparent color?

As it is for a GPL project it should be a non-commercial one that can be included without licensing problems into the app.

Edit: My be this question was too specific. Therefore let be redesign my question:

Who knows alternative implementations and libraries that are able to load PNG files?

I will then test the implementations for their capabilities to load some test png images.

Edit2: The end result have to be a BufferedImage


回答1:


Finally I found a suitable PNG reader which fits my needs perfectly:

Sixlegs Java PNG Decoder

Main features:

  • Open Source (LGPL)
  • Loads PNG correctly including alpha transparency and transparent colors
  • Returns a BufferedImage
  • Has no further dependencies to other libraries
  • Has a very small size (46KB for the whole library).



回答2:


Have you tried the Apache Commons Imaging library? The PNG support is specified as:

Supported through version 1.2/ISO/IEC standard (15948:2003). Controlling the exact format when writing is incomplete.

Being a pure Java library, it should work well on J2SE.




回答3:


Use the following to acquire the image:

Toolkit.getDefaultToolkit().getImage(theFilenameOfTheImage)

Edit: If you need a BufferedImage, you can use the following:

ImageIcon iic=new ImageIcon(theFilenameOfTheImage);
BufferedImage bimg=((ToolkitImage)iic.getImage()).getBufferedImage();

It basically loads the image the same way, but the ImageIcon class is using a MediaTracker to make sure the image is fully loaded. This way, you can access the resulting BufferedImage, and it will always contain the pixmap.



来源:https://stackoverflow.com/questions/4443288/alternatives-libraries-for-loading-png-images

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