生成带有红黄绿码的二维码,并转base64
一、依赖(pom)
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>core</artifactId>
<version>3.5.0</version>
</dependency>
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>javase</artifactId>
<version>3.5.0</version>
</dependency>
二、源码
package com.cxhl.common.utils;
import com.google.zxing.*;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.geom.RoundRectangle2D;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.net.URL;
import java.io.IOException;
import com.cxhl.common.utils.sign.Base64;
import lombok.extern.slf4j.Slf4j;
import java.util.HashMap;
import java.util.Map;
/**
* @description: 生成二维码的写法
* @author:
* @date:
**/
@Slf4j
public class CreateQrCodeUtils {
/**
* 二维码尺寸
*/
private static final int QRCODE_SIZE = 300;
/**
* LOGO宽度
*/
private static final int LOGO_WIDTH = 80;
/**
* LOGO高度
*/
private static final int LOGO_HEIGHT = 80;
/**
* 生成二维码
*
* @param content 二维码信息
* @param logoPath logo地址 如果为空则表示不带logo
* @return 图片
* @throws Exception
*/
public static String getQrLogoCode(String content, String logoPath,String color) throws Exception {
Map<EncodeHintType, Object> hints = new HashMap<>(8);
hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
hints.put(EncodeHintType.MARGIN, 1);
BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, QRCODE_SIZE, QRCODE_SIZE, hints);
int width = bitMatrix.getWidth();
int height = bitMatrix.getHeight();
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
for (int x = 0; x < width; x++) {
for (int y = 0; y < height; y++) {
// 设置颜色位置
if ("2".equals(color)){
//2:红色
image.setRGB(x, y, bitMatrix.get(x, y) ? 0xDD4747 : 0xFFFFFFFF);
}else if ("1".equals(color)){
//1:黄色
image.setRGB(x, y, bitMatrix.get(x, y) ? 0xEE854E : 0xFFFFFFFF);
}else{
//0:绿色
image.setRGB(x, y, bitMatrix.get(x, y) ? 0x2DBD8D : 0xFFFFFFFF);
}
}
}
if (logoPath == null || "".equals(logoPath)) {
log.info("------------生成红黄绿码工具类-------------->logo传参为空");
return "logo传参为空";
}
// 插入图片
CreateQrCodeUtils.setLogoImage(image, logoPath);
// 输出jpg格式图片
ByteArrayOutputStream stream = new ByteArrayOutputStream();
ImageIO.write(image, "png", stream);
String a = Base64.encode(stream.toByteArray());
String base64 = "data:image/png;base64,"+a;
return base64;
}
/**
* 插入LOGO
*
* @param source 二维码图片
* @param logoPath LOGO图片地址
* @throws IOException
*/
private static void setLogoImage(BufferedImage source, String logoPath) throws Exception {
URL url = new URL(logoPath);
Image imageIo = ImageIO.read(url);
int width = imageIo.getWidth(null);
int height = imageIo.getHeight(null);
// 设置图片尺寸,如果超过指定大小,则进行响应的缩小
if (width > LOGO_WIDTH || height > LOGO_HEIGHT) {
width = LOGO_WIDTH;
height = LOGO_HEIGHT;
}
Image image = imageIo.getScaledInstance(width, height, Image.SCALE_SMOOTH);
BufferedImage tag = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics g = tag.getGraphics();
// 重新绘制Image对象
g.drawImage(image, 0, 0, null);
g.dispose();
imageIo = image;
// 插入LOGO
Graphics2D graph = source.createGraphics();
// 设置为居中
int x = (QRCODE_SIZE - width) / 2;
int y = (QRCODE_SIZE - height) / 2;
graph.drawImage(imageIo, x, y, width, height, null);
Shape shape = new RoundRectangle2D.Float(x, y, width, width, 6, 6);
graph.setStroke(new BasicStroke(3f));
graph.draw(shape);
graph.dispose();
}
public static String getBaseQrCode(String num,String logoPath) {
try {
String content = "抱歉,当前二维码不支持扫码!";
String image = getQrLogoCode(content, logoPath, num);
return image;
}catch (Exception e){
e.printStackTrace();
return "生成二维码失败";
}
}
public static void main(String[] args) {
//logo图片,可以是本地路径
String logoPath = "https://cxhl-oss.oss-cn-shenzhen.aliyuncs.com/cxhl/logo/cxhl_logo.jpg";
//2:红色
//1:黄色
//0:绿色
String num = "0";
String base64 = getBaseQrCode(num,logoPath);
System.out.println(base64);
}
}
总结
if ("2".equals(color)){
//2:红色
image.setRGB(x, y, bitMatrix.get(x, y) ? 0xDD4747 : 0xFFFFFFFF);
}else if ("1".equals(color)){
//1:黄色
image.setRGB(x, y, bitMatrix.get(x, y) ? 0xEE854E : 0xFFFFFFFF);
}else{
//0:绿色
image.setRGB(x, y, bitMatrix.get(x, y) ? 0x2DBD8D : 0xFFFFFFFF);
}
这个位置为16进制的颜色码,记得前面跟0×,#号去掉即可
颜色表:http://t.zoukankan.com/liyuspace-p-8978139.html
记得删除前缀:data:image/png;base64, 有逗号
转base64网址:http://www.ec95.com/