zoukankan      html  css  js  c++  java
  • java 根据图片文字动态生成图片

    今天在做热敏打印机打印二维码,并有文字描述,想到的简单的方法就是根据热敏打印机的纸张宽度和高度,生成对应的图片,如下:

    package com.orisdom.utils;

    import lombok.extern.slf4j.Slf4j;

    import javax.imageio.ImageIO;
    import java.awt.*;
    import java.awt.image.BufferedImage;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.IOException;

    /**
    * @author chenxiaokang
    * @date 2019/7/8 13:48
    */
    @Slf4j
    public class ImageBuilderUtils {

    /**
    * 热敏打印机纸张大约宽度(4cm*6cm)
    */
    private static final int width = 151;
    /**
    * 热敏打印机纸张大约高度(4cm*6cm)
    */
    private static final int height = 227;

    /**
    *
    * @param code 编码
    * @param path 二维码存放路劲
    * @return 最新图片生成路径
    */
    public static String ImageBuilder(String code,String path) {
    //得到图片缓冲区
    FileInputStream fileInputStream = null;
    try {
    BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
    File file = new File(path);
    if(!file.exists()){
    throw new BusinessException("二维码存放地址有误");
    }
    fileInputStream = new FileInputStream(file);
    BufferedImage image2 = ImageIO.read(fileInputStream);
    //得到它的绘制环境(这张图片的笔)
    Graphics2D g2 = (Graphics2D) bi.getGraphics();
    g2.fillRect(0, 0, width, height);
    //设置颜色
    g2.setColor(Color.WHITE);
    // 将图片大小设置为大约4cm*4cm 具体根据纸张大小设定
    g2.drawImage(image2, 10, 85, 135, 135,null);
    g2.drawRect(0, 0, width - 1, height - 1);
    //设置字体:字体、字号、大小
    g2.setFont(new Font("黑体", Font.BOLD, 26));
    //设置背景颜色
    g2.setColor(Color.BLACK);
    //向图片上写字符串
    g2.drawString("东久", 15, 30);
    g2.setFont(new Font("黑体", Font.BOLD, 20));
    g2.drawString("·", 65, 28);
    g2.setFont(new Font("黑体", Font.BOLD, 26));
    g2.drawString("佳能", 83, 30);
    g2.setFont(new Font("黑体", Font.BOLD, 18));
    g2.drawString("iR-ADV C3525", 15, 60);
    g2.setFont(new Font("黑体", Font.ITALIC, 14));
    g2.drawString(code, 30, 80);
    // 图片上传后的路径
    String savePath = "D:/a.jpg";
    ImageIO.write(bi, "JPEG", new FileOutputStream(savePath));
    return savePath;
    }catch (Exception e){
    log.error("生成图片错误",e);
    throw new BusinessException("生成图片错误",e);
    }finally {
    if(fileInputStream != null){
    try {
    fileInputStream.close();
    } catch (IOException e) {
    e.printStackTrace();
    }
    }
    }
    }
    }
    编写测试类
    package com.orisdom.utils;

    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.springframework.boot.test.context.SpringBootTest;
    import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

    /**
    * @author chenxiaokang
    * @date 2019/7/8 15:39
    */
    @RunWith(SpringJUnit4ClassRunner.class)
    @SpringBootTest
    public class ImageBuilderUtilsTest {

    @Test
    public void imageBuilder() {
    String path = ImageBuilderUtils.ImageBuilder("NO.1132323232","D:\toolschenxiaokang\qrcode_for_gh_02122d6b2ea8_258.jpg");
    System.out.println(path);
    }
    }

    最后生成的图片模板

    
    
  • 相关阅读:
    RTSP协议转RTMP协议的行业视频接入网关EasyRTSPLive如何实现音频转码的
    RTSP协议转RTMP协议的行业视频接入网关EasyRTSPLive之跨平台ini配置及通道的方法
    GB/T28181协议EasyGBS播放1080p视频直播会花屏
    国标GB/T28181协议下播放器起播慢或者延迟高如何解决?
    EasyGBS查找大华设备的录像列表时失败
    ffmpeg增加h264编解码功能模块方法
    EasyNVR控制台运行出现invalid license关于计算机保护软件类似于360、腾讯云管家等限制相关问题
    摄像机经过多级路由转换无法被EasyNVR拉流问题处理方法
    使用EasyNVR软件对接海康摄像头对接失败问题解析
    GB/T28181协议使用EasyNVR降低播放延迟方法
  • 原文地址:https://www.cnblogs.com/xiaokangk/p/11151774.html
Copyright © 2011-2022 走看看