cuilei
2025-06-20 ac4d1a5962441156ff22a0c87f4eacc083daa9a6
lxzn-module-tms/src/main/java/org/jeecg/modules/tms/utils/QrCodePrinterUtils.java
@@ -1,74 +1,92 @@
package org.jeecg.modules.tms.utils;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.QRCodeWriter;
import liquibase.pro.packaged.S;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import javax.print.*;
import javax.print.attribute.HashPrintRequestAttributeSet;
import javax.print.attribute.PrintRequestAttributeSet;
import java.awt.print.*;
import java.awt.image.BufferedImage;
import java.awt.*;
import java.io.ByteArrayOutputStream;
import java.io.OutputStream;
import java.util.*;
public class QrCodePrinterUtils implements Printable {
@RestController
@RequestMapping("/tms/qyCode")
@Slf4j
public class QrCodePrinterUtils{
    private final BufferedImage image;
    @GetMapping("/generate")
    public Result<Map<String, String>> generateQrCode() throws Exception {
        String content = "Q201204330002-0001";
        // 生成二维码图片(Base64)
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        generateQRCodeImage(content, 200, 200, os);
        String base64Img = "data:image/png;base64," + Base64.getEncoder().encodeToString(os.toByteArray());
    public QrCodePrinterUtils(BufferedImage image) {
        this.image = image;
        // 返回结果
        Map<String, String> result = new HashMap<>();
        result.put("image", base64Img);
        result.put("content", content);
        return Result.OK(result);
    }
    /**
     * 打印二维码
     */
    public void print() {
        PrinterJob job = PrinterJob.getPrinterJob();
        job.setPrintable(this);
        // 弹出打印对话框(可选)
        if (job.printDialog()) {
            try {
                job.print();
            } catch (PrinterException e) {
                System.err.println("打印失败: " + e.getMessage());
            }
        }
    public static void generateQRCodeImage(String text, int width, int height, OutputStream os) throws Exception {
        QRCodeWriter qrCodeWriter = new QRCodeWriter();
        BitMatrix bitMatrix = qrCodeWriter.encode(text, BarcodeFormat.QR_CODE, width, height);
        MatrixToImageWriter.writeToStream(bitMatrix, "PNG", os);
    }
    /**
     * 实现 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());
        // 计算居中坐标
        double pageWidth = pageFormat.getImageableWidth();
        double pageHeight = pageFormat.getImageableHeight();
        double imgWidth = image.getWidth();
        double imgHeight = image.getHeight();
        double scale = Math.min(pageWidth / imgWidth, pageHeight / imgHeight);
        g2d.scale(scale, scale);
        g2d.drawImage(image, 0, 0, null);
        return PAGE_EXISTS;
    }
    /**
     * 使用默认打印机打印,不弹对话框
     */
    public static void noDialogPrint(){
        BufferedImage qrImage = QrCodeUtils.generateQrCode("Silent Print", 300, 300);
        QrCodePrinterUtils printer = new QrCodePrinterUtils(qrImage);
        PrinterJob job = PrinterJob.getPrinterJob();
        job.setPrintable(printer);
    public static String generateBacthQRCode(String content, int width, int height) {
        try {
            job.print();
        } catch (PrinterException e) {
            e.printStackTrace();
            Map<EncodeHintType, Object> hints = new HashMap<>();
            hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
            hints.put(EncodeHintType.MARGIN, 1);
            BitMatrix matrix = new MultiFormatWriter().encode(
                    content,
                    BarcodeFormat.QR_CODE,
                    width,
                    height,
                    hints
            );
            ByteArrayOutputStream os = new ByteArrayOutputStream();
            MatrixToImageWriter.writeToStream(matrix, "PNG", os);
            return "data:image/png;base64," +
                    Base64.getEncoder().encodeToString(os.toByteArray());
        } catch (Exception e) {
            throw new RuntimeException("生成二维码失败", e);
        }
    }
    @GetMapping("/batchGenerate")
    public Result<List<Map<String,String>>> generateBatchQrCode(){
        List<Map<String,String>> results = new ArrayList<>();
        List<String> contentList = new ArrayList<>();
        contentList.add("Q201204330002-0001");
        contentList.add("Q201204330002-0002");
        contentList.add("Q201204330002-0003");
        for(String content : contentList){
            Map<String,String> result = new HashMap<>();
            result.put("content",content);
            result.put("image",generateBacthQRCode(content, 200, 200));
            // 生成二维码图片(Base64)
            results.add(result);
        }
        return Result.OK(results);
    }
}