欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

生成二维码 java二维码 

程序员文章站 2022-05-28 12:49:52
...

1.pom.xml

<dependency>
		    <groupId>com.google.zxing</groupId>
		    <artifactId>core</artifactId>
		    <version>3.3.0</version>
</dependency>
<dependency>
		    <groupId>com.google.zxing</groupId>
		    <artifactId>javase</artifactId>
		    <version>3.3.0</version>
</dependency>

 2.后台代码

@RequestMapping("/imageCode.action")
	public void imageCode(HttpServletRequest request,HttpServletResponse response) {
		
		String url = "http://www.baidu.com";
        ServletOutputStream stream = null;
        try {
	        stream = response.getOutputStream();
	        QRCodeWriter writer = new QRCodeWriter();
	        BitMatrix m = writer.encode(url, BarcodeFormat.QR_CODE, 300, 300);
	        MatrixToImageWriter.writeToStream(m, "png", stream);
	    } catch (Exception e) {
	        e.printStackTrace();
	    } finally {
		    if (stream != null) {
		    	try {
					stream.flush();
					stream.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
		    }
	    }
	 }

 3.前端代码

<img src="imageCode.action">

 

相关标签: java 二维码