生成二维码 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">
上一篇: 纯前端实现图片背景透明化
下一篇: jQuery表单验证的几种方法