Thumbnailator是一个Java缩略图生成类库。利用其提供的简单API,两三行代码就能够从现有图片生成缩略图。支持根据一个目录批量生成缩略图。还支持旋转,水印等

官网学习:http://code.google.com/p/thumbnailator/

最简单的例子:

  1. //一种更加简单的方式 
  2.     public void thumbnail_02() throws IOException{ 
  3.         Thumbnails.of("mm.JPG"
  4.                   .size(160160
  5.                   .toFile("mm2.jpg"); 
  6.     } 

一种常用的方式:

  1. public void thumbnail_01() throws IOException{ 
  2.         //Thumbnails:This class provides a fluent interface to create thumbnails. 
  3.         //调用它的静态方法of(File...files),返回一个Builder<File> 
  4.         //Builder<T>是一个静态类,是thumbnail的创建器 
  5.          
  6.         //Builder<T>的size()方法,设置缩放的大小,返回Bulider<T> 
  7.         //Builder<T>的toFile(File)方法,Create a thumbnail and writes it to a {@link File}. 
  8.         Thumbnails.of(new File("mm.JPG")) 
  9.                   .size(160160
  10.                   .toFile(new File("mm1.jpg")); 
  11.         //mm.JPG 大小731*1024  
  12.         //讲过上述缩放后为:114*160,默认是按照比例缩放的。 
  13.     } 

旋转+水印

  1. public void thumbnail_01() throws IOException{ 
  2.         //rotate(90),顺时针旋转90度 
  3.         //watermark(),添加水印,参数1:位置;参数2:BufferedImage;参数3:透明度 
  4.         Thumbnails.of(new File("mm.JPG")) 
  5.                   .size(10241024
  6.                   .rotate(90
  7.                   .watermark(Positions.BOTTOM_RIGHT, ImageIO.read(new File("watermark.png")), 0.5f) 
  8.                   .outputQuality(0.8f) 
  9.                   .toFile(new File("mm3.jpg")); 
  10.     } 

Create a thumbnail and write to an OutputStream:

  1. public void thumbnail_01() throws IOException{ 
  2.         OutputStream os = null
  3.          
  4.         Thumbnails.of("mm.JPG"
  5.                 .size(200200
  6.                 .outputFormat("png"
  7.                 .toOutputStream(os); 
  8.          
  9.     } 

不按照比例缩放,按照指定的宽度和高度进行缩放:

  1. //Creating fixed-size thumbnails 
  2.     public void test1() throws IOException{ 
  3.          
  4.         BufferedImage originalImage = ImageIO.read(new File("mm_copy.JPG")); 
  5.  
  6.         //通过keepAspectRation(false)不按照比例缩放,默认是按照比例缩放的 
  7.         BufferedImage thumbnail = Thumbnails.of(originalImage) 
  8.                 .size(200200
  9.                 .keepAspectRatio(false
  10.                 .asBufferedImage(); 
  11.          
  12.         ImageIO.write(thumbnail, "jpg"new File("mm4.jpg")); 
  13.          
  14.     } 

按照比例进行缩放:

  1. /** 
  2.      * Scaling an image by a given factor 
  3.      * @throws IOException 
  4.      */ 
  5.     public void test2() throws IOException{ 
  6.         BufferedImage originalImage = ImageIO.read(new File("mm_copy.JPG")); 
  7.  
  8.         BufferedImage thumbnail = Thumbnails.of(originalImage) 
  9.                 .scale(0.25f) 
  10.                 .asBufferedImage(); 
  11.          
  12.         ImageIO.write(thumbnail, "jpg"new File("mm5.jpg")); 
  13.     } 

旋转:

  1. /** 
  2.      * Rotating an image when creating a thumbnail 
  3.      * @throws IOException 
  4.      */ 
  5.     public void test3() throws IOException{ 
  6.         BufferedImage originalImage = ImageIO.read(new File("mm_copy.JPG")); 
  7.  
  8.         BufferedImage thumbnail = Thumbnails.of(originalImage) 
  9.                 .size(10241024
  10.                 .rotate(90
  11.                 .asBufferedImage(); 
  12.         ImageIO.write(thumbnail, "jpg"new File("mm6.jpg")); 
  13.     } 

水印:

  1. /** 
  2.      * Creating a thumbnail with a watermark 
  3.      * @throws IOException 
  4.      */ 
  5.     public void test4() throws IOException{ 
  6.         BufferedImage originalImage = ImageIO.read(new File("mm.JPG")); 
  7.         BufferedImage watermarkImage = ImageIO.read(new File("watermark.png")); 
  8.  
  9.         BufferedImage thumbnail = Thumbnails.of(originalImage) 
  10.                 .size(10241024
  11.                 .watermark(Positions.TOP_LEFT, watermarkImage, 0.8f) 
  12.                 .asBufferedImage(); 
  13.         ImageIO.write(thumbnail, "jpg"new File("mm7.jpg")); 
  14.     } 

Create all thumbnails for a file directory:

  1. //toFiles(Rename.xxxx)用来说明命名规则 
  2.     // Creates the thumbnails and stores them to the files, using the Rename function to determine the filenames. 
  3.     public void test() throws IOException{ 
  4.         Thumbnails.of(new File("images").listFiles()) 
  5.         .size(200200
  6.         .outputFormat("jpg"
  7.         .toFiles(Rename.PREFIX_DOT_THUMBNAIL); 
  8.          
  9.         //命名规则thumbnail.qizhimeinv1.jpg 
  10.         //如果上面名字存在,那么就为:thumbnail.thumbnail.qizhimeinv1.jpg 
  11.          
  12.     }