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

网友投稿 356 2022-11-15

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

背景

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

代码

1、Maven依赖

com.google.zxing core 3.5.0 com.google.zxing javase 3.5.0

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 */@Slf4jpublic 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、效果图

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

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:k-近邻算法
下一篇:SPI总线的特点工作方式以及常见错误解析
相关文章

 发表评论

暂时没有评论,来抢沙发吧~