对图片进行各种操作,等比例缩放,按照大小缩放,缩放,裁剪,转换格式,输出到各种流中。
首先你要引用thumbnailator 的jar包最新的版本现在是 0.4.8下载地址https://code.google.com/p/thumbnailator/
包的使用api在 http://thumbnailator.googlecode.com/hg/javadoc/index.html里面,感觉用处挺多的。
先说图片刚才说的哪几种处理吧:
1、指定大小进行缩放
//size(宽度, 高度)
/*
* 若图片横比200小,高比300小,不变
* 若图片横比200小,高比300大,高缩小到300,图片比例不变
* 若图片横比200大,高比300小,横缩小到200,图片比例不变
* 若图片横比200大,高比300大,图片按比例缩小,横为200或高为300
*/
Thumbnails.of("images/a380_1280x1024.jpg")
.size(200, 300)
.toFile("c:/a380_200x300.jpg");
Thumbnails.of("images/a380_1280x1024.jpg")
.size(2560, 2048)
.toFile("c:/a380_2560x2048.jpg");
2、按照比例进行缩放
//scale(比例)
Thumbnails.of("images/a380_1280x1024.jpg")
.scale(0.25f)
.toFile("c:/a380_25%.jpg");
Thumbnails.of("images/a380_1280x1024.jpg")
.scale(1.10f)
.toFile("c:/a380_110%.jpg");
3、不按照比例,指定大小进行缩放
//keepAspectRatio(false) 默认是按照比例缩放的
Thumbnails.of("images/a380_1280x1024.jpg")
.size(200, 200)
.keepAspectRatio(false)
.toFile("c:/a380_200x200.jpg");
4、旋转
//rotate(角度),正数:顺时针 负数:逆时针
Thumbnails.of("images/a380_1280x1024.jpg")
.size(1280, 1024)
.rotate(90)
.toFile("c:/a380_rotate+90.jpg");
Thumbnails.of("images/a380_1280x1024.jpg")
.size(1280, 1024)
.rotate(-90)
.toFile("c:/a380_rotate-90.jpg");
5、水印
//watermark(位置,水印图,透明度)
Thumbnails.of("images/a380_1280x1024.jpg")
.size(1280, 1024)
.watermark(Positions.BOTTOM_RIGHT, ImageIO.read(new File("images/watermark.png")), 0.5f)
.outputQuality(0.8f)
.toFile("c:/a380_watermark_bottom_right.jpg");
Thumbnails.of("images/a380_1280x1024.jpg")
.size(1280, 1024)
.watermark(Positions.CENTER, ImageIO.read(new File("images/watermark.png")), 0.5f)
.outputQuality(0.8f)
.toFile("c:/a380_watermark_center.jpg");
6、裁剪
//sourceRegion()
//图片中心400*400的区域
Thumbnails.of("images/a380_1280x1024.jpg")
.sourceRegion(Positions.CENTER, 400,400)
.size(200, 200)
.keepAspectRatio(false)
.toFile("c:/a380_region_center.jpg");
//图片右下400*400的区域
Thumbnails.of("images/a380_1280x1024.jpg")
.sourceRegion(Positions.BOTTOM_RIGHT, 400,400)
.size(200, 200)
.keepAspectRatio(false)
.toFile("c:/a380_region_bootom_right.jpg");
//指定坐标
Thumbnails.of("images/a380_1280x1024.jpg")
.sourceRegion(600, 500, 400, 400)
.size(200, 200)
.keepAspectRatio(false)
.toFile("c:/a380_region_coord.jpg");
7、转化图像格式
//outputFormat(图像格式)
Thumbnails.of("images/a380_1280x1024.jpg")
.size(1280, 1024)
.outputFormat("png")
.toFile("c:/a380_1280x1024.png");
Thumbnails.of("images/a380_1280x1024.jpg")
.size(1280, 1024)
.outputFormat("gif")
.toFile("c:/a380_1280x1024.gif");
8、输出到OutputStream
//toOutputStream(流对象)
OutputStream os = new FileOutputStream("c:/a380_1280x1024_OutputStream.png");
Thumbnails.of("images/a380_1280x1024.jpg")
.size(1280, 1024)
.toOutputStream(os);
9、输出到BufferedImage
//asBufferedImage() 返回BufferedImage
BufferedImage thumbnail = Thumbnails.of("images/a380_1280x1024.jpg")
.size(1280, 1024)
.asBufferedImage();
ImageIO.write(thumbnail, "jpg", new File("c:/a380_1280x1024_BufferedImage.jpg"));
需要注意的是,对于CMYK模式的图像,由于JDK的Bug,目前还不能够处理,会出以下异常:
这些问题可以JAI.create()来代替ImageIO.read()解决。而高清图的内存溢出OOM问题只能使用ImageMagick转换了。
我这次图片是要先保存原图。在进行图片等比例缩放在进行裁剪,代码可以粘出来做一下备份,以可以大家参考一下
public String uploadPic () throws IOException {
String[] str = { ".jpg", ".jpeg", ".bmp", ".gif" ,".png"};
// 获取用户登录名
// TbUser curruser = (TbUser) getValue(SCOPE_SESSION, "curruser");
// 限定文件大小是4MB
if (pic == null || pic.length() > 4194304) {
//文件过大
return "error";
}
for (String s : str) {
if (picFileName.endsWith(s)) {
String realPath = ServletActionContext.getServletContext().getRealPath("/uploadPic");// 在tomcat中保存图片的实际路径 == "webRoot/uploadpic/"
File saveFile = new File(new File(realPath), "org"+picFileName); // 在该实际路径下实例化一个文件
// 判断父目录是否存在
if (!saveFile.getParentFile().exists()) {
saveFile.getParentFile().mkdirs();
}
try {
// 执行文件上传
// FileUtils 类名 org.apache.commons.io.FileUtils;
// 是commons-io包中的,commons-fileupload 必须依赖
// commons-io包实现文件上次,实际上就是将一个文件转换成流文件进行读写
FileUtils.copyFile(pic, saveFile);
File file_thum=new java.io.File(realPath+"/org"+picFileName);
Image src=javax.imageio.ImageIO.read(file_thum);
float tagsize=200;
int old_w=src.getWidth(null);
int old_h=src.getHeight(null);
int def_w=320;
int def_h=180;
int new_w=0;
int new_h=0;
float tempdouble=(float)old_w/(float)old_h;
if(def_w>def_h){
new_w=def_w;//new
new_h=Math.round(new_w/tempdouble);
}else{
new_h=def_h;
new_w=Math.round(new_h*tempdouble);
}
BufferedImage tag = new BufferedImage(new_w,new_h,BufferedImage.TYPE_INT_RGB);
tag.getGraphics().drawImage(src, 0, 0, new_w, new_h, null);
File testFile = new File(new File(realPath+"/thum"), "org"+picFileName); // 在该实际路径下实例化一个文件
// 判断父目录是否存在
if (!testFile.getParentFile().exists()) {
testFile.getParentFile().mkdirs();
}
Thumbnails.of(tag).sourceRegion(Positions.CENTER,320,180).size(320, 180).keepAspectRatio(false).toFile(realPath+"/thum/2015"+picFileName);
//FileOutputStream newimage=new FileOutputStream(realPath+"/thum/2015old"+picFileName);
// JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(newimage);
// encoder.encode(tag);//JPEG编码
// newimage.close();
//Thumbnails.of(tag).sourceRegion(Positions.CENTER,30,30).size(320, 180).keepAspectRatio(true).toFile(realPath+"/thum/2015"+picFileName);
//Thumbnails.of(realPath+"org"+picFileName).sourceRegion(Positions.CENTER,30,30).size(320, 180).keepAspectRatio(true).toFile(realPath+"/thum/2014"+picFileName);
picFileName="2015"+picFileName;
} catch (IOException e) {
e.printStackTrace();
return "imageError";
}
}
}
picFileName="2015"+picFileName;
return "success";
}