Img4JavaUtil

Img4JavaUtil,第1张

Img4JavaUtil

package com.lingoace.edu.util;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

import com.lingoace.edu.util.AmazonS3CacheUtil;
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;

public class Img4JavaUtil {

    static {
        //判断是否是windows系统
        System.out.println("------------ START 初始化ImageMagickPath -------------");
        ProcessStarter.setGlobalSearchPath("C:\Program Files\ImageMagick-7.1.0-Q8");
        System.out.println("------------ END 初始化ImageMagickPath -------------");
    }

   
    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);
    }


   
    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);
    }

   
    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();
    }

   
    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);
    }


   
    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);
    }

   
    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;
    }


   
    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("c")) {
            //分析宽高 先原图等比生成缩略图(缩略规则按原图大的一边为准取值)
            Info info = new Info(inImgPath);
            Integer init_width = info.getImageWidth();
            Integer init_height = info.getImageHeight();
            double width_v = (double) init_width / width;
            double height_v = (double) init_height / height;
            //原图是正方形
           
            if (init_width >= init_height) {
                if (width_v >= height_v) {
                    operation.thumbnail(null, height);
                } else if (width_v < height_v) {
                    operation.thumbnail(width, null);
                }
                //原图是高大于宽的长方形
            } else if (init_width < init_height) {
                if (width_v > height_v) {
                    operation.thumbnail(null, height);
                } else if (width_v <= height_v) {
                    operation.thumbnail(width, null);
                }
            }

            //再中心化截图
            operation.gravity("center");
            operation.crop(width, height, 0, 0);
        } else {
            operation.thumbnail(width, height);
        }
        operation.addImage(outImgPath);
        cmd.run(operation);
    }

   
    public static 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);
    }

   
    public static 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);
    }

   
    public static 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);
    }

   
    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);
    }


   
    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;
    }

   
    public static String createFileName(String rootPath, String oldFileName, int width, int height, String type) {

        // 初始化目录
        String pathName = oldFileName.substring(0, oldFileName.lastIndexOf("/"));
        String newFilePath = rootPath + pathName;

        File dirFile = new File(newFilePath);
        if (!dirFile.exists()) {
            dirFile.mkdirs();
        }

        // 生成文件名
        String imgName = oldFileName.substring(oldFileName.lastIndexOf("/"));
        String a = imgName.substring(0, imgName.lastIndexOf("."));
        String b = imgName.substring(imgName.lastIndexOf("."));

        return newFilePath + a + "_" + width + "x" + height + "_" + type + b;
    }


   
    public static String getSuffix(String filePath) {
        return filePath.substring(filePath.lastIndexOf("."));
    }


   
    private static final String IMG_TEMP_PATH = "\tmp\img\%s";

   
    public static void main(String[] args) throws IOException, InterruptedException, IM4JavaException {
        String inImgPath = "C:\Users\LingoAce\Desktop\test_1.heic";
        String outImgPath = "C:\Users\LingoAce\Desktop\test_5";
        // 中心化缩略图(常用)
//        Img4JavaUtil.thumbnailImg(inImgPath, outImgPath, 400, 400, "c");
        // 裁剪图片
//        Img4JavaUtil.cropImg(inImgPath, outImgPath + "裁剪-test.jpg", 300, 400, 0, 0);
        // 获取图片信息 //*  /
//        Img4JavaUtil.getImgInfo(inImgPath);
        // 等比缩放
//        Img4JavaUtil.resizeImg(inImgPath,outImgPath+"等比缩放-test.jpg",400,null);
        // 旋转图片
//        Img4JavaUtil.rotateImg(inImgPath,outImgPath+"旋转45.jpg",45.0);
        // 转黑白图片
//        Img4JavaUtil.monochrome(inImgPath,outImgPath+"转黑白.jpg");
        // 加水印
//        Img4JavaUtil.addImgText(inImgPath,outImgPath+"加水印.jpg","CCTV-100");
        // 原图质量缩放
//        Img4JavaUtil.scaleImg(inImgPath,outImgPath+"缩略图scale.jpg",100,null);
        // 自动中心化裁剪
//        Img4JavaUtil.cropCenterImg(inImgPath,outImgPath+"中心.jpg",400,300);
        String url = "d:\tmp\img\c8321ec1-5cdf-4b35-927f-9d5e1b3bfc38/gfjzjj-20210916144358.jpeg";
        Img4JavaUtil.addImgText(url,outImgPath+"加水印.jpg","CCTV-100");
    }
}
 

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/5438095.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-11
下一篇 2022-12-11

发表评论

登录后才能评论

评论列表(0条)

保存