图片裁剪与缩放

package test;

import java.awt.Image;
import java.awt.Rectangle;
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.IOException;
import java.io.InputStream;
import java.util.Iterator;

import javax.imageio.ImageIO;
import javax.imageio.ImageReadParam;
import javax.imageio.ImageReader;
import javax.imageio.stream.ImageInputStream;

public class ImageHelper {
	/*
	 * 根据尺寸图片居中裁剪
	 */
	public static void cutCenterImage(String inputPath, String outputPath, int width, int height) throws IOException {
		Iterator iterator = ImageIO.getImageReadersByFormatName("jpg");
		ImageReader reader = (ImageReader) iterator.next();
		InputStream in = new FileInputStream(inputPath);
		ImageInputStream iis = ImageIO.createImageInputStream(in);
		reader.setInput(iis, true);
		ImageReadParam param = reader.getDefaultReadParam();
		int imageIndex = 0;
		Rectangle rect = new Rectangle(
				(reader.getWidth(imageIndex) - width) / 2,
				(reader.getHeight(imageIndex) - height) / 2, width, height);
		param.setSourceRegion(rect);
		BufferedImage bi = reader.read(0, param);
		ImageIO.write(bi, "jpg", new File(outputPath));

	}

	/*
	 * 图片裁剪二分之一
	 */
	public static void cutHalfImage(String inputPath, String outputPath) throws IOException {
		Iterator iterator = ImageIO.getImageReadersByFormatName("jpg");
		ImageReader reader = (ImageReader) iterator.next();
		InputStream in = new FileInputStream(inputPath);
		ImageInputStream iis = ImageIO.createImageInputStream(in);
		reader.setInput(iis, true);
		ImageReadParam param = reader.getDefaultReadParam();
		int imageIndex = 0;
		int width = reader.getWidth(imageIndex) / 2;
		int height = reader.getHeight(imageIndex) / 2;
		Rectangle rect = new Rectangle(width / 2, height / 2, width, height);
		param.setSourceRegion(rect);
		BufferedImage bi = reader.read(0, param);
		ImageIO.write(bi, "jpg", new File(outputPath));
	}

	/*
	 * 图片裁剪通用接口
	 */

	public static void cutImage(String inputPath, String outputPath, int x, int y, int width, int height) throws IOException {
		Iterator iterator = ImageIO.getImageReadersByFormatName("jpg");
		ImageReader reader = (ImageReader) iterator.next();
		InputStream in = new FileInputStream(inputPath);
		ImageInputStream iis = ImageIO.createImageInputStream(in);
		reader.setInput(iis, true);
		ImageReadParam param = reader.getDefaultReadParam();
		Rectangle rect = new Rectangle(x, y, width, height);
		param.setSourceRegion(rect);
		BufferedImage bi = reader.read(0, param);
		ImageIO.write(bi, "jpg", new File(outputPath));

	}

	/*
	 * 图片缩放固定尺寸
	 */
	public static void zoomImage(String inputPath, String outputPath, int width, int height) throws Exception {
		double wr = 0, hr = 0;
		File srcFile = new File(inputPath);
		File destFile = new File(outputPath);
		BufferedImage bufImg = ImageIO.read(srcFile);
		Image Itemp = bufImg.getScaledInstance(width, height,
				bufImg.SCALE_SMOOTH);
		wr = width * 1.0 / bufImg.getWidth();
		hr = height * 1.0 / bufImg.getHeight();
		AffineTransformOp ato = new AffineTransformOp(
				AffineTransform.getScaleInstance(wr, hr), null);
		Itemp = ato.filter(bufImg, null);
		try {
			ImageIO.write((BufferedImage) Itemp,
					outputPath.substring(outputPath.lastIndexOf(".") + 1),
					destFile);
		} catch (Exception ex) {
			ex.printStackTrace();
		}

	}
	
	/*
	 * 图片缩放多少倍
	 */
	public static void zoomImage(String inputPath, String outputPath, double times) throws Exception {
		File srcFile = new File(inputPath);
		File destFile = new File(outputPath);
		BufferedImage bufImg = ImageIO.read(srcFile);
		int width = (int) (bufImg.getWidth()*times);
		int height = (int) (bufImg.getHeight()*times);
		Image Itemp = bufImg.getScaledInstance(width, height, bufImg.SCALE_SMOOTH);
		AffineTransformOp ato = new AffineTransformOp(
				AffineTransform.getScaleInstance(times, times), null);
		Itemp = ato.filter(bufImg, null);
		try {
			ImageIO.write((BufferedImage) Itemp,
					outputPath.substring(outputPath.lastIndexOf(".") + 1),
					destFile);
		} catch (Exception ex) {
			ex.printStackTrace();
		}

	}
}

猜你喜欢

转载自coder-xpf.iteye.com/blog/2241392