JAVA 生成数据表图标LOGO二维码
private static final int QRCOLOR = 0xFF000000; // 默认是黑色
private static final int BGWHITE = 0xFFFFFFFF; // 背景颜色
private static final int WIDTH = 140; // 二维码宽
private static final int HEIGHT = 140; // 二维码高
// 用于设置QR二维码参数
private static Map<EncodeHintType, Object> hints = new HashMap<EncodeHintType, Object>() {
private static final long serialVersionUID = 1L;
{
put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);// 设置QR二维码的纠错级别(H为最高级别)具体级别信息
put(EncodeHintType.CHARACTER_SET, "utf-8");// 设置编码方式
put(EncodeHintType.MARGIN, 0);
}
};
/**
* @Title: drawLogoQR_Code
* @Description: TODO(生成带logo的二维码图片)
* @author: LWJ-PC
* @date: 2018年3月23日 上午11:38:17
* @param: @param logoFile logo图片
* @param: @param codeFile 生成图片位置
* @param: @param qrUrl 生成二维码链接参数
* @param: @return
* @param: @throws IOException
* @param: @throws WriterException
* @return: BufferedImage
* @throws
*/
public static BufferedImage drawLogoQR_Code(BufferedImage logoImg, String qrUrl) throws IOException, WriterException {
MultiFormatWriter multiFormatWriter = new MultiFormatWriter();
// 参数顺序分别为:编码内容,编码类型,生成图片宽度,生成图片高度,设置参数
BitMatrix bm = bm = multiFormatWriter.encode(qrUrl, BarcodeFormat.QR_CODE, WIDTH, HEIGHT, hints);
BufferedImage image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
// 开始利用二维码数据创建Bitmap图片,分别设为黑(0xFFFFFFFF)白(0xFF000000)两色
for (int x = 0; x < WIDTH; x++) {
for (int y = 0; y < HEIGHT; y++) {
image.setRGB(x, y, bm.get(x, y) ? QRCOLOR : BGWHITE);
}
}
int width = image.getWidth();
int height = image.getHeight();
// 构建绘图对象
Graphics2D g = image.createGraphics();
// 开始绘制logo图片
g.drawImage(logoImg, width * 2 / 5, height * 2 / 5, width * 2 / 10, height * 2 / 10, null);
g.dispose();
image.flush();
return image;
}
代码下载链接
https://download.csdn.net/download/dd_lemon/10304527