maven應用jar包:
<!-- 生成二維碼 -->
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>core</artifactId>
<version>2.2</version>
</dependency>
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>javase</artifactId>
<version>2.2</version>
</dependency>
生成二維碼代碼:
/**
* produceQR:生成二維碼. <br/>
* @author lcma
* @param request
* @param response
* @param pointId 點位id
* @throws IOException
* @since JDK 1.7
*/
@RequestMapping("produceQR")
public void produceQR(HttpServletRequest request,HttpServletResponse response,String pointId) throws IOException{
ServletOutputStream stream = null;
try {
int size = 500;
String content = ResourceBean.QR_CONTENT + pointId;
stream = response.getOutputStream();
QRCodeWriter writer = new QRCodeWriter();
BitMatrix m = writer.encode(content, BarcodeFormat.QR_CODE, size, size);
MatrixToImageWriter.writeToStream(m, IMAGETYPE, stream);
} catch (Exception e) {
LOGGER.error("produceQR error:", e);
} finally {
if (stream != null) {
stream.flush();
stream.close();
}
}
}
直接訪問該方法,就會直接在頁面生成二維碼。