图片压缩工具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 dbPathList = new ArrayList();
			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 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 result = new HashMap();
			Collections.reverse(dbPathList);//反转顺序
			result.put("picurl",dbPathList);
			renderJson(BeanUtil.getSucessRes(result));
			printReslutJson(result);
		}catch(Exception e){
			e.printStackTrace();
			log.info("上传图片异常.");
			renderJson(BeanUtil.getFaileRes("上传图片异常."));
		}
	}



你可能感兴趣的:(JAVA)