1. 程式人生 > >圖片裁剪工具類(支援jpg,png,bmp,gif(剪下後為靜態))

圖片裁剪工具類(支援jpg,png,bmp,gif(剪下後為靜態))

import java.awt.Rectangle;
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;

/**
 * 圖片裁剪操作
 * @author 
 */
public class ImageCutUtil {

	/**
     * 獲取圖片寬度
     * @param file  圖片檔案
     * @return 寬度
     */
    public static int getImgWidth(File file) {
        InputStream is = null;
        BufferedImage src = null;
        int ret = -1;
        try {
            is = new FileInputStream(file);
            src = javax.imageio.ImageIO.read(is);
            ret = src.getWidth(null); // 得到源圖寬
            is.close();
        } catch (Exception e) {
        	e.printStackTrace();
        }
        return ret;
    }
    
    /**
     * 獲取圖片高度
     * @param sourcePath  圖片路徑
     * @return 寬度
     */
    public static int getImgWidth(String sourcePath) {
       File file=new File(sourcePath);
       return getImgWidth(file);
    }
    
    /**
     * 獲取圖片高度
     * @param file  圖片檔案
     * @return 高度
     */
    public static int getImgHeight(File file) {
        InputStream is = null;
        BufferedImage src = null;
        int ret = -1;
        try {
            is = new FileInputStream(file);
            src = javax.imageio.ImageIO.read(is);
            ret = src.getHeight(null); // 得到源圖高
            is.close();
        } catch (Exception e) {
        	e.printStackTrace();
        }
        return ret;
    }
    
    /**
     * 獲取圖片高度
     * @param sourcePath  圖片路徑
     * @return 高度
     */
    public static int getImgHeight(String sourcePath) {
    	 File file=new File(sourcePath);
         return getImgHeight(file);
    }

	/**
	 * 圖片裁切
	 * 選擇的總區域必須與圖片原始大小相同
	 * @param X 選擇區域左上角的x座標
	 * @param Y 選擇區域左上角的y座標
	 * @param width 選擇區域的寬度
	 * @param height 選擇區域的高度
	 * @param sourcePath 源圖片路徑
	 * @param descpath 裁切後圖片的儲存路徑
	 */
	public static void cut(int X, int Y, int width, int height,
			String sourcePath, String descpath) {

		FileInputStream is = null;
		ImageInputStream iis = null;
		try {
			is = new FileInputStream(sourcePath);
			String fileSuffix = sourcePath.substring(sourcePath
					.lastIndexOf(".") + 1);
			Iterator<ImageReader> it = ImageIO
					.getImageReadersByFormatName(fileSuffix);
			ImageReader reader = it.next();
			iis = ImageIO.createImageInputStream(is);
			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, fileSuffix, new File(descpath));
			System.out.println("圖片剪下成功,生成圖片路徑:"+descpath);
		} catch (Exception ex) {
			ex.printStackTrace();
		} finally {
			if (is != null) {
				try {
					is.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
				is = null;
			}
			if (iis != null) {
				try {
					iis.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
				iis = null;
			}
		}

	}

	/**
	 * 圖片裁切
	 * 選擇的總區域與圖片原始大小不同
	 * @param selectWidth 選擇區域的寬度
	 * @param selectHeight 選擇區域的高度
	 * @param selectX 選擇區域左上角的x座標
	 * @param selectY 選擇區域左上角的y座標
	 * @param areaWidth 選擇區域總寬度
	 * @param areaHeight 選擇區域總高度
	 * @param sourcePath 源圖片路徑
	 * @param descpath 裁切後圖片的儲存路徑
	 */
	public static void cut(int areaWidth, int areaHeight,int selectX, int selectY, int selectWidth, int selectHeight,String sourcePath, String descpath) {
		int imgWidth=getImgWidth(sourcePath);
		int imgHeight=getImgHeight(sourcePath);
		int X=selectX*imgWidth/areaWidth;
		int Y=selectY*imgHeight/areaHeight;
		int width=selectWidth*imgWidth/areaWidth;
		int height=selectHeight*imgHeight/areaHeight;
		cut(X, Y, width, height, sourcePath, descpath);
	}
	public static void main(String[] args) {
		System.out.println("圖片的寬度:"+getImgWidth("d:/1.bmp"));
		System.out.println("圖片的高度:"+getImgHeight("d:/1.bmp"));
		ImageCutUtil.cut(30, 50, 300, 400, "d:/1.bmp", "d:/2.bmp");

	}

}