---
title: "按比例生成缩小比例的图片"
author: "fanyang219"
date: "2008-04-27T22:16:37Z"
tags: ["Java"]
slug: "untitled"
draft: false
lastmod: "2008-04-27T22:16:37Z"
id: 187371
url: "https://fanyang219.iteye.com/blog/187371"
---

2008年4月27日22:29   
    前段时间照了一些照片，但容量比较大，想传到班级相册里，要缩小点容量，写了个程序生成小图片，但现在质量一般，正在研究如何生成高质量缩略图。。。。。。
    现在写的程序如下：
    
```java
package test;

import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

public class ConvertImage
{
	private String fromPath = "E:/1.jpg";
	private String toPath = "E:/image/17.jpg";
	
	public ConvertImage()
	{
		File fromFile = new File(fromPath);
		File toFile = new File(toPath);

		try 
		{
			Image img = ImageIO.read(fromFile);
			BufferedImage bi = ImageIO.read(fromFile);
			int width = bi.getWidth();
			int height = bi.getHeight();
			BufferedImage tag= new BufferedImage(2*width/3, 2*height/3,   
	                BufferedImage.TYPE_INT_RGB);   
			tag.getGraphics().drawImage(img.getScaledInstance(2*width/3, 2*height/3, Image.SCALE_AREA_AVERAGING),0 , 0 , null);
			ImageIO.write(tag, "jpg", toFile);
		} 
		catch (IOException e) 
		{
			e.printStackTrace();
		}
	}
	
	public static void main(String args[])
	{
		new ConvertImage();
	}
}
```