Java生成二维码,Spring Boot整合ZXing实现二维码生成,支持自定义二维码

前言

最近在项目中开发过程中,需要用到生成二维码的工具,经过考量后,选择了Google的ZXing工具来实现,因为ZXing能很好的通过Maven的方式集成进去,本篇文章主要介绍ZXing的快速集成和使用,通过本篇文章能快速使用ZXing组件进行二维码的快速开发。

Spring Boot整合ZXing实现二维码生成

一、基础环境


Java JDK1.8
Spring Boot2.4.3
Apache-Maven-3.6.3

二、新增Spring Boot整合ZXing的依赖


在项目工程pom.xml配置文件中加入ZXing的依赖

        
        
            com.google.zxing
            javase
            3.3.0
        
        
            com.google.zxing
            core
            3.3.0
        

三、新增常量类


在Constant.java常量类中新增ZXing配置参数

package com.huazai.platform.commons.constant;

/**
 * Company:     b2c哎呦商城
 * Department:  研发一组
 * Title:       [aiyou — User模块]
 * Description: [Constant 常量模块]
 * Created on:  2020-04-26
 * Contacts:    [[email protected]]
 *
 * @author huazai
 * @version V1.1.0
 */
public interface Constant {

    /** ======================== 数据库业务逻辑状态 =======================*/
    /**
     * 正常
     */
    Integer LOGIC_STATE_NORMAL = 0;
    /**
     * 删除
     */
    Integer LOGIC_STATE_DELETE = 1;


    /** ======================== true or false =======================*/
    boolean IS_TRUE = true;
    boolean IS_FALSE = false;


    /** ======================== 二维码业务相关 =======================*/
    /**
     * 默认宽度
     */
    Integer DEFAULT_WIDTH = 150;
    /**
     * 默认高度
     */
    Integer DEFAULT_HEIGHT = 150;
    /**
     * LOGO 默认宽度
     */
    Integer LOGO_DEFAULT_WIDTH = 30;
    /**
     * LOGO 默认高度
     */
    Integer LOGO_DEFAULT_HEIGHT = 30;
    /**
     * 默认格式
     */
    String DEFAULT_IMAGE_FORMAT = "png";
    /**
     * 默认编码
     */
    String DEFAULT_CHARSET = "UTF-8";
    /**
     * 如果想要生成的二维码直接被前端解析,需要带上:data:image/png;base64,否则需要前端加上这个字符串才能正常解析
     */
    String BASE64_IMAGE = "data:image/png;base64,%s";

}

四、新建二维码工具类


在QRCodeUtil.java二维码工具类中,新增自定义二维码生成的业务逻辑

package com.huazai.platform.commons.utils;

import cn.hutool.core.codec.Base64;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.Writer;
import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.datamatrix.encoder.SymbolShapeHint;
import com.google.zxing.qrcode.QRCodeWriter;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
import com.huazai.platform.commons.constant.Constant;
import com.huazai.platform.commons.exception.AppException;
import lombok.experimental.UtilityClass;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import java.awt.*;
import java.awt.geom.RoundRectangle2D;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;

/**
 * Company:     b2c哎呦商城
 * Department:  研发一组
 * Title:       [aiyou — User模块]
 * Description: [QRCodeUtil 工具类]
 * Created on:  2020-07-13
 * Contacts:    [[email protected]]
 *
 * @author huazai
 * @version V1.1.0
 */
@UtilityClass
@Slf4j
public class QRCodeUtil {


    /**
     * Description:[二维码生成方法(Base64格式),不带LOGO]
     *
     * @param jsonContent [生成二维码中的内容]
     * @return String
     * @date 2020-07-13
     * @author huazai
     */
    public String getBase64QRCode(String jsonContent) throws AppException {

        return generateBase64QRCode(jsonContent, Constant.DEFAULT_WIDTH, Constant.DEFAULT_HEIGHT, null, null, null);
    }

    /**
     * Description:[二维码生成方法(Base64格式),带LOGO]
     *
     * @param jsonContent [生成二维码中的内容]
     * @return String
     * @date 2020-07-13
     * @author huazai
     */
    public String getBase64QRCode(String jsonContent, String logoUrl) throws AppException {

        return generateBase64QRCode(jsonContent, Constant.DEFAULT_WIDTH, Constant.DEFAULT_HEIGHT, logoUrl, Constant.LOGO_DEFAULT_WIDTH, Constant.LOGO_DEFAULT_HEIGHT);
    }

    /**
     * Description:[二维码生成方法(Base64格式)]
     *
     * @param jsonContent [生成二维码中的内容]
     * @param width       [二维码宽度]
     * @param height      [二维码高度]
     * @param logoUrl     [Logo 地址]
     * @param logoWidth   [Logo 宽度]
     * @param logoHeight  [Logo 高度]
     * @return String
     * @date 2020-07-13
     * @author huazai
     */
    private String generateBase64QRCode(String jsonContent, Integer width, Integer height, String logoUrl, Integer logoWidth, Integer logoHeight) throws AppException {
        // 字节输出流
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();

        try {
            // 生成二维码
            BufferedImage bufferedImage = generateQRCode(jsonContent, width, height, logoUrl, logoWidth, logoHeight);
            // 写入图片流
            ImageIO.write(bufferedImage, Constant.DEFAULT_IMAGE_FORMAT, outputStream);
        } catch (IOException e) {
            log.error("generateBase64QRCode 二维码Base64Code生成异常:{}", e.getMessage());
            throw new AppException("generateBase64QRCode 二维码Base64Code生成异常");
        }

        return String.format(Constant.BASE64_IMAGE, Base64.encode(outputStream.toByteArray()));
    }

    /**
     * Description:[获取简单二维码]
     *
     * @param jsonContent  [二维码内容]
     * @param outputStream [输出流]
     * @return void
     * @date 2020-07-13
     * @author huazai
     */
    public void getQRCode(String jsonContent, OutputStream outputStream) throws AppException, IOException {
        if (StringUtils.isNotEmpty(jsonContent)) {
            BufferedImage bufferedImage = generateQRCode(jsonContent, Constant.DEFAULT_WIDTH, Constant.DEFAULT_HEIGHT, null, null, null);

            ImageIO.write(bufferedImage, Constant.DEFAULT_IMAGE_FORMAT, outputStream);
        }
    }

    /**
     * Description:[获取带LOGO二维码]
     *
     * @param jsonContent  [二维码内容]
     * @param logoUrl      [LOGO地址]
     * @param outputStream [输出流]
     * @return void
     * @date 2020-07-13
     * @author huazai
     */
    public void getQRCode(String jsonContent, String logoUrl, OutputStream outputStream) throws AppException, IOException {
        BufferedImage bufferedImage = generateQRCode(jsonContent, Constant.DEFAULT_WIDTH, Constant.DEFAULT_HEIGHT, logoUrl, Constant.LOGO_DEFAULT_WIDTH, Constant.LOGO_DEFAULT_HEIGHT);

        ImageIO.write(bufferedImage, Constant.DEFAULT_IMAGE_FORMAT, outputStream);
    }

    /**
     * Description:[二维码生成方法]
     *
     * @param jsonContent [生成二维码中的内容]
     * @param width       [二维码宽度]
     * @param height      [二维码高度]
     * @param logoUrl     [Logo 地址]
     * @param logoWidth   [Logo 宽度]
     * @param logoHeight  [Logo 高度]
     * @return BufferedImage
     * @date 2020-07-13
     * @author huazai
     */
    private BufferedImage generateQRCode(String jsonContent, Integer width, Integer height, String logoUrl, Integer logoWidth, Integer logoHeight) throws AppException {
        if (StringUtils.isNotEmpty(jsonContent)) {
            // 配置二维码
            Map hints = new HashMap<>(5);
            // 设定编码
            hints.put(EncodeHintType.CHARACTER_SET, Constant.DEFAULT_CHARSET);
            // 设定错误纠正级别,L = ~7%,M = ~15%,Q = ~25%,H = ~30%
            hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M);
            // 设定边距(单位PX,像素)
            hints.put(EncodeHintType.MARGIN, 0);
            // 设定矩阵形状
            hints.put(EncodeHintType.DATA_MATRIX_SHAPE, SymbolShapeHint.FORCE_SQUARE);
            // 设定二维码版本
            hints.put(EncodeHintType.QR_VERSION, "1");

            ServletOutputStream outputStream = null;

            try {
                // 生成二维码
                Writer writer = new QRCodeWriter();
                BitMatrix bitMatrix = writer.encode(jsonContent, BarcodeFormat.QR_CODE, width, height, hints);

                BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
                for (int x = 0; x < width; x++) {
                    for (int y = 0; y < height; y++) {
                        // 码,底
                        bufferedImage.setRGB(x, y, bitMatrix.get(x, y) ? 0xFFFF0000 : 0xFFFFFFFF);
                    }
                }

                // 二维码中插入Logo内容
                if (StringUtils.isNotEmpty(logoUrl)) {
                    insertQRCodeLogo(bufferedImage, width, height, logoUrl, logoWidth, logoHeight);
                }
                return bufferedImage;
            } catch (WriterException | IOException e) {
                log.error("generateQRCode 二维码生成异常:{}", e.getMessage());
                throw new AppException("generateQRCode 二维码生成异常");
            } finally {
                if (outputStream != null) {
                    try {
                        outputStream.flush();
                        outputStream.close();
                    } catch (IOException e) {
                        log.error("generateQRCode 二维码流输出:{}", e.getMessage());
                        throw new AppException("generateQRCode 二维码流输出");
                    }
                }
            }
        }
        return null;
    }

    /**
     * Description:[二维码生成方法]
     *
     * @param bufferedImage [生成图片流]
     * @param width         [二维码宽度]
     * @param height        [二维码高度]
     * @param logoUrl       [Logo 地址]
     * @param logoWidth     [Logo 宽度]
     * @param logoHeight    [Logo 高度]
     * @return BufferedImage
     * @date 2020-07-13
     * @author huazai
     */
    private void insertQRCodeLogo(BufferedImage bufferedImage, Integer width, Integer height, String logoUrl, Integer logoWidth, Integer logoHeight) throws IOException {
        // 获取Logo流
        Image imageUrl = ImageIO.read(new URL(logoUrl));

        // 二维码中插入Logo
        Graphics2D graphics = bufferedImage.createGraphics();
        int x = (width - logoWidth) / 2;
        int y = (height - logoHeight) / 2;
        graphics.drawImage(imageUrl, x, y, logoWidth, logoHeight, null);
        Shape shape = new RoundRectangle2D.Float(x, y, logoHeight, logoHeight, 5, 5);
        graphics.setStroke(new BasicStroke());
        graphics.draw(shape);
        graphics.dispose();
    }


}

四、新建Controller类

package com.huazai.b2c.aiyou.inventory.controller;

import com.huazai.platform.commons.utils.QRCodeUtil;
import com.huazai.platform.commons.constant.ResultCodeEnum;
import com.huazai.platform.commons.result.AppException;
import com.huazai.platform.commons.result.JSONResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;


/**
 * Company:     b2c哎呦商城
 * Department:  研发一组
 * Title:       [aiyou — User模块]
 * Description: [InventoryController 模块]
 * Created on:  2020-07-13
 * Contacts:    [[email protected]]
 *
 * @author huazai
 * @version V1.1.0
 */
@Slf4j
@RestController
@Api(value = "InventoryEntity 控制器")
@RequestMapping("/inventory")
public class InventoryController {


    /**
     * Description:[获取二维码]
     *
     * @param jsonContent [二维码内容]
     * @return JSONResult
     * @date 2020-07-13
     * @author huazai
     */
    @GetMapping("/getQRCode")
    @ApiOperation(value = "/getQRCode", notes = "获取二维码")
    public void getQRCode(@ApiParam(name = "jsonContent", value = "jsonContent") String jsonContent, HttpServletResponse response) {
        ServletOutputStream outputStream = null;
        try {
            outputStream = response.getOutputStream();
            QRCodeUtil.getQRCode(jsonContent, outputStream);
        } catch (AppException | IOException e) {
            log.info("异常信息:{}", e);
        }
    }

    /**
     * Description:[获取带LOGO二维码]
     *
     * @param jsonContent [二维码内容]
     * @return JSONResult
     * @date 2020-07-13
     * @author huazai
     */
    @GetMapping("/getLogoQRCode")
    @ApiOperation(value = "/getLogoQRCode", notes = "获取带LOGO二维码")
    public void getLogoQRCode(@ApiParam(name = "jsonContent", value = "jsonContent") String jsonContent, @ApiParam(name = "logoUrl", value = "logoUrl") String logoUrl, HttpServletResponse response) {
        ServletOutputStream outputStream = null;
        try {
            outputStream = response.getOutputStream();
            QRCodeUtil.getQRCode(jsonContent, logoUrl, outputStream);
        } catch (AppException | IOException e) {
            log.info("异常信息:{}", e);
        }
    }

    /**
     * Description:[获取Base64二维码]
     *
     * @param jsonContent [二维码内容]
     * @return JSONResult
     * @date 2020-07-13
     * @author huazai
     */
    @GetMapping("/getBase64QRCode")
    @ApiOperation(value = "/getBase64QRCode", notes = "获取Base64二维码")
    public JSONResult getBase64QRCode(@ApiParam(name = "jsonContent", value = "jsonContent") String jsonContent) {
        String base64QRCode;
        try {
            base64QRCode = QRCodeUtil.getBase64QRCode(jsonContent);
        } catch (AppException e) {
            log.info("异常信息:{}", e);
            return JSONResult.failure(ResultCodeEnum.Failure.getCode(), e.getMessage());
        }
        return JSONResult.success(base64QRCode);
    }

    /**
     * Description:[获取带LOGO的Base64二维码]
     *
     * @param jsonContent [二维码内容]
     * @return JSONResult
     * @date 2020-07-13
     * @author huazai
     */
    @GetMapping("/getBase64LogoQRCode")
    @ApiOperation(value = "/getBase64LogoQRCode", notes = "获取带LOGO的Base64二维码")
    public JSONResult getBase64LogoQRCode(@ApiParam(name = "jsonContent", value = "jsonContent") String jsonContent, @ApiParam(name = "logoUrl", value = "logoUrl") String logoUrl) {
        String base64QRCode;
        try {
            base64QRCode = QRCodeUtil.getBase64QRCode(jsonContent, logoUrl);
        } catch (AppException e) {
            log.info("异常信息:{}", e);
            return JSONResult.failure(ResultCodeEnum.Failure.getCode(), e.getMessage());
        }
        return JSONResult.success(base64QRCode);
    }

}

五、测试二维码

1、测试图片二维码

测试请求地址:http://localhost:8085/inventory/getQRCode?jsonContent=欢迎使用ZXing组件!

如下图:

Java生成二维码,Spring Boot整合ZXing实现二维码生成,支持自定义二维码_第1张图片

2、测试带Logo的图片二维码

测试请求地址:http://localhost:8085/inventory/getLogoQRCode?jsonContent=欢迎使用ZXing组件!&logoUrl=https://raw.githubusercontent.com/Jackson-AndyLau/pictures-storage/master/logo/logo011.png

如下图:

Java生成二维码,Spring Boot整合ZXing实现二维码生成,支持自定义二维码_第2张图片

3、测试Base64二维码

使用Postman进行请求,

测试请求地址:http://localhost:8085/inventory/getBase64QRCode?jsonContent=欢迎使用ZXing组件!

获得Base64图片CODE,如下图:

Java生成二维码,Spring Boot整合ZXing实现二维码生成,支持自定义二维码_第3张图片

内容如下: 

{
    "code": 18200,
    "data": "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAJYAAACWCAYAAAA8AXHiAAADlUlEQVR42u3dwXLrIAwF0Pz/T7uLt3ztog26ksJhJt049WA4mRHIwOtRlILy+vf3lf38V4vm+/261cL3f7uX8/0LFlhggQUWWGAVPMi7DfXu99+FV/39NMSG/gULLLDAAgsssJ76YLgaSjq4re746u8H+hcssMACCyywwBoAKx3cpyco0z8EsMACCyywwAILrIrg+fT/bxuMgAUWWGCBBRZYO2B1P1h1cF0NcXrw3paEBgsssMACC6zLYHUC99vq1q3RcBwsssMByPQxrelI1vbgiXd/qCdqOLgULLLDAAgsssPoXC0wP/k9PkKbrU72AtmzmHSywwAILLLCWwzod/HYnnbs3VktvFDfwxUSwwAILLLDAAuvJd9Tp4L+6oar/v3sz34LFHmCBBRZYYIEFVkPSeHtwOm1B7MAkP1hggQUWWGCBtSDJfLoh06X6+boHQ2WjQrDAAgsssMD6cFjp4DOdlJ5+UOfCAwbAAgsssMACC6xAMNwdzHdPeE4L7qshggUWWGCBBRZYr0jF08F39WKK7vaadj+wwAILLLDAAut7WAsOVowGp9MGLws3RQELLLDAAgsssAZUbFrwfxrKtGC/LXgHCyywwAILrMthBTY/HdWx6eC8+n4D6gMWWGCBBRZYYA1MyqYnELsnVLff/8fgHSywwAILLLAuhzVtweT0pHj3YKF6sFR2+hdYYIEFFlhgXQ4rXfH0p3tCd1qSvqA9wAILLLDAAgus5/yEZ/pFuOkTktsPWIgF72CBBRZYYIEF1tFgsXuTkdtfNCwYHIEFFlhggQUWWAMmNKsHA90HHqSTxOmXBL57RLDAAgsssMACK7+B/7QFnun7TbteMEELFlhggQUWWGA95xtm2mHi3QtQt21ue6C/wAILLLDAAgusAljVwfynHwiQHhwVtB9YYIEFFlhggRVI+k5fDNBdumGvOUAALLDAAgsssJbBmtaw2w4zn17/hsEIWGCBBRZYYIEVCAbTwX4a9jQIp384f6g/WGCBBRZYYIH15JOip4PpaihpCNUTyNXPCxZYYIEFFlhg1WwKkoZy+4uB3Uno2MZrYIEFFlhggQXW0eB92oTltMUSgWD8t/cHCyywwAILLLAWwJqWhE0nxbt/CGN2TQYLLLDAAgusD4PVnYSetqlGOthOHwhQtmsyWGCBBRZYYF0Oa/vmrKfhNAS/o4L1YxOkYIEFFlhggXUZLEU5XL4A9tEVeJvtgoQAAAAASUVORK5CYII=",
    "msg": "成功"
}

 将Base64码转换后如下图:

Java生成二维码,Spring Boot整合ZXing实现二维码生成,支持自定义二维码_第4张图片

4、测试带Logo的Base64二维码

测试请求地址:http://localhost:8085/inventory/getBase64LogoQRCode?jsonContent=欢迎使用ZXing组件!&logoUrl=https://raw.githubusercontent.com/Jackson-AndyLau/pictures-storage/master/logo/logo011.png

内容如下:

{
    "code": 18200,
    "data": "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAJYAAACWCAYAAAA8AXHiAAAKL0lEQVR42u3dC1BU1xkHcNOmse202EQNggvs7oVdHiIEFiii5lGjJjFipLHNTNN2kkymk0mnM04Sxzaj1dgmsdKZOmo6amx88FLkEUAzRlLrK9IErbFIG61OkjqiCRFCXBaW3X/PvRAEEauw99y77P/MfOLsa+7e+9tzvnPOveeOAguLDmVU97+j5MaArTD48256r0n+/GEfZfnHl7AIi7AIi7AIi7B0+CLD3VHDff1w4en9etkQDTi+hEVYhEVYhEVYhAX9k2G9ochObvU+8Hq/XsLxJSzCIizCIizCIiwTwJKd3MseoJT9QyAswiIswiIswiIswtIjeQ70+4OtM0JYhEVYhEVYhEVYwQHL6C+md3KtN0SzJ++GTUITFmERFmERFmGFGCyjL6bg8/o+H7JX6fB5wiIswiIsPi8ZltknVWVfXCF7e/UeoDXikBIWYREWYREWYRGW8RcLmD35D/QAqezt0fsCWt1G3gmLsAiLsAiLsIIcVqCTX6MnnY1eWE32QnEmPDGRsAiLsAiLsAiLsCD/QAU6+dd7R+n9fqMX89XhYg/CIizCIizCIizCMmDSONiTU7NdEGvCSX7CIizCIizCIizCCoJJ5kDvSNlF7+9ndGdIt14hYREWYREWYRHWCIclO/mUPSlt9ht1BuENBgiLsAiLsAiLsAhLQjJsdDJv9ICn2ZJ7vSESFmERFmERVtDBMrqELCyjf6EST1Tz+XyoqclD0eYH8Ne3foF3a59H/eHfov7vL6Ph2J/wr8bXcPHjKvWVQ//ORg/4SvghE9ZV5bLnHKrL87Cr/EfYu+sJ7K99Fof3PYf6Iytw/L18NB5fi5Mn3xDw8ob+vQkr9GDtrnwcb+6cj6qyPNRouJ7BgdpfoW7/izha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uwEPgdY7abicsewDS6s6N3Mn8jh4iwCIuwCIuwCIuw9E8+A50sB9ukr9k7RzrsP8IiLMIiLMIiLMKSMOlr9osBjC5Gww6aGwgQFmERFmERFmEFGSyz7dhgu5m52bffgM4IYREWYREWYREWYUlIBmUn+7Jhmw1CoH84Q9h+wiIswiIswiIswoL8SdFAJ9N6Q5ENQe8BZL2/L2ERFmERFmERFmHpsyiIbCihfmKg0ZPQ0hZeIyzCIizCIizCIqyAJu9mG7A028USEpLxm/18wiIswiIswiIswgoCWGabhJU9KW70D8E0qyYTFmERFmERFmGNMFhGT0KbbVEN2cm27BsC6LZqMmERFmERFmERVojDCvbFWQMNx4Dk11TJesAGSAmLsAiLsAiLsEIMFgtLgMv/AF3V8NEIicEyAAAAAElFTkSuQmCC",
    "msg": "成功"
}

 将Base64码转换后如下图:

Java生成二维码,Spring Boot整合ZXing实现二维码生成,支持自定义二维码_第5张图片

六、总结

在二维码中,建议最多存2K左右的信息。因为在二维码中存储的信息越多,识别起来难度就越大,所以只把最重要的信息存进去就可以了。如果加密就得写一个加密算法,与二维码编码算法接起来就行了。建议采用标准算法与私有算法相结合的方案。

参考文献:

Google ZXing : https://github.com/zxing/zxing

在线工具:

Base64转换图片在线工具:https://tool.jisuapi.com/base642pic.html

Base64图片互转在线工具:https://www.qtool.net/picconvert


 好了,关于 Java生成二维码,Spring Boot整合ZXing实现二维码生成,支持自定义二维码  就写到这儿了,如果还有什么疑问或遇到什么问题欢迎扫码提问,也可以给我留言哦,我会一一详细的解答的。 
歇后语:“ 共同学习,共同进步 ”,也希望大家多多关注CSND的IT社区。


作       者: 华    仔
联系作者: [email protected]
来        源: CSDN (Chinese Software Developer Network)
原        文: https://blog.csdn.net/Hello_World_QWP/article/details/115730269
版权声明: 本文为博主原创文章,请在转载时务必注明博文出处!

你可能感兴趣的:(Spring,Boot,Spring,Cloud,Java,Java,Spring,MVC,Spring,boot,ZXing,二维码)