Merge remote-tracking branch 'origin/master'
This commit is contained in:
@@ -0,0 +1,52 @@
|
||||
package com.jero.util;
|
||||
|
||||
import com.google.zxing.BarcodeFormat;
|
||||
import com.google.zxing.client.j2se.MatrixToImageConfig;
|
||||
import com.google.zxing.client.j2se.MatrixToImageWriter;
|
||||
import com.google.zxing.common.BitMatrix;
|
||||
import com.google.zxing.qrcode.QRCodeWriter;
|
||||
import com.jero.common.exception.JeroBootException;
|
||||
import org.checkerframework.checker.units.qual.K;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.util.Base64;
|
||||
|
||||
/**
|
||||
* 生成二维码base64工具类
|
||||
* @author liJiaRao
|
||||
* @date 2021-07-15 10:15
|
||||
*/
|
||||
public class QrcodeUtil {
|
||||
/**
|
||||
* 蓝色
|
||||
*/
|
||||
public static final int BLUE = 0xFF23CEFD;
|
||||
/**
|
||||
* 蓝色
|
||||
*/
|
||||
public static final int BLACK = 0xFF000000;
|
||||
/**
|
||||
* 白色透明
|
||||
*/
|
||||
public static final int OFF_COLOR = 0x00FFFFFF;
|
||||
/**
|
||||
* 转换url为二维码base64
|
||||
* @param content 要转换的url
|
||||
* @return base64 data:image/png;base64,
|
||||
*/
|
||||
public static String create(String content){
|
||||
try {
|
||||
QRCodeWriter qrCodeWriter = new QRCodeWriter();
|
||||
BitMatrix bitMatrix = qrCodeWriter.encode(content, BarcodeFormat.QR_CODE, 600, 600);
|
||||
|
||||
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
|
||||
MatrixToImageWriter.writeToStream(bitMatrix, "PNG", outputStream, new MatrixToImageConfig(BLACK,OFF_COLOR));
|
||||
|
||||
Base64.Encoder encoder = Base64.getEncoder();
|
||||
|
||||
return "data:image/png;base64,"+encoder.encodeToString(outputStream.toByteArray());
|
||||
}catch (Exception e){
|
||||
throw new JeroBootException("生成二维码失败");
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user