圖片切割工具類

十五樓亮哥發表於2015-01-29


import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.color.ColorSpace;
import java.awt.image.BufferedImage;
import java.awt.image.ColorConvertOp;
import java.awt.image.CropImageFilter;
import java.awt.image.FilteredImageSource;
import java.awt.image.ImageFilter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

import javax.imageio.ImageIO;

public class ImageCutUtil {

	/**
	 * 影象切割(改) *
	 * 
	 * @param srcImageFile
	 *            源影象地址
	 * @param dirImageFile
	 *            新影象地址
	 * @param x
	 *            目標切片起點x座標
	 * @param y
	 *            目標切片起點y座標
	 * @param destWidth
	 *            目標切片寬度
	 * @param destHeight
	 *            目標切片高度
	 */
	public static void abscut(String srcImageFile, String dirImageFile, int x,
			int y, int destWidth, int destHeight) {
		try {
			Image img;
			ImageFilter cropFilter;
			// 讀取源影象
			BufferedImage bi = ImageIO.read(new File(srcImageFile));
			int srcWidth = bi.getWidth(); // 源圖寬度
			int srcHeight = bi.getHeight(); // 源圖高度
			if (srcWidth >= destWidth && srcHeight >= destHeight) {
				Image image = bi.getScaledInstance(srcWidth, srcHeight,
						Image.SCALE_DEFAULT);
				// 改進的想法:是否可用多執行緒加快切割速度
				// 四個引數分別為影象起點座標和寬高
				// 即: CropImageFilter(int x,int y,int width,int height)
				cropFilter = new CropImageFilter(x, y, destWidth, destHeight);
				img = Toolkit.getDefaultToolkit().createImage(
						new FilteredImageSource(image.getSource(), cropFilter));
				BufferedImage tag = new BufferedImage(destWidth, destHeight,
						BufferedImage.TYPE_INT_RGB);
				Graphics g = tag.getGraphics();
				g.drawImage(img, 0, 0, null); // 繪製縮小後的圖
				g.dispose();
				// 輸出為檔案
				ImageIO.write(tag, "JPEG", new File(dirImageFile));
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 縮放影象
	 * 
	 * @param srcImageFile
	 *            源影象檔案地址
	 * @param result
	 *            縮放後的影象地址
	 * @param scale
	 *            縮放比例
	 * @param flag
	 *            縮放選擇:true 放大; false 縮小;
	 */
	public static void scale(String srcImageFile, String result, int scale,
			boolean flag) {
		try {
			BufferedImage src = ImageIO.read(new File(srcImageFile)); // 讀入檔案
			int width = src.getWidth(); // 得到源圖寬
			int height = src.getHeight(); // 得到源圖長
			if (flag) {
				// 放大
				width = width * scale;
				height = height * scale;
			} else {
				// 縮小
				width = width / scale;
				height = height / scale;
			}
			Image image = src.getScaledInstance(width, height,
					Image.SCALE_DEFAULT);
			BufferedImage tag = new BufferedImage(width, height,
					BufferedImage.TYPE_INT_RGB);
			Graphics g = tag.getGraphics();
			g.drawImage(image, 0, 0, null); // 繪製縮小後的圖
			g.dispose();
			ImageIO.write(tag, "JPEG", new File(result));// 輸出到檔案流
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 重新生成按指定寬度和高度的影象
	 * 
	 * @param srcImageFile
	 *            源影象檔案地址
	 * @param result
	 *            新的影象地址
	 * @param _width
	 *            設定新的影象寬度
	 * @param _height
	 *            設定新的影象高度
	 */
	public static void scale(String srcImageFile, String result, int _width,
			int _height) {
		scale(srcImageFile, result, _width, _height, 0, 0);
	}

	public static void scale(String srcImageFile, String result, int _width,
			int _height, int x, int y) {
		try {

			BufferedImage src = ImageIO.read(new File(srcImageFile)); // 讀入檔案

			int width = src.getWidth(); // 得到源圖寬
			int height = src.getHeight(); // 得到源圖長

			if (width > _width) {
				width = _width;
			}
			if (height > _height) {
				height = _height;
			}
			Image image = src.getScaledInstance(width, height,
					Image.SCALE_DEFAULT);
			BufferedImage tag = new BufferedImage(width, height,
					BufferedImage.TYPE_INT_RGB);
			Graphics g = tag.getGraphics();
			g.drawImage(image, x, y, null); // 繪製縮小後的圖
			g.dispose();
			ImageIO.write(tag, "JPEG", new File(result));// 輸出到檔案流
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 影象型別轉換 GIF->JPG GIF->PNG PNG->JPG PNG->GIF(X)
	 */
	public static void convert(String source, String result) {
		try {
			File f = new File(source);
			f.canRead();
			f.canWrite();
			BufferedImage src = ImageIO.read(f);
			ImageIO.write(src, "JPG", new File(result));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 彩色轉為黑白
	 * 
	 * @param source
	 * @param result
	 */
	public static void gray(String source, String result) {
		try {
			BufferedImage src = ImageIO.read(new File(source));
			ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_GRAY);
			ColorConvertOp op = new ColorConvertOp(cs, null);
			src = op.filter(src, null);
			ImageIO.write(src, "JPEG", new File(result));
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * @param args
	 * @throws IOException
	 * @throws FileNotFoundException
	 */
	public static void main(String[] args) throws FileNotFoundException,
			IOException {
		// 縮放一倍
		 scale("d:/flowers.jpg", "d:/flowers2.jpg",2,false);
		// 放大一倍
		// scale("d:/flowers.jpg", "d:/flowers2.jpg",2,true);
		// 重新設定寬高
		//scale("d:/flowers.jpg", "d:/flowers2.jpg", 80, 80);
		/**
		 * 44.7 500 370
		 */

		File picture = new File("d:/flowers2.jpg");
		BufferedImage sourceImg = ImageIO.read(new FileInputStream(picture));
		System.out.println(String.format("%.1f", picture.length() / 1024.0));
		System.out.println(sourceImg.getWidth());
		System.out.println(sourceImg.getHeight());

	}
}

相關文章