图片裁剪工具-将图片按指定宽度缩小剪切

package com.img.cut.utils;


import net.coobird.thumbnailator.Thumbnails;
import net.coobird.thumbnailator.Thumbnails.Builder;
import org.apache.commons.lang3.ObjectUtils;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;

/**
 * 图片裁剪工具
 *
 * @author leemeea
 * @date 2021/12/25
 */
public class ImageCroppingUtil {

    public static void main(String[] args) {
        cutImgByMaxWidth("E:\\data\\test\\123.jpg", null,"", 280);
    }

    /**
     * 将图片按传入的最大宽度裁剪
     * <p>targetName与suffixAdded二选一</p>
     * @param sourceFilePath 文件名
     * @param targetName    目标文件名(如果保留原始文件名的话,这里传null)
     * @param suffixAdded    在目标文件名之后,且在后缀名之前添加的字符,如果不想添加这个字符,则不传
     * @param maxWidth       图片最大宽度
     * @return 切图之后的路径
     */
    public static String cutImgByMaxWidth(String sourceFilePath, String targetName , String suffixAdded, int maxWidth){
        if (ObjectUtils.isEmpty(targetName) && ObjectUtils.isEmpty(suffixAdded)) {
            System.out.println("目标文件名与目标文件添加字符必传一个:" + sourceFilePath);
            return null;
        }
        return cutImgByMaxWidth(sourceFilePath, targetName, suffixAdded, maxWidth, false);
    }

    /**
     * 将原图切到以maxWidth的宽度进行保存(替换原图)
     * @param sourceFilePath    原图
     * @param maxWidth  图片最大宽度
     * @return  图片地址
     */
    public static String replaceImgByMaxWidth(String sourceFilePath, int maxWidth) {
        return cutImgByMaxWidth(sourceFilePath, null, null, maxWidth, true);
    }




    /**
     * 将图片按传入的最大宽度裁剪
     * @param sourceFilePath 文件名
     * @param targetName    目标文件名(如果保留原始文件名的话,这里传null)
     * @param suffixAdded    在目标文件名之后,且在后缀名之前添加的字符,如果不想添加这个字符,则不传
     * @param maxWidth       图片最大宽度
     * @param replaceSource       是否替换原图
     * @return 切图之后的路径
     */
    private static String cutImgByMaxWidth(String sourceFilePath, String targetName , String suffixAdded, int maxWidth, boolean replaceSource) {
        File sourceFile = new File(sourceFilePath);
        if (!sourceFile.exists()) {
            System.out.println("源文件不存在:" + sourceFilePath);
            return null;
        }

        if (!sourceFile.getName().contains(".")) {
            System.out.println("源文件不存在后缀名:" + sourceFilePath);
            return null;
        }
        // 原始文件名
        String sourceFileName = sourceFile.getName().substring(0, sourceFile.getName().lastIndexOf("."));
        // 后缀名
        String suffix = sourceFile.getName().substring(sourceFile.getName().lastIndexOf(".") + 1);

        // 目标文件
        File targetFile;
        if (ObjectUtils.isNotEmpty(targetName)) {
            targetFile = new File(sourceFile.getParent(), targetName + suffixAdded + "." + suffix);
        } else {
            targetFile = new File(sourceFile.getParent(), sourceFileName + suffixAdded + "." + suffix);
        }

        // 不替换原图的时候
        if (!replaceSource) {
            if (targetFile.exists()) {
                System.out.println("目标文件已存在:" + targetFile.getAbsolutePath());
                return null;
            }
        }

        try {
            BufferedImage image = ImageIO.read(sourceFile);

            int imageWidth = image.getWidth();
            int imageHeight = image.getHeight();

            // 使用宽度为界限,计算宽度与高度
            if (imageWidth > maxWidth) {
                double b = (double) imageHeight / (double) imageWidth;
                imageHeight = (int) (b * maxWidth);
                imageWidth = maxWidth;
            }

            Builder<BufferedImage> builder = Thumbnails.of(image).size(imageWidth, imageHeight);
            builder.outputFormat(suffix).toFile(targetFile);
            return targetFile.getPath();
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值