图片处理工具类梳理

引入jar包


    net.coobird
    thumbnailator
    0.4.8

工具类
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

你可能感兴趣的:(技术点滴,图片处理,java,thumbnails)