google 生成二維碼

maven依賴java

<!--google 二維碼-->
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>javase</artifactId>
<version>${google-zxing}</version>
</dependency>
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>core</artifactId>
<version>${google-zxing}</version>
</dependency>
<properties>
  <google-zxing>3.2.1</google-zxing>
</properties>

代碼

//生成二維碼
//1 定義一個json格式字符串
JSONObject jsonObject = new JSONObject();
jsonObject.put("company", "www.baidu.com");
jsonObject.put("name", "百度");
String content = String.valueOf(jsonObject);

int width = 200;
int hight = 200;
//2 編碼格式設置等
Map<EncodeHintType, Object> map = new HashMap<EncodeHintType, Object>();
map.put(EncodeHintType.CHARACTER_SET, "UTF-8");

//3 建立一個二維矩陣對象
BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, width, hight, map);


//矩陣 格式(jpj) 路徑
//4 生成的路徑
String filePath = "D://";
String fileName = "QRCode.jpg";
Path path = FileSystems.getDefault().getPath(filePath, fileName);
//5 將矩陣對象轉成圖片MatrixToImageWriter.writeToPath(bitMatrix, "jpg", path);
相關文章
相關標籤/搜索