pom配置
<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>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.1.29</version>
</dependency>
生成一串包含json數(shù)據(jù)的二維碼
- 此方法會生成一個二維碼, 手機掃描后得到一串json數(shù)據(jù)
public class QRCodeTest {
public static void main(String[] args) throws WriterException, IOException {
String filePath = "D://";
String fileName = "zxing.png";
JSONObject json = new JSONObject();
json.put("zxing", "https://www.baidu.com");
json.put("author", "yangzhongyang");
String content = json.toJSONString(); //內(nèi)容
int width = 200; //圖像寬度
int height = 200; //圖像高度
String format = "png"; //圖像類型
Map<EncodeHintType, Object> hints = new HashMap<>();
hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
BitMatrix bitMatrix = new MultiFormatWriter()
.encode(content, BarcodeFormat.QR_CODE, width, height, hints);
Path path = FileSystems.getDefault().getPath(filePath, fileName);
MatrixToImageWriter.writeToPath(bitMatrix, format, path);// 輸出圖像
}
}
生成一個帶Url連接的二維碼
- 此方法生成二維碼包含一個Url地址連接, 使用手機掃描后會自動跳轉(zhuǎn)到指定的Url地址
public static void main(String[] args) throws WriterException, IOException {
String filePath = "D://";
String fileName = "zxing.png";
int width = 200; //圖像寬度
int height = 200; //圖像高度
String format = "png"; //圖像類型
Map<EncodeHintType, Object> hints = new HashMap<>();
hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
BitMatrix bitMatrix = new MultiFormatWriter()
.encode("http://www.huiedu.com.cn", BarcodeFormat.QR_CODE, width, height, hints);
Path path = FileSystems.getDefault().getPath(filePath, fileName);
MatrixToImageWriter.writeToPath(bitMatrix, format, path);// 輸出圖像
}
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者