二维码生成工具类,其中此工具类默认为生成带logo与个性颜色块,根据个人情况可以改造自己需要的工具类,也可直接使用,其中logo图片可为文件也可以以文件流传入,话不多说上代码
package com.jiamuxin.modules.utils;
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 javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.Map;
/**
* @ProjectName: vue_springboot-backend
* @Package: com.jiamuxin.modules.utils
* @ClassName: QRCodeUtil
* @Author: sparkle-summer
* @Description: 二维码工具类
* @Date: 2022/9/16 11:23
* @Version: 1.0.0
*/
public class QRCodeUtil {
// logo图片宽
private static final int LOGO_WIDTH = 60;
// logo图片高
private static final int LOGO_HEIGHT = 60;
// 圆角半径
private static final int RADIUS = 10;
// 二维码宽
private static final int IMAGE_WIDTH = 300;
// 二维码高
private static final int IMAGE_HEIGHT = 300;
// 用于计算二维码生成起始位置、logo图片插入位置、logo周边留白宽度计算
private static final int IMAGE_HALF_WIDTH = LOGO_WIDTH / 2;
// logo图片留白填充宽度
private static final int FRAME_WIDTH = 5;
// logo图片文件地址
private static final String logoImagePath = "C:\\Users\\Administrator\\Desktop\\test\\微信图片_20220711102849.jpg";
// 二维码生成类
private static final MultiFormatWriter mutiWriter = new MultiFormatWriter();
/**
* 二维码生成入口
* @param content 需要加入至二维码中的内容
* @return 返回对象BufferedImage
* @throws WriterException 写异常
* @throws IOException IO异常
*/
public static BufferedImage genBarcode(String content) throws WriterException, IOException {
// 处理logo图片
int[][] srcPixels = getPic(new File(logoImagePath),"isFile");
Map<EncodeHintType, Object> hint = new HashMap<>();
hint.put(EncodeHintType.CHARACTER_SET, "utf-8");
hint.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
hint.put(EncodeHintType.MARGIN, 0);
// 生成二维码
BitMatrix matrix = mutiWriter.encode(content, BarcodeFormat.QR_CODE, IMAGE_WIDTH, IMAGE_HEIGHT, hint);
// 二维矩阵转为一维像素数组
int halfW = matrix.getWidth() / 2;
int halfH = matrix.getHeight() / 2;
int[] pixels = new int[IMAGE_WIDTH * IMAGE_HEIGHT];
for (int y = 0; y < matrix.getHeight(); y++) {
for (int x = 0; x < matrix.getWidth(); x++) {
// 若右上角不需要颜色注释掉第一个if判定
if (x > 10 && x < 83 && y > 10 && y < 83) {
// 左上角颜色,根据自己需要调整颜色范围和颜色
Color color = new Color(231, 135, 56, 242);
int colorInt = color.getRGB();
pixels[y * IMAGE_WIDTH + x] = matrix.get(x, y) ? colorInt : 16777215;
} else if (x > halfW - IMAGE_HALF_WIDTH && x < halfW + IMAGE_HALF_WIDTH
&& y > halfH - IMAGE_HALF_WIDTH && y < halfH + IMAGE_HALF_WIDTH) {
// 读取图片
pixels[y * IMAGE_WIDTH + x] = srcPixels[x - halfW + IMAGE_HALF_WIDTH][y - halfH + IMAGE_HALF_WIDTH];
} else if ((x > halfW - IMAGE_HALF_WIDTH - FRAME_WIDTH && x < halfW - IMAGE_HALF_WIDTH + FRAME_WIDTH
&& y > halfH - IMAGE_HALF_WIDTH - FRAME_WIDTH && y < halfH + IMAGE_HALF_WIDTH + FRAME_WIDTH)
|| (x > halfW + IMAGE_HALF_WIDTH - FRAME_WIDTH && x < halfW + IMAGE_HALF_WIDTH + FRAME_WIDTH
&& y > halfW - IMAGE_HALF_WIDTH - FRAME_WIDTH
&& y < halfH + IMAGE_HALF_WIDTH + FRAME_WIDTH)
|| (x > halfW - IMAGE_HALF_WIDTH - FRAME_WIDTH && x < halfW + IMAGE_HALF_WIDTH + FRAME_WIDTH
&& y > halfH - IMAGE_HALF_WIDTH - FRAME_WIDTH
&& y < halfH - IMAGE_HALF_WIDTH + FRAME_WIDTH)
|| (x > halfW - IMAGE_HALF_WIDTH - FRAME_WIDTH && x < halfW + IMAGE_HALF_WIDTH + FRAME_WIDTH
&& y > halfH + IMAGE_HALF_WIDTH - FRAME_WIDTH
&& y < halfH + IMAGE_HALF_WIDTH + FRAME_WIDTH)) {
pixels[y * IMAGE_WIDTH + x] = 0xfffffff;
// 在logo图片四周形成边框
} else {
// 二维码颜色
int num1 = (int) (50 - (50.0 - 13.0) / matrix.getHeight() * (y + 1));
int num2 = (int) (200 - (165.0 - 72.0) / matrix.getHeight() * (y + 1));
int num3 = (int) (162 - (162.0 - 107.0) / matrix.getHeight() * (y + 1));
Color color = new Color(num1, num2, num3);
int colorInt = color.getRGB();
// 此处可以修改二维码的颜色,可以分别制定二维码和背景的颜色;
pixels[y * IMAGE_WIDTH + x] = matrix.get(x, y) ? colorInt : 16777215;
// 0x000000:0xffffff
}
}
}
BufferedImage image = new BufferedImage(IMAGE_WIDTH, IMAGE_HEIGHT, BufferedImage.TYPE_INT_RGB);
image.getRaster().setDataElements(0, 0, IMAGE_WIDTH, IMAGE_HEIGHT, pixels);
return image;
}
/**
* 图片的压缩、圆角处理,并生成数组,其中两个参数紧能传入一个值
* @param logoPic logo图片
* @param logoType logo图片传入类型,文件或文件流
* @return 将logo图片处理后以二维数组返回
*/
private static int[][] getPic(Object logoPic, String logoType) {
BufferedImage biSrc = null;
try {
// 判定传入参数类型
biSrc = (!"isFile".equals(logoType)?ImageIO.read((InputStream)logoPic):ImageIO.read((File)logoPic));
} catch (IOException e) {
e.printStackTrace();
}
BufferedImage biTarget = new BufferedImage(LOGO_WIDTH, LOGO_HEIGHT, BufferedImage.TYPE_3BYTE_BGR);
assert biSrc != null;
biTarget.getGraphics().drawImage(biSrc.getScaledInstance(LOGO_WIDTH, LOGO_HEIGHT, Image.SCALE_SMOOTH), 0, 0, null);
int[][] src = new int[LOGO_WIDTH][LOGO_HEIGHT];
// 圆角处理半径
int r = RADIUS;
int max = LOGO_WIDTH;
int bordercolor = 0x00000000;
int whitecolor = 0xffffffff;
for (int x = 0; x < LOGO_WIDTH; x++) {
for (int y = 0; y < LOGO_HEIGHT; y++) {
// 将相同的参数提取
int i = (r - x) * (r - x) + (r - y) * (r - y);
if(x<r&&y<r&&(i >(r-1)*(r-1))){
// 左上圆角
if(i >r*r){
src[x][y] = whitecolor;
} else {
src[x][y] = bordercolor;
}
} else {
int i1 = (x + r - max) * (x + r - max);
int i2 = i1 + (r - y) * (r - y);
if (x>(max-r)&&y<r&& i2 >(r-1)*(r-1)){
// 右上圆角
if(i2 >r*r){
src[x][y] = whitecolor;
}else{
src[x][y] = bordercolor;
}
} else {
int i4 = (y + r - max) * (y + r - max);
int i3 = (r - x) * (r - x) + i4;
if (x<r&&y>(max-r)&& i3 >(r-1)*(r-1)){
// 左下圆角
if(i3 >r*r){
src[x][y] = whitecolor;
}else{
src[x][y] = bordercolor;
}
} else if (x>(max-r)&&y>(max-r)&& i1 + i4 >(r-1)*(r-1)){
// 右下圆角
if(i1 + i4 >r*r){
src[x][y] = whitecolor;
}else{
src[x][y] = bordercolor;
}
} else {
if(x >= r && x <= max - r && (y == 0 || y == 1 || y == max - 1) || y >= r && y <= max - r && (x == 0 || x == 1 || x == max - 1)){
// 四周除圆角的边框
src[x][y] = bordercolor;
} else {
// 图片值
src[x][y] = biTarget.getRGB(x, y);
}
}
}
}
}
}
return src;
}
/**
* 测试主方法
* @param args 测试参数
*/
public static void main(String [] args){
try {
// 二维码图片生成本地存放位置
File qrcFilePic = new File("C:\\Users\\Administrator\\Desktop\\test\\images", LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMddHHmmssSSS"))+".png");
BufferedImage image = genBarcode("https://baidu.com?userId=yyds&userName=didiao");
ImageIO.write(image, "jpg", qrcFilePic);
} catch (WriterException | IOException e) {
e.printStackTrace();
}
}
}
希望本工具类需要的朋友有所帮助!!!