工具类:

package com.mchweb.common.utils.image;

import com.google.common.collect.Lists;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.List;

public class ImageMergedUtil {

    /**
     * 合成图片并添加文字
     *
     * @param outFile 最终合成文件
     */
    public static void merged(File targetImage, List<ImgMergeVO> imgMergeVOList, List<ImgFontVO> imgFontVOList, File outFile) throws IOException {
        // 加载模版图片
        BufferedImage imageLocal = ImageIO.read(targetImage);
        Graphics2D g = null;
        // 加载叠加图片
        if (imgFontVOList != null) {
            for (ImgMergeVO imgMerge : imgMergeVOList) {
                BufferedImage imageCode = ImageIO.read(imgMerge.getMergedImage());
                g = imageLocal.createGraphics();
                // 在模板上添加叠加图片(地址,左边距,上边距,图片宽度,图片高度,未知)
                g.drawImage(imageCode, imgMerge.getLeft(), imgMerge.getTop(), imgMerge.getWidth(), imgMerge.getHeight(), null);
            }
        }

        // 添加文本说明
        if (imgFontVOList != null) {
            for (ImgFontVO imgFont : imgFontVOList) {
                // 设置文本样式
                g.setFont(imgFont.getFont());
                g.setColor(imgFont.getColor());
                g.drawString(imgFont.getText(), imgFont.getLeft(), imgFont.getTop());
            }
        }
        g.dispose();// 完成模板修改
        ImageIO.write(imageLocal, "png", outFile);
    }
}


文字对象类:

package com.mchweb.common.utils.image;

import lombok.*;
import lombok.experimental.Accessors;

import java.awt.*;

@Getter
@Setter
@Accessors(chain = true)
public class ImgFontVO {

    private String text;

    private Font font;

    private int left;

    private int top;

    private Color color;

    public ImgFontVO() {
        this.color = Color.BLACK;
        this.font = new Font("PingFangSC-Regular", Font.PLAIN, 35);
    }

    public ImgFontVO(String text, int left, int top, Font font, Color color) {
        this();
        this.text = text;
        this.font = font;
        this.left = left;
        this.top = top;
        this.color = color;
    }

    public ImgFontVO(String text, int left, int top, Font font) {
        this();
        this.text = text;
        this.font = font;
        this.left = left;
        this.top = top;
    }

    public ImgFontVO(String text, int left, int top) {
        this();
        this.text = text;
        this.left = left;
        this.top = top;
    }

    public ImgFontVO(String text, int left, int top, Color color) {
        this();
        this.text = text;
        this.left = left;
        this.top = top;
        this.color = color;
    }
}


图片对象类:

package com.mchweb.common.utils.image;

import lombok.Builder;
import lombok.Getter;
import lombok.Setter;
import lombok.experimental.Accessors;

import java.io.File;

@Getter
@Setter
@Accessors(chain = true)
@Builder(toBuilder = true)
public class ImgMergeVO {

    //private File targetImage;
    private File mergedImage;
    private int left;
    private int top;
    private int width;
    private int height;

    public ImgMergeVO() {
        this.width = 30;
        this.height = 30;
    }

    public ImgMergeVO(File mergedImage, int left, int top) {
        this();
        //this.targetImage = targetImage;
        this.mergedImage = mergedImage;
        this.left = left;
        this.top = top;
    }

    public ImgMergeVO(File mergedImage, int left, int top, int width, int height) {
        this();
        //this.targetImage = targetImage;
        this.mergedImage = mergedImage;
        this.left = left;
        this.top = top;
        this.width = width;
        this.height = height;
    }
}


测试案例:

package com.mchweb.common.utils.image;

import com.google.common.collect.Lists;

import java.awt.*;
import java.io.File;
import java.io.IOException;
import java.util.List;

public class Test {

    public static void main(String[] args) {
        File targetImage = new File("C:/1111.png");
        File mergedImage = new File("C:/22.png");
        File outputfile = new File("C:/AAA.png");
        Font font = new Font("PingFangSC-Regular", Font.PLAIN, 35);
        java.util.List<ImgFontVO> imgFontVOList = Lists.newArrayList();
        imgFontVOList.add(new ImgFontVO("XXXX", 20, 100, font));
        imgFontVOList.add(new ImgFontVO("XXXX", 20, 200, font));
        imgFontVOList.add(new ImgFontVO("XXXX", 20, 300, font));
        imgFontVOList.add(new ImgFontVO("XXXX", 20, 400, font));

        List<ImgMergeVO> imgMergeVOList = Lists.newArrayList();
        imgMergeVOList.add(new ImgMergeVO(mergedImage, 10, 100));
        imgMergeVOList.add(new ImgMergeVO(mergedImage, 10, 200));
        imgMergeVOList.add(new ImgMergeVO(mergedImage, 10, 300));

        try {
            ImageMergedUtil.merged(targetImage, imgMergeVOList, imgFontVOList, outputfile);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}




点赞(0) 打赏

Comment list 共有 0 条评论

暂无评论

微信小程序

微信扫一扫体验

立即
投稿

微信公众账号

微信扫一扫加关注

发表
评论
返回
顶部