Swing图片处理工具类ImageHelper

package zju.archeo.images;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URL;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;

import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGEncodeParam;
import com.sun.image.codec.jpeg.JPEGImageEncoder;

public class ImageHelper {

	public static ImageIcon loadImage(String name) {
		ImageIcon image = null;
		try {
			URL url = ImageHelper.class.getResource(name);
			if (url != null) {
				Image img = Toolkit.getDefaultToolkit().createImage(url);
				if (img != null)	image = new ImageIcon(img);
			}
		} catch (Throwable ex) {
			System.out.println("ERROR: loading image " + name
					+ " failed. Exception: " + ex.getMessage());
		}
		return image;
	}

	public static ImageIcon loadLocalImage(String path) {
		return new ImageIcon(path);
	}

	/**
	 * 
	 * @param dir 
	 *            图片所在的文件夹路径,以路径分隔符结尾
	 * @param name
	 *            图片文件名,包括扩展名
	 * @param smName
	 *            输出文件名,包括扩展名
	 * @param n
	 *            图片名
	 * @param width
	 *            目标宽
	 * @param height
	 *            目标高
	 * @param per
	 *            压缩百分比
	 */
	public static void compressPic(String dir, String name, String smName, int width, int height, double per){
		try {
			File file = new File(dir+name);
			Image src = ImageIO.read(file); // 构造Image对象
			int old_w = src.getWidth(null); // 得到源图宽
			int old_h = src.getHeight(null);// 得到源图高

			// 图片跟据长宽留白,成一个正方形图。
			BufferedImage oldpic;
			if (old_w > old_h) {
				oldpic = new BufferedImage(old_w, old_w,BufferedImage.TYPE_INT_RGB);
			}else if(old_w < old_h) {
				oldpic = new BufferedImage(old_h, old_h,BufferedImage.TYPE_INT_RGB);
			}else{
				oldpic = new BufferedImage(old_w, old_h,BufferedImage.TYPE_INT_RGB);
			}
			
			Graphics2D g = oldpic.createGraphics();
			g.setColor(Color.white);
			if (old_w > old_h) {
				g.fillRect(0, 0, old_w, old_w);
				g.drawImage(src, 0, (old_w - old_h) / 2, old_w, old_h,Color.white, null);
			}else if(old_w < old_h) {
				g.fillRect(0, 0, old_h, old_h);
				g.drawImage(src, (old_h - old_w) / 2, 0, old_w, old_h,Color.white, null);
			}else{
				// g.fillRect(0,0,old_h,old_h);
				g.drawImage(src.getScaledInstance(old_w, old_h,Image.SCALE_SMOOTH), 0, 0, null);
			}
			g.dispose();
			src = oldpic;
			
			// 图片调整为方形结束
			int new_w = 0, new_h = 0; 
			double w2 = (old_w * 1.00) / (width * 1.00);
			double h2 = (old_h * 1.00) / (height * 1.00);
			if(old_w > width)	new_w = (int) Math.round(old_w / w2);
			else	new_w = old_w;
			if(old_h > height)	new_h = (int) Math.round(old_h / h2);// 计算新图长宽
			else	new_h = old_h;
			
			BufferedImage tag = new BufferedImage(new_w, new_h,BufferedImage.TYPE_INT_RGB);
			// tag.getGraphics().drawImage(src,0,0,new_w,new_h,null); //绘制缩小后的图
			tag.getGraphics().drawImage(src.getScaledInstance(new_w, new_h, Image.SCALE_SMOOTH), 0,	0, null);
			FileOutputStream newimage = new FileOutputStream(dir+smName); // 输出到文件流
			JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(newimage);
			JPEGEncodeParam jep = JPEGCodec.getDefaultJPEGEncodeParam(tag);
			jep.setQuality((float) per, true); // 压缩质量 
			encoder.encode(tag, jep);
			// encoder.encode(tag); //近JPEG编码
			newimage.close();
		} catch (IOException ex) {
			ex.printStackTrace();
		}
	}
	/**
	 * 根据路径名和文件名获取小图片,返回小图片路径
	 * @param dir
	 * 				图片路径,以分隔符结束
	 * @param name
	 * 				图片名,最好包含扩展名
	 * @return
	 * 				缩小后的图片路径,以jpg为扩展名,默认大小45*45,压缩率0.7
	 */
	public static String getSmallImage(String dir, String name){
		String trueName = name, ext = "";
		int pos = name.lastIndexOf('.');
		if(pos != -1){ //包含扩展名
			trueName = name.substring(0, pos);
			ext = name.substring(pos); //包含.
		}
		String smallName = trueName+"_small"+ext;
		compressPic(dir, name, smallName, 45, 45, 0.7);
		return dir+smallName;
	}

	 public static void main(String args[]){ 
		 getSmallImage("tmp"+File.separator, "Koala.jpg");
	 }
	 
}

猜你喜欢

转载自blog.csdn.net/cs064/article/details/13006709