使用Jmagick压缩高质量图片

jar包地址  http://downloads.jmagick.org/6.3.9/      

 

我在window下使用的是    jmagick-win-6.3.9-Q8.zip 和  ImageMagick-6.3.9-0-Q8-windows-dll.exe

      linux下使用的是         JMagick-6.2.6-0.tar.gz    和 ImageMagick-x86_64-pc-linux-gnu.tar.gz             

 

 

doc地址   http://downloads.jmagick.org/jmagick-doc/

 

windows

一 .  安装 ImageMagick-6.3.9-0-Q8-windows-dll.exe

 

二 .  将 安装目录下 “C:/Program Files/ImageMagick-6.3.9-Q8/ ”(按自己所安装的目录找) 下的所有dll文件 copy 到系统盘下的 “C:/WINDOWS/system32/”文件夹里

 

三 .  配置环境变量

     再环境变量path里添加新的值 “C:/Program Files/ImageMagick-6.3.9-Q8”

 

四 .  解压jmagick-win-6.3.9-Q8.zip 

       将 jmagick.dll 复制到系统盘下的 “C:/WINDOWS/system32/”文件夹里 和 复制到jdk的bin(例“E:/Java/jdk1.5.0_10/bin”)文件里各一份 

      将 jmagick.jar 复制到Tomcat下的lib文件夹里 和 所使用项目的WEB-INF下lib文件里 各一份

 

linux

  朋友配的 得自己去网上找资料 :(

 

 

 

 

操作范例

  1. package com.utils;  
  2.   
  3. import java.awt.Dimension;  
  4. import java.awt.Rectangle;  
  5. import java.text.SimpleDateFormat;  
  6. import java.util.Date;  
  7.   
  8.   
  9.   
  10. import magick.CompositeOperator;  
  11. import magick.CompressionType;  
  12. import magick.DrawInfo;  
  13. import magick.ImageInfo;  
  14. import magick.MagickException;  
  15. import magick.MagickImage;  
  16. import magick.PixelPacket;  
  17. import magick.PreviewType;  
  18.   
  19. public class Aa {  
  20.   
  21.       
  22.     static{  
  23.         //不能漏掉这个,不然jmagick.jar的路径找不到  
  24.         System.setProperty("jmagick.systemclassloader","no");  
  25.     }  
  26.       
  27.     /** 
  28.      * 压缩图片 
  29.      * @param filePath 源文件路径 
  30.      * @param toPath   缩略图路径 
  31.      */  
  32.     public static void createThumbnail(String filePath, String toPath) throws MagickException{  
  33.         ImageInfo info = null;  
  34.         MagickImage image = null;  
  35.         Dimension imageDim = null;  
  36.         MagickImage scaled = null;  
  37.         try{  
  38.             info = new ImageInfo(filePath);  
  39.             image = new MagickImage(info);  
  40.             imageDim = image.getDimension();  
  41.             int wideth = imageDim.width;  
  42.             int height = imageDim.height;  
  43.             if (wideth > height) {  
  44.                 height = 660 * height / wideth;  
  45.                 wideth = 660;  
  46.             }   
  47.             scaled = image.scaleImage(wideth, height);//小图片文件的大小.  
  48.             scaled.setFileName(toPath);  
  49.             scaled.writeImage(info);  
  50.         }finally{  
  51.             if(scaled != null){  
  52.                 scaled.destroyImages();  
  53.             }  
  54.         }    
  55.     }  
  56.       
  57.     /** 
  58.      * 水印(图片logo) 
  59.      * @param filePath  源文件路径 
  60.      * @param toImg     修改图路径 
  61.      * @param logoPath  logo图路径 
  62.      * @throws MagickException 
  63.      */  
  64.     public static void initLogoImg(String filePath, String toImg, String logoPath) throws MagickException {  
  65.         ImageInfo info = new ImageInfo();  
  66.         MagickImage fImage = null;  
  67.         MagickImage sImage = null;  
  68.         MagickImage fLogo = null;  
  69.         MagickImage sLogo = null;  
  70.         Dimension imageDim = null;  
  71.         Dimension logoDim = null;  
  72.         try {  
  73.             fImage = new MagickImage(new ImageInfo(filePath));  
  74.             imageDim = fImage.getDimension();  
  75.             int width = imageDim.width;  
  76.             int height = imageDim.height;  
  77.             if (width > 660) {  
  78.                 height = 660 * height / width;  
  79.                 width = 660;  
  80.             }  
  81.             sImage = fImage.scaleImage(width, height);  
  82.               
  83.             fLogo = new MagickImage(new ImageInfo(logoPath));  
  84.             logoDim = fLogo.getDimension();  
  85.             int lw = width / 8;  
  86.             int lh = logoDim.height * lw / logoDim.width;  
  87.             sLogo = fLogo.scaleImage(lw, lh);  
  88.   
  89.             sImage.compositeImage(CompositeOperator.AtopCompositeOp, sLogo,  width-(lw + lh/10), height-(lh + lh/10));  
  90.             sImage.setFileName(toImg);  
  91.             sImage.writeImage(info);  
  92.         } finally {  
  93.             if(sImage != null){  
  94.                 sImage.destroyImages();  
  95.             }  
  96.         }  
  97.     }  
  98.       
  99.     /** 
  100.      * 水印(文字) 
  101.         * @param filePath 源文件路径 
  102.      * @param toImg    修改图路径 
  103.      * @param text     名字(文字内容自己随意) 
  104.      * @throws MagickException 
  105.      */  
  106.     public static void initTextToImg(String filePath, String toImg,  String text) throws MagickException{  
  107.             ImageInfo info = new ImageInfo(filePath);  
  108.             if (filePath.toUpperCase().endsWith("JPG") || filePath.toUpperCase().endsWith("JPEG")) {  
  109.                 info.setCompression(CompressionType.JPEGCompression); //压缩类别为JPEG格式  
  110.                 info.setPreviewType(PreviewType.JPEGPreview); //预览格式为JPEG格式  
  111.                 info.setQuality(95);  
  112.             }  
  113.             MagickImage aImage = new MagickImage(info);  
  114.             Dimension imageDim = aImage.getDimension();  
  115.             int wideth = imageDim.width;  
  116.             int height = imageDim.height;  
  117.             if (wideth > 660) {  
  118.                 height = 660 * height / wideth;  
  119.                 wideth = 660;  
  120.             }  
  121.             int a = 0;  
  122.             int b = 0;  
  123.             String[] as = text.split("");  
  124.             for (String string : as) {  
  125.                 if(string.matches("[/u4E00-/u9FA5]")){  
  126.                     a++;  
  127.                 }  
  128.                 if(string.matches("[a-zA-Z0-9]")){  
  129.                     b++;  
  130.                 }  
  131.             }  
  132.             int tl = a*12 + b*6 + 300;  
  133.             MagickImage scaled = aImage.scaleImage(wideth, height);  
  134.             if(wideth > tl && height > 5){  
  135.                 DrawInfo aInfo = new DrawInfo(info);  
  136.                 aInfo.setFill(PixelPacket.queryColorDatabase("white"));  
  137.                 aInfo.setUnderColor(new PixelPacket(0,0,0,100));  
  138.                 aInfo.setPointsize(12);  
  139.                 //解决中文乱码问题,自己可以去随意定义个自己喜欢字体,我在这用的微软雅黑  
  140.                 String fontPath = "C:/WINDOWS/Fonts/MSYH.TTF";  
  141. //              String fontPath = "/usr/maindata/MSYH.TTF";  
  142.                 aInfo.setFont(fontPath);  
  143.                 aInfo.setTextAntialias(true);  
  144.                 aInfo.setOpacity(0);  
  145.                 aInfo.setText(" " + text + "于 " + new SimpleDateFormat("yyyy-MM-dd").format(new Date()) + " 上传于XXXX网,版权归作者所有!");  
  146.                 aInfo.setGeometry("+" + (wideth-tl) + "+" + (height-5));  
  147.                 scaled.annotateImage(aInfo);  
  148.             }  
  149.             scaled.setFileName(toImg);  
  150.             scaled.writeImage(info);  
  151.             scaled.destroyImages();  
  152.     }  
  153.       
  154.       
  155.     /** 
  156.      * 切图 
  157.      * @param imgPath 源图路径 
  158.      * @param toPath  修改图路径 
  159.      * @param w        
  160.      * @param h        
  161.      * @param x        
  162.      * @param y 
  163.      * @throws MagickException 
  164.      */  
  165.     public static void cutImg(String imgPath, String toPath, int w, int h, int x, int y) throws MagickException {  
  166.         ImageInfo infoS = null;  
  167.         MagickImage image = null;  
  168.         MagickImage cropped = null;  
  169.         Rectangle rect = null;  
  170.         try {  
  171.             infoS = new ImageInfo(imgPath);  
  172.             image = new MagickImage(infoS);  
  173.             rect = new Rectangle(x, y, w, h);  
  174.             cropped = image.cropImage(rect);  
  175.             cropped.setFileName(toPath);  
  176.             cropped.writeImage(infoS);  
  177.               
  178.         } finally {  
  179.             if (cropped != null) {  
  180.                 cropped.destroyImages();  
  181.             }  
  182.         }  
  183.     }  
  184.     
  185.     
  186.     /**
  187.      * 图片翻转
  188.      * @param filePath
  189.      * @param jaodu
  190.      * @throws MagickException
  191.      */
  192.     public static void createFanImg(String filePath, Double jaodu) throws MagickException {
  193.         ImageInfo info = null;
  194.         MagickImage image = null;
  195.         try {
  196.             info = new ImageInfo(filePath);
  197.             image = new MagickImage(info);
  198.             MagickImage rotated = image.rotateImage(jaodu);
  199.             rotated.setFileName(filePath);
  200.             rotated.writeImage(info);
  201.         } finally {
  202.             if (image != null) {
  203.                 image.destroyImages();
  204.             }
  205.         }
  206.     }
  207. }  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值