二次元コードJAVAリンクを生成する(リンク指定されたジャンプスキャン二次元コード)

1、Mavenの依存関係を使用する必要

<!-- https://mvnrepository.com/artifact/com.google.zxing/core -->
        <dependency>
            <groupId>com.google.zxing</groupId>
            <artifactId>core</artifactId>
            <version>3.3.0</version>
        </dependency>
        <dependency>
            <groupId>com.google.zxing</groupId>
            <artifactId>javase</artifactId>
            <version>3.3.0</version>
        </dependency>

現在のプロジェクトのほとんどは、我々はちょうど彼らの達人が良い環境を構築していることを提供し、プロジェクトへの自動インポートのjarパッケージに依存することができるようになります上記のプロジェクトを配置する必要があり、私は信じてpom.xmlファイルは、我々は良いを構築している、プロジェクトをMavenのさ、ここでは疲れません。

図2に示すように、コード

package com.example.demo.qrcode;
 
import java.io.File;
import java.nio.file.Path;
import java.util.HashMap;
 
import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
 
 
/**  
* <p>Description: </p>  
* @author xuyangwei 
* @date 2019年9月13日  
*/  
public class Zxing {
    public static void main(String[] args) {
        Zxing zxing = new Zxing();
 
        // 传参:二维码内容和生成路径
        if (zxing.orCode("https://blog.csdn.net/q15102780705/article/details/100060137", "D:\\1.jpg")) {
            System.out.println("ok,成功");
        } else {
            System.out.println("no,失败");
        }
    }
 
    private boolean orCode(String content, String path) {
        /*
         * 图片的宽度和高度
         */
        int width = 300;
        int height = 300;
        // 图片的格式
        String format = "png";   
 
        // 定义二维码的参数
        HashMap<EncodeHintType, Object> hints = new HashMap<EncodeHintType, Object>();
        // 定义字符集编码格式
        hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
        // 纠错的等级 L > M > Q > H 纠错的能力越高可存储的越少,一般使用M
        hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M);
        // 设置图片边距
        hints.put(EncodeHintType.MARGIN, 2);
 
        try {
            // 最终生成 参数列表 (1.内容 2.格式 3.宽度 4.高度 5.二维码参数)
            BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, width, height, hints);
            // 写入到本地
            Path file = new File(path).toPath();
            MatrixToImageWriter.writeToPath(bitMatrix, format, file);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
 
    }
 
}

ミアンメソッドが正常に生成された2次元コードの画像をテスト使用してください。

3、効果

二次元コードスキャンジャンプリンクした後、

公開された14元の記事 ウォンの賞賛2 ビュー812

おすすめ

転載: blog.csdn.net/breakaway_01/article/details/102636794