44 lines
1.2 KiB
Java
44 lines
1.2 KiB
Java
package cn.iocoder.yudao.util;
|
|
|
|
import com.google.zxing.BarcodeFormat;
|
|
import com.google.zxing.MultiFormatWriter;
|
|
import com.google.zxing.WriterException;
|
|
import com.google.zxing.client.j2se.MatrixToImageWriter;
|
|
import com.google.zxing.common.BitMatrix;
|
|
import org.springframework.stereotype.Component;
|
|
|
|
import java.io.ByteArrayOutputStream;
|
|
import java.io.IOException;
|
|
|
|
/**
|
|
* 用于生成二维码的工具类
|
|
*
|
|
* @author 小李
|
|
* @date 14:56 2024/10/30
|
|
**/
|
|
public class CreateQRCodeUtil {
|
|
|
|
/**
|
|
* 生成二维码的方法
|
|
*
|
|
* @author 小李
|
|
* @date 15:00 2024/10/30
|
|
* @param text 二维码的内容
|
|
* @param width 二维码的宽度
|
|
* @param height 二维码的高度
|
|
**/
|
|
public static byte[] GenerateQRCode(String text, int width, int height) {
|
|
try {
|
|
BitMatrix bitMatrix = new MultiFormatWriter().encode(text, BarcodeFormat.QR_CODE, width, height);
|
|
|
|
try (ByteArrayOutputStream pngOutputStream = new ByteArrayOutputStream()) {
|
|
MatrixToImageWriter.writeToStream(bitMatrix, "PNG", pngOutputStream);
|
|
return pngOutputStream.toByteArray();
|
|
}
|
|
}catch (Exception e){
|
|
e.printStackTrace();
|
|
return null;
|
|
}
|
|
}
|
|
}
|