基於google zxing二維碼的生成,直接返回頁面圖片

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();
				}
			}
		}
直接訪問該方法,就會直接在頁面生成二維碼。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章