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
package com.ruoyi.auction.util;
 
import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.QRCodeWriter;
import com.ruoyi.system.api.util.HuaWeiOBSUtil;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Base64;
import java.util.HashMap;
 
public class CreateQrCode {
 
    private static final String base64Url = "data:image/png;base64,";
 
    /**
     * 创建二维码
     *
     * @param
     * @param
     * @return
     * @throws IOException
     * @throws WriterException
     */
    public static String createQRCode(String json) throws IOException, WriterException {
        QRCodeWriter qrCodeWriter = new QRCodeWriter();
 
        HashMap<EncodeHintType, Object> hints = new HashMap<>();
        hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
        BitMatrix bitMatrix = qrCodeWriter.encode(json, BarcodeFormat.QR_CODE, 600, 600, hints);
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        MatrixToImageWriter.writeToStream(bitMatrix, "PNG", outputStream);
        Base64.Encoder encoder = Base64.getEncoder();
        String text = encoder.encodeToString(outputStream.toByteArray());
        return base64Url + text;
    }
 
    /**
     * 创建二维码
     *
     * @param
     * @param
     * @return
     * @throws IOException
     * @throws WriterException
     */
    public static String createQRCodeToObs(String json) throws IOException, WriterException {
        QRCodeWriter qrCodeWriter = new QRCodeWriter();
 
        HashMap<EncodeHintType, Object> hints = new HashMap<>();
        hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
        BitMatrix bitMatrix = qrCodeWriter.encode(json, BarcodeFormat.QR_CODE, 600, 600, hints);
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        MatrixToImageWriter.writeToStream(bitMatrix, "PNG", outputStream);
        InputStream is = new ByteArrayInputStream(
                outputStream.toByteArray());
        return HuaWeiOBSUtil.obsUploadStream(json, is);
    }
 
    public static void main(String[] args) throws IOException, WriterException {
        String qrCodeToObs = createQRCodeToObs("123456");
        System.out.println(qrCodeToObs);
    }
}