图片处理工具类梳理

引入jar包
<!-- 谷歌图片压缩处理插件 -->
<dependency>
    <groupId>net.coobird</groupId>
    <artifactId>thumbnailator</artifactId>
    <version>0.4.8</version>
</dependency>
工具类
import net.coobird.thumbnailator.Thumbnails;
import net.coobird.thumbnailator.geometry.Position;
import net.coobird.thumbnailator.geometry.Positions;

import javax.imageio.ImageIO;
import java.io.*;

/**
 * Module:
 * Description:图片处理工具类
 * Author: zyp
 * Date: 2019/8/5
 **/
public class ImgUtil {

    /**
     * 压缩图片
     * @param srcImagePath 源图片路径 C:\Users\gzpost05\Pictures\Saved Pictures\1DBD4AA986094C4AB3EBF85C7A9647AB.png
     * @param desImagePath 目标图片完整路径 C:\Users\gzpost05\Pictures\Saved Pictures\1DBD4AA986094C4AB3EBF85C7A9647AB_PRESS.png
     * @param scale 压缩率(缩放比例,为“1”则是原图大小) 范围:0.0~1.0
     * @param outputQuality 输出的图片质量,范围:0.0~1.0,1为最高质量
     * @param suffix 文件后缀 png
     */
    public static void compressImage(String srcImagePath, String desImagePath, double scale,float outputQuality,String suffix) {
        try {
            OutputStream out = new FileOutputStream(desImagePath);
            Thumbnails.of(srcImagePath).scale(scale).outputQuality(outputQuality).outputFormat(suffix).toOutputStream(out);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 图片旋转
     * @param srcImagePath 源图片路径 C:\Users\gzpost05\Pictures\Saved Pictures\1DBD4AA986094C4AB3EBF85C7A9647AB.png
     * @param desImagePath 目标图片完整路径 C:\Users\gzpost05\Pictures\Saved Pictures\1DBD4AA986094C4AB3EBF85C7A9647AB_PRESS.png
     * @param rotate 角度,正数:顺时针 负数:逆时针
     * @param x 旋转前横长度
     * @param y 旋转前高长度
     */
    public static void rotateImage(String srcImagePath, String desImagePath, double rotate,int x,int y) {
        try {
            OutputStream out = new FileOutputStream(desImagePath);
            Thumbnails.of(srcImagePath).size(x,y).rotate(rotate).toOutputStream(out);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 图片增加水印
     * @param srcImagePath 源图片路径 C:\Users\gzpost05\Pictures\Saved Pictures\1DBD4AA986094C4AB3EBF85C7A9647AB.png
     * @param desImagePath 目标图片完整路径 C:\Users\gzpost05\Pictures\Saved Pictures\1DBD4AA986094C4AB3EBF85C7A9647AB_PRESS.png
     * @param waterMarkImgPath 水印图片路径 C:\Users\gzpost05\Pictures\Saved Pictures\waterMarkImg.png
     * @param x 横长度
     * @param y 高长度
     * @param position 水印位置,可选Positions.[BOTTOM_RIGHT、BOTTOM_CENTER、BOTTOM_LEFT、CENTER_RIGHT、CENTER、CENTER_LEFT、TOP_RIGHT、TOP_CENTER、TOP_LEFT]
     * @param openness 透明度 0.5f
     * @param outputQuality 输出的图片质量,范围:0.0~1.0,1为最高质量
     */
    private static void waterMarkImage(String srcImagePath, String desImagePath,String waterMarkImgPath,int x,int y,Position position,float openness,float outputQuality) {
        try {
            OutputStream out = new FileOutputStream(desImagePath);
            Thumbnails.of(srcImagePath).size(x, y).watermark(position, ImageIO.read(new File(waterMarkImgPath)), openness).outputQuality(outputQuality).toOutputStream(out);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 指定区域裁剪
     * @param srcImagePath 源图片路径 C:\Users\gzpost05\Pictures\Saved Pictures\1DBD4AA986094C4AB3EBF85C7A9647AB.png
     * @param desImagePath 目标图片完整路径 C:\Users\gzpost05\Pictures\Saved Pictures\1DBD4AA986094C4AB3EBF85C7A9647AB_PRESS.png
     * @param x 长度
     * @param y 高度
     * @param position 裁剪区域位置,可选Positions.[BOTTOM_RIGHT、BOTTOM_CENTER、BOTTOM_LEFT、CENTER_RIGHT、CENTER、CENTER_LEFT、TOP_RIGHT、TOP_CENTER、TOP_LEFT]
     * @param isScale 是否按比例缩放
     */
    private static void regionCropImage(String srcImagePath, String desImagePath, int x, int y, Position position,boolean isScale) {
        try {
            OutputStream out = new FileOutputStream(desImagePath);
            Thumbnails.of(srcImagePath).sourceRegion(position, x, y).size(x, y).keepAspectRatio(isScale).toOutputStream(out);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 指定坐标裁剪,图片左上角为原点开始计算
     * @param srcImagePath 源图片路径 C:\Users\gzpost05\Pictures\Saved Pictures\1DBD4AA986094C4AB3EBF85C7A9647AB.png
     * @param desImagePath 目标图片完整路径 C:\Users\gzpost05\Pictures\Saved Pictures\1DBD4AA986094C4AB3EBF85C7A9647AB_PRESS.png
     * @param x1 坐标1横轴位置
     * @param y1 坐标1纵轴位置
     * @param x2 坐标2横轴位置
     * @param y2 坐标2纵轴位置
     * @param isScale 是否按比例缩放
     */
    private static void coordinateCropImage(String srcImagePath, String desImagePath,int x1,int y1,int x2,int y2,boolean isScale) {
        try {
            if(x2<x1 || y2<y1){
                System.out.println("输入坐标有误,请重新输入!");
            }
            OutputStream out = new FileOutputStream(desImagePath);
            Thumbnails.of(srcImagePath).sourceRegion(x1, y1, x2, y2).size(x2-x1, y2-y1).keepAspectRatio(isScale).toOutputStream(out);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        String srcImagePath = "C:\\Users\\gzpost05\\Pictures\\Saved Pictures\\1DBD4AA986094C4AB3EBF85C7A9647AB.png";
        String desImagePath = "C:\\Users\\gzpost05\\Pictures\\asw.png";
        String waterMarkImgPth = "C:\\Users\\gzpost05\\Pictures\\Saved Pictures\\20190807105738.png";
        //指定坐标裁剪
        ImgUtil.coordinateCropImage(srcImagePath,desImagePath,0,0,800,600,false);
        //指定区域裁剪
        ImgUtil.regionCropImage(srcImagePath,desImagePath,300,400,Positions.CENTER,false);
        //图片增加水印
        ImgUtil.waterMarkImage(srcImagePath,desImagePath,waterMarkImgPth,600,400,Positions.CENTER,0.5f,1);
        //图片旋转
        ImgUtil.rotateImage(srcImagePath,desImagePath,90,600,300);
        // 压缩图片
        ImgUtil.compressImage(srcImagePath,desImagePath,0.5f,0.2f,"png");
    }

}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值