Java生成二维码QRCode(亲测可通过扫码枪扫出)

背景

项目上对接一个支付渠道,C Scan B(用户扫商家收款码)场景下,我们会先调用Xx支付渠道,它返回给我一个收款码字符串,我需要把这个收款码搞成QRCode。

代码

1、Maven依赖

<!--qrcode-->
<dependency>
    <groupId>com.google.zxing</groupId>
    <artifactId>core</artifactId>
    <version>3.5.0</version>
</dependency>
<dependency>
    <groupId>com.google.zxing</groupId>
    <artifactId>javase</artifactId>
    <version>3.5.0</version>
</dependency>

2、工具类

package com.saint.base.util;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.WriterException;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.QRCodeWriter;
import lombok.extern.slf4j.Slf4j;
import sun.misc.BASE64Encoder;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.file.Paths;

/**
 * generate QR Code
 *
 * @author Saint
 */
@Slf4j
public class GenerateQRCodeUtil {
    
    

    private static final String IMAGE_FORMAT = "PNG";

    private static final String PNG_BASE64_PRE = "data:image/png;base64,";

    public static String generateQRCodeToBase64(String text) {
    
    
        String qrCode = generateQRCodeToBase64(text, 360, 360);
        return qrCode;
    }

    /**
     * generate QR Code data, format --> data:image/png:base64
     */
    public static String generateQRCodeToBase64(String text, int width, int height) {
    
    
        byte[] bytes = generateQRCodeBytes(text, width, height);
        BASE64Encoder encoder = new BASE64Encoder();

        // bytes to base64 string, and delete \r\n
        String image_base64 = encoder.encodeBuffer(bytes).trim().replaceAll("\n", "").replaceAll("\r", "");
        String pngBase64 = PNG_BASE64_PRE + image_base64;
        return pngBase64;
    }

    /**
     * generate QR Code into Stream
     *
     * @param outputStream outputStream
     */
    public static void generateQRCodeToStream(String text, int width, int height, OutputStream outputStream) {
    
    
        BitMatrix bitMatrix = getQRCodeBitMatrix(text, width, height);
        try {
    
    
            // Write QR Code image into stream
            MatrixToImageWriter.writeToStream(bitMatrix, IMAGE_FORMAT, outputStream);
        } catch (IOException e) {
    
    
            log.error("Could not generate QRCode to Stream, ", e);
        }
    }

    /**
     * generate QR Code into file
     *
     * @param filePath file path
     */
    public static void generateQRCodeToFile(String text, int width, int height, String filePath) {
    
    
        BitMatrix bitMatrix = getQRCodeBitMatrix(text, width, height);
        try {
    
    
            // Write QR Code image into file
            MatrixToImageWriter.writeToPath(bitMatrix, IMAGE_FORMAT, Paths.get(filePath));
        } catch (IOException e) {
    
    
            log.error("Could not generate QRCode to FilePath: {}, ", filePath, e);
        }
    }

    /**
     * generate qrCode bitMatrix by text / width / height
     *
     * @param text   qrCode context
     * @param width  qrCode width
     * @param height qrCode height
     * @return BitMatrix
     */
    public static BitMatrix getQRCodeBitMatrix(String text, int width, int height) {
    
    
        QRCodeWriter qrCodeWriter = new QRCodeWriter();
        BitMatrix bitMatrix = null;
        try {
    
    
            bitMatrix = qrCodeWriter.encode(text, BarcodeFormat.QR_CODE, width, height);
        } catch (WriterException e) {
    
    
            log.error("Could not generate QR Code, WriterException is : {}", e);
        }
        return bitMatrix;
    }

    /**
     * obtain QR Code bytes
     *
     * @return byte[]
     */
    public static byte[] generateQRCodeBytes(String text, int width, int height) {
    
    
        byte[] pngData;
        ByteArrayOutputStream pngOutputStream = new ByteArrayOutputStream();
        generateQRCodeToStream(text, width, height, pngOutputStream);
        pngData = pngOutputStream.toByteArray();
        return pngData;
    }
    

    public static void main(String[] args) {
    
    
        // 此处返回的PNG的图片base64数据,有需要自行换方式使用
        final String s = generateQRCodeToBase64("12434324");
        System.out.println(s);
        
    }
}

3、效果图

。。。。很难受。。。。二维码挑不出来,CSDN会判断图片违规。

猜你喜欢

转载自blog.csdn.net/Saintmm/article/details/125577627