You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Java实现Brother QL-810W热敏打印机打印带下方文字的二维码问题求助

解决Brother QL-810W打印带下方数字的二维码问题

Hey there! Let's figure out how to get your Brother QL-810W printing that QR code with a small number underneath. The key issue here is that the QL series uses Brother's proprietary QL Command Language, not standard ESC/POS—so libraries like escpos-coffe (built for receipt printers) won't work out of the box. Let's go through two practical solutions:


方案1:合成二维码+数字为一张图片(最适合新手)

Since you already know how to print PNG images, the simplest fix is to create a single image that includes both your QR code and the small number below it, then print that combined image. Here's how to do it in Java:

代码示例:合成图片并打印

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import javax.print.*;
import javax.print.attribute.HashPrintRequestAttributeSet;
import javax.print.attribute.standard.MediaSizeName;
import javax.print.attribute.standard.OrientationRequested;

public class QRWithNumberPrinter {
    public static void main(String[] args) {
        try {
            // 1. 加载已有的二维码图片
            BufferedImage qrImage = ImageIO.read(new File("C:/Local/QRCode1.png"));
            
            // 2. 创建新的图片,预留下方数字的空间(比如在二维码下方加30像素高度)
            int newHeight = qrImage.getHeight() + 30;
            BufferedImage combinedImage = new BufferedImage(qrImage.getWidth(), newHeight, BufferedImage.TYPE_INT_ARGB);
            Graphics2D g2d = combinedImage.createGraphics();
            
            // 3. 绘制二维码到新图片顶部
            g2d.drawImage(qrImage, 0, 0, null);
            
            // 4. 设置字体样式(小数字)
            g2d.setFont(new Font("Arial", Font.PLAIN, 12));
            g2d.setColor(Color.BLACK);
            
            // 5. 计算数字的位置,居中显示在二维码下方
            String number = "12345";
            FontMetrics metrics = g2d.getFontMetrics();
            int x = (combinedImage.getWidth() - metrics.stringWidth(number)) / 2;
            int y = qrImage.getHeight() + metrics.getAscent() + 5; // 5像素间距
            
            // 6. 绘制数字
            g2d.drawString(number, x, y);
            g2d.dispose();
            
            // 7. 保存合成后的图片(可选,用于预览)
            ImageIO.write(combinedImage, "PNG", new File("C:/Local/QRWithNumber.png"));
            
            // 8. 用你原有的打印代码打印合成后的图片
            DocFlavor flavor = DocFlavor.INPUT_STREAM.PNG;
            PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();
            aset.add(OrientationRequested.PORTRAIT);
            aset.add(MediaSizeName.INVOICE);
            
            PrintService[] services = PrintServiceLookup.lookupPrintServices(null, null);
            for (PrintService printService : services) {
                if (printService.getName().equals("Brother QL-810W")) {
                    DocPrintJob pj = printService.createPrintJob();
                    FileInputStream fis = new FileInputStream("C:/Local/QRWithNumber.png");
                    Doc doc = new SimpleDoc(fis, flavor, null);
                    pj.print(doc, aset);
                    break;
                }
            }
            
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

方案2:直接发送Brother QL命令(更灵活)

If you want more control over print positioning and formatting, you can send raw QL commands directly to the printer. You'll need to reference the Brother QL-810W Programming Manual for full command details, but here's a basic example to print text and a QR code:

代码示例:发送QL命令

import javax.print.PrintService;
import javax.print.PrintServiceLookup;
import java.io.OutputStream;
import javax.print.StreamPrintService;

public class QLPrinterCommands {
    public static void main(String[] args) {
        try {
            PrintService[] services = PrintServiceLookup.lookupPrintServices(null, null);
            PrintService qlPrinter = null;
            for (PrintService service : services) {
                if (service.getName().equals("Brother QL-810W")) {
                    qlPrinter = service;
                    break;
                }
            }
            
            if (qlPrinter != null) {
                // 获取打印机输出流
                StreamPrintService streamService = (StreamPrintService) qlPrinter;
                OutputStream out = streamService.getOutputStream();
                
                // QL命令初始化(参考手册)
                out.write(new byte[]{0x1B, 0x40}); // 重置打印机
                
                // 设置打印位置和字体(打印小数字)
                out.write(new byte[]{0x1B, 0x61, 0x01}); // 居中对齐
                out.write(new byte[]{0x1B, 0x21, 0x00}); // 字体大小:标准
                out.write("12345".getBytes()); // 要打印的数字
                out.write(new byte[]{0x0A}); // 换行
                
                // 打印二维码(这里假设你有QR码的字节数据,或者用命令生成)
                // 示例:发送QR码生成命令(具体参数需参考手册)
                out.write(new byte[]{0x1B, 0x5A, 0x01, 0x00, 0x00, 0x00}); // 二维码命令前缀
                // 这里需要填充QR码的实际数据,或者使用图片转字节的方式
                // 如果你已经有QR码的PNG,可以将其转换为单色字节流发送
                
                out.write(new byte[]{0x1B, 0x64, 0x02}); // 切纸命令
                out.flush();
                out.close();
            }
            
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

注意事项:

  • Brother QL命令的细节需要严格遵循官方手册,比如二维码的生成命令需要指定尺寸、纠错等级和数据内容。
  • 如果直接发送图片数据,需要将PNG转换为单色(黑白)的字节流,符合QL打印机的图像格式要求。

为什么escpos-coffe没用?

The escpos-coffe library sends standard ESC/POS commands, which are designed for receipt printers (like Epson models). The Brother QL series doesn't support ESC/POS—it uses its own command set, so those commands are ignored by the printer. That's why your escpos-coffe code didn't produce any text output.

内容的提问来源于stack exchange,提问作者Lukas

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.30 05:49:07