使用Java在图片中添加文字

  • Post author:
  • Post category:java




有这样一张图片,需要修改图中的时间

在这里插入图片描述



实现思路:

先使用绘图工具将上图中的时间抹成白色。将图片加载到BufferedImage类,使用Graphics2D类对图片进行文字绘制,最后输出流,前端通过a标签访问即可完成下载。在我的电脑中,Arial字体样式和图片中的时间的字体样式一致,将项目部署到linux中后由于系统中没有该字体,需要先向linux中安装字体,安装方式见:

https://blog.csdn.net/wangxintong_1992/article/details/81194896

以下代码基于Springboot框架开发

@RequestMapping("generate")
    public void drawTextInImg(HttpServletResponse response, String startTime, String endTime) {
        String filePath = "/Users/yuho/Documents/IMG_7205.png";
        //String filePath = "/root/jars/IMG_7205.png";
        ImageIcon imgIcon = new ImageIcon(filePath);
        Image img = imgIcon.getImage();
        int width = img.getWidth(null);
        int height = img.getHeight(null);
        BufferedImage bimage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        Graphics2D g = bimage.createGraphics();
        g.setColor(getColor("#AF2222"));
        g.setBackground(Color.white);
        g.drawImage(img, 0, 0, null);
        //设置字体样式,风格,大小
        Font font = new Font("Arial", Font.PLAIN, 27);
        g.setFont(font);
        //以左上角为坐标轴原点,left为横坐标,top为纵坐标
        int left = 330;
        int top = 1079;
        //抗锯齿
        g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        //绘制开始时间
        g.drawString(startTime, left, top);
        //绘制结束时间
        g.drawString(endTime, left, top+65);
        //释放对象
        g.dispose();
        try {
            //BufferedImage 转 InputStream
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            ImageOutputStream imageOutput = ImageIO.createImageOutputStream(byteArrayOutputStream);
            ImageIO.write(bimage, "png", imageOutput);
            InputStream inputStream = new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
            long length = imageOutput.length();
            //设置response
            response.setContentType("image/png");
            response.setContentLength((int)length);
            response.setHeader("Content-Disposition","attachment;filename="+new String("license.png".getBytes("gbk"),"iso-8859-1"));
            //输出流
            byte[] bytes = new byte[1024];
            OutputStream outputStream = response.getOutputStream();
            long count = 0;
            while(count < length){
                int len = inputStream.read(bytes, 0, 1024);
                count +=len;
                outputStream.write(bytes, 0, len);
            }
            outputStream.flush();
        } catch (Exception e){
            e.printStackTrace();
        }
    }
    public static Color getColor(String color) {
        if (color.charAt(0) == '#') {
            color = color.substring(1);
        }
        if (color.length() != 6) {
            return null;
        }
        try {
            int r = Integer.parseInt(color.substring(0, 2), 16);
            int g = Integer.parseInt(color.substring(2, 4), 16);
            int b = Integer.parseInt(color.substring(4), 16);
            return new Color(r, g, b);
        } catch (NumberFormatException nfe) {
            return null;
        }
    }



版权声明:本文为u012198023原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。