zxing生成二维码

前台页面图片src

请稍后再试

生成二维码图片接口

    @RequestMapping(value = "createQRCode", method = { RequestMethod.GET })
    public ResponseEntity createQRCode(HttpServletRequest request)throws     WriterException, IOException {
        String staffCode = request.getParameter("staffCode");
	    String content = waiterEvaluateUrl + staffCode;
	    return getResponseEntity(content, 300, 300, "png");
    }
	public static ResponseEntity getResponseEntity(String url,
			int width, int height, String format) throws WriterException,
			IOException {
		Map hints = new HashMap();
		hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
		BitMatrix bitMatrix = new MultiFormatWriter().encode(url,
				BarcodeFormat.QR_CODE, width, height, hints);// 生成矩阵
		// 将矩阵转为Image
		ByteArrayOutputStream out = new ByteArrayOutputStream();
		MatrixToImageWriter.writeToStream(bitMatrix, "png", out);
		HttpHeaders headers = new HttpHeaders();
		headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
		return new ResponseEntity(out.toByteArray(), headers,
				HttpStatus.CREATED);
	}

生成二维码图片到某个目录下

Map hints=new HashMap();
hints.put(EncodeHintType.CHARACTER_SET, "utf-8");//设置编码
hints.put(EncodeHintType.ERROR_CORRECTION,ErrorCorrectionLevel.M);//设置容错等级 hints.put(EncodeHintType.MARGIN,2);//设置边距默认是5 
BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, 300, 300,hints);
File path = new File("D:\\qr.png");
MatrixToImageWriter.writeToFile(bitMatrix, "png", path);//写到指定路径下

 

你可能感兴趣的:(代码积累)