Thumbnailator是一个Java缩略图生成类库。利用其提供的简单API,两三行代码就能够从现有图片生成缩略图。支持根据一个目录批量生成缩略图。还支持旋转,水印等
官网学习:http://code.google.com/p/thumbnailator/
最简单的例子:
- //一种更加简单的方式
- public void thumbnail_02() throws IOException{
- Thumbnails.of("mm.JPG")
- .size(160, 160)
- .toFile("mm2.jpg");
- }
一种常用的方式:
- public void thumbnail_01() throws IOException{
- //Thumbnails:This class provides a fluent interface to create thumbnails.
- //调用它的静态方法of(File...files),返回一个Builder<File>
- //Builder<T>是一个静态类,是thumbnail的创建器
- //Builder<T>的size()方法,设置缩放的大小,返回Bulider<T>
- //Builder<T>的toFile(File)方法,Create a thumbnail and writes it to a {@link File}.
- Thumbnails.of(new File("mm.JPG"))
- .size(160, 160)
- .toFile(new File("mm1.jpg"));
- //mm.JPG 大小731*1024
- //讲过上述缩放后为:114*160,默认是按照比例缩放的。
- }
旋转+水印
- public void thumbnail_01() throws IOException{
- //rotate(90),顺时针旋转90度
- //watermark(),添加水印,参数1:位置;参数2:BufferedImage;参数3:透明度
- Thumbnails.of(new File("mm.JPG"))
- .size(1024, 1024)
- .rotate(90)
- .watermark(Positions.BOTTOM_RIGHT, ImageIO.read(new File("watermark.png")), 0.5f)
- .outputQuality(0.8f)
- .toFile(new File("mm3.jpg"));
- }
Create a thumbnail and write to an OutputStream:
- public void thumbnail_01() throws IOException{
- OutputStream os = null;
- Thumbnails.of("mm.JPG")
- .size(200, 200)
- .outputFormat("png")
- .toOutputStream(os);
- }
不按照比例缩放,按照指定的宽度和高度进行缩放:
- //Creating fixed-size thumbnails
- public void test1() throws IOException{
- BufferedImage originalImage = ImageIO.read(new File("mm_copy.JPG"));
- //通过keepAspectRation(false)不按照比例缩放,默认是按照比例缩放的
- BufferedImage thumbnail = Thumbnails.of(originalImage)
- .size(200, 200)
- .keepAspectRatio(false)
- .asBufferedImage();
- ImageIO.write(thumbnail, "jpg", new File("mm4.jpg"));
- }
按照比例进行缩放:
- /**
- * Scaling an p_w_picpath by a given factor
- * @throws IOException
- */
- public void test2() throws IOException{
- BufferedImage originalImage = ImageIO.read(new File("mm_copy.JPG"));
- BufferedImage thumbnail = Thumbnails.of(originalImage)
- .scale(0.25f)
- .asBufferedImage();
- ImageIO.write(thumbnail, "jpg", new File("mm5.jpg"));
- }
旋转:
- /**
- * Rotating an p_w_picpath when creating a thumbnail
- * @throws IOException
- */
- public void test3() throws IOException{
- BufferedImage originalImage = ImageIO.read(new File("mm_copy.JPG"));
- BufferedImage thumbnail = Thumbnails.of(originalImage)
- .size(1024, 1024)
- .rotate(90)
- .asBufferedImage();
- ImageIO.write(thumbnail, "jpg", new File("mm6.jpg"));
- }
水印:
- /**
- * Creating a thumbnail with a watermark
- * @throws IOException
- */
- public void test4() throws IOException{
- BufferedImage originalImage = ImageIO.read(new File("mm.JPG"));
- BufferedImage watermarkImage = ImageIO.read(new File("watermark.png"));
- BufferedImage thumbnail = Thumbnails.of(originalImage)
- .size(1024, 1024)
- .watermark(Positions.TOP_LEFT, watermarkImage, 0.8f)
- .asBufferedImage();
- ImageIO.write(thumbnail, "jpg", new File("mm7.jpg"));
- }
Create all thumbnails for a file directory:
- //toFiles(Rename.xxxx)用来说明命名规则
- // Creates the thumbnails and stores them to the files, using the Rename function to determine the filenames.
- public void test() throws IOException{
- Thumbnails.of(new File("p_w_picpaths").listFiles())
- .size(200, 200)
- .outputFormat("jpg")
- .toFiles(Rename.PREFIX_DOT_THUMBNAIL);
- //命名规则thumbnail.qizhimeinv1.jpg
- //如果上面名字存在,那么就为:thumbnail.thumbnail.qizhimeinv1.jpg
- }
转载于:https://blog.51cto.com/shpshao/846867