Thumbnailator图片处理,压缩、旋转、水印、剪切等

第一步:修改pom.xml文件,添加依赖

>
    >net.coobird>
    >thumbnailator>
    >0.4.11>
>

第二步:工具类展示(废话不多,直接干货
此工具类只做简单示范,具体使用请结合业务实现

public class ImgYsUtil {

    //测试原图
    private static File fromPic = new File("F:\\869074038846502_20200610140742_0A_0_0_res.jpg");

    //水印
    private static File waterPic = new File("F:\\20150327232807070.jpg");

    /**
     * 压缩图片
     * @throws Exception
     */
    public static void reduceImage() throws Exception{
        File toPic = new File("F:\\20150327232556186_test.jpg");
        Thumbnails.of(fromPic).scale(1f).outputQuality(0.25f).toFile(toPic);
    }

    /**
     * 修改图片大小:遵循原图高宽比例
     * @throws Exception
     */
    public static void modifyImageSize(int width,int height) throws Exception{
        File toPic = new File("F:\\20150327232556186_"+ width + "x" +height + ".jpg");
        Thumbnails.of(fromPic).size(width,height).toFile(toPic);
    }

    /**
     * 修改图片大小:不遵循原图高宽比例
     * @throws Exception
     */
    public static void modifyImageSizeKeepAspect(int width,int height) throws Exception{
        File toPic = new File("F:\\20150327232556186_keep_"+ width + "x" +height + ".jpg");
        Thumbnails.of(fromPic).size(width,height).keepAspectRatio(false).toFile(toPic);
    }


    /**
     * 按照指定比例进行缩小和放大
     * @throws Exception
     */
    public static void modifyImageSizeByScale(Double scale) throws Exception{
        File toPic = new File("F:\\20150327232556186_scale"+ scale + ".jpg");
        Thumbnails.of(fromPic).scale(scale).toFile(toPic);
    }

    /**
     * 旋转图片
     * @param direction 角度 正数正向转,负数逆向转
     * @throws Exception
     */
    public static void modifyImageDirection(int width,int height,double direction) throws Exception{
        File toPic = new File("F:\\20150327232556186_direction"+ direction + ".jpg");
        Thumbnails.of(fromPic).size(width,height).rotate(direction).toFile(toPic);
    }

    /**
     * 图片加水印
     * @throws Exception
     */
    public static void modifyImageAddSignet(int width,int height) throws Exception{
        File toPic = new File("F:\\20150327232556186_Signet.jpg");
        Thumbnails.of(fromPic)
                .size(width,height)
                .watermark(Positions.TOP_LEFT, ImageIO.read(waterPic),0.5f)
                .outputQuality(1f)
                .toFile(toPic);
    }

    /**
     * 图片裁剪
     * @throws Exception
     */
    public static void catImage(int width,int height) throws Exception{
        File toPic = new File("F:\\20150327232556186_cat.jpg");
        Thumbnails.of(fromPic)
                .sourceRegion(Positions.CENTER,width,height)
                .size(width,height)
                .outputQuality(1f)
                .toFile(toPic);
        /** 也可以自定义定位0,0表示左上角开始
        Thumbnails.of(fromPic)
                .sourceRegion(0,0,width,height)
                .size(width,height)
                .outputQuality(1f)
                .toFile(toPic);
        */
    }

    /**
     * 转换图片格式
     * @throws Exception
     */
    public static void modifyImageFormat() throws Exception{
        File toPic = new File("F:\\20150327232556186_Format.png");
        Thumbnails.of(fromPic)
                .scale(1f)
                .outputFormat("png")
                .outputQuality(1f)
                .toFile(toPic);
    }

    /**
     * 输出成文件流OutputStream
     * @throws Exception
     */
    public static void modifyImageStream() throws Exception{
        File toPic = new File("F:\\20150327232556186_stream.png");
        OutputStream os=new FileOutputStream(toPic);
        Thumbnails.of(fromPic).scale(1f).toOutputStream(os);
    }


    public static void main(String[] args) throws Exception {
       reduceImage(); //压缩图片

        modifyImageSize(200,200); //修改图片大小:遵循原图高宽比例

        modifyImageSizeByScale(0.5); //按比例放大缩小
        modifyImageSizeByScale(2.0); //按比例放大缩小

        modifyImageSizeKeepAspect(200,200); //修改图片大小:不遵循原图高宽比例

        modifyImageDirection(200,200,90); //旋转图片

        modifyImageAddSignet(1024,200); //图片加水印

        catImage(200,200); //图片裁剪

        modifyImageFormat(); //转换图片格式

        modifyImageStream();
    }
}

效果
Thumbnailator图片处理,压缩、旋转、水印、剪切等_第1张图片

你可能感兴趣的:(Thumbnailator图片处理,压缩、旋转、水印、剪切等)