`
忧里修斯
  • 浏览: 426514 次
  • 性别: Icon_minigender_1
社区版块
存档分类
最新评论

创建图片缩略图

    博客分类:
  • J2SE
阅读更多
import java.io.*;
import java.awt.*;
import java.awt.image.*;
import com.sun.image.codec.jpeg.*;

public class ImageUtil {
	private String srcFile;
	private String destFile;
	private int width;
	private int height;
	private Image img;

	public ImageUtil(String srcFile) throws IOException {
		File _file = new File(srcFile); 
		this.srcFile = srcFile;
		this.destFile = srcFile;
		img = javax.imageio.ImageIO.read(_file);
		width = img.getWidth(null);
		height = img.getHeight(null);
	}

	public void resize(int w, int h) throws IOException {
		BufferedImage _image = new BufferedImage(w, h,BufferedImage.TYPE_INT_RGB);
		_image.getGraphics().drawImage(img, 0, 0, w, h, null);
		FileOutputStream out = new FileOutputStream(destFile);
		JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
		encoder.encode(_image);
		out.close();
	}

	public void resize(double t) throws IOException {
		int w = (int) (width * t);
		int h = (int) (height * t);
		resize(w, h);
	}

	public void resizeByWidth(int w) throws IOException {
		int h = (int) (height * w / width);
		resize(w, h);
	}

	public void resizeByHeight(int h) throws IOException {
		int w = (int) (width * h / height);
		resize(w, h);
	}

	public void resizeFix(int w, int h) throws IOException {
		if (width / height > w / h) {
			resizeByWidth(w);
		} else {
			resizeByHeight(h);
		}
	}

	public int getSrcWidth() {
		return width;
	}

	public int getSrcHeight() {
		return height;
	}
}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics