【Java】Java 使用 Graphics2D 在图片上添加文字,并解决图片变红问题

【Java】Java 使用 Graphics2D 在图片上添加文字,并解决图片变红问题

完整案例

    public static void main(String[] args) {
        try {
            String filePath = "D:\\Workspace\\XunshuWorkspace\\engage\\engage-service\\uploadPath\\upload\\template\\register-certificate.jpg";
            Image src = Toolkit.getDefaultToolkit().getImage(filePath);
            BufferedImage image = BufferedImageBuilder.toBufferedImage(src);

            Graphics2D g = image.createGraphics();
            Font font = new Font("宋体", Font.BOLD, 80);
            g.setFont(font);
            g.setColor(Color.black);
            g.drawString("张三", 500, 950);
            g.drawString("522323199812345678", 1750, 950);
            String date = "2024-03-01";
            String end = "2024-03-11";
            g.drawString(date.substring(0, 4), 550, 1150);
            g.drawString(date.substring(5, 7), 950, 1150);
            g.drawString(date.substring(8, 10), 1250, 1150);
            g.drawString(end.substring(0, 4), 1650, 1150);
            g.drawString(end.substring(5, 7), 2000, 1150);
            g.drawString(end.substring(8, 10), 2350, 1150);
            g.drawString("耳鼻咽喉头颈外科", 1000, 1350);
            long day = DateUtil.betweenDay(DateUtil.parse(date), DateUtil.parse(end), true);
            g.drawString(String.valueOf(day), 2800, 1350);
            String currentDate = DateUtil.today();
            g.drawString(currentDate.substring(0, 4), 2320, 2100);
            g.drawString(currentDate.substring(5, 7), 2660, 2100);
            g.drawString(currentDate.substring(8, 10), 2900, 2100);
            g.dispose();
            // ServletOutputStream outputStream = ServletUtils.getResponse().getOutputStream();
            File output = new File(filePath.replace(".jpg", "-" + UUID.randomUUID() + ".jpg"));
            ImageIO.write(image, "jpg", output);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 使用 ImageIO.read(file); 压缩后的图片会变红改为使用
     * Toolkit.getDefaultToolkit().getImage
     */
    public static class BufferedImageBuilder {
        public static BufferedImage toBufferedImage(Image image) {
            if (image instanceof BufferedImage) {
                return (BufferedImage) image;
            }
            // This code ensures that all the pixels in the image are loaded
            image = new ImageIcon(image).getImage();
            BufferedImage bimage = null;
            GraphicsEnvironment ge = GraphicsEnvironment
                    .getLocalGraphicsEnvironment();
            try {
                int transparency = Transparency.OPAQUE;
                GraphicsDevice gs = ge.getDefaultScreenDevice();
                GraphicsConfiguration gc = gs.getDefaultConfiguration();
                bimage = gc.createCompatibleImage(image.getWidth(null),
                        image.getHeight(null), transparency);
            } catch (HeadlessException e) {
                // The system does not have a screen
            }
            if (bimage == null) {
                // Create a buffered image using the default color model
                int type = BufferedImage.TYPE_INT_RGB;
                bimage = new BufferedImage(image.getWidth(null),
                        image.getHeight(null), type);
            }
            // Copy image to buffered image
            Graphics g = bimage.createGraphics();
            // Paint the image onto the buffered image
            g.drawImage(image, 0, 0, null);
            g.dispose();
            return bimage;
        }
    }
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Tellsea

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值