Thumbnailator--google图片处理项目

来源:互联网 发布:mynba2k18网络维护中 编辑:程序博客网 时间:2024/06/06 19:24

Thumbnailator是一个非常好的图片开源工具,可以很好的完成图片处理。

从API提供现有的图像文件和图像对象的缩略图中简化了缩略过程,两三行代码就能够从现有图片生成缩略图,且允许微调缩略图生成,同时保持了需要写入到最低限度的代码量。同时还支持根据一个目录批量生成缩略图。

Maven中pom.xml 配置:

[html] view plain copy
 print?
  1. <dependency>  
  2.     <groupId>net.coobird</groupId>  
  3.     <artifactId>thumbnailator</artifactId>  
  4.     <version>0.4.8</version>  
  5. </dependency>  

以下是包装类的代码:

[java] view plain copy
 print?
  1. package com.sun4j.app.util;  
  2.   
  3. import java.awt.image.BufferedImage;  
  4. import java.io.File;  
  5. import java.io.FileOutputStream;  
  6. import java.io.IOException;  
  7. import java.io.OutputStream;  
  8.   
  9. import javax.imageio.ImageIO;  
  10.   
  11. import net.coobird.thumbnailator.Thumbnails;  
  12. import net.coobird.thumbnailator.geometry.Position;  
  13.   
  14. public class ThumbnailatorUtils {  
  15.   
  16.     /** 
  17.      * 指定大小进行缩放  
  18.      * 若图片横比width小,高比height小,不变 若图片横比width小,高比height大,高缩小到height,图片比例不变 
  19.      * 若图片横比width大,高比height小,横缩小到width,图片比例不变 
  20.      * 若图片横比width大,高比height大,图片按比例缩小,横为width或高为height 
  21.      *  
  22.      * @param source 
  23.      *            输入源(指定路径)
  24.      * @param output 
  25.      *            输出源 
  26.      * @param width 
  27.      *            宽 
  28.      * @param height 
  29.      *            高 
  30.      */  
  31.     public static void ImgThumb(String source, String output, int width, int height) {  
  32.         try {  
  33.             Thumbnails.of(source).size(width, height).toFile(output);  
  34.         } catch (IOException e) {  
  35.             e.printStackTrace();  
  36.         }  
  37.     }  
  38.   
  39.     /** 
  40.      * 指定大小进行缩放 
  41.      *  
  42.      * @param source 
  43.      *            输入源(指定文件)
  44.      * @param output 
  45.      *            输出源 
  46.      * @param width 
  47.      *            宽 
  48.      * @param height 
  49.      *            高 
  50.      */  
  51.     public static void ImgThumb(File source, String output, int width, int height) {  
  52.         try {  
  53.             Thumbnails.of(source).size(width, height).toFile(output);  
  54.         } catch (IOException e) {  
  55.             e.printStackTrace();  
  56.         }  
  57.     }  
  58.   
  59.     /** 
  60.      * 按照比例进行缩放 
  61.      *  
  62.      * @param source 
  63.      *            输入源(支持String(文件地址) file(文件))
  64.      * @param output 
  65.      *            输出源 
  66.      * @param scale
  67.      *            比例(大于零时为扩大,小于零时为缩小)
  68.      */  
  69.     public static void ImgScale(String source, String output, double scale) {  
  70.         try {  
  71.             Thumbnails.of(source).scale(scale).toFile(output);  
  72.         } catch (IOException e) {  
  73.             e.printStackTrace();  
  74.         }  
  75.     }  
  76.   
  77.     public static void ImgScale(File source, String output, double scale) {  
  78.         try {  
  79.             Thumbnails.of(source).scale(scale).toFile(output);  
  80.         } catch (IOException e) {  
  81.             e.printStackTrace();  
  82.         }  
  83.     }  
  84.   
  85.     /** 
  86.      * 不按照比例,指定大小进行缩放 
  87.      *  
  88.      * @param source 
  89.      *            输入源 
  90.      * @param output 
  91.      *            输出源 
  92.      * @param width 
  93.      *            宽 
  94.      * @param height 
  95.      *            高 
  96.      * @param keepAspectRatio 
  97.      *            默认是按照比例缩放的,值为false 时不按比例缩放 
  98.      */  
  99.     public static void ImgNoScale(String source, String output, int width, int height, boolean keepAspectRatio) {  
  100.         try {  
  101.             Thumbnails.of(source).size(width, height).keepAspectRatio(keepAspectRatio).toFile(output);  
  102.         } catch (IOException e) {  
  103.             e.printStackTrace();  
  104.         }  
  105.     }  
  106.   
  107.     public static void ImgNoScale(File source, String output, int width, int height, boolean keepAspectRatio) {  
  108.         try {  
  109.             Thumbnails.of(source).size(width, height).keepAspectRatio(keepAspectRatio).toFile(output);  
  110.         } catch (IOException e) {  
  111.             e.printStackTrace();  
  112.         }  
  113.     }  
  114.   
  115.     /** 
  116.      * 旋转 ,正数:顺时针 负数:逆时针 
  117.      *  
  118.      * @param source 
  119.      *            输入源 
  120.      * @param output 
  121.      *            输出源 
  122.      * @param width 
  123.      *            宽 
  124.      * @param height 
  125.      *            高 
  126.      * @param rotate 
  127.      *            角度(旋转 ,正数:顺时针 负数:逆时针) 
  128.      */  
  129.     public static void ImgRotate(String source, String output, int width, int height, double rotate) {  
  130.         try {  
  131.             Thumbnails.of(source).size(width, height).rotate(rotate).toFile(output);  
  132.         } catch (IOException e) {  
  133.             e.printStackTrace();  
  134.         }  
  135.     }  
  136.   
  137.     public static void ImgRotate(File source, String output, int width, int height, double rotate) {  
  138.         try {  
  139.             Thumbnails.of(source).size(width, height).rotate(rotate).toFile(output);  
  140.         } catch (IOException e) {  
  141.             e.printStackTrace();  
  142.         }  
  143.     }  
  144.   
  145.     /** 
  146.      * 水印 
  147.      *  
  148.      * @param source 
  149.      *            输入源 
  150.      * @param output 
  151.      *            输入源 
  152.      * @param width 
  153.      *            宽 
  154.      * @param height 
  155.      *            高 
  156.      * @param position 
  157.      *            水印位置 Positions.BOTTOM_RIGHT o.5f 
  158.      * @param watermark 
  159.      *            水印图片地址 
  160.      * @param transparency 
  161.      *            透明度 0.5f 
  162.      * @param quality 
  163.      *            图片质量 0.8f 
  164.      */  
  165.     public static void ImgWatermark(String source, String output, int width, int height, Position position, String watermark, float transparency, float quality) {  
  166.         try {  
  167.             Thumbnails.of(source).size(width, height).watermark(position, ImageIO.read(new File(watermark)), transparency).outputQuality(0.8f).toFile(output);  
  168.         } catch (IOException e) {  
  169.             e.printStackTrace();  
  170.         }  
  171.     }  
  172.   
  173.     public static void ImgWatermark(File source, String output, int width, int height, Position position, String watermark, float transparency, float quality) {  
  174.         try {  
  175.             Thumbnails.of(source).size(width, height).watermark(position, ImageIO.read(new File(watermark)), transparency).outputQuality(0.8f).toFile(output);  
  176.         } catch (IOException e) {  
  177.             e.printStackTrace();  
  178.         }  
  179.     }  
  180.   
  181.     /** 
  182.      * 裁剪图片 
  183.      *  
  184.      * @param source 
  185.      *            输入源 
  186.      * @param output 
  187.      *            输出源 
  188.      * @param position 
  189.      *            裁剪位置 
  190.      * @param x 
  191.      *            裁剪区域x 
  192.      * @param y 
  193.      *            裁剪区域y 
  194.      * @param width 
  195.      *            宽 
  196.      * @param height 
  197.      *            高 
  198.      * @param keepAspectRatio 
  199.      *            默认是按照比例缩放的,值为false 时不按比例缩放 
  200.      */  
  201.     public static void ImgSourceRegion(String source, String output, Position position, int x, int y, int width, int height, boolean keepAspectRatio) {  
  202.         try {  
  203.             Thumbnails.of(source).sourceRegion(position, x, y).size(width, height).keepAspectRatio(keepAspectRatio).toFile(output);  
  204.         } catch (IOException e) {  
  205.             e.printStackTrace();  
  206.         }  
  207.     }  
  208.   
  209.     public static void ImgSourceRegion(File source, String output, Position position, int x, int y, int width, int height, boolean keepAspectRatio) {  
  210.         try {  
  211.             Thumbnails.of(source).sourceRegion(position, x, y).size(width, height).keepAspectRatio(keepAspectRatio).toFile(output);  
  212.         } catch (IOException e) {  
  213.             e.printStackTrace();  
  214.         }  
  215.     }  
  216.   
  217.     /** 
  218.      * 按坐标裁剪 
  219.      *  
  220.      * @param source 
  221.      *            输入源 
  222.      * @param output 
  223.      *            输出源 
  224.      * @param x 
  225.      *            起始x坐标 
  226.      * @param y 
  227.      *            起始y坐标 
  228.      * @param x1 
  229.      *            结束x坐标 
  230.      * @param y1 
  231.      *            结束y坐标 
  232.      * @param width 
  233.      *            宽 
  234.      * @param height 
  235.      *            高 
  236.      * @param keepAspectRatio 
  237.      *            默认是按照比例缩放的,值为false 时不按比例缩放 
  238.      */  
  239.     public static void ImgSourceRegion(String source, String output, int x, int y, int x1, int y1, int width, int height, boolean keepAspectRatio) {  
  240.         try {  
  241.             Thumbnails.of(source).sourceRegion(x, y, x1, y1).size(width, height).keepAspectRatio(keepAspectRatio).toFile(output);  
  242.         } catch (IOException e) {  
  243.             e.printStackTrace();  
  244.         }  
  245.     }  
  246.   
  247.     public static void ImgSourceRegion(File source, String output, int x, int y, int x1, int y1, int width, int height, boolean keepAspectRatio) {  
  248.         try {  
  249.             Thumbnails.of(source).sourceRegion(x, y, x1, y1).size(width, height).keepAspectRatio(keepAspectRatio).toFile(output);  
  250.         } catch (IOException e) {  
  251.             e.printStackTrace();  
  252.         }  
  253.     }  
  254.   
  255.     /** 
  256.      * 转化图像格式 
  257.      *  
  258.      * @param source 
  259.      *            输入源 
  260.      * @param output 
  261.      *            输出源 
  262.      * @param width 
  263.      *            宽 
  264.      * @param height 
  265.      *            高 
  266.      * @param format 
  267.      *            图片类型,gif、png、jpg 
  268.      */  
  269.     public static void ImgFormat(String source, String output, int width, int height, String format) {  
  270.         try {  
  271.             Thumbnails.of(source).size(width, height).outputFormat(format).toFile(output);  
  272.         } catch (IOException e) {  
  273.             e.printStackTrace();  
  274.         }  
  275.     }  
  276.   
  277.     public static void ImgFormat(File source, String output, int width, int height, String format) {  
  278.         try {  
  279.             Thumbnails.of(source).size(width, height).outputFormat(format).toFile(output);  
  280.         } catch (IOException e) {  
  281.             e.printStackTrace();  
  282.         }  
  283.     }  
  284.   
  285.     /** 
  286.      * 输出到OutputStream 
  287.      *  
  288.      * @param source 
  289.      *            输入源 
  290.      * @param output 
  291.      *            输出源 
  292.      * @param width 
  293.      *            宽 
  294.      * @param height 
  295.      *            高 
  296.      * @return toOutputStream(流对象) 
  297.      */  
  298.     public static OutputStream ImgOutputStream(String source, String output, int width, int height) {  
  299.         OutputStream os = null;  
  300.         try {  
  301.             os = new FileOutputStream(output);  
  302.             Thumbnails.of(source).size(width, height).toOutputStream(os);  
  303.         } catch (IOException e) {  
  304.             e.printStackTrace();  
  305.         }  
  306.         return os;  
  307.     }  
  308.   
  309.     public static OutputStream ImgOutputStream(File source, String output, int width, int height) {  
  310.         OutputStream os = null;  
  311.         try {  
  312.             os = new FileOutputStream(output);  
  313.             Thumbnails.of(source).size(width, height).toOutputStream(os);  
  314.         } catch (IOException e) {  
  315.             e.printStackTrace();  
  316.         }  
  317.         return os;  
  318.     }  
  319.   
  320.     /** 
  321.      * 输出到BufferedImage 
  322.      *  
  323.      * @param source 
  324.      *            输入源 
  325.      * @param output 
  326.      *            输出源 
  327.      * @param width 
  328.      *            宽 
  329.      * @param height 
  330.      *            高 
  331.      * @param format 
  332.      *            图片类型,gif、png、jpg 
  333.      * @return BufferedImage 
  334.      */  
  335.     public static BufferedImage ImgBufferedImage(String source, String output, int width, int height, String format) {  
  336.         BufferedImage buf = null;  
  337.         try {  
  338.             buf = Thumbnails.of(source).size(width, height).asBufferedImage();  
  339.             ImageIO.write(buf, format, new File(output));  
  340.         } catch (IOException e) {  
  341.             e.printStackTrace();  
  342.         }  
  343.         return buf;  
  344.     }  
  345.   
  346.     public static BufferedImage ImgBufferedImage(File source, String output, int width, int height, String format) {  
  347.         BufferedImage buf = null;  
  348.         try {  
  349.             buf = Thumbnails.of(source).size(width, height).asBufferedImage();  
  350.             ImageIO.write(buf, format, new File(output));  
  351.         } catch (IOException e) {  
  352.             e.printStackTrace();  
  353.         }  
  354.         return buf;  
  355.     }  
  356. }