im4java包处理图片

Posted 技术记录

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了im4java包处理图片相关的知识,希望对你有一定的参考价值。

package com.stu.util;  
  
import java.io.IOException;  
import java.util.ArrayList;  
  
import org.im4java.core.CompositeCmd;  
import org.im4java.core.ConvertCmd;  
import org.im4java.core.IM4JavaException;  
import org.im4java.core.IMOperation;  
import org.im4java.core.IdentifyCmd;  
import org.im4java.process.ArrayListOutputConsumer;  
  
public class ImagesUtil {  
        /**  
         * 根据坐标裁剪图片  
         *  
         * @param srcPath   要裁剪图片的路径  
         * @param newPath   裁剪图片后的路径  
         * @param x   起始横坐标  
         * @param y   起始纵坐标  
         * @param x1  结束横坐标  
         * @param y1  结束纵坐标  
         */    
        public static void cutImage(String srcPath, String newPath, int x, int y, int x1,    
                int y1)  throws Exception {    
            int width = x1 - x;    
            int height = y1 - y;    
            IMOperation op = new IMOperation();    
            op.addImage(srcPath);    
            /**  width:裁剪的宽度    * height:裁剪的高度 * x:裁剪的横坐标 * y:裁剪纵坐标  */    
            op.crop(width, height, x, y);    
            op.addImage(newPath);    
            ConvertCmd convert = new ConvertCmd(true);    
            convert.run(op);    
        }    
        /**  
         * 根据尺寸缩放图片  
         * @param width  缩放后的图片宽度  
         * @param height  缩放后的图片高度  
         * @param srcPath   源图片路径  
         * @param newPath   缩放后图片的路径  
         */    
        public static void zoomImage(Integer width, Integer height, String srcPath, String newPath) throws Exception {    
            IMOperation op = new IMOperation();    
            op.addImage(srcPath);    
            if(width == null){//根据高度缩放图片  
                op.resize(null, height);      
            }else if(height == null){//根据宽度缩放图片  
                op.resize(width, null);  
            }else {  
                op.resize(width, height);  
            }  
            op.addImage(newPath);    
            ConvertCmd convert = new ConvertCmd(true);    
            convert.run(op);    
        }    
  
             
        /**  
         * 给图片加水印  
         * @param srcPath   源图片路径  
         */    
        public static void addImgText(String srcPath,String content) throws Exception {    
            IMOperation op = new IMOperation();    
            op.font("微软雅黑");  
            op.gravity("southeast");  
            op.pointsize(18).fill("#BCBFC8").draw("text 0,0 "+content);   //("x1 x2 x3 x4") x1 格式,x2 x轴距离 x3 y轴距离  x4名称      
            op.addImage();    
            op.addImage();    
            ConvertCmd convert = new ConvertCmd(true);    
            try {  
              convert.run(op,srcPath,srcPath);  
            } catch (Exception e) {  
                e.printStackTrace();  
            }  
        }    
        /** 
         * 图片水印 
         * 
         * @param srcImagePath   源图片 
         * @param waterImagePath 水印 
         * @param destImagePath  生成图片 
         * @param gravity  图片位置 
         * @param dissolve 水印透明度 
         */  
        public static void waterMark(String waterImagePath, String srcImagePath, String destImagePath, String gravity, int dissolve) {  
            IMOperation op = new IMOperation();  
            op.gravity(gravity);  
            op.dissolve(dissolve);  
            op.addImage(waterImagePath);  
            op.addImage(srcImagePath);  
            op.addImage(destImagePath);  
            CompositeCmd cmd = new CompositeCmd(true);  
            try {  
                cmd.run(op);  
            } catch (IOException e) {  
                e.printStackTrace();  
            } catch (InterruptedException e) {  
                e.printStackTrace();  
            } catch (IM4JavaException e) {  
                e.printStackTrace();  
            }  
        }  
        /** 
         * 图片旋转 
         * 
         * @param srcImagePath 
         * @param destImagePath 
         * @param angle 
         */  
        public static void rotate(String srcImagePath, String destImagePath, double angle) {  
            try {  
                IMOperation op = new IMOperation();  
                op.rotate(angle);  
                op.addImage(srcImagePath);  
                op.addImage(destImagePath);  
                ConvertCmd cmd = new ConvertCmd(true);  
                cmd.run(op);  
            } catch (Exception e) {  
                e.printStackTrace();  
            }  
        }  
          
        /** 
         * 图片信息 
         * 
         * @param imagePath 
         * @return 
         */  
        public static String showImageInfo(String imagePath) {  
            String line = null;  
            try {  
                IMOperation op = new IMOperation();  
                op.format("width:%w,height:%h,path:%d%f,size:%b%[EXIF:DateTimeOriginal]");  
                op.addImage(1);  
                IdentifyCmd identifyCmd = new IdentifyCmd(true);  
                ArrayListOutputConsumer output = new ArrayListOutputConsumer();  
                identifyCmd.setOutputConsumer(output);  
                identifyCmd.run(op, imagePath);  
                ArrayList<String> cmdOutput = output.getOutput();  
                assert cmdOutput.size() == 1;  
                line = cmdOutput.get(0);  
       
            } catch (Exception e) {  
                e.printStackTrace();  
            }  
            return line;  
        }  
        /** 
         * 图片合成 
         * @param args 
         * @param maxWidth 
         * @param maxHeight 
         * @param newpath 
         * @param mrg 
         * @param type 1:横,2:竖 
         */  
        public static void montage(String[] args,Integer maxWidth,Integer maxHeight,String newpath,Integer mrg,String type) {  
            IMOperation op = new IMOperation();  
            ConvertCmd cmd = new ConvertCmd(true);  
            String thumb_size = maxWidth+"x"+maxHeight+"^";  
            String extent = maxWidth+"x"+maxHeight;  
            if("1".equals(type)){  
                op.addRawArgs("+append");  
            }else if("2".equals(type)){  
                op.addRawArgs("-append");  
            }  
              
            op.addRawArgs("-thumbnail",thumb_size);  
            op.addRawArgs("-gravity","center");  
            op.addRawArgs("-extent",extent);  
              
            Integer border_w = maxWidth / 40;  
            op.addRawArgs("-border",border_w+"x"+border_w);  
            op.addRawArgs("-bordercolor","#ccc");  
              
            op.addRawArgs("-border",1+"x"+1);  
            op.addRawArgs("-bordercolor","#fff");  
              
            for(String img : args){  
                op.addImage(img);  
            }  
            if("1".equals(type)){  
                Integer whole_width = ((mrg / 2) +1 + border_w + maxWidth + border_w + (mrg / 2) +1)*args.length - mrg;  
                Integer whole_height = maxHeight + border_w + 1;  
                op.addRawArgs("-extent",whole_width + "x" +whole_height);  
            }else if("2".equals(type)){  
                Integer whole_width = maxWidth + border_w + 1;  
                Integer whole_height = ((mrg / 2) +1 + border_w + maxHeight + border_w + (mrg / 2) +1)*args.length - mrg;  
                op.addRawArgs("-extent",whole_width + "x" +whole_height);  
            }  
            op.addImage(newpath);  
            try {  
                cmd.run(op);  
            } catch (Exception e) {  
                e.printStackTrace();  
            }  
        }  
  
        public static void main(String[] args) throws Exception{    
          //addImgText("e://a2.jpg");    
            //zoomImage(300, 150, "e://a.jpg", "e://a1.jpg");  
            //zoomImage(300, 150, "e://b.jpg", "e://b1.jpg");  
            //zoomImage(300, 150, "e://c.jpg", "e://c1.jpg");  
            //zoomImage(300, 150, "e://d.jpg", "e://d1.jpg");  
            //zoomImage(300, 150, "e://e.jpg", "e://e1.jpg");  
        //  zoomImage(300, 150, "e://f.jpg", "e://f1.jpg");  
             //waterMark("e://cc.jpg", "e://aa.jpg", "e://bb.jpg", "southeast", 30);  
            //rotate("e://aa.jpg", "e://ee.jpg", 90);  
            //System.out.println(showImageInfo("e://aa.jpg"));  
            String[] files = new String[5];  
            files[0] = "e://a1.jpg";  
            files[1] = "e://b1.jpg";  
            files[2] = "e://c1.jpg";  
            files[3] = "e://d1.jpg";  
            files[4] = "e://e1.jpg";  
            //montage(files, 280, 200, "e://liboy1.jpg", 0,"2");  
            //cropImage("e://a.jpg", "e://liboy22.jpg", 1024, 727, 500, 350);  
            cutImage("e://a.jpg", "e://liboy222.jpg", 5, 10, 100, 120);  
        }    
} 

原文转自:http://blog.csdn.net/xielibohoudi/article/details/7986908

  1. package com.stu.util;  
  2.   
  3. import java.io.IOException;  
  4. import java.util.ArrayList;  
  5.   
  6. import org.im4java.core.CompositeCmd;  
  7. import org.im4java.core.ConvertCmd;  
  8. import org.im4java.core.IM4JavaException;  
  9. import org.im4java.core.IMOperation;  
  10. import org.im4java.core.IdentifyCmd;  
  11. import org.im4java.process.ArrayListOutputConsumer;  
  12.   
  13. public class ImagesUtil {  
  14.         /**  
  15.          * 根据坐标裁剪图片  
  16.          *  
  17.          * @param srcPath   要裁剪图片的路径  
  18.          * @param newPath   裁剪图片后的路径  
  19.          * @param x   起始横坐标  
  20.          * @param y   起始纵坐标  
  21.          * @param x1  结束横坐标  
  22.          * @param y1  结束纵坐标  
  23.          */    
  24.         public static void cutImage(String srcPath, String newPath, int x, int y, int x1,    
  25.                 int y1)  throws Exception {    
  26.             int width = x1 - x;    
  27.             int height = y1 - y;    
  28.             IMOperation op = new IMOperation();    
  29.             op.addImage(srcPath);    
  30.             /**  width:裁剪的宽度    * height:裁剪的高度 * x:裁剪的横坐标 * y:裁剪纵坐标  */    
  31.             op.crop(width, height, x, y);    
  32.             op.addImage(newPath);    
  33.             ConvertCmd convert = new ConvertCmd(true);    
  34.             convert.run(op);    
  35.         }    
  36.         /**  
  37.          * 根据尺寸缩放图片  
  38.          * @param width  缩放后的图片宽度  
  39.          * @param height  缩放后的图片高度  
  40.          * @param srcPath   源图片路径  
  41.          * @param newPath   缩放后图片的路径  
  42.          */    
  43.         public static void zoomImage(Integer width, Integer height, String srcPath, String newPath) throws Exception {    
  44.             IMOperation op = new IMOperation();    
  45.             op.addImage(srcPath);    
  46.             if(width == null){//根据高度缩放图片  
  47.                 op.resize(null, height);      
  48.             }else if(height == null){//根据宽度缩放图片  
  49.                 op.resize(width, null);  
  50.             }else {  
  51.                 op.resize(width, height);  
  52.             }  
  53.             op.addImage(newPath);    
  54.             ConvertCmd convert = new ConvertCmd(true);    
  55.             convert.run(op);    
  56.         }    
  57.   
  58.              
  59.         /**  
  60.          * 给图片加水印  
  61.          * @param srcPath   源图片路径  
  62.          */    
  63.         public static void addImgText(String srcPath,String content) throws Exception {    
  64.             IMOperation op = new IMOperation();    
  65.             op.font("微软雅黑");  
  66.             op.gravity("southeast");  
  67.             op.pointsize(18).fill("#BCBFC8").draw("text 0,0 "+content);   //("x1 x2 x3 x4") x1 格式,x2 x轴距离 x3 y轴距离  x4名称      
  68.             op.addImage();    
  69.             op.addImage();    
  70.             ConvertCmd convert = new ConvertCmd(true);    
  71.             try {  
  72.               convert.run(op,srcPath,srcPath);  
  73.             } catch (Exception e) {  
  74.                 e.printStackTrace();  
  75.             }  
  76.         }    
  77.         /** 
  78.          * 图片水印 
  79.          * 
  80.          * @param srcImagePath   源图片 
  81.          * @param waterImagePath 水印 
  82.          * @param destImagePath  生成图片 
  83.          * @param gravity  图片位置 
  84.          * @param dissolve 水印透明度 
  85.          */  
  86.         public static void waterMark(String waterImagePath, String srcImagePath, String destImagePath, String gravity, int dissolve) {  
  87.             IMOperation op = new IMOperation();  
  88.             op.gravity(gravity);  
  89.             op.dissolve(dissolve);  
  90.             op.addImage(waterImagePath);  
  91.             op.addImage(srcImagePath);  
  92.             op.addImage(destImagePath);  
  93.             CompositeCmd cmd = new CompositeCmd(true);  
  94.             try {  
  95.                 cmd.run(op);  
  96.             } catch (IOException e) {  
  97.                 e.printStackTrace();  
  98.             } catch (InterruptedException e) {  
  99.                 e.printStackTrace();  
  100.             } catch (IM4JavaException e) {  
  101.                 e.printStackTrace();  
  102.             }  
  103.         }  
  104.         /** 
  105.          * 图片旋转 
  106.          * 
  107.          * @param srcImagePath 
  108.          * @param destImagePath 
  109.          * @param angle 
  110.          */  
  111.         public static void rotate(String srcImagePath, String destImagePath, double angle) {  
  112.             try {  
  113.                 IMOperation op = new IMOperation();  
  114.                 op.rotate(angle);  
  115.                 op.addImage(srcImagePath);  
  116.                 op.addImage(destImagePath);  
  117.                 ConvertCmd cmd = new ConvertCmd(true);  
  118.                 cmd.run(op);  
  119.             } catch (Exception e) {  
  120.                 e.printStackTrace();  
  121.             }  
  122.         }  
  123.           
  124.         /** 
  125.          * 图片信息 
  126.          * 
  127.          * @param imagePath 
  128.          * @return 
  129.          */  
  130.         public static String showImageInfo(String imagePath) {  
  131.             String line = null;  
  132.             try {  
  133.                 IMOperation op = new IMOperation();  
  134.                 op.format("width:%w,height:%h,path:%d%f,size:%b%[EXIF:DateTimeOriginal]");  
  135.                 op.addImage(1);  
  136.                 IdentifyCmd identifyCmd = new IdentifyCmd(true);  
  137.                 ArrayListOutputConsumer output = new ArrayListOutputConsumer();  
  138.                 identifyCmd.setOutputConsumer(output);  
  139.                 identifyCmd.run(op, imagePath);  
  140.                 ArrayList<String> cmdOutput = output.getOutput();  
  141.                 assert cmdOutput.size() == 1;  
  142.                 line = cmdOutput.get(0);  
  143.        
  144.             } catch (Exception e) {  
  145.                 e.printStackTrace();  
  146.             }  
  147.             return line;  
  148.         }  
  149.         /** 
  150.          * 图片合成 
  151.          * @param args 
  152.          * @param maxWidth 
  153.          * @param maxHeight 
  154.          * @param newpath 
  155.          * @param mrg 
  156.          * @param type 1:横,2:竖 
  157.          */  
  158.         public static void montage(String[] args,Integer maxWidth,Integer maxHeight,String newpath,Integer mrg,String type) {  
  159.             IMOperation op = new IMOperation();  
  160.             ConvertCmd cmd = new ConvertCmd(true);  
  161.             String thumb_size = maxWidth+"x"+maxHeight+"^";  
  162.             String extent = maxWidth+"x"+maxHeight;  
  163.             if("1".equals(type)){  
  164.                 op.addRawArgs("+append");  
  165.             }else if("2".equals(type)){  
  166.                 op.addRawArgs("-append");  
  167.             }  
  168.               
  169.             op.addRawArgs("-thumbnail",thumb_size);  
  170.             op.addRawArgs("-gravity","center");  
  171.             op.addRawArgs("-extent",extent);  
  172.               
  173.             Integer border_w = maxWidth / 40;  
  174.             op.addRawArgs("-border",border_w+"x"+border_w);  
  175.             op.addRawArgs("-bordercolor","#ccc");  
  176.               
  177.             op.addRawArgs("-border",1+"x"+1);  
  178.             op.addRawArgs("-bordercolor","#fff");  
  179.               
  180.             for(String img : args){  
  181.                 op.addImage(img);  
  182.             }  
  183.             if("1".equals(type)){  
  184.                 Integer whole_width = ((mrg / 2) +1 + border_w + maxWidth + border_w + (mrg / 2) +1)*args.length - mrg;  
  185.                 Integer whole_height = maxHeight + border_w + 1;  
  186.                 op.addRawArgs("-extent",whole_width + "x" +whole_height);  
  187.             }else if("2".equals(type)){  
  188.                 Integer whole_width = maxWidth + border_w + 1;  
  189.                 Integer whole_height = ((mrg / 2) +1 + border_w + maxHeight + border_w + (mrg / 2) +1)*args.length - mrg;  
  190.                 op.addRawArgs("-extent",whole_width + "x" +whole_height);  
  191.             }  
  192.             op.addImage(newpath);  
  193.             try {  
  194.                 cmd.run(op);  
  195.             } catch (Exception e) {  
  196.                 e.printStackTrace();  
  197.             }  
  198.         }  
  199.   
  200.         public static void main(String[] args) throws Exception{    
  201.           //addImgText("e://a2.jpg");    
  202.             //zoomImage(300, 150, "e://a.jpg", "e://a1.jpg");  
  203.             //zoomImage(300, 150, "e://b.jpg", "e://b1.jpg");  
  204.             //zoomImage(300, 150, "e://c.jpg", "e://c1.jpg");  
  205.             //zoomImage(300, 150, "e://d.jpg", "e://d1.jpg");  
  206.             //zoomImage(300, 150, "e://e.jpg", "e://e1.jpg");  
  207.         //  zoomImage(300, 150, "e://f.jpg", "e://f1.jpg");  
  208.              //waterMark("e://cc.jpg", "e://aa.jpg", "e://bb.jpg", "southeast", 30);  
  209.             //rotate("e://aa.jpg", "e://ee.jpg", 90);  
  210.             //System.out.println(showImageInfo("e://aa.jpg"));  
  211.             String[] files = new String[5];  
  212.             files[0] = "e://a1.jpg";  
  213.             files[1] = "e://b1.jpg";  
  214.             files[2] = "e://c1.jpg";  
  215.             files[3] = "e://d1.jpg";  
  216.             files[4] = "e://e1.jpg";  
  217.             //montage(files, 280, 200, "e://liboy1.jpg", 0,"2");  
  218.             //cropImage("e://a.jpg", "e://liboy22.jpg", 1024, 727, 500, 350);  
  219.             cutImage("e://a.jpg""e://liboy222.jpg"510100120);  
  220.         }    

以上是关于im4java包处理图片的主要内容,如果未能解决你的问题,请参考以下文章

org.im4java.core.CommandException:返回码:137

调用imagick做图片等比例压缩5M到100kb

java知识汇总

im4java+GraphicsMagick 的问题

im4java+GraphicsMagick

使用 im4java + gm4java 运行 GraphicsMagick 比较命令