www.pudn.com > MoviesWeb.zip > ImageGenerator.java


package com.blue.imageio; 
 
import java.awt.geom.AffineTransform; 
import java.awt.image.BufferedImage; 
import java.io.File; 
import java.io.FileInputStream; 
import java.io.FileNotFoundException; 
import java.io.FileOutputStream; 
import java.io.IOException; 
 
import javax.imageio.ImageIO; 
import javax.imageio.stream.FileImageOutputStream; 
 
import com.blue.imageio.plugins.gif.GIFImageWriter; 
import com.blue.imageio.plugins.gif.GIFImageWriterSpi; 
import com.sun.image.codec.jpeg.JPEGCodec; 
import com.sun.image.codec.jpeg.JPEGEncodeParam; 
import com.sun.image.codec.jpeg.JPEGImageEncoder; 
import com.sun.jimi.core.Jimi; 
import com.sun.jimi.core.JimiException; 
 
public class ImageGenerator { 
 
	/** 
	 * 生成缩略图 
	 * @param sourcePath 
	 * @param destPath 
	 * @param size 
	 * @throws ImageException 
	 */ 
	public static void generateThumb(String sourcePath, String destPath, Size size) throws Exception, ImageException { 
		generateImage(sourcePath, destPath, size); 
	} 
	/** 
	 * 生成新图片 
	 * @param sourcePath 
	 * @param destPath 
	 * @param size 
	 * @throws ImageException 
	 */ 
	public static void generateImage(String sourcePath, String destPath, Size size) throws Exception, ImageException { 
		// read images. Here we read from files but it can be any source 
		// (internet, database etc.) 
		BufferedImage image = null; 
		try { 
			image = ImageIO.read(new FileInputStream(sourcePath)); 
		} catch (FileNotFoundException e) { 
			throw new ImageException("Can not open source image file with filename : " + sourcePath); 
		} catch (IOException e) { 
			throw new ImageException("Error to read source image file."); 
		} 
		BufferedImage resultImage = ImageGenerator.cuteImage(image, size); 
		try { 
			// 其实生成图片不需要对png/jpg/bmp/gif分开处理,但为了能对细节进行调整,所以.... 
			if (destPath.toLowerCase().endsWith(".jpg")) { 
				FileOutputStream fos = new FileOutputStream(destPath); 
				JPEGEncodeParam param = JPEGCodec.getDefaultJPEGEncodeParam(resultImage); 
				param.setQuality(0.99f, true); 
				JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(fos, param); 
				encoder.encode(resultImage); 
				fos.close(); 
			} else if (destPath.toLowerCase().endsWith(".gif")) { 
	            File iosFile = new File(destPath); 
	            FileImageOutputStream fios = new FileImageOutputStream(iosFile); 
				GIFImageWriterSpi gifSpi = new GIFImageWriterSpi(); 
				GIFImageWriter gifWriter = (GIFImageWriter)gifSpi.createWriterInstance(); 
				gifWriter.setOutput(fios); 
				gifWriter.write(resultImage); 
				fios.close(); 
			} else if (destPath.toLowerCase().endsWith(".png") || destPath.toLowerCase().endsWith(".bmp")) { 
	        	// 缺省处理方式,未做细节调整 
	        	Jimi.putImage(resultImage, destPath); 
			} else { 
				throw new ImageException("Unsupported image type for filename : " + destPath); 
			} 
		} catch (IOException e) { 
			throw new ImageException("IOException while try to outout image file." + e.getMessage()); 
		} catch (JimiException e) { 
			throw new ImageException("JimiException. " + e.getMessage()); 
		} 
	} 
	/** 
	 * 图片裁剪 
	 * @param imgOrigin 
	 * @param newSize 
	 * @return 
	 * @throws ImageException 
	 */ 
	public static BufferedImage cuteImage(BufferedImage imgOrigin, Size newSize) throws Exception, ImageException { 
		Size size = new Size(imgOrigin.getWidth(), imgOrigin.getHeight()); 
 
		// Get ratio of height / width for both source and destination images. 
		double s_ratio = (double) size.getWidth() / (double) size.getHeight(); 
		double n_ratio = (double) newSize.getWidth() / (double) newSize.getHeight(); 
 
		// Calc cuted image size. 
		int x, y, width, height; 
		if (s_ratio < n_ratio) { 
			width = size.getWidth(); 
			height = (int)((double)size.getWidth() / n_ratio); 
		} else { 
			width = (int) ((double)size.getHeight() * n_ratio); 
			height = size.getHeight(); 
		} 
		double ratio = (double)newSize.getWidth() / (double)width; 
		x = (int)(((imgOrigin.getWidth() - width) / 2) * ratio); 
		y = (int)(((imgOrigin.getHeight() - height) / 2) * ratio); 
		System.out.println("============ cuteImage ============="); 
		System.out.println("x=" + x); 
		System.out.println("y=" + y); 
		System.out.println("width=" + width); 
		System.out.println("height=" + height); 
		System.out.println("ratio=" + ratio); 
 
		return resizeImage(imgOrigin, ratio, x, y, /*width*/newSize.getWidth(), /*height*/newSize.getHeight()); 
	} 
	 
	/** 
	 * 图片尺寸缩放 
	 * @param imageOrigin 
	 * @param ratio 
	 * @param x 
	 * @param y 
	 * @param width 
	 * @param height 
	 * @return 
	 * @throws ImageException 
	 */ 
	public static BufferedImage resizeImage(BufferedImage imageOrigin, 
			double ratio, int x, int y, int width, int height) throws Exception, ImageException { 
		BufferedImage bi = new BufferedImage((int) ((double) imageOrigin.getWidth() * ratio), 
				(int) ((double) imageOrigin.getHeight() * ratio), 
				BufferedImage.TYPE_INT_RGB); 
 
		AffineTransform transform = AffineTransform.getScaleInstance(ratio, ratio); 
		bi.createGraphics().drawImage(imageOrigin, transform, null); 
		System.out.println("TRACE: bi.width=" + bi.getWidth()); 
		System.out.println("TRACE: bi.height=" + bi.getHeight()); 
		System.out.println("TRACE: x=" + x); 
		System.out.println("TRACE: y=" + y); 
		System.out.println("TRACE: width=" + width); 
		System.out.println("TRACE: height=" + height); 
 
		// 消除缩放比例类型转换可能带来的误差。 
		if ((x + width) > bi.getWidth())	width = bi.getWidth() - x; 
		if ((y + height > bi.getHeight()))	height = bi.getHeight() - y; 
		return bi.getSubimage(x, y, width, height); 
	} 
	/** 
	 * 测试 
	 * @param args 
	 */ 
	public static void main(String[] args) { 
		try { 
			generateThumb("c:\\1.jpg", "c:\\2.png", new Size(100, 100)); 
		} catch (Exception e) { 
			// TODO Auto-generated catch block 
			e.printStackTrace(); 
		} 
	} 
}