cuilei
2025-06-20 ac4d1a5962441156ff22a0c87f4eacc083daa9a6
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
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.io.ByteArrayOutputStream;
import java.io.OutputStream;
import java.util.*;
 
@RestController
@RequestMapping("/tms/qyCode")
@Slf4j
public class QrCodePrinterUtils{
 
    @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());
 
        // 返回结果
        Map<String, String> result = new HashMap<>();
        result.put("image", base64Img);
        result.put("content", content);
        return Result.OK(result);
    }
 
    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);
    }
 
    public static String generateBacthQRCode(String content, int width, int height) {
        try {
            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);
    }
}