MergeImgUtil.java 5.31 KB
package cn.com.glsx.entmicro.util;

import cn.com.glsx.entmicro.model.request.DracChannelCodeBillRequest;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;
import java.net.URL;

/**
 * @author Lichongyuan
 * @Description: 图片合成工具类
 * @date 2021/11/15 14:01
 */
public class MergeImgUtil {

    /**
     * url图片转BufferedImage
     * @param imageName 文件路径
     * @return
     */
    public static BufferedImage loadImage(String imageName){
        try {
            URL urlFile = new URL(imageName);
            return ImageIO.read(urlFile);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     *
     * @Title: 构造图片
     * @Description: 生成图片并返回java.awt.image.BufferedImage
     * @param backImg
     *            层图(图片)
     * @param codeImg
     *            底图(图片)
     * @param x
     *            距离左上角的X偏移量
     * @param y
     *            距离左上角的Y偏移量
     * @return BufferedImage
     * @throws IOException
     */
    public static BufferedImage mergeImg(BufferedImage backImg, BufferedImage codeImg, int x, int y) throws IOException {
        // 创建Graphics2D对象,用在底图对象上绘图
        Graphics2D g2d = backImg.createGraphics();
        //抗锯齿 预防低像素
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        //图像之间混合的透明度 0-1, 1:不透明
        g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, 1));
        g2d.drawImage(codeImg, x, y, null);
        g2d.dispose();// 释放图形上下文使用的系统资源
        return backImg;
    }

    /**
     * 重新绘制图片像素大小(解决层图在背景图中位置偏移问题)
     * @param backImg
     * @param backWidth 长度
     * @param backHeight 宽度
     * @return
     */
    public static BufferedImage setBackGround(BufferedImage backImg, int backWidth, int backHeight ) {
        BufferedImage bufferedImage = new BufferedImage(backWidth, backHeight, BufferedImage.TYPE_INT_ARGB);
        Graphics2D graphics2D = bufferedImage.createGraphics();
        //0 代表没有坐标位移
        graphics2D.drawImage(backImg, 0,0,  backWidth, backHeight, null);
        graphics2D.dispose();
        return bufferedImage;
    }

    /**
     * 指定生成路径
     * @param name
     * @param bufferedImage
     * @throws IOException
     */
    private static void createImage(String name, BufferedImage bufferedImage) throws  IOException {
        File file = new File(name);
        ImageIO.write(bufferedImage, "png", file);
    }

    /**
     * 输出图片在服务器
     * @param bufferedImage
     * @return FileUploadServerData
     */
    public static FileUploadServerData uploadToFileSystem(BufferedImage bufferedImage) throws IOException {
        //转化成字节数组
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        //输出格式必须是png jpg是有损压缩的图片格式,有可能导致图片位深度不满足真彩显示和色彩失真
        ImageIO.write(bufferedImage, "png", byteArrayOutputStream);
        byteArrayOutputStream.flush();
        byte[] imageByte = byteArrayOutputStream.toByteArray();
        byteArrayOutputStream.close();
        //上传服务器
        FileUploadServerData fileUploadServerData = FileUploadUtil.uploadToFileSystem(UUIDUtils.getUUID(), imageByte, "png");
        return fileUploadServerData;
    }

    /**
     * 尺寸计算和图片生成
     * @param codeUrl 二维码
     * @param backUrl 海报背景
     * @param x 右下角距离x偏移量(缩放)
     * @param y 右下角距离y偏移量(缩放)
     * @param width 二维码入参宽度(缩放)
     * @param height 二维码入参高度(缩放)
     * @param backWidth 背景入参宽度(缩放)
     * @param backHeight 背景入参高度(缩放)
     * @return
     */
    public static BufferedImage drawImage(String codeUrl, String backUrl, Integer x, Integer y,
                                          Integer width, Integer height, Integer backWidth, Integer backHeight) throws IOException {
        //二维码
        BufferedImage codeImg = loadImage(codeUrl);

        //海报背景
        BufferedImage backImg = loadImage(backUrl);

        //通过缩放的背景和二维码比 计算出 缩放比例 确定二维码原图扩大的比例
        double widthScale = width  * 1.0 / backWidth;
        double heightScale = height  * 1.0 / backHeight;

        //坐标缩放比例
        double xScale = x  * 1.0 / backWidth;
        double yScale = y * 1.0 / backHeight;

        //通过比例计算原图正确的二维码长宽
        Integer codeWidth = (int)(backImg.getWidth() * widthScale);
        Integer codeHeight = (int)(backImg.getHeight() * heightScale);

        //计算新的坐标值
        Integer newX = (int)(backImg.getWidth() * xScale);
        Integer newY = (int)(backImg.getHeight() * yScale);

        //重新绘制二维码图片大小
        BufferedImage codeImage = setBackGround(codeImg, codeWidth, codeHeight);
        //拼接图片
        BufferedImage bufferedImage = mergeImg(backImg, codeImage, newX, newY);

        return bufferedImage;
    }
}