基于hutool的图片合成工具类

发布时间 2023-07-28 18:12:44作者: huanruke
import lombok.Data;

import java.io.Serializable;
import java.util.List;

/**
 * @description 缩略图信息DTO
 */
@Data
public class ThumbnailInfoDTO implements Serializable {

    /** 背景图片 */
    private String baseImageUrl;

    /** 背景图款 */
    private Integer baseWidth;

    /** 背景图高 */
    private Integer baseHeight;

    /** 水印图片 */
    private List<WatermarkImage> wmImageList;

    @Data
    public static class WatermarkImage implements Serializable {

        /** 上偏移 */
        private Integer top;

        /** 左偏移 */
        private Integer left;

        /** 图宽 */
        private Integer width;

        /** 图高 */
        private Integer height;

        /** 图片地址 */
        private String imageUrl;

    }

}
import cn.hutool.core.img.ImgUtil;
import cn.hutool.core.util.URLUtil;

import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.InputStream;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

/**
 * @description 图片工具类
 */
public class ImageUtils {

    /**
     * 获取图片
     * @param imageUrl 图片地址
     * @return java.awt.image.BufferedImage
     */
    public static BufferedImage getImage(String imageUrl, int width, int height) {
        try {
            URL imgUrl = URLUtil.url(imageUrl);
            BufferedImage image = ImgUtil.read(imgUrl);
            int w = image.getHeight();
            int h = image.getWidth();
            if (w == width && h == height) {
                return image;
            }
            Image scaleImg = ImgUtil.scale(image, width, height);
            return ImgUtil.toBufferedImage(scaleImg);
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * 获取纯色图片
     * @param width  宽
     * @param height 高
     * @param color  颜色
     * @return java.awt.image.BufferedImage
     */
    public static BufferedImage getSolidImage(int width, int height, Color color) {
        BufferedImage image = new BufferedImage(width, height, 1);
        Graphics g = image.getGraphics();
        g.setColor(color);
        g.fillRect(0, 0, width, height);
        g.dispose();
        return image;
    }

    /**
     * 获取图片流
     * @param image 图片
     * @return java.io.InputStream
     */
    public static InputStream getInputStream(Image image) {
        return ImgUtil.toStream(image, ImgUtil.IMAGE_TYPE_PNG);
    }

    /**
     * 生成图片
     * @param image 图片
     * @param file  文件
     */
    public static void getFile(Image image, File file) {
        ImgUtil.write(image, file);
    }

    /**
     * 图片合成
     * @param baseImg 基础图片
     * @param wmImg   水印图片
     * @param top     上偏移
     * @param left    左偏移
     * @return java.awt.Image
     */
    public static Image composite(BufferedImage baseImg, BufferedImage wmImg, int top, int left) {
        int x1 = baseImg.getWidth() / 2;
        int y1 = baseImg.getHeight() / 2;
        int x2 = wmImg.getWidth() / 2;
        int y2 = wmImg.getHeight() / 2;
        int x = (x2 + left) - x1;
        int y = (y2 + top) - y1;
        return ImgUtil.pressImage(baseImg, wmImg, x, y, 1f);
    }

    /**
     * 图片合成
     * @param thumbnailInfoDTO	缩略图信息
     * @return java.awt.Image
     */
    public static Image composite(ThumbnailInfoDTO thumbnailInfoDTO) {
        String baseImageUrl = thumbnailInfoDTO.getBaseImageUrl();
        int baseWidth = thumbnailInfoDTO.getBaseWidth() - 5;
        int baseHeight = thumbnailInfoDTO.getBaseHeight() - 5;
        BufferedImage baseImg = getImage(baseImageUrl, baseWidth, baseHeight);
        if (baseImg == null) {
            baseImg = getSolidImage(baseWidth, baseHeight, Color.white);
        }

        int x1 = baseImg.getWidth() / 2;
        int y1 = baseImg.getHeight() / 2;

        // 目标图片
        Image destImg = baseImg;
        List<WatermarkImage> wmImageList = thumbnailInfoDTO.getWmImageList();
        for (WatermarkImage watermarkImage : wmImageList) {
            String imageUrl = watermarkImage.getImageUrl();
            Integer width = watermarkImage.getWidth();
            Integer height = watermarkImage.getHeight();
            int left = watermarkImage.getLeft();
            int top = watermarkImage.getTop();
            BufferedImage wmImg = getImage(imageUrl, width, height);
            if (wmImg == null) {
                wmImg = getSolidImage(width - 5, height - 5, Color.gray);
            }
            int x2 = wmImg.getWidth() / 2;
            int y2 = wmImg.getHeight() / 2;

            int x = (x2 + left) - x1;
            int y = (y2 + top) - y1;
            destImg = ImgUtil.pressImage(destImg, wmImg, x, y, 1f);
        }
        return destImg;
    }

    public static void main(String[] args) {
        File destFile = new File("C:\\Users\\Administrator\\Desktop\\dest.png");
        ThumbnailInfoDTO thumbnailInfoDTO = new ThumbnailInfoDTO();
        thumbnailInfoDTO.setBaseImageUrl(null);
        thumbnailInfoDTO.setBaseWidth(1000);
        thumbnailInfoDTO.setBaseHeight(500);
        List<WatermarkImage> list = new ArrayList<>();
        thumbnailInfoDTO.setWmImageList(list);
        WatermarkImage img1 = new WatermarkImage();
        img1.setImageUrl(null);
        img1.setTop(0);
        img1.setLeft(0);
        img1.setWidth(500);
        img1.setHeight(250);
        list.add(img1);
        WatermarkImage img4 = new WatermarkImage();
        img4.setImageUrl(null);
        img4.setTop(250);
        img4.setLeft(0);
        img4.setWidth(500);
        img4.setHeight(250);
        list.add(img4);
        WatermarkImage img2 = new WatermarkImage();
        img2.setImageUrl(null);
        img2.setTop(0);
        img2.setLeft(500);
        img2.setWidth(500);
        img2.setHeight(250);
        list.add(img2);
        WatermarkImage img3 = new WatermarkImage();
        img3.setImageUrl(null);
        img3.setTop(250);
        img3.setLeft(500);
        img3.setWidth(500);
        img3.setHeight(250);
        list.add(img3);
        Image image2 = composite(thumbnailInfoDTO);
        getFile(image2, destFile);
    }

}