二维码生成



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

public void test(){

int width=300;

int height=300;

String format=“png”;

//这里如果你想自动跳转的话,需要加上https://

String content=“https://www.baidu.com ”;

HashMap hits=new HashMap();

hits.put(EncodeHintType.CHARACTER_SET, “utf-8”);//编码

//纠错等级,纠错等级越高存储信息越少

hits.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M);

//边距

hits.put(EncodeHintType.MARGIN, 2);

try {

BitMatrix bitMatrix=new MultiFormatWriter().encode(content,

BarcodeFormat.QR_CODE, width, height,hits);

//如果做网页版输出可以用输出到流

//MatrixToImageWriter.writeToStream(matrix, format, stream);

Path path=new File(“D:/zxingQRCode.png”).toPath();

MatrixToImageWriter.writeToPath(bitMatrix, format, path);

} catch (Exception e) {

// TODO Auto-generated catch block

e.printStackTrace();

    }

System.out.println(“that is all”);

}