1. 程式人生 > >圖片壓縮工具Thumbnailator的使用

圖片壓縮工具Thumbnailator的使用

        Thumbnailator 是一個為Java介面更流暢的縮圖生成庫。從API提供現有的影象檔案和影象物件的縮圖中簡化了縮略過程,兩三行程式碼就能夠從現有圖片生成縮圖,且允許微調縮圖生成,同時保持了需要寫入到最低限度的程式碼量。同時還支援根據一個目錄批量生成縮圖。

http://code.google.com/p/thumbnailator/

版本:thumbnailator-0.4.8.jar

package com.zspr.utils.img;

import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import net.coobird.thumbnailator.Thumbnails;
import net.coobird.thumbnailator.geometry.Positions;

/**
 * 圖片工具
 * @author jichao
 *
 */
public class Pic {

	/**
	 * 指定大小進行縮放
	 * @param srcUrl 源圖片地址
	 * @param targetUrl 目標圖片地址
	 * @param width 寬
	 * @param height 高
	 * @throws IOException
	 */
	public static void resize(String srcUrl,String targetUrl,int width,int height) throws IOException {
		/*
		 * size(width,height) 若圖片橫比200小,高比300小,不變
		 * 若圖片橫比200小,高比300大,高縮小到300,圖片比例不變 若圖片橫比200大,高比300小,橫縮小到200,圖片比例不變
		 * 若圖片橫比200大,高比300大,圖片按比例縮小,橫為200或高為300
		 */
		Thumbnails.of(srcUrl).size(width, height).toFile(targetUrl);
	}
	
	
	/**
	 * 按照比例進行縮放
	 * @param srcUrl 源圖片地址
	 * @param targetUrl 目標圖片地址
	 * @param num 質量比例如 0.8
	 * @throws IOException
	 */
	public static void scale(String srcUrl,String targetUrl,double num) throws IOException {
		Thumbnails.of(srcUrl).scale(num).toFile(targetUrl );
	}
	
	/**
	 * 水印
	 * @param srcUrl 源圖片地址
	 * @param targetUrl 目標圖片地址
	 * @param width 寬
	 * @param height 高
	 * @param num 質量比例如 0.8
	 * @param pos 顯示位置:  Positions.BOTTOM_RIGHT  
	 * @throws IOException
	 */
	public static void watermark(String srcUrl,String targetUrl,int width,int height,float num,Positions pos) throws IOException {
		Thumbnails.of(srcUrl).size(width,height).watermark(pos,ImageIO.read(new File(targetUrl)), num).outputQuality(num).toFile(targetUrl);
	}
	
	/**
	 * 裁剪
	 * @param srcUrl 源圖片地址
	 * @param targetUrl 目標圖片地址
	 * @param width 寬
	 * @param height 高
	 * @param pos 顯示位置:  Positions.BOTTOM_RIGHT  
	 * @param x 區域寬度 
	 * @param y 區域高度
	 * @throws IOException
	 */
	public static void cut(String srcUrl,String targetUrl,int width,int height,Positions pos,int x,int y)throws IOException {
		Thumbnails.of(srcUrl).sourceRegion(pos,x,y).size(width, height).keepAspectRatio(false).toFile(targetUrl);
	}
	
	
	/**
	 * 裁剪--指定座標/大小
	 * @param srcUrl 源圖片地址
	 * @param targetUrl 目標圖片地址
	 * @param width 寬
	 * @param height 高
	 * @param pointA_1 座標A1 
	 * @param pointA_2座標A2 
	 * @param pointB_1座標B1
	 * @param pointB_2座標B2
	 * @throws IOException
	 */
	public static void cut(String srcUrl,String targetUrl,int width,int height,int pointA_1,int pointA_2,int pointB_1,int pointB_2) throws IOException {
		Thumbnails.of(srcUrl).sourceRegion(pointA_1, pointA_2, pointB_1, pointB_2).size(width, height).keepAspectRatio(false).toFile(targetUrl);
	}
	
	/**
	 * 轉化影象格式
	 * @param srcUrl 源圖片地址
	 * @param targetUrl 目標圖片地址
	 * @param width 寬
	 * @param height 高
	 * @param format 格式 如png/gif/jpg
	 * @throws IOException
	 */
	public static void format(String srcUrl,String targetUrl,int width,int height,String format) throws IOException {
		Thumbnails.of(srcUrl).size(width, height).outputFormat(format).toFile(targetUrl);
	}
}

圖片上傳呼叫程式碼(jfinal)
/**
	 * 上傳多圖片
	 */
	public void uploadFiles(){
		try{
			List<String> dbPathList = new ArrayList<String>();
			String folderName = UploadUtils.createFolder(SysCommonConstant.SERVER_IMGS_PATH);// 建立日期資料夾名稱  :/imgs/2015-10
			String serverFolderPath = SysCommonConstant.SERVER_IMGS_PATH + File.separator + folderName + File.separator ; //檔案儲存目錄
			String dbFolerPath = SysCommonConstant.SAVE_IMG_PATH + File.separator + folderName + File.separator ; //資料庫檔案儲存目錄
			List<UploadFile> fileList = getFiles(serverFolderPath, 999999999);
			System.out.println("size:"+fileList.size());
			
			for(UploadFile uf : fileList){
				File tmpFile = uf.getFile();
				String saveImgName = uf.getFileName();//圖片名稱
				Pattern reg = Pattern.compile("[\\.](jpg|png|jpeg|gif|JPG|PNG|JPEG|GIF)$");
				Matcher matcher = reg.matcher(saveImgName);
				if (!matcher.find()) {
					renderJson("圖片格式只支援jpg|png|jpeg|gif.");
					return ;
				}  
				
				String newImgName = UploadUtils.rename(saveImgName);//檔案重新命名
				String newImgName2 = UploadUtils.rename(saveImgName);//檔案重新命名-用於壓縮
				String serverPath = serverFolderPath + File.separator + newImgName;//伺服器檔案儲存地址
				String dbPath = "";//資料庫檔案儲存地址
				
				//重新命名圖片拷貝
				int imageWidthResource = Integer.parseInt(SysCommonConstant.IMAGE_WIDTH);
				int imageHeightResource = Integer.parseInt(SysCommonConstant.IMAGE_HEIGHT);
				File targetFile = new File(serverPath);
				Image img = ImageIO.read(tmpFile);
				int widthReal = img.getWidth(null);
				FileUtil.copyFile(tmpFile, targetFile);
				
				if(widthReal>imageWidthResource){
					/*PicUtils mypic = new PicUtils();
					mypic.compressPic(serverFolderPath, serverFolderPath, newImgName, newImgName2, imageWidthResource, imageHeightResource, true);
					*/ 
					Pic.resize(serverPath,  serverFolderPath + File.separator + newImgName2, imageWidthResource, imageHeightResource);
					dbPath = dbFolerPath + newImgName2;//資料庫儲存地址
				}else{
					dbPath = dbFolerPath + newImgName;//資料庫儲存地址
				}
				dbPathList.add(dbPath);
				//將未重新命名的圖片刪除
				tmpFile.delete();
				if(widthReal>imageWidthResource){
					targetFile.delete();
				}
			}
			Map<String,Object> result = new HashMap<String, Object>();
			Collections.reverse(dbPathList);//反轉順序
			result.put("picurl",dbPathList);
			renderJson(BeanUtil.getSucessRes(result));
			printReslutJson(result);
		}catch(Exception e){
			e.printStackTrace();
			log.info("上傳圖片異常.");
			renderJson(BeanUtil.getFaileRes("上傳圖片異常."));
		}
	}