• java 实现二维码打印并设计标题


    概要

    小编最近业务需求牵扯到自动打印小票,小编一开始的思路前端处理打印功能奈何前端调用浏览器几乎都需要预览这一步,重点为何是几乎应为有些插件可以实现自动打印用能的可是但是需要“超能力”。于是只能专攻后端路线了,这里小编先给各位看官抽出java生成二维码一部分代码后续自动打印等功能后续一一解析。本文代码jar引入后可直接使用根据自身业务进行调整即可
    环境:jdk8,maven
    框架:SpringBoot
    引用jar:

    
            
                com.google.zxing
                core
                3.3.3
            
            
                com.google.zxing
                javase
                3.3.3
            
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    成果描述:
    在这里插入图片描述

    源码

    import cn.hutool.core.util.StrUtil;
    import cn.hutool.extra.qrcode.QrCodeUtil;
    import cn.hutool.extra.qrcode.QrConfig;
    import sun.misc.BASE64Encoder;
    import javax.imageio.ImageIO;
    import java.awt.*;
    import java.awt.image.BufferedImage;
    import java.io.*;
    
    /**
     * @ClassName 合成二维码工具类
     * @Description TODO
     * @Author wang yan
     * @Date 2023/8/30 9:35
     */
    public class QrCoder {
        /**
         * 图片的宽度
         */
        private static final int IMAGE_WIDTH = 350;
        /**
         * 图片的高度
         */
        private static final int IMAGE_HEIGHT = 350;
        /**
         * 二维码的宽度
         */
        private static final int QR_CODE_WIDTH = 150;
        /**
         * 二维码的高
         */
        private static final int QR_CODE_HEIGHT = 150;
    	/**
    	* 文件格式
    	*/
        private static final String FORMAT_NAME = "JPG";
    
        /**
         * @param imgLogo logo图片
         * @param title   头部标题
         * @param content 内容 
    * @param footer 底部文字 */
    public static BufferedImage createQrCode(String imgLogo, String title, String content, String footer) { // 头部文字区域高度 int titleHeight = 40; // 创建主模板图片 BufferedImage image = new BufferedImage(IMAGE_WIDTH, IMAGE_HEIGHT, BufferedImage.TYPE_INT_RGB); Graphics2D main = image.createGraphics(); // 设置图片的背景色 main.setColor(Color.white); //白色 main.fillRect(0, 0, IMAGE_WIDTH, IMAGE_HEIGHT); // 动态高度 int height = 0; //============页面头部 文字============ Graphics2D titleRight = image.createGraphics(); // 设置字体颜色 titleRight.setColor(Color.black); Font titleFont = new Font("宋体", Font.BOLD, 25); titleRight.setFont(titleFont); titleRight.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_LCD_HRGB); // 居中 x开始的位置:(图片宽度-字体大小*字的个数)/2 int x = (IMAGE_WIDTH - (titleFont.getSize() * title.length())) / 2; titleRight.drawString(title, x, (titleHeight) / 2 + 10); height += titleHeight; //==========中间文字部分====== Graphics2D centerWord = image.createGraphics(); // 设置字体颜色,先设置颜色,再填充内容 centerWord.setColor(Color.black); Font wordFont = new Font("宋体", Font.PLAIN, 15); centerWord.setFont(wordFont); centerWord.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_LCD_HRGB); String[] info = content.split("-"); for (String s : info) { // x开始的位置:(图片宽度-字体大小*字的个数)/2 int strWidth = centerWord.getFontMetrics().stringWidth(s); // 总长度减去文字长度的一半 (居中显示) int startX = (IMAGE_WIDTH - strWidth) / 2; height += 20; centerWord.drawString(s, startX, height); } //=====插入二维码图片====== Graphics codePic = image.getGraphics(); BufferedImage codeImg; QrConfig config = new QrConfig(); config.setWidth(QR_CODE_WIDTH); config.setHeight(QR_CODE_HEIGHT); if (StrUtil.isNotBlank(imgLogo)) { config.setImg(imgLogo); } codeImg = QrCodeUtil.generate(content, config); // 绘制二维码 codePic.drawImage(codeImg, (IMAGE_WIDTH - QR_CODE_WIDTH) / 2, height, QR_CODE_WIDTH, QR_CODE_HEIGHT, null); codePic.dispose(); //=====底部文字=== Graphics2D typeLeft = image.createGraphics(); // 设置字体颜色 typeLeft.setColor(Color.black); // 设置字体 Font footerFont = new Font("宋体", Font.PLAIN, 10); typeLeft.setFont(footerFont); typeLeft.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_LCD_HRGB); // x开始的位置:(图片宽度-字体大小*字的个数)/2 int startX = (IMAGE_WIDTH - (footerFont.getSize() * footer.length())) / 2; height += QR_CODE_HEIGHT; typeLeft.drawString(footer, startX, height); return image; } /** * @param imgLogo logo图片 * @param title 头部标题 多个以-隔开自动换行 * @param middle 文字中间描述 * @param content 内容 * @param footer 底部文字 */ public static BufferedImage createQrCode(String imgLogo, String title,String middle, String content, String footer) { int titleHeight = 50; // 创建主模板图片 BufferedImage image = new BufferedImage(IMAGE_WIDTH, IMAGE_HEIGHT, BufferedImage.TYPE_INT_RGB); Graphics2D main = image.createGraphics(); // 设置图片的背景色 main.setColor(Color.white); main.fillRect(0, 0, IMAGE_WIDTH, IMAGE_HEIGHT); // 动态高度 int height = 0; //=========页面头部 文字===== Graphics2D titleRight = image.createGraphics(); // 设置字体颜色 titleRight.setColor(Color.black); Font titleFont = new Font("宋体", Font.BOLD, 14); titleRight.setFont(titleFont); titleRight.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_LCD_HRGB); // 居中 x开始的位置:(图片宽度-字体大小*字的个数)/2 int x = (IMAGE_WIDTH - (titleFont.getSize() * title.length())) / 2; titleRight.drawString(title, x, (titleHeight) / 2 + 10); height += titleHeight; //======中间文字部分===== Graphics2D centerWord = image.createGraphics(); // 设置字体颜色,先设置颜色,再填充内容 centerWord.setColor(Color.black); Font wordFont = new Font("宋体", Font.PLAIN, 12); centerWord.setFont(wordFont); centerWord.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_LCD_HRGB); String[] info = middle.split("-"); for (String s : info) { // x开始的位置:(图片宽度-字体大小*字的个数)/2 int strWidth = centerWord.getFontMetrics().stringWidth(s); // 总长度减去文字长度的一半 (居中显示) int startX = (IMAGE_WIDTH - strWidth) / 2; height += 20; centerWord.drawString(s, startX, height); } //====插入二维码图片====== Graphics codePic = image.getGraphics(); BufferedImage codeImg; QrConfig config = new QrConfig(); config.setWidth(QR_CODE_WIDTH); config.setHeight(QR_CODE_HEIGHT); if (StrUtil.isNotBlank(imgLogo)) { config.setImg(imgLogo); } codeImg = QrCodeUtil.generate(content, config); // 绘制二维码 codePic.drawImage(codeImg, (IMAGE_WIDTH - QR_CODE_WIDTH) / 2, height, QR_CODE_WIDTH, QR_CODE_HEIGHT, null); codePic.dispose(); //=====底部描述===== if (StrUtil.isNotBlank(footer)){ Graphics2D typeLeft = image.createGraphics(); // 设置字体颜色 typeLeft.setColor(Color.black); Font footerFont = new Font("宋体", Font.PLAIN, 10); typeLeft.setFont(footerFont); typeLeft.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_LCD_HRGB); // x开始的位置:(图片宽度-字体大小*字的个数)/2 int startX = (IMAGE_WIDTH - (footerFont.getSize() * footer.length())) / 2; height += QR_CODE_HEIGHT; typeLeft.drawString(footer, startX, height); } return image; } // 生成图片文件 public static void createImage(BufferedImage image, String fileLocation) { if (image != null) { try { ImageIO.write(image, "png", new File(fileLocation)); } catch (IOException e) { e.printStackTrace(); } } } }
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165
    • 166
    • 167
    • 168
    • 169
    • 170
    • 171
    • 172
    • 173
    • 174
    • 175
    • 176
    • 177
    • 178
    • 179
    • 180
    • 181
    • 182
    • 183
    • 184
    • 185
    • 186
    • 187
    • 188
    • 189
    • 190
    • 191
    • 192
    • 193
    • 194
    • 195
    • 196
    • 197
    • 198
    • 199

    以上就是生成二维码的基础配置代码了,根据个人需求进行调整

    调用

    public static void main(String[] args) throws IOException, PrinterException {
     String content = "A00180901000001";
            BufferedImage bufferedImage = createQrCode(null, "计量打印","车牌号:A86fg-运单号"+ content +"-发货单位:上海码头" + "-2023/08/30", content, null);
            createImage(bufferedImage, "D:/test6.png");
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    小结

    创建spring项目引入相关jar包对mave没有特殊要求 jdk8,以上就是实现整体流程,有点让小编琢磨不透的是 二维码图片渲染与文字接触部分可能会存在汉字展示不全问题,所以小编这里二维码与文字接触部以时间隔开了;

  • 相关阅读:
    .NET 程序读取当前目录避坑指南
    JavaScript闭包
    基于leetcode的算法训练:Day3
    图形库篇 | EasyX | 基本介绍
    rerank来提升RAG的准确度的策略
    STL应用——vector
    NLP | 注意力机制Attention Mechannism图文详解及代码
    linux系统docker的使用命令
    css第十二课:盒子模型
    C语言实现“队列“
  • 原文地址:https://blog.csdn.net/selectDelete/article/details/132643441