写这个篇文章是为了记录一下使用Java操作二维码的一些套路。因为在做这件事的时候,是遇到了一些问题的,这里记录一下,以备不时之需。
需求
根据文字内容生成二维码,在二维码中间加入logo图片,最后将二维码嵌入外部背景图中,写入到指定路径
效果
测试代码:
String content = "这是二维码内容";
String logoPath = "F:/test/qrcode/logo.png";
String backImagePath = "F:/test/qrcode/backImage.jpg";
String outputPath = "F:/test/qrcode/result.jpg";
boolean result = QrCodeUtil.createAndSaveQrCodeImg(content, logoPath, backImagePath, outputPath);
效果:
maven依赖
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>core</artifactId>
<version>3.3.0</version>
</dependency>
源码
- 文字生成二维码
此处生成二维码图片,使用了自定义的encode方法和resizeAndCreateBufferedImage方法,相关源码和原因见下方的问题1
/**
* 生成二维码image
*
* @param content 二维码内容
* @return BufferedImage
*/
private static BufferedImage createQrCodeImage(String content) {
return createQrCodeImage(content, 200, 200);
}
/**
* 生成二维码image
*
* @param content 二维码内容
* @param width 宽度
* @param height 长度
* @return bufferImage
*/
private static BufferedImage createQrCodeImage(String content, int width, int height) {
long start = System.currentTimeMillis();
BufferedImage image = null;
try {
Hashtable<EncodeHintType, Object> hints = new Hashtable<>();
hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
hints.put(EncodeHintType.MARGIN, 1);
// 使用自定义的方法,解决白边问题
BitMatrix bitMatrix = encode(content, BarcodeFormat.QR_CODE, width, height, hints);
// 重新调整大小,满足输入宽高
image = resizeAndCreateBufferedImage(bitMatrix, width, height);
} catch (WriterException e) {
logger.error("QRCodeUtil-createQrCodeImage 生成二维码异常:", e);
}
logger.info("QRCodeUtil-createQrCodeImage end. cost:{} ", (System.currentTimeMillis() - start));
return image;
}
- 读取图片信息
/**
* 从图片路径读取生成image
*
* @param imagePath 图片文件地址
* @return bufferedImage
*/
private static BufferedImage createBufferedImage(String imagePath) {
long start = System.currentTimeMillis();
BufferedImage bi = null;
try {
BufferedImage tmpImage = ImageIO.read(new File(imagePath));
// 防止写入jpg时出现失真异常,这里new一个新的image包一下
bi = new BufferedImage(tmpImage.getWidth(), tmpImage.getHeight(), BufferedImage.TYPE_INT_RGB);
bi.getGraphics().drawImage(tmpImage, 0, 0, null);
} catch (IOException e) {
logger.error("读取文件{} 生成bufferedImage失败:", imagePath, e);
}
logger.info("QRCodeUtil-createBufferedImage end. cost:{}", System.currentTimeMillis() - start);
return bi;
}
- 二维码中间填充logo图片
/**
* 二维码中间插入logo
*
* @param codeImage 二维码image
* @param logoImage logo image
* @return 插入结果
*/
private static boolean combineCodeAndInnerLogo(BufferedImage codeImage, BufferedImage logoImage) {
return combineCodeAndInnerLogo(codeImage, logoImage, true);
}
/**
* 二维码中间插入logo
*
* @param codeImage 二维码image
* @param logoImage logo image
* @param needCompress 是否需要压缩
* @return 插入结果
*/
private static boolean combineCodeAndInnerLogo(BufferedImage codeImage, Image logoImage, boolean needCompress) {
boolean result;
try {
int logoWidth = logoImage.getWidth(null);
int logoHeight = logoImage.getHeight(null);
// 如果设置了需要压缩,则进行压缩
if (needCompress) {
logoWidth = logoWidth > LOGO_MAX_HEIGHT ? LOGO_MAX_WIDTH : logoWidth;
logoHeight = logoHeight > LOGO_MAX_HEIGHT ? LOGO_MAX_HEIGHT : logoHeight;
Image image = logoImage.getScaledInstance(logoWidth, logoHeight, Image.SCALE_SMOOTH);
BufferedImage tag = new BufferedImage(logoWidth, logoHeight, BufferedImage.TYPE_INT_RGB);
Graphics gMaker = tag.getGraphics();
// 绘制缩小后的图
gMaker.drawImage(image, 0, 0, null);
gMaker.dispose();
logoImage = image;
}
// 在中心位置插入logo
Graphics2D codeImageGraphics = codeImage.createGraphics();
int codeWidth = codeImage.getWidth();
int codeHeight = codeImage.getHeight();
int x = (codeWidth - logoWidth) / 2;
int y = (codeHeight - logoHeight) / 2;
codeImageGraphics.drawImage(logoImage, x, y, logoWidth, logoHeight, null);
Shape shape = new RoundRectangle2D.Float(x, y, logoWidth, logoHeight, 6, 6);
codeImageGraphics.setStroke(new BasicStroke(3f));
codeImageGraphics.draw(shape);
codeImageGraphics.dispose();
result = true;
} catch (Exception e) {
logger.error("QRCodeUtil-combineCodeAndInnerLogo 二维码中间插入logo失败:", e);
result = false;
}
return result;
}
- 将背景图填充上生成的二维码
/**
* 合成二维码image和背景图image
*
* @param codeImage 二维码image
* @param backImage 背景图image
*/
private static BufferedImage combineCodeAndBackImage(BufferedImage codeImage, BufferedImage backImage) {
return combineCodeAndBackImage(codeImage, backImage, -1, 100);
}
/**
* 合成二维码image和背景图image,指定二维码底部距离背景图底部的距离
*
* @param codeImage 二维码image
* @param backImage 背景图image
* @param marginLeft 二维码距离背景图左边距离,如果为-1,则左右居中
* @param marginBottom 二维码距离背景图底部距离
* @return bufferedImage
*/
private static BufferedImage combineCodeAndBackImage(BufferedImage codeImage, BufferedImage backImage, int marginLeft, int marginBottom) {
long start = System.currentTimeMillis();
Graphics2D backImageGraphics = backImage.createGraphics();
// 确定二维码在背景图的左上角坐标
int x = marginLeft;
if (marginLeft == -1) {
x = (backImage.getWidth() - codeImage.getWidth()) / 2;
}
int y = backImage.getHeight() - codeImage.getHeight() - marginBottom;
// 组合绘图
backImageGraphics.drawImage(codeImage, x, y, codeImage.getWidth(), codeImage.getHeight(), null);
backImageGraphics.dispose();
logger.info("QRCodeUtil-combineCodeAndBackImage end. cost:{}", System.currentTimeMillis() - start);
return backImage;
}
- 保存图片文件到指定路径
/**
* 保存图片文件到指定路径
*
* @param image 图片image
* @param outputPath 指定路径
* @return 操作结果
*/
private static boolean imageSaveToFile(BufferedImage image, String outputPath) {
boolean result;
try {
// 为了保证大图背景不变色,formatName必须为"png"
ImageIO.write(image, "png", new File(outputPath));
result = true;
} catch (IOException e) {
logger.error("QRCodeUtil-imageSaveToFile 保存图片到{} 失败:,", outputPath, e);
result = false;
}
return result;
}
问题
下面列举一下当时遇到的一些问题
- 生成的二维码白边很大
默认使用zxing生成的二维码可以指定二维码长宽,但是整个图片规格是固定的,只能是固定的几个规格。这就导致如果我们需要指定生成二维码长宽的话,外边框会有留白。具体原因可以网上搜索,这里不赘述。
解决方法是重写zxing相应的方法(com.google.zxing.qrcode.QRCodeWriter#encode(java.lang.String,com.google.zxing.BarcodeFormat, int, int, java.util.Map)),重新缩放调整二维码大小
/**
* 修改encode生成逻辑,删除白边
* 源码见com.google.zxing.qrcode.QRCodeWriter#encode(java.lang.String,
* com.google.zxing.BarcodeFormat, int, int, java.util.Map)
*
* @param contents 二维码内容
* @param format 格式
* @param width 宽度
* @param height 长度
* @param hints hints
* @return BitMatrix
* @throws WriterException exception
*/
private static BitMatrix encode(String contents, BarcodeFormat format, int width, int height,
Hashtable<EncodeHintType, ?> hints) throws WriterException {
if (contents.isEmpty()) {
throw new IllegalArgumentException("Found empty contents");
}
if (format != BarcodeFormat.QR_CODE) {
throw new IllegalArgumentException("Can only encode QR_CODE, but got " + format);
}
if (width < 0 || height < 0) {
throw new IllegalArgumentException("Requested dimensions are too small: " + width + 'x' +
height);
}
ErrorCorrectionLevel errorCorrectionLevel = ErrorCorrectionLevel.L;
int quietZone = QUIET_ZONE_SIZE;
if (hints != null) {
if (hints.containsKey(EncodeHintType.ERROR_CORRECTION)) {
errorCorrectionLevel = ErrorCorrectionLevel.valueOf(hints.get(EncodeHintType.ERROR_CORRECTION).toString());
}
if (hints.containsKey(EncodeHintType.MARGIN)) {
quietZone = Integer.parseInt(hints.get(EncodeHintType.MARGIN).toString());
}
}
QRCode code = Encoder.encode(contents, errorCorrectionLevel, hints);
return renderResult(code, width, height, quietZone);
}
/**
* 对 zxing 的 QRCodeWriter 进行扩展, 解决白边过多的问题。去除白边的主要逻辑
*
* @param code qrcode
* @param width 期望宽度
* @param height 期望高度
* @param quietZone quietZone
* @return BitMatrix
*/
private static BitMatrix renderResult(QRCode code, int width, int height, int quietZone) {
ByteMatrix input = code.getMatrix();
if (input == null) {
throw new IllegalStateException();
}
// xxx 二维码宽高相等, 即 qrWidth == qrHeight
int inputWidth = input.getWidth();
int inputHeight = input.getHeight();
int qrWidth = inputWidth + (quietZone * 2);
int qrHeight = inputHeight + (quietZone * 2);
// 白边过多时, 缩放
int minSize = Math.min(width, height);
int scale = calculateScale(qrWidth, minSize);
if (scale > 0) {
int padding, tmpValue;
// 计算边框留白
padding = (minSize - qrWidth * scale) / QUIET_ZONE_SIZE * quietZone;
tmpValue = qrWidth * scale + padding;
if (width == height) {
width = tmpValue;
height = tmpValue;
} else if (width > height) {
width = width * tmpValue / height;
height = tmpValue;
} else {
height = height * tmpValue / width;
width = tmpValue;
}
}
int outputWidth = Math.max(width, qrWidth);
int outputHeight = Math.max(height, qrHeight);
int multiple = Math.min(outputWidth / qrWidth, outputHeight / qrHeight);
int leftPadding = (outputWidth - (inputWidth * multiple)) / 2;
int topPadding = (outputHeight - (inputHeight * multiple)) / 2;
BitMatrix output = new BitMatrix(outputWidth, outputHeight);
for (int inputY = 0, outputY = topPadding; inputY < inputHeight; inputY++, outputY += multiple) {
// Write the contents of this row of the barcode
for (int inputX = 0, outputX = leftPadding; inputX < inputWidth; inputX++, outputX += multiple) {
if (input.get(inputX, inputY) == 1) {
output.setRegion(outputX, outputY, multiple, multiple);
}
}
}
return output;
}
/**
* 如果留白超过15% , 则需要缩放
* (15% 可以根据实际需要进行修改)
*
* @param qrCodeSize 二维码大小
* @param expectSize 期望输出大小
* @return 返回缩放比例, <= 0 则表示不缩放, 否则指定缩放参数
*/
private static int calculateScale(int qrCodeSize, int expectSize) {
if (qrCodeSize >= expectSize) {
return 0;
}
int scale = expectSize / qrCodeSize;
int abs = expectSize - scale * qrCodeSize;
if (abs < expectSize * 0.15) {
return 0;
}
return scale;
}
/**
* 缩放调整二维码大小,使之符合期望大小
*
* @param matrix matrix
* @param width 期望宽度
* @param height 期望高度
* @return bufferedImage
*/
private static BufferedImage resizeAndCreateBufferedImage(BitMatrix matrix, int width, int height) {
int qrCodeWidth = matrix.getWidth();
int qrCodeHeight = matrix.getHeight();
BufferedImage qrCode = new BufferedImage(qrCodeWidth, qrCodeHeight, BufferedImage.TYPE_INT_RGB);
for (int x = 0; x < qrCodeWidth; x++) {
for (int y = 0; y < qrCodeHeight; y++) {
qrCode.setRGB(x, y, matrix.get(x, y) ? BLACK : WHITE);
}
}
// 若二维码的实际宽高和预期的宽高不一致, 则缩放
if (qrCodeWidth != width || qrCodeHeight != height) {
BufferedImage tmp = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
tmp.getGraphics().drawImage(
qrCode.getScaledInstance(width, height,
java.awt.Image.SCALE_SMOOTH), 0, 0, null);
qrCode = tmp;
}
return qrCode;
}
然后在调用生成二维码时,使用自定义的方法
- 生成的图片有颜色失真现象
我在使用的时候,如果生成的图片存储为jpg格式时,可能会出现图片颜色异常的情况。这是因为jpg格式采用了有损压缩,会导致图片失真。
解决方法是在保存到本地时(imageSaveToFile),存储为png格式
// 为了保证大图背景不变色,formatName必须为"png"
ImageIO.write(image, "png", new File(outputPath));
这种方法有一个不足,就是如果原图是png格式的话,图片一般都比较大,比较占用本地内存,网络传输时,也会比较慢,影响体验。
如果我们将原图转为jpg格式,可以有效减少图片的大小,生成的图片大小也会相应的减少。但是测试发现也可能出现图片失真的情况。后来发现,在读取图片文件时(createBufferedImage),转存一下就可以解决这个问题。(具体为啥会这样,如果有人知道,欢迎指教)
// 防止写入jpg时出现失真异常,这里new一个新的image包一下
bi = new BufferedImage(tmpImage.getWidth(), tmpImage.getHeight(), BufferedImage.TYPE_INT_RGB);
bi.getGraphics().drawImage(tmpImage, 0, 0, null);
源码地址
https://download.csdn.net/download/somehow1002/12262631