合成两个张图片,并添加文字

public class ImageRemarkUtils {

    private static Logger LOG = Logger.getLogger(ImageRemarkUtils.class);
    public static final String IMG_TEMP_FILE_PATH = ConfigUtil.getServerPath() + "/tempImg/";
    public static final String IMG_COMPOSITE_PATH = ConfigUtil.getServerPath() + "/compositeImg/";

    public static String imageJointAndRemark(String img1Path, String img2Path, String resImgPath, String content1, String content2) {
//        File file = new File(IMG_TEMP_FILE_PATH);
//        if (!file.exists()) {
//            file.mkdirs();
//        }
//        File file2 = new File(IMG_COMPOSITE_PATH);
//        if (!file2.exists()) {
//            file2.mkdirs();
//        }
        LOG.debug("------target pic path=:" + resImgPath);
        try {
            File fileOne = new File(img1Path);
            BufferedImage imageFirst = ImageIO.read(fileOne);
            int[] imageArrayFirst = new int[imageFirst.getWidth() * imageFirst.getHeight()];
            imageArrayFirst = imageFirst.getRGB(0, 0, imageFirst.getWidth(), imageFirst.getHeight(), imageArrayFirst, 0, imageFirst.getWidth());

            File fileTwo = new File(img2Path);
            BufferedImage imageSecond = ImageIO.read(fileTwo);
            int[] imageArraySecond = new int[imageSecond.getWidth() * imageSecond.getHeight()];
            imageArraySecond = imageSecond.getRGB(0, 0, imageSecond.getWidth(), imageSecond.getHeight(), imageArraySecond, 0, imageSecond.getWidth());

            int width = imageFirst.getWidth() + imageSecond.getWidth();
            int height = imageFirst.getHeight() > imageSecond.getHeight() ? imageFirst.getHeight() : imageSecond.getHeight();

            int fontBgHeight = 120;
            BufferedImage imageResult = new BufferedImage(width, height + fontBgHeight, BufferedImage.TYPE_INT_RGB);

            Graphics2D g2d = imageResult.createGraphics();
            Color color = new Color(250, 5, 5, 255);
            Color color_bg = new Color(245, 245, 245, 255);
            Font font = new Font("微软雅黑", Font.BOLD, 40);
//            Font font = new Font("仿宋", Font.BOLD, 40);
            g2d.setBackground(color_bg);
            g2d.clearRect(0, 0, width, fontBgHeight);
            g2d.setColor(color);
            g2d.setFont(font);
            g2d.drawString(content1, 12, 50);
            g2d.drawString(content2, 12, 100);
            g2d.dispose();
            imageResult.setRGB(0, 0, width, 0, new int[width * 10], 0, width);
            imageResult.setRGB(0, fontBgHeight, imageFirst.getWidth(), imageFirst.getHeight(), imageArrayFirst, 0, imageFirst.getWidth());
            imageResult.setRGB(imageFirst.getWidth(), fontBgHeight, imageSecond.getWidth(), imageSecond.getHeight(), imageArraySecond, 0,
                    imageSecond.getWidth());

            File outFile = new File(resImgPath);
            LOG.debug("new file path=" + resImgPath);
            boolean flag = ImageIO.write(imageResult, "jpg", outFile);
            return flag ? resImgPath : null;
        } catch (Exception e) {
            LOG.error("img jointing failed!", e);
        }
        return null;
    }

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值