這是一個工具類,根據傳入的url,再設置生成二維碼的寬、高、生成一個普通的二維碼
/**
*
* @param url
* @param agentQrCodeWidth //二維碼寬
* @param agentQrCodeHeight //二維碼高
* @return
*/
public BufferedImage getMemberQrCode(String url, Integer agentQrCodeWidth, Integer agentQrCodeHeight) {
BufferedImage image = null;
try {
MultiFormatWriter multiFormatWriter = new MultiFormatWriter();
Map hints = new HashMap();
hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
hints.put(EncodeHintType.MARGIN, 1); //控制二維碼白邊寬度
hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);// 設置QR二維碼的糾錯級別(H爲最高級別)具體級別信息
BitMatrix bitMatrix = multiFormatWriter.encode(url, BarcodeFormat.QR_CODE, agentQrCodeWidth, agentQrCodeHeight, hints);
image = MatrixToImageWriter.toBufferedImage(bitMatrix);// 生成二維碼
Graphics2D g = image.createGraphics();
g.dispose();
image.flush();
} catch (Exception e) {
logger.error("生成二維碼圖異常," + e.getMessage(), e);
}
return image;
}