工具使用:ImageTools图片处理 (ImageMagick)

  • 需要注意的是:在mac和linux上不要添加imageMagickPath
    和设置 convert.setSearchPath(imageMagickPath);
    public class ImageTools {  

    /** 
     * ImageMagick的路径 
     */  
    public static String imageMagickPath = null;  
    static {  
        /** 
         *  
         * 获取ImageMagick的路径 
         */  
        Properties prop = new PropertiesFile().getPropertiesFile();  
        //linux下不要设置此值,不然会报错  
        imageMagickPath = prop.getProperty("imageMagickPath");  
    }  

    /** 
     *  
     * 根据座标裁剪图片 
     *  
     * @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();  

        // linux下不要设置此值,不然会报错  
        convert.setSearchPath(imageMagickPath);  

        convert.run(op);  
    }  

    /** 
     *  
     * 根据尺寸缩放图片 
     * @param width             缩放后的图片宽度 
     * @param height            缩放后的图片高度 
     * @param srcPath           源图片路径 
     * @param newPath           缩放后图片的路径 
     */  
    public static void cutImage(int width, int height, String srcPath,  String newPath) throws Exception {  
        IMOperation op = new IMOperation();  
        op.addImage(srcPath);  
        op.resize(width, height);  
        op.addImage(newPath);  
        ConvertCmd convert = new ConvertCmd();  
        // linux下不要设置此值,不然会报错  
        convert.setSearchPath(imageMagickPath);  
        convert.run(op);  

    }  

    /** 
     * 根据宽度缩放图片 
     *  
     * @param width            缩放后的图片宽度 
     * @param srcPath          源图片路径 
     * @param newPath          缩放后图片的路径 
     */  
    public static void cutImage(int width, String srcPath, String newPath)  throws Exception {  
        IMOperation op = new IMOperation();  
        op.addImage(srcPath);  
        op.resize(width, null);  
        op.addImage(newPath);  
        ConvertCmd convert = new ConvertCmd();  
        // linux下不要设置此值,不然会报错  
        convert.setSearchPath(imageMagickPath);  
        convert.run(op);  
    }  

    /** 
     * 给图片加水印 
     * @param srcPath            源图片路径 
     */  
    public static void addImgText(String srcPath) throws Exception {  
        IMOperation op = new IMOperation();  
        op.font("宋体").gravity("southeast").pointsize(18).fill("#BCBFC8")  
                .draw("text 5,5 juziku.com");  
        op.addImage();  
        op.addImage();  
        ConvertCmd convert = new ConvertCmd();  
        // linux下不要设置此值,不然会报错  
        convert.setSearchPath(imageMagickPath);  
        convert.run(op, srcPath, srcPath);  
    }  

    public static void main(String[] args) throws Exception {  
        // cutImage("D:\\test.jpg", "D:\\new.jpg", 98, 48, 370,320);  
        // cutImage(200,300, "/home/1.jpg", "/home/2.jpg");  
        addImgText("//home//1.jpg");  
    }  
}  

下面测试一个macdown绘制的流程图

Created with Raphaël 2.1.0Here is a titleAABBCCDDNormal lineDashed lineOpen arrowDashed open arrow
Created with Raphaël 2.1.0连接建立的过程客户主机客户主机服务器主机服务器主机连接请求(SYN=1,seq=client_isn)授予连接(SYN=1,seq=client_isn) ack=client_isn+1确认(SYN=0,seq=client_isn+1)ack=server_isn+1
发布了117 篇原创文章 · 获赞 29 · 访问量 24万+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章