[图像处理] Java将大图片转成小图片

javapub 2010-09-07
我们经常会将一个小的图片变成小一些的图片,利用java可以方便的实现,而且实现了这个功能后就可以实现更强大的功能,将一个文件夹中的所有图片都变成一个尺寸。这里提供一个将大图变成小图的方法。

并且提供一个根据这个方法的写好的一个:图片批量尺寸处理器。可以将一个文件夹下的所有图片,批量的按照一定尺寸都保存到另一个文件夹中。该工具在操作超大图片的时候会出现内存溢出的错误。功能简单也没有做太多出错处理,一般情况下挺好用的,大家将就着用吧。

import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.geom.AffineTransform;
import java.awt.image.AffineTransformOp;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;

import javax.imageio.ImageIO;


public class Temp01{
	
	private Temp01(){
		
	}
	
	private void imageOp(InputStream inFile, String outFilePath, int width, int height){
		Image image = null;
		try {
			image = ImageIO.read(inFile);
		} catch (IOException e) {
			System.out.println("file path error...");
		}
		
		int originalImageWidth = image.getWidth(null);
		int originalImageHeight = image.getHeight(null);
		
		BufferedImage originalImage = new BufferedImage(
				originalImageWidth,
				originalImageHeight,
				BufferedImage.TYPE_3BYTE_BGR);
		Graphics2D g2d = originalImage.createGraphics();
		g2d.drawImage(image, 0, 0, null);
		
		BufferedImage changedImage =
			new BufferedImage(
					width,
					height,
					BufferedImage.TYPE_3BYTE_BGR);
		
		double widthBo = (double)width/originalImageWidth;
		double heightBo = (double)width/originalImageHeight;
		
		AffineTransform transform = new AffineTransform();
		transform.setToScale(widthBo, heightBo);
		
		AffineTransformOp ato = new AffineTransformOp(transform, null);
		ato.filter(originalImage, changedImage);
		
		File fo = new File(outFilePath); //将要转换出的小图文件 

		try {
			ImageIO.write(changedImage, "jpeg", fo);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	private void imageOp(String inFilePath, String outFilePath, int width, int height){
		File tempFile = new File(inFilePath);
		Image image = null;
		try {
			image = ImageIO.read(tempFile);
		} catch (IOException e) {
			System.out.println("file path error...");
		}
		
		int originalImageWidth = image.getWidth(null);
		int originalImageHeight = image.getHeight(null);
		
		BufferedImage originalImage = new BufferedImage(
				originalImageWidth,
				originalImageHeight,
				BufferedImage.TYPE_3BYTE_BGR);
		Graphics2D g2d = originalImage.createGraphics();
		g2d.drawImage(image, 0, 0, null);
		
		BufferedImage changedImage =
			new BufferedImage(
					width,
					height,
					BufferedImage.TYPE_3BYTE_BGR);
		
		double widthBo = (double)width/originalImageWidth;
		double heightBo = (double)width/originalImageHeight;
		
		AffineTransform transform = new AffineTransform();
		transform.setToScale(widthBo, heightBo);
		
		AffineTransformOp ato = new AffineTransformOp(transform, null);
		ato.filter(originalImage, changedImage);
		
		File fo = new File(outFilePath); //将要转换出的小图文件 

		try {
			ImageIO.write(changedImage, "jpeg", fo);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public static void main(String[] args) throws FileNotFoundException {
		Temp01 t1 = new Temp01();
		t1.imageOp("C:/p02.jpg", "C:/p03.jpg", 400, 300);

		InputStream in = new FileInputStream(new File("C:/p02.jpg"));
		t1.imageOp(in, "C:/p04.jpg", 400, 300);

	}
	
}


文章地址:http://javapub.iteye.com/blog/666607

相关文章
Java图像图片 边缘检测 http://javapub.iteye.com/blog/685468
Java图像变灰 http://javapub.iteye.com/blog/687082
Java 给图片增加 水印图片 水印文字 [url]Java 给图片增加 水印图片 水印文字[/url]
Java图像渐变 http://javapub.iteye.com/blog/736799
Java将大图片转成小图片 http://javapub.iteye.com/blog/666607
Global site tag (gtag.js) - Google Analytics