thumbnailator-0.4.8目前是最新的jar
code:
package yingyue; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStream; import javax.imageio.ImageIO; import net.coobird.thumbnailator.Thumbnails; import net.coobird.thumbnailator.geometry.Positions; public class HandlerImg { private static String fromPic = "from/1600x1200-Asia EMEA.jpg"; private static String toPic = "to/"; public static void main(String[] args){ try { /* 按指定大小把图片进行缩放(会遵循原图高宽比例)*/ Thumbnails.of(fromPic). size(100, 200). toFile(toPic + "/new1.jpg");//变为400*300,遵循原图比例缩或放到400*某个高度 /* 按照指定比例进行缩小和放大 */ Thumbnails.of(fromPic). scale(0.2f). toFile(toPic + "/new2_small.jpg");//按比例缩小 Thumbnails.of(fromPic). scale(2f). toFile(toPic + "/new2_large.jpg");//按比例放大 /* 图片尺寸不变,压缩图片文件大小 */ Thumbnails.of(fromPic). scale(1f). outputQuality(0.25f). toFile(toPic + "/new3.jpg");// 图片尺寸不变,压缩图片文件大小outputQuality实现,参数1为最高质量 /* 图片旋转 rotate(角度),正数:顺时针负数:逆时针 */ Thumbnails.of(fromPic) .size(1280,1024) .rotate(90) .toFile(toPic + "/new4.jpg"); /* 水印 watermark(位置,水印图,透明度) */ Thumbnails.of(fromPic) .size(1280,1024) .watermark(Positions.BOTTOM_RIGHT,ImageIO.read(new File("from/ss.png")),0.8f) .outputQuality(0.8f) .toFile(toPic + "/new5-1.jpg"); Thumbnails.of(fromPic) .size(1280,1024) .watermark(Positions.CENTER,ImageIO.read(new File("from/ss.png")),0.5f) .outputQuality(0.8f) .toFile(toPic + "/new5-2.jpg"); Thumbnails.of(fromPic) .size(1280,1024) .watermark(Positions.TOP_LEFT,ImageIO.read(new File("from/ss.png")),0.9f) .outputQuality(0.8f) .toFile(toPic + "/new5-3.jpg"); /* 裁剪 */ Thumbnails.of(fromPic) .sourceRegion(Positions.CENTER,400,400) .size(200,200) .keepAspectRatio(false) .toFile(toPic + "/new6-1.jpg"); //图片右下400*400的区域 Thumbnails.of(fromPic) .sourceRegion(Positions.BOTTOM_RIGHT,400,400) .size(200,200) .keepAspectRatio(false) .toFile(toPic + "/new6-2.jpg"); //指定坐标 Thumbnails.of(fromPic) .sourceRegion(600,500,400,400) .size(200,200) .keepAspectRatio(false) .toFile(toPic + "/new6-3.jpg"); /* 转化图像格式 */ Thumbnails.of(fromPic) .size(1280,1024) .outputFormat("png") .toFile(toPic + "/new7-1"); Thumbnails.of(fromPic) .size(1280,1024) .outputFormat("gif") .toFile(toPic + "/new7-2"); } catch (IOException e) { e.printStackTrace(); } } }
package yingyue;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import javax.imageio.ImageIO;
import net.coobird.thumbnailator.Thumbnails;
import net.coobird.thumbnailator.geometry.Positions;
public class TestImg {
private static String fromPic = "from/1600x1200-Asia EMEA.jpg";
private static String toPic = "to/";
public static void main(String[] args){
try {
/* 按指定大小把图片进行缩放(会遵循原图高宽比例)*/
Thumbnails.of(fromPic).
size(100, 200).
toFile(toPic + "/new1.jpg");//变为400*300,遵循原图比例缩或放到400*某个高度
/* 按照指定比例进行缩小和放大 */
Thumbnails.of(fromPic).
scale(0.2f).
toFile(toPic + "/new2_small.jpg");//按比例缩小
Thumbnails.of(fromPic).
scale(2f).
toFile(toPic + "/new2_large.jpg");//按比例放大
/* 图片尺寸不变,压缩图片文件大小 */
Thumbnails.of(fromPic).
scale(1f).
outputQuality(0.25f).
toFile(toPic + "/new3.jpg");// 图片尺寸不变,压缩图片文件大小outputQuality实现,参数1为最高质量
/* 图片旋转 rotate(角度),正数:顺时针负数:逆时针 */
Thumbnails.of(fromPic)
.size(1280,1024)
.rotate(90)
.toFile(toPic + "/new4.jpg");
/* 水印 watermark(位置,水印图,透明度) */
Thumbnails.of(fromPic)
.size(1280,1024)
.watermark(Positions.BOTTOM_RIGHT,ImageIO.read(new File("from/ss.png")),0.8f)
.outputQuality(0.8f)
.toFile(toPic + "/new5-1.jpg");
Thumbnails.of(fromPic)
.size(1280,1024)
.watermark(Positions.CENTER,ImageIO.read(new File("from/ss.png")),0.5f)
.outputQuality(0.8f)
.toFile(toPic + "/new5-2.jpg");
Thumbnails.of(fromPic)
.size(1280,1024)
.watermark(Positions.TOP_LEFT,ImageIO.read(new File("from/ss.png")),0.9f)
.outputQuality(0.8f)
.toFile(toPic + "/new5-3.jpg");
/* 裁剪 */
Thumbnails.of(fromPic)
.sourceRegion(Positions.CENTER,400,400)
.size(200,200)
.keepAspectRatio(false)
.toFile(toPic + "/new6-1.jpg");
//图片右下400*400的区域
Thumbnails.of(fromPic)
.sourceRegion(Positions.BOTTOM_RIGHT,400,400)
.size(200,200)
.keepAspectRatio(false)
.toFile(toPic + "/new6-2.jpg");
//指定坐标
Thumbnails.of(fromPic)
.sourceRegion(600,500,400,400)
.size(200,200)
.keepAspectRatio(false)
.toFile(toPic + "/new6-3.jpg");
/* 转化图像格式 */
Thumbnails.of(fromPic)
.size(1280,1024)
.outputFormat("png")
.toFile(toPic + "/new7-1");
Thumbnails.of(fromPic)
.size(1280,1024)
.outputFormat("gif")
.toFile(toPic + "/new7-2");
} catch (IOException e) {
e.printStackTrace();
}
}
}