java使用Thumbnailator操作图片

来源:互联网 发布:宿州淘宝招聘信息 编辑:程序博客网 时间:2024/05/05 04:41

Thumbnailator 是一个用来生成图像缩略图、裁切、旋转、添加水印等操作的 Java 类库,通过很简单的代码即可生成图片缩略图,也可直接对一整个目录的图片生成缩略图。

Thumbnailator对图片的简单操作代码如下:

[html] view plaincopy
  1. import java.awt.image.BufferedImage;  
  2. import java.io.File;  
  3. import java.io.FileOutputStream;  
  4. import java.io.IOException;  
  5. import java.io.OutputStream;  
  6.   
  7. import javax.imageio.ImageIO;  
  8.   
  9. import net.coobird.thumbnailator.Thumbnails;  
  10. import net.coobird.thumbnailator.geometry.Positions;  
  11.   
  12. /**  
  13.  *   
  14.  * @author yaohucaizi  
  15.  *   
  16.  */  
  17. public class ThumbnailatorTest {  
  18.   
  19.     /**  
  20.      *   
  21.      * @param args  
  22.      * @throws IOException  
  23.      */  
  24.     public static void main(String[] args) throws IOException {  
  25.         ThumbnailatorTest thumbnailatorTest = new ThumbnailatorTest();  
  26.         thumbnailatorTest.test1();  
  27.         thumbnailatorTest.test2();  
  28.         thumbnailatorTest.test3();  
  29.         thumbnailatorTest.test4();  
  30.         thumbnailatorTest.test5();  
  31.         thumbnailatorTest.test6();  
  32.         thumbnailatorTest.test7();  
  33.         thumbnailatorTest.test8();  
  34.         thumbnailatorTest.test9();  
  35.     }  
  36.   
  37.     /**  
  38.      * 指定大小进行缩放  
  39.      *   
  40.      * @throws IOException  
  41.      */  
  42.     private void test1() throws IOException {  
  43.         /*  
  44.          * size(width,height) 若图片横比200小,高比300小,不变  
  45.          * 若图片横比200小,高比300大,高缩小到300,图片比例不变 若图片横比200大,高比300小,横缩小到200,图片比例不变  
  46.          * 若图片横比200大,高比300大,图片按比例缩小,横为200或高为300  
  47.          */  
  48.         Thumbnails.of("images/test.jpg").size(200, 300).toFile(  
  49.                 "C:/image_200x300.jpg");  
  50.         Thumbnails.of("images/test.jpg").size(2560, 2048).toFile(  
  51.                 "C:/image_2560x2048.jpg");  
  52.     }  
  53.   
  54.     /**  
  55.      * 按照比例进行缩放  
  56.      *   
  57.      * @throws IOException  
  58.      */  
  59.     private void test2() throws IOException {  
  60.         /**  
  61.          * scale(比例)  
  62.          */  
  63.         Thumbnails.of("images/test.jpg").scale(0.25f)  
  64.                 .toFile("C:/image_25%.jpg");  
  65.         Thumbnails.of("images/test.jpg").scale(1.10f).toFile(  
  66.                 "C:/image_110%.jpg");  
  67.     }  
  68.   
  69.     /**  
  70.      * 不按照比例,指定大小进行缩放  
  71.      *   
  72.      * @throws IOException  
  73.      */  
  74.     private void test3() throws IOException {  
  75.         /**  
  76.          * keepAspectRatio(false) 默认是按照比例缩放的  
  77.          */  
  78.         Thumbnails.of("images/test.jpg").size(120, 120).keepAspectRatio(false)  
  79.                 .toFile("C:/image_120x120.jpg");  
  80.     }  
  81.   
  82.     /**  
  83.      * 旋转  
  84.      *   
  85.      * @throws IOException  
  86.      */  
  87.     private void test4() throws IOException {  
  88.         /**  
  89.          * rotate(角度),正数:顺时针 负数:逆时针  
  90.          */  
  91.         Thumbnails.of("images/test.jpg").size(1280, 1024).rotate(90).toFile(  
  92.                 "C:/image+90.jpg");  
  93.         Thumbnails.of("images/test.jpg").size(1280, 1024).rotate(-90).toFile(  
  94.                 "C:/iamge-90.jpg");  
  95.     }  
  96.   
  97.     /**  
  98.      * 水印  
  99.      *   
  100.      * @throws IOException  
  101.      */  
  102.     private void test5() throws IOException {  
  103.         /**  
  104.          * watermark(位置,水印图,透明度)  
  105.          */  
  106.         Thumbnails.of("images/test.jpg").size(1280, 1024).watermark(  
  107.                 Positions.BOTTOM_RIGHT,  
  108.                 ImageIO.read(new File("images/watermark.png")), 0.5f)  
  109.                 .outputQuality(0.8f).toFile(  
  110.                         "C:/image_watermark_bottom_right.jpg");  
  111.         Thumbnails.of("images/test.jpg").size(1280, 1024).watermark(  
  112.                 Positions.CENTER,  
  113.                 ImageIO.read(new File("images/watermark.png")), 0.5f)  
  114.                 .outputQuality(0.8f).toFile("C:/image_watermark_center.jpg");  
  115.     }  
  116.   
  117.     /**  
  118.      * 裁剪  
  119.      *   
  120.      * @throws IOException  
  121.      */  
  122.     private void test6() throws IOException {  
  123.         /**  
  124.          * 图片中心400*400的区域  
  125.          */  
  126.         Thumbnails.of("images/test.jpg").sourceRegion(Positions.CENTER, 400,  
  127.                 400).size(200, 200).keepAspectRatio(false).toFile(  
  128.                 "C:/image_region_center.jpg");  
  129.         /**  
  130.          * 图片右下400*400的区域  
  131.          */  
  132.         Thumbnails.of("images/test.jpg").sourceRegion(Positions.BOTTOM_RIGHT,  
  133.                 400, 400).size(200, 200).keepAspectRatio(false).toFile(  
  134.                 "C:/image_region_bootom_right.jpg");  
  135.         /**  
  136.          * 指定坐标  
  137.          */  
  138.         Thumbnails.of("images/test.jpg").sourceRegion(600, 500, 400, 400).size(  
  139.                 200, 200).keepAspectRatio(false).toFile(  
  140.                 "C:/image_region_coord.jpg");  
  141.     }  
  142.   
  143.     /**  
  144.      * 转化图像格式  
  145.      *   
  146.      * @throws IOException  
  147.      */  
  148.     private void test7() throws IOException {  
  149.         /**  
  150.          * outputFormat(图像格式)  
  151.          */  
  152.         Thumbnails.of("images/test.jpg").size(1280, 1024).outputFormat("png")  
  153.                 .toFile("C:/image_1280x1024.png");  
  154.         Thumbnails.of("images/test.jpg").size(1280, 1024).outputFormat("gif")  
  155.                 .toFile("C:/image_1280x1024.gif");  
  156.     }  
  157.   
  158.     /**  
  159.      * 输出到OutputStream  
  160.      *   
  161.      * @throws IOException  
  162.      */  
  163.     private void test8() throws IOException {  
  164.         /**  
  165.          * toOutputStream(流对象)  
  166.          */  
  167.         OutputStream os = new FileOutputStream(  
  168.                 "C:/image_1280x1024_OutputStream.png");  
  169.         Thumbnails.of("images/test.jpg").size(1280, 1024).toOutputStream(os);  
  170.     }  
  171.   
  172.     /**  
  173.      * 输出到BufferedImage  
  174.      *   
  175.      * @throws IOException  
  176.      */  
  177.     private void test9() throws IOException {  
  178.         /**  
  179.          * asBufferedImage() 返回BufferedImage  
  180.          */  
  181.         BufferedImage thumbnail = Thumbnails.of("images/test.jpg").size(1280,  
  182.                 1024).asBufferedImage();  
  183.         ImageIO.write(thumbnail, "jpg", new File(  
  184.                 "C:/image_1280x1024_BufferedImage.jpg"));  
  185.     }  
  186. }  

0 0
原创粉丝点击