工具使用: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萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章