<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>core</artifactId>
<version>3.0.0</version>
</dependency>
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>javase</artifactId>
<version>3.0.0</version>
</dependency>
public class QRCodeUtils { /** * 生成二维码 * */ public static BitMatrix createCode(String content) throws IOException { //二维码的宽高 int width = 200; int height = 200; //其他参数,如字符集编码 Map<EncodeHintType, Object> hints = new HashMap<EncodeHintType, Object>(); hints.put(EncodeHintType.CHARACTER_SET, "UTF-8"); //容错级别为H hints.put(EncodeHintType.ERROR_CORRECTION , ErrorCorrectionLevel.H); //白边的宽度,可取0~4 hints.put(EncodeHintType.MARGIN , 0); BitMatrix bitMatrix = null; try { //生成矩阵,因为我的业务场景传来的是编码之后的URL,所以先解码 bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, width, height, hints); bitMatrix = deleteWhite(bitMatrix); } catch (WriterException e) { e.printStackTrace(); } return bitMatrix; } /** * 删除生成的二维码周围的白边,根据审美决定是否删除 * */ private static BitMatrix deleteWhite(BitMatrix matrix) { int[] rec = matrix.getEnclosingRectangle(); int resWidth = rec[2] + 1; int resHeight = rec[3] + 1; BitMatrix resMatrix = new BitMatrix(resWidth, resHeight); resMatrix.clear(); for (int i = 0; i < resWidth; i++) { for (int j = 0; j < resHeight; j++) { if (matrix.get(i + rec[0], j + rec[1])) resMatrix.set(i, j); } } return resMatrix; } }
/** * 生成二维码 * */ @RequestMapping(value = "/activityCode") public void getCode(HttpServletResponse response,HttpServletRequest request) throws IOException { String share = (String) request.getSession().getAttribute("share"); // 设置响应流信息 response.setContentType("image/jpg"); response.setHeader("Pragma", "no-cache"); response.setHeader("Cache-Control", "no-cache"); response.setDateHeader("Expires", 0); OutputStream stream = response.getOutputStream(); //type是1,生成活动详情、报名的二维码,type是2,生成活动签到的二维码 //String content = (""); String content = ("https://shijietong.keyten.net/login.html?share="+share); //获取一个二维码图片 BitMatrix bitMatrix = QRCodeUtils.createCode(content); //以流的形式输出到前端 MatrixToImageWriter.writeToStream(bitMatrix , "jpg" , stream); }