分享

Google提供的Thumbnails对图片的各种操作(缩放、加水印、翻转、转换图片格式)性能比jdk提供的好N倍,压缩出来的图片更清晰

 藏经阁_苍穹 2016-07-28
  1. <span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">jar下载:</span><span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">http://code.google.com/p/thumbnailator/</span>  
  1. <span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">版本:thumbnailator-0.4.8.jar</span>  


  1. package com.lvmama.comm.utils.pic;  
  1. import java.awt.Image;  
  2. import java.io.File;  
  3. import java.io.IOException;  
  4.   
  5. import javax.imageio.ImageIO;  
  6.   
  7. import net.coobird.thumbnailator.Thumbnails;  
  8. import net.coobird.thumbnailator.geometry.Positions;  
  9.   
  10. @SuppressWarnings("restriction")  
  11. public class ImageUtil2 {  
  12.       
  13.     /** 
  14.      * 缩放图像(按高度和宽度缩放) 
  15.      * @param srcImageFile 源图像文件地址 
  16.      * @param result 缩放后的图像地址 
  17.      * @param height 缩放后的高度 
  18.      * @param width 缩放后的宽度 
  19.      * @throws IOException  
  20.      * @isPHP_PC php的pc端调用 
  21.      * @isPHP_MOBILE php的移动无线端调用 
  22.      */  
  23.     public final static void scale(String srcImageFile, String newImageFile, int width,int height, boolean isPHP_PC,boolean isPHP_MOBILE) throws IOException {  
  24.         Image img = ImageIO.read(new File(srcImageFile));  
  25.         int newWidth = img.getWidth(null),newHeight = img.getHeight(null);  
  26.           
  27.         // 计算比例  
  28.         if(isPHP_MOBILE && !isPHP_PC && img.getWidth(null) <= width){  
  29.             //无线调用时,宽度没有压缩宽大,则返回原图  
  30.         }else if ((img.getHeight(null) > height) || (img.getWidth(null) > width)) {  
  31.               
  32.             if(isPHP_PC && !isPHP_MOBILE){//PHP的微游记PC端缩略图以定宽或定高(以较长的一边)为基准  
  33.                 if(img.getWidth(null) > img.getHeight(null)){  
  34.                     newWidth = width;  
  35.                     newHeight = (int)(new Integer(width).doubleValue()/img.getWidth(null)*img.getHeight(null));  
  36.                 }else{  
  37.                     newHeight = height;  
  38.                     newWidth = (int)(new Integer(height).doubleValue()/img.getHeight(null)*img.getWidth(null));  
  39.                 }  
  40.                   
  41.             }else if(isPHP_MOBILE && !isPHP_PC){//PHP的微游记(移动)端缩略图以定宽为基准  
  42.                 newWidth = width;  
  43.                 newHeight = (int)(new Integer(width).doubleValue()/img.getWidth(null)*img.getHeight(null));  
  44.                   
  45.             }else {//vst端:原图长/压缩长 > 原图宽/压缩图宽   ? 原图长/压缩长:原图宽/压缩图宽,以比例较大的为基准压缩  
  46.                 // 压缩比判断方法二  
  47.                 double ratio_height = (new Integer(height)).doubleValue()/ img.getHeight(null);  
  48.                 double ratio_width = (new Integer(width)).doubleValue() / img.getWidth(null);  
  49.                   
  50.                 if(ratio_height>ratio_width){  
  51.                     newHeight = height;  
  52.                     newWidth = (int)((new Integer(height)).doubleValue()/ img.getHeight(null) * img.getWidth(null));  
  53.                 }else{  
  54.                     newWidth = width;  
  55.                     newHeight = (int)((new Integer(width)).doubleValue() / img.getWidth(null) * img.getHeight(null));  
  56.                 }  
  57.             }  
  58.         }  
  59.           
  60.         Thumbnails.of(srcImageFile)  
  61.         .size(newWidth, newHeight)  
  62.         .toFile(newImageFile);  
  63.           
  64.     }  
  65.       
  66.     /** 
  67.      * 添加图片水印 
  68.      *  
  69.      * @param targetImg 
  70.      *            目标图片路径,如:C:\\myPictrue\\1.jpg 
  71.      * @param waterImg 
  72.      *            水印图片路径,如:C:\\myPictrue\\logo.png 
  73.      * @param positions 水印图片所在位置 
  74.      *  
  75.      * @param alpha 
  76.      *            透明度(0.0 -- 1.0, 0.0为完全透明,1.0为完全不透明) * @param quality 压缩清晰度 
  77.      *            <b>建议为1.0</b> 
  78.      * @throws IOException  
  79.      */  
  80.     public final static void pressImage(String targetImg, File waterImg, Positions positions, float alpha) throws IOException {  
  81.         Thumbnails.of(targetImg)  
  82.         .watermark(positions, ImageIO.read(waterImg), alpha)  
  83.         .scale(1)//缩放比例  
  84.         .toFile(targetImg);  
  85.     }  
  86.   
  87.     /** 
  88.      * 把图片印刷到图片上 
  89.      *  
  90.      * @param pressImg 
  91.      *            -- 水印文件 
  92.      * @param targetImg 
  93.      *            -- 目标文件 
  94.      * @param alpha 
  95.      *            透明度(0.0 -- 1.0, 0.0为完全透明,1.0为完全不透明) * @param quality 压缩清晰度 
  96.      *            <b>建议为1.0</b> 
  97.      *             
  98.      * @throws IOException  
  99.      */  
  100.     public final static void pressImage(String targetImg, String pressImg,float alpha) throws IOException {  
  101.         Thumbnails.of(targetImg)  
  102.         .watermark(Positions.TOP_LEFT, ImageIO.read(new File(pressImg)), alpha)  
  103.         .outputQuality(1)//生成质量100%  
  104.         .scale(1)//缩放比例  
  105.         .toFile(targetImg);  
  106.     }  
  107.   
  108.     /** 
  109.      * 把图片印刷到图片上 
  110.      *  
  111.      * @param pressImg 
  112.      *            -- 水印文件 
  113.      * @param targetImg 
  114.      *            -- 目标文件 
  115.      * @param position 
  116.      *  
  117.       @param alpha 
  118.      *            透明度(0.0 -- 1.0, 0.0为完全透明,1.0为完全不透明) * @param quality 压缩清晰度 
  119.      *            <b>建议为1.0</b> 
  120.      *  
  121.      * @throws IOException  
  122.      */  
  123.     public final static void pressImage(String targetImg, String pressImg, Positions position,float alpha) throws IOException {  
  124.         Thumbnails.of(targetImg)  
  125.         .watermark(position, ImageIO.read(new File(pressImg)), alpha)  
  126.         .outputQuality(1)//生成质量100%  
  127.         .scale(1)//缩放比例  
  128.         .toFile(targetImg);  
  129.     }  
  130.   
  131.     /** 
  132.      * 裁剪图片 
  133.      * @throws IOException 
  134.      */  
  135.     public static void region(String sourceImg,String newImgPath,int x,int y,int width,int height) throws IOException{  
  136.         //指定坐标    
  137.         Thumbnails.of(sourceImg)    
  138.         .sourceRegion(x, y, width, height)//x轴、y轴,裁剪宽、裁剪高  
  139.         .size(width, height)//裁剪后的图片生成的尺寸  
  140.         //设置是否按比例  false,图片可能会走形 默认true,必须在设置尺寸后设置  
  141.         .keepAspectRatio(false)   
  142.         .toFile(newImgPath);  
  143.     }  
  144.       
  145.     /** 
  146.      * 旋转图片 
  147.      * @param sourceImage 原图片 
  148.      * @param newImage 生成的新图片 
  149.      * @param degrees 旋转度数 
  150.      * @throws IOException 
  151.      */  
  152.     public static void rorate(String sourceImage,String newImage,double degrees) throws IOException{  
  153.         Thumbnails.of(sourceImage)  
  154.         .rotate(degrees)//旋转度数  
  155.         .scale(1)//缩放比例  
  156.         .toFile(newImage);  
  157.     }  
  158.       
  159.     /** 
  160.      * 转换图片格式 
  161.      * @param sourceImg 原图  
  162.      * @param newImg    转换后的新图 
  163.      * @param format    格式 
  164.      * @throws IOException 
  165.      */  
  166.     public static void transferImageFormat(String sourceImg,String newImg,String format) throws IOException{  
  167.         Thumbnails.of(sourceImg)  
  168.         .outputFormat(format)  
  169.         .scale(1)  
  170.         .toFile(newImg);  
  171.     }  
  172.   
  173.     public static void main(String[] args) {  
  174.         try {  
  175.             //ImageUtil2.scale("C:\\Users\\chengjiangbo\\Desktop\\images\\c743d228-3ecf-4711-9187-725f252d14b1.jpg", "C:\\Users\\chengjiangbo\\Desktop\\images\\480_300.jpg", 480, 300, true, false);  
  176.             //ImageUtil2.pressImage("C:\\Users\\chengjiangbo\\Desktop\\images\\IMG_waterImage.jpg", "C:\\Users\\chengjiangbo\\Desktop\\images\\QRCode.png",Positions.BOTTOM_CENTER, 1);  
  177.             //ImageUtil2.region("C:\\Users\\chengjiangbo\\Desktop\\images\\IMG_waterImage.jpg", "C:\\Users\\chengjiangbo\\Desktop\\images\\IMG_waterImage123.jpg",0,0,100,100);  
  178.             //ImageUtil2.rorate("C:\\Users\\chengjiangbo\\Desktop\\images\\IMG_waterImage.jpg", "C:\\Users\\chengjiangbo\\Desktop\\images\\IMG_waterImage111.jpg", 90);  
  179.             ImageUtil2.transferImageFormat("C:\\Users\\chengjiangbo\\Desktop\\images\\QRCode.png""C:\\Users\\chengjiangbo\\Desktop\\images\\QRCode.jpg""JPEG");  
  180.         } catch (IOException e) {  
  181.             e.printStackTrace();  
  182.         }  
  183.     }  
  184. }  


    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约