Houjie
2025-05-26 7f99e4a369319397c44767b5e8d558fefa049c4e
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
package org.jeecg.modules.tms.utils;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.QRCodeWriter;
 
import javax.print.*;
import javax.print.attribute.HashPrintRequestAttributeSet;
import javax.print.attribute.PrintRequestAttributeSet;
import javax.print.attribute.standard.Copies;
import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.awt.print.*;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.util.HashMap;
import java.util.Map;
 
 
public class QrCodeUtils {
    /**
     * 生成二维码图片
     * @param content 二维码内容
     * @param width   图片宽度
     * @param height  图片高度
     * @return BufferedImage 对象
     */
    public static BufferedImage generateQrCode(String content, int width, int height) {
        try {
            Map<EncodeHintType, Object> hints = new HashMap<>();
            hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
            QRCodeWriter writer = new QRCodeWriter();
            BitMatrix bitMatrix = writer.encode(content, BarcodeFormat.QR_CODE, width, height, hints);
            return MatrixToImageWriter.toBufferedImage(bitMatrix);
        } catch (Exception e) {
            throw new RuntimeException("生成二维码失败", e);
        }
    }
 
    /**
     * 打印二维码
     */
    public static void printBarcode(final BufferedImage image) {
        PrinterJob job = PrinterJob.getPrinterJob();
 
        // 设置打印页面
        PageFormat pf = job.defaultPage();
        Paper paper = pf.getPaper();
 
        // 设置纸张大小匹配条形码图像
        paper.setSize(image.getWidth(), image.getHeight() + 50);
        paper.setImageableArea(0, 0, image.getWidth(), image.getHeight() + 50);
        pf.setPaper(paper);
 
        // 设置打印内容
        job.setPrintable(new Printable() {
            @Override
            public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) {
                if (pageIndex > 0) {
                    return NO_SUCH_PAGE;
                }
 
                Graphics2D g2d = (Graphics2D) graphics;
                g2d.translate(pageFormat.getImageableX(), pageFormat.getImageableY());
                g2d.drawImage(image, 0, 0, null);
 
                return PAGE_EXISTS;
            }
        }, pf);
 
        // 显示打印对话框
        if (job.printDialog()) {
            try {
                job.print();
                JOptionPane.showMessageDialog(null, "打印任务已发送到打印机");
            } catch (PrinterException e) {
                JOptionPane.showMessageDialog(null, "打印失败: " + e.getMessage());
                e.printStackTrace();
            }
        }
    }
 
 
    /**
     * 默认打印机打印二维码
     */
    private static void printBarcode2(BufferedImage image) throws Exception {
        // 1. 将BufferedImage转换为字节数组
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        javax.imageio.ImageIO.write(image, "png", baos);
        byte[] imageData = baos.toByteArray();
 
        // 2. 查找打印服务
        PrintService[] services = PrintServiceLookup.lookupPrintServices(null, null);
        if (services.length == 0) {
            throw new Exception("没有找到可用的打印机");
        }
 
        // 3. 选择默认打印机(也可以让用户选择)
        PrintService defaultPrinter = PrintServiceLookup.lookupDefaultPrintService();
        if (defaultPrinter == null) {
            defaultPrinter = services[0]; // 使用第一个找到的打印机
        }
 
        // 4. 创建打印作业
        DocPrintJob job = defaultPrinter.createPrintJob();
 
        // 5. 设置打印属性
        PrintRequestAttributeSet attributes = new HashPrintRequestAttributeSet();
        attributes.add(new Copies(1)); // 打印份数
 
        // 6. 创建打印文档
        DocFlavor flavor = DocFlavor.INPUT_STREAM.PNG;
        Doc doc = new SimpleDoc(new ByteArrayInputStream(imageData), flavor, null);
 
        // 7. 执行打印
        job.print(doc, attributes);
    }
}