最近在学习一个电商的项目其中就有一个用zxing生成二维码的,通过在网上学习和查阅了解了点zxing,那就记录一下吧!
一、配置文件
1、pom.xml
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>javase</artifactId>
<version>3.3.0</version>
</dependency>
二、java代码
public static void generateQRCodeImage(String text, int width, int height, String filePath) throws WriterException, IOException {
QRCodeWriter qrCodeWriter = new QRCodeWriter();
//生成编码后的结果
BitMatrix bitMatrix = qrCodeWriter.encode(text, BarcodeFormat.QR_CODE, width, height);
//存放图片的地址
Path path = FileSystems.getDefault().getPath(filePath);
MatrixToImageWriter.writeToPath(bitMatrix,"PNG",path);
}
三、测试代码
public static void main(String[] args) {
try {
generateQRCodeImage("Hello World",350,350,"这里存放自己要存放图片的地址/QRTest.png");
} catch (WriterException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
四、测试
运行代码后可以在自己定义的文件夹中看到生成的二维码,然后去二维码解码的网址去解码。
五、zxing
zxing的官方地址
https://github.com/zxing/zxing
zxing还有很多强大的功能,以后在慢慢的去探索吧!