BitMatrix 生普通二维码,加logo二维码,和二维码底部加文字得二维码 com.google.zxing
因开发需求,并且搜了很多博客无果,参考了几篇博客后,终于做出来了,在此记录一下,注:带logo的二维码按照底部文字使用,这里没有这个需求,就没做
上代码
package com.fc.test.util;
import com.fc.test.test.erweima.QRCodeUtil;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
import java.awt.*;
import java.awt.geom.RoundRectangle2D;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Map;
/**
* <p>
* 生成二维码的工具类
* </p>
*
* @author robocon
* @date 2019-05-09
* */
public class QRCodeUtils {
/**
* 生成二维码
* @param content 二维码的内容
* @return BitMatrix对象
* */
public static BitMatrix createCode(Map<String, String> content) throws IOException {
//二维码的宽高
int width = 400;
int height = 400;
//其他参数,如字符集编码
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 , 3);
BitMatrix bitMatrix = null;
try {
//生成矩阵,
bitMatrix = new MultiFormatWriter().encode(String.valueOf(content),
BarcodeFormat.QR_CODE, width, height, hints);
//bitMatrix = deleteWhite(bitMatrix);
} catch (WriterException e) {
e.printStackTrace();
}
return bitMatrix;
}
/**
* 创建 二维码所需图片
* @param content 内容
* @return
* @throws Exception
*/
public static BufferedImage createImage(Map<String, String> content, String bottomDes) throws Exception {
Hashtable hints = new Hashtable();
int width = 400;
int height = 400;
//容错级别 H->30%
hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
hints.put(EncodeHintType.CHARACTER_SET, "utf-8");</