二维码

二维码

_

依赖


        <dependency>
            <groupId>com.google.zxing</groupId>
            <artifactId>core</artifactId>
            <version>3.4.1</version>
        </dependency>

        <dependency>
            <groupId>com.google.zxing</groupId>
            <artifactId>javase</artifactId>
            <version>3.4.1</version>
        </dependency>

实现

    /**
     * 生成二维码实现
     */
    public static void getQRCodePicture(){

        //定义二维码宽度
        int width = 600;
        //高度
        int height = 600;
        //图片格式
        String type = "png";
        //图片类容
        String content = "halo";
        //定义二维码的配置
        HashMap<EncodeHintType, Object> hints = new HashMap<>();
        //定义字符集
        hints.put(EncodeHintType.CHARACTER_SET,"UTF-8");
        //设置容错等级
        hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M);
        //设置边距  二维码边距空白宽度为0
        hints.put(EncodeHintType.MARGIN,2);

        try {
            //生成二维码对象,传入各种属性
            BitMatrix bitMatrix = new MultiFormatWriter()
                    .encode(content, BarcodeFormat.QR_CODE, width,height , hints);

            //定义路径
            Path file = new File("F:\\下载\\code.png").toPath();
            //生成路径并生成文件
            MatrixToImageWriter.writeToPath(bitMatrix,type,file);
        }catch (Exception e){
            e.printStackTrace();
        }

    }
go 下载、安装与环境配置 2021-07-19
Linux常用命令 2022-02-08

评论区