Thumbnaillator是一个非常优秀的图片处理框架,使用很简单,下载地址http://pan.baidu.com/s/1chAkrW
1.基本使用方法
基本使用方法有很多介绍,推荐一个笔者学习的博客http://blog.youkuaiyun.com/chenleixing/article/details/44685817
//这是我学习时写的代码
public static void main(String[] args) throws Exception{
//得到工程路径
//1.若路径中有中文则乱码
// String savePath = ThumbnaillatorTest.class.getResource("/").getPath()+"img/";
//2.得到的路径中使用"\",无法被识别
// String savePath = System.getProperty("user.dir")+"\\src\\img";
//3.将1中的乱码解析转码为utf-8
// savePath = URLDecoder.decode(savePath,"utf-8");
//4
String savePath = ThumbnaillatorTest.class.getResource("/").toURI().getPath();
File fromPic = new File(savePath + "img/1.jpg");
File toPic = new File(savePath+"img/3.jpg");
File waterPic = new File(savePath + "img/2.jpg");
//按比例缩放
Thumbnails.of(fromPic).size(400,500).toFile(toPic);
//不按比例缩放
// Thumbnails.of(fromPic).forceSize(100,100).toFile(toPic);
//或者
// Thumbnails.of(fromPic).size(200,200).keepAspectRatio(false).toFile(toPic);
//按倍数缩放
// Thumbnails.of(fromPic).scale(0.5f).toFile(toPic);
// Thumbnails.of(fromPic).scale(2f).toFile(toPic);
//旋转图片,rotate(角度),正数则为顺时针,负数则为逆时针
// Thumbnails.of(fromPic).scale(0.5f).rotate(90).toFile(toPic);
//图片尺寸不变,压缩图片文件大小outputQuality实现,参数1为最高质量
// Thumbnails.of(fromPic).scale(1f).outputQuality(0.5f).toFile(toPic);
//给图片加水印,watermark(位置,水印图,透明度)Positions.CENTER表示加在中间
// Thumbnails.of(fromPic).scale(0.5f)
// .watermark(Positions.BOTTOM_RIGHT,ImageIO.read(waterPic),0.5f)
// .toFile(toPic);
//用sourceRegion()实现图片裁剪
//图片中心300*300的区域,Positions.CENTER表示中心,还有许多其他位置可选
// Thumbnails.of(fromPic).sourceRegion(Positions.CENTER,300,300).size(300,300).toFile(toPic);
//用outputFormat(图像格式)转换图片格式,保持原尺寸不变
// Thumbnails.of(fromPic).outputFormat("bmp").scale(1f).toFile(savePath + "img/4.bmp");
// File testPic = new File(savePath + "img/4.bmp");
// File testToPic = new File(savePath + "img/5.png");
// Thumbnails.of(testPic).scale(1f).outputQuality(0.5f).toFile(testToPic);
//批量处理
//方法一:缺点 在img文件中有多少图片,就需要新建多少图片
// File[] fs = new File(savePath + "img").listFiles();
// List<File> list = new ArrayList<File>();
// list.add(new File(savePath + "/imgs/1.jpg"));
// list.add(new File(savePath + "/imgs/2.jpg"));
// list.add(new File(savePath + "/imgs/3.jpg"));
// list.add(new File(savePath + "/imgs/4.jpg"));
// list.add(new File(savePath + "/imgs/5.jpg"));
// Iterable<File> toPic = list;
// Thumbnails.of(fs).scale(0.5f).outputFormat("jpg").toFiles(toPic);
//方法二: 缺点 新生成的图片和旧图片在同一文件夹下,容易混淆
// Thumbnails.of(fs).scale(0.5f).outputFormat("jpg").toFiles(Rename.PREFIX_HYPHEN_THUMBNAIL);
}
2.批量处理
方法一批量处理的缺点让我挺不舒服的,琢磨着写了个工具类,用for……in循环将文件add到list里
/**
* 返回已经封装好的Iterable
* @param getFiles
* @param toPath
* @return
* @throws Exception
*/
public static Iterable<File> batchPic (File[] getFiles, String toPath) throws Exception{
if (getFiles == null) {
throw new Exception("未找到需要处理的图片,请检查路径");
}
List<File> list = new ArrayList<File>();
for (File f : getFiles) {
list.add(new File(toPath + "/" + f.getName()));
}
return (Iterable<File>)list;
}
3.保持原比例,裁剪图片到指定的尺寸
在我推荐的博客里,裁剪图片的代码有些问题而且不够精简,重写了一份。
可以批量裁剪图片
/**
* 将所有图片裁剪为统一尺寸
* @param getPath
* @param toPath
* @param width
* @param height
* @return
* @throws Exception
*/
public static void cutPic (String getPath, String toPath, int width, int height) throws Exception{
File[] getFiles = new File(getPath).listFiles();
if (getFiles == null) {
throw new Exception("未找到需要处理的图片,请检查路径");
}
float imgWidth = 0f;
float imgHeight = 0f;
float imgScole = 0f;
float scole = 0f;
for (File f : getFiles) {
BufferedImage img = ImageIO.read(f);
imgWidth = img.getWidth();
imgHeight = img.getHeight();
imgScole = imgHeight / imgWidth;
scole = (float)height / width;
if (imgScole > scole) {
img = Thumbnails.of(f.getPath()).width(width).asBufferedImage();
} else {
img = Thumbnails.of(f.getPath()).height(height).asBufferedImage();
}
Thumbnails.of(img).sourceRegion(Positions.CENTER,width,height).size(width, height)
.outputFormat("jpg").toFile(toPath + "/" + f.getName());
}
}
其实刚开博客没多久,有什么问题欢迎各位评论或者私信我。