Sun 公司开发的 Jimi 是一个小型的图像处理开源包,对于不是很复杂的图像处理还是很在手的。
它的官方网址是 http://java.sun.com/products/jimi/index.html
以下是摘自其官方网站的说明:
Jimi is a class library for managing images. Its primary function is image I/O. Jimi was formerly a product of Activated Intelligence. Sun is making it available for developers who have code with dependencies on Jimi or for those who need image I/O functionality in applications running under 1.1.x versions of the Java Platform. Jimi's range of supported formats includes GIF, JPEG, TIFF, PNG, PICT, Photoshop, BMP, Targa, ICO, CUR, Sunraster, XBM, XPM, and PCX, although some of these formats do not have complete support for all features.
最近翻看以前的程序,整理了一下,发上来备忘。
这个程序实现的是将图像转换成 jpg 和 png 格式,所需的包请在上述网址下载。源代码如下:
package
com.xxx.utility; /** */
/** ***************************************************************************** * <p>Title: xxx</p> * <p>Description: xxx</p> * <p>Copyright: Copyright (c) 2006 DavidHsing All Rights Reserved</p> * <p>Company: xxx</p> * <p>WebSite: http://blog.csdn.net/DavidHsing </p> * * @author : DavidHsing <DavidHsing(At)163.com> * @version : 1.00 * @date : 2006-11-30 * @direction: 图像格式转换类(转换时不需要关心源图的格式) * @support : GIF(no compressed encoding), JPEG, TIFF, PNG, PICT, BMP, Targa, ICO, CUR, XBM, XPM, PCX, DCX ***************************************************************************** */
import com.sun.jimi.core. *
; import com.sun.jimi.core.options. *
; import java.awt.image. *
; import java.io. *
; // ******************************************************************************
public class
JimiImage ...
{ // public JimiImage() {} // ========================================================================== /** */ /** * 转换图像格式为 JPG * @param String : sSourceImage, 其它格式的源图像文件路径 * @param String : sDestImage, 目标 JPG 图像文件存放路径 * @param int : nQuality, 品质, 0-100, 数值越高品质越好 * @return boolean */ public boolean convertToJPG(String sSourceImage, String sDestImage, int nQuality) ... { if (sSourceImage == null || sSourceImage.trim().equals( "" )) ... { System.out.println( " @> JimiImage.convertToJPG() : 要转换的源图像文件路径不能为空! " ); return false ; } if (sDestImage == null || sDestImage.trim().equals( "" )) ... { sDestImage = sSourceImage.substring( 0 , sSourceImage.lastIndexOf( " . " )) + " .jpg " ; } else if ( ! sDestImage.endsWith( " .jpg " )) ... { sDestImage += " .jpg " ; } // ---------------------------------------------------------------------- // 检查源图像文件 File tSourceImageFile = new File(sSourceImage); if ( ! tSourceImageFile.exists()) ... { System.out.println( " @> JimiImage.convertToJPG() : 要转换的源图像文件路径不存在! " ); return false ; } else if ( ! tSourceImageFile.canRead()) ... { System.out.println( " @> JimiImage.convertToJPG() : 要转换的源图像文件路径不可读! " ); return false ; } else if ( ! tSourceImageFile.isFile()) ... { System.out.println( " @> JimiImage.convertToJPG() : 要转换的源图像路径不是一个有效的文件名! " ); return false ; } tSourceImageFile = null ; // ---------------------------------------------------------------------- try ... { // long lRunStartTime = System.currentTimeMillis(); JPGOptions tJPGOptions = new JPGOptions(); if (nQuality < 0 || nQuality > 100 ) ... { tJPGOptions.setQuality( 75 ); } else ... { tJPGOptions.setQuality(nQuality); } ImageProducer tImageProducer = Jimi.getImageProducer(sSourceImage); JimiWriter tJimiWriter = Jimi.createJimiWriter(sDestImage); tJimiWriter.setSource(tImageProducer); tJimiWriter.setOptions(tJPGOptions); tJimiWriter.putImage(sDestImage); tImageProducer = null ; tJimiWriter = null ; tJPGOptions = null ; // long lRunEndTime = System.currentTimeMillis(); // long lRunTime = lRunEndTime - lRunStartTime; // System.out.println(" @> JimiImage.convertToJPG() 运行时间 : " + lRunTime + " 毫秒"); } catch (JimiException je) ... { System.out.println( " @> JimiImage.convertToJPG() : 转换图像格式发生异常! " ); je.printStackTrace(); return false ; } catch (Exception ex) ... { ex.printStackTrace(); return false ; } return true ; } // ========================================================================== /** */ /** * 转换图像格式为 PNG * @param String : sSourceImage, 其它格式的源图像文件路径 * @param String : sDestImage, 目标 PNG 图像文件存放路径 * @param String : sCompression, 压缩比, none, default, fast, max * @return boolean */ public boolean convertToPNG(String sSourceImage, String sDestImage, String sCompression) ... { if (sSourceImage == null || sSourceImage.trim().equals( "" )) ... { System.out.println( " @> JimiImage.convertToPNG() : 要转换的源图像文件路径不能为空! " ); return false ; } if (sDestImage == null || sDestImage.trim().equals( "" )) ... { sDestImage = sSourceImage.substring( 0 , sSourceImage.lastIndexOf( " . " )) + " .png " ; } else if ( ! sDestImage.endsWith( " .png " )) ... { sDestImage += " .png " ; } // ---------------------------------------------------------------------- // 检查源图像文件 File tSourceImageFile = new File(sSourceImage); if ( ! tSourceImageFile.exists()) ... { System.out.println( " @> JimiImage.convertToPNG() : 要转换的源图像文件路径不存在! " ); return false ; } else if ( ! tSourceImageFile.canRead()) ... { System.out.println( " @> JimiImage.convertToPNG() : 要转换的源图像文件路径不可读! " ); return false ; } else if ( ! tSourceImageFile.isFile()) ... { System.out.println( " @> JimiImage.convertToPNG() : 要转换的源图像路径不是一个有效的文件名! " ); return false ; } tSourceImageFile = null ; // ---------------------------------------------------------------------- try ... { // long lRunStartTime = System.currentTimeMillis(); PNGOptions tPNGOptions = new PNGOptions(); if (sCompression == null || sCompression.trim().equals( "" )) ... { tPNGOptions.setCompressionType(PNGOptions.COMPRESSION_DEFAULT); } else if (sCompression.equalsIgnoreCase( " none " )) ... { tPNGOptions.setCompressionType(PNGOptions.COMPRESSION_NONE); } else if (sCompression.equalsIgnoreCase( " fast " )) ... { tPNGOptions.setCompressionType(PNGOptions.COMPRESSION_FAST); } else if (sCompression.equalsIgnoreCase( " max " )) ... { tPNGOptions.setCompressionType(PNGOptions.COMPRESSION_MAX); } else ... { tPNGOptions.setCompressionType(PNGOptions.COMPRESSION_DEFAULT); } ImageProducer tImageProducer = Jimi.getImageProducer(sSourceImage); JimiWriter tJimiWriter = Jimi.createJimiWriter(sDestImage); tJimiWriter.setSource(tImageProducer); tJimiWriter.setOptions(tPNGOptions); tJimiWriter.putImage(sDestImage); tImageProducer = null ; tJimiWriter = null ; tPNGOptions = null ; // long lRunEndTime = System.currentTimeMillis(); // long lRunTime = lRunEndTime - lRunStartTime; // System.out.println(" @> JimiImage.convertToPNG() 运行时间 : " + lRunTime + " 毫秒"); } catch (JimiException je) ... { System.out.println( " @> JimiImage.convertToPNG() : 转换图像格式发生异常! " ); je.printStackTrace(); return false ; } catch (Exception ex) ... { ex.printStackTrace(); return false ; } return true ; } // ========================================================================== }