当前位置: 首页 > news >正文

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

文章目录

  • 【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 loadedimage = 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 modelint type = BufferedImage.TYPE_INT_RGB;bimage = new BufferedImage(image.getWidth(null),image.getHeight(null), type);}// Copy image to buffered imageGraphics g = bimage.createGraphics();// Paint the image onto the buffered imageg.drawImage(image, 0, 0, null);g.dispose();return bimage;}}
http://www.lryc.cn/news/373828.html

相关文章:

  • 初识docker插件
  • springboot整合redis以及使用
  • ARM32开发--电源管理单元
  • Android Studio项目升级报错:Namespace not specified
  • 渗透测试nginx增加400,500报错页面
  • OpenCV读取和显示和保存图像
  • 【面经总结】Java集合 - Map
  • CompletableFuture方法介绍及代码示例
  • 基于springboot的宠物商城网站
  • DM存储ontap系统修改管理IP
  • Web前端商业素材:挖掘价值,释放创意的无限可能
  • LeetCode206-反转链表
  • 5000天后的世界
  • Photoshop中颜色与色调的调整
  • 【退役之重学Java】终结篇,暂别 Java !
  • 查找——顺序查找和折半查找
  • Bio-Info每日一题:Rosalind-07-Mendel‘s First Law(孟德尔第一定律 python实现)
  • C++ 47 之 函数调用运算符重载
  • [Qt的学习日常]--常用控件1
  • 模型实战(23)之 yolov10 使用总结及训练自己的数据集
  • AIRNet模型使用与代码分析(All-In-One Image Restoration Network)
  • 欧洲杯“球迷狂欢趴”开启,容声带来“健康养鲜”新理念
  • 人工智能对零售业的影响
  • Spring Boot + EasyExcel + SqlServer 进行批量处理数据
  • 深入理解指针(四)
  • k-means聚类模型的优缺点
  • 我的创作纪念日(1825天)
  • Studio One 6.6.2 for Mac怎么激活,有Studio One 6激活码吗?
  • Windows搭建nacos集群
  • kotlin 中的字符