java-生成普通二維碼

這是一個工具類,根據傳入的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;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章