ImageMagick加JAVA实现图片处理 工具方法类

Img4JavaUtil 工具类

  1. 安装ImageMagick 获取路径

  2. 初始化ImageMagick 搜索环境 只有windows环境下才需要,linux不需要
    设置全局的方法
    ProcessStarter.setGlobalSearchPath(“D:/Program Files/ImageMagick-7.0.8-Q16”);

package com.bdxh.fileserver.utils;

import java.io.File;
import java.io.IOException;

import org.im4java.core.ConvertCmd;
import org.im4java.core.IM4JavaException;
import org.im4java.core.IMOperation;
import org.im4java.core.Info;
import org.im4java.core.InfoException;
import org.im4java.process.ProcessStarter;
import org.springframework.boot.system.ApplicationHome;
import org.springframework.util.ClassUtils;
import org.springframework.util.ResourceUtils;

public class Img4JavaUtil {

    static {
        if(OSinfo.isWindows()) {
            System.out.println("------------ START 初始化ImageMagickPath -------------");
            ProcessStarter.setGlobalSearchPath("D:/Program Files/ImageMagick-7.0.8-Q16");
            System.out.println("------------ END 初始化ImageMagickPath -------------");
        }
    }
    /**
     * 裁剪图片
     * @param inImgPath 源图片地址
     * @param outImgPath 目标图片地址
     * @param width 宽
     * @param height 高
     * @param x 起点横坐标
     * @param y 起点纵坐标
     * @throws IOException
     * @throws InterruptedException
     * @throws IM4JavaException
     */
    public static void cropImg(String inImgPath,String outImgPath,Integer width,Integer height,Integer x,Integer y) throws IOException, InterruptedException, IM4JavaException{
        ConvertCmd cmd = new ConvertCmd();
        IMOperation operation = new IMOperation();
        operation.addImage(inImgPath);
        //宽  高 起点横坐标 起点纵坐标
        operation.crop(width, height, x, y);
        operation.addImage(outImgPath);
        cmd.run(operation);
    }

    /**
     * 裁剪中心化图片
     * @param inImgPath 源图片地址
     * @param outImgPath 目标图片地址
     * @param width 宽
     * @param height 高
     * @throws IOException
     * @throws InterruptedException
     * @throws IM4JavaException
     */
    public static void cropCenterImg(String inImgPath,String outImgPath,Integer width,Integer height) throws IOException, InterruptedException, IM4JavaException{
        ConvertCmd cmd = new ConvertCmd();
        IMOperation operation = new IMOperation();

        Info info = new Info(inImgPath);
        Integer init_width = info.getImageWidth();
        Integer init_height = info.getImageHeight();

        // 如果裁剪的宽高都大于原始图片宽高 则先等比放大再裁剪
        if(width>init_width || height>init_height){
            // 等比放大
            Integer temp_width = width;
            if(init_height<init_width){
                // 如果原图宽大于高 则以高为准放大
                temp_width = null;
            }
            operation.resize(temp_width, height);
        }
        operation.addImage(inImgPath);
        //宽  高 起点横坐标 起点纵坐标
        operation.gravity("center");
        operation.crop(width, height, 0, 0);
        operation.addImage(outImgPath);
        cmd.run(operation);
    }

    /**
     * 得到图片的信息
     * @throws InfoException
     */
    public static String getImgInfo(String inImgPath) throws InfoException{
        Info info = new Info(inImgPath);
        System.out.println(info.getImageHeight());
        System.out.println(info.getImageWidth());
        return info.getImageWidth()+"x"+info.getImageHeight();
    }

    /**
     * 等比缩放图片
     * @param inImgPath
     * @param outImgPath
     * @param width 宽度不为空时 以宽度为准,为空时以高度为准(不能同时为空)
     * @param height
     * @throws IOException
     * @throws InterruptedException
     * @throws IM4JavaException
     */
    public static void resizeImg(String inImgPath,String outImgPath,Integer width,Integer height) throws IOException, InterruptedException, IM4JavaException{
        ConvertCmd cmd = new ConvertCmd();
        IMOperation operation = new IMOperation();
        operation.addImage(inImgPath);
        //等比缩放图片
        operation.resize(width, height);//高度为null则按宽度缩放
        operation.addImage(outImgPath);
        cmd.run(operation);
    }


    /**
     * 等比缩放   (使用像素平均缩小)
     * @param inImgPath
     * @param outImgPath
     * @param width
     * @param height
     * @throws IOException
     * @throws InterruptedException
     * @throws IM4JavaException
     */
    public static void scaleImg(String inImgPath,String outImgPath,Integer width,Integer height) throws IOException, InterruptedException, IM4JavaException{
        ConvertCmd cmd = new ConvertCmd();
        IMOperation operation = new IMOperation();
        operation.addImage(inImgPath);
        //缩略图
        operation.scale(width,height);
        operation.addImage(outImgPath);
        cmd.run(operation);
    }

    /**
     * 把原图裁剪成正方形
     * @param inImgPath
     * @param operation
     * @return
     * @throws IM4JavaException
     */
    public static IMOperation getCenterSquare(String inImgPath,IMOperation operation)throws IM4JavaException{
        Info info = new Info(inImgPath);
        Integer init_width = info.getImageWidth();
        Integer init_height = info.getImageHeight();
        if (init_width > init_height) {
            init_width = init_height;
        } else if (init_width < init_height) {
            init_height = init_width;
        }
        operation.gravity("center");
        operation.crop(init_width, init_height,0,0);
        return operation;
    }


    /**
     * 缩略图 (专门用于将非常大的图像缩小为小缩略图)
     * @param inImgPath
     * @param outImgPath
     * @param width
     * @param height
     * @param editType = center 取中心化缩略图,否则取原等比缩略图
     * @throws IOException
     * @throws InterruptedException
     * @throws IM4JavaException
     */
    public static void thumbnailImg(String inImgPath,String outImgPath,Integer width,Integer height,String editType) throws IOException, InterruptedException, IM4JavaException{
        ConvertCmd cmd = new ConvertCmd();
        IMOperation operation = new IMOperation();
        operation.addImage(inImgPath);
        //取中心化或者原图同比
        if(editType!=null && editType.equals("center")) {
            //裁剪中心化正方形
            getCenterSquare(inImgPath,operation);
        }
        //生成缩略图
        operation.thumbnail(width,height);
        operation.addImage(outImgPath);
        cmd.run(operation);
    }

    /**
     * 旋转图片
     * @param inImgPath
     * @param outImgPath
     * @param x 旋转角度
     * @throws IOException
     * @throws InterruptedException
     * @throws IM4JavaException
     */
    public void rotateImg(String inImgPath,String outImgPath,Double x) throws IOException, InterruptedException, IM4JavaException{
        ConvertCmd cmd = new ConvertCmd();
        IMOperation operation = new IMOperation();
        operation.addImage(inImgPath);
        operation.rotate(x);
        operation.addImage(outImgPath);
        cmd.run(operation);
    }

    /**
     * 缩略图 (原图画质缩略)
     * @param inImgPath
     * @param outImgPath
     * @param width
     * @param height
     * @param editType
     * @throws IOException
     * @throws InterruptedException
     * @throws IM4JavaException
     */
    public void sampleImg(String inImgPath,String outImgPath,Integer width,Integer height,String editType) throws IOException, InterruptedException, IM4JavaException{
        ConvertCmd cmd = new ConvertCmd();
        IMOperation operation = new IMOperation();
        operation.addImage(inImgPath);
        //取中心化或者原图同比
        if(editType!=null && editType.equals("center")) {
            getCenterSquare(inImgPath,operation);
        }
        operation.sample(width,height);
//        operation.quality(5.0); //设置生成图片质量
        operation.addImage(outImgPath);
        cmd.run(operation);
    }

    /**
     * 将图片变成黑白图片
     * @throws IOException
     * @throws InterruptedException
     * @throws IM4JavaException
     */
    public void monochrome(String inImgPath,String outImgPath) throws IOException, InterruptedException, IM4JavaException{
        ConvertCmd cmd = new ConvertCmd();
        IMOperation operation = new IMOperation();
        operation.addImage(inImgPath);
        operation.monochrome();
        operation.addImage(outImgPath);
        cmd.run(operation);
    }

    /** 
     * 给图片加水印 
     * @param srcPath            源图片路径 
     */
    public static void addImgText(String inImgPath,String outImgPath,String str)throws IOException, InterruptedException, IM4JavaException{
        IMOperation operation = new IMOperation();
        operation.font("宋体").gravity("southeast").pointsize(18).fill("#BCBFC8")
                .draw("text 5,5 "+str);
        operation.encoding("UTF-8");
        operation.addImage();
        operation.addImage();
        ConvertCmd convert = new ConvertCmd();
        convert.run(operation, inImgPath,outImgPath);
    }


    /**
     * 创建文件名称
     * @param folder
     * @param fileName
     * @return
     */
    public static File createFile(String folder, String fileName) {
        File serverFile;
        String suffix = getSuffix(fileName);
        String destFileName = null;
        long timeMillis = System.currentTimeMillis();
        int i = 1;
        do {
            if (suffix.length() == 0) {
                destFileName = String.format("%s_%d", timeMillis, i++);
            } else {
                destFileName = String.format("%s_%d.%s", timeMillis, i++, suffix);
            }
            serverFile = new File(folder, destFileName);
        } while (serverFile.exists());
        serverFile.getParentFile().mkdirs();
        return serverFile;
    }

    /**
     * 生成图片名称(根据旧的文件类型)
     * @param oldFileName 旧文件路径
     * @param prefix 前缀
     * @return
     */
    public static String  createFileName(String oldFileName,String prefix) {
        String fileName[] = oldFileName.split("/");
        return prefix+fileName[fileName.length-1];
    }

    /**
     * 截取文件名称
     * @param filePath
     * @return
     */
    public static String getSuffix(String filePath){
        return filePath.substring(filePath.lastIndexOf("."));
    }


    /**
     * 测试
     * @param args
     * @throws IOException
     * @throws InterruptedException
     * @throws IM4JavaException
     */
    public static void main(String[] args) throws IOException, InterruptedException, IM4JavaException{
        String inImgPath = "D:\\Documents\\Pictures\\fj.png";
        String outImgPath = "D:\\Documents\\Pictures\\";
        Img4JavaUtil test = new Img4JavaUtil();
        // 裁剪图片
//        test.cropImg(inImgPath,outImgPath+"裁剪-300x300.jpg",300,300,0,0);
        // 获取图片信息
//        test.getImgInfo(inImgPath);
        // 等比缩放
//        test.resizeImg(inImgPath,outImgPath+"等比缩放-w300.jpg",300,null);
        // 旋转图片
//        test.rotateImg(inImgPath,outImgPath+"旋转45.jpg",45.0);
        // 转黑白图片
//        test.monochrome(inImgPath,outImgPath+"转黑白.jpg");
        // 加水印
//        test.addImgText(inImgPath,outImgPath+"加水印.jpg","CCTV-100");
//        test.scaleImg(inImgPath,outImgPath+"缩略图scale.jpg",100,null);
//        test.resizeImg(inImgPath,outImgPath+"缩略图resize.jpg",100,null);
//        test.thumbnailImg("http://localhost:8080/upload/fj.jpg","D:\\workspace-idea-jack\\PhotoEdit\\src\\main\\webapp\\upload\\1552621019735_1.jpg"+"缩略图thumbnail.jpg",400,null);
//        test.thumbnailImg("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1552631430725&di=cd6029dd8522771d6ae0933653172a0d&imgtype=0&src=http%3A%2F%2Fe.hiphotos.baidu.com%2Fimage%2Fpic%2Fitem%2Ffc1f4134970a304e210531d0dfc8a786c9175cf0.jpg","D:\\workspace-idea-jack\\PhotoEdit\\src\\main\\webapp\\upload\\1552621019735_1.jpg"+"缩略图thumbnail.jpg",400,null);
//        System.out.println("------------    操作完成    ------------");
//        ProcessStarter.setGlobalSearchPath("D:\\Program Files\\ImageMagick-7.0.8-Q16");
//        test.cropImg(inImgPath,outImgPath+"36.jpg",300,600,0,0);
//        test.sampleImg(inImgPath,outImgPath+"sample.jpg",300,300,"center");
//        test.thumbnailImg(inImgPath,outImgPath+"thumbnail.png",400,400,"center");
//        test.cropCenterImg(inImgPath,outImgPath+"中心.jpg",400,300);
//        test.scaleImg(inImgPath,outImgPath+"sale.jpg",200,200);

    }
}

猜你喜欢

转载自blog.csdn.net/fangchao2011/article/details/88747335