修改完善二维码生成,增加内容文字;可能二维码内容与打印的文字不一样
This commit is contained in:
parent
eb92b93845
commit
5c4c7e74ff
@ -167,11 +167,11 @@ public class EbikeOperateSystemInfoController {
|
|||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
@PostMapping("ebikeQrCodeGenerate")
|
@PostMapping("ebikeQrCodeGenerate")
|
||||||
public JsonResult<?> ebikeQrGenerate(@RequestParam(name = "content") String content) {
|
public JsonResult<?> ebikeQrGenerate(@RequestParam(name = "content") String content, @RequestParam(name = "text") String text) {
|
||||||
if (content == null || content.isEmpty()) {
|
if (content == null || content.isEmpty()) {
|
||||||
return JsonResult.failed("二维码内容不能为空");
|
return JsonResult.failed("二维码内容不能为空");
|
||||||
}
|
}
|
||||||
return JsonResult.success(QRGenUtil.generateQRCodeBase64(content));
|
return JsonResult.success(QRGenUtil.generateQRCodeBase64(content, text));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@ -25,9 +25,10 @@ public class QRGenUtil {
|
|||||||
* <p>增加内容文字
|
* <p>增加内容文字
|
||||||
*
|
*
|
||||||
* @param content 二维码内容
|
* @param content 二维码内容
|
||||||
|
* @param text 文字内容
|
||||||
* @return Base64编码的二维码
|
* @return Base64编码的二维码
|
||||||
*/
|
*/
|
||||||
public static String generateQRCodeBase64(String content) {
|
public static String generateQRCodeBase64(String content, String text) {
|
||||||
// 1. 生成基础二维码
|
// 1. 生成基础二维码
|
||||||
try (ByteArrayOutputStream qrStream = QRCode.from(content)
|
try (ByteArrayOutputStream qrStream = QRCode.from(content)
|
||||||
.withSize(300, 300)
|
.withSize(300, 300)
|
||||||
@ -35,6 +36,9 @@ public class QRGenUtil {
|
|||||||
.withErrorCorrection(ErrorCorrectionLevel.H) // 纠错等级(H为最高)
|
.withErrorCorrection(ErrorCorrectionLevel.H) // 纠错等级(H为最高)
|
||||||
.to(ImageType.PNG) // 输出格式
|
.to(ImageType.PNG) // 输出格式
|
||||||
.stream()) {
|
.stream()) {
|
||||||
|
if(text == null || text.isEmpty()){
|
||||||
|
return Base64.getEncoder().encodeToString(qrStream.toByteArray());
|
||||||
|
}
|
||||||
|
|
||||||
// 2. 将二维码转换为 BufferedImage
|
// 2. 将二维码转换为 BufferedImage
|
||||||
ByteArrayInputStream inputStream = new ByteArrayInputStream(qrStream.toByteArray());
|
ByteArrayInputStream inputStream = new ByteArrayInputStream(qrStream.toByteArray());
|
||||||
@ -53,12 +57,12 @@ public class QRGenUtil {
|
|||||||
g2d.setColor(Color.BLACK);
|
g2d.setColor(Color.BLACK);
|
||||||
|
|
||||||
// 计算文字位置(底部居中)
|
// 计算文字位置(底部居中)
|
||||||
int textWidth = g2d.getFontMetrics().stringWidth(content);
|
int textWidth = g2d.getFontMetrics().stringWidth(text);
|
||||||
int x = (qrImage.getWidth() - textWidth) / 2;
|
int x = (qrImage.getWidth() - textWidth) / 2;
|
||||||
int y = qrImage.getHeight() - 2; // 底部留白
|
int y = qrImage.getHeight() - 2; // 底部留白
|
||||||
|
|
||||||
// 添加文字说明
|
// 添加文字说明
|
||||||
g2d.drawString(content, x, y);
|
g2d.drawString(text, x, y);
|
||||||
g2d.dispose();
|
g2d.dispose();
|
||||||
|
|
||||||
// 使用 Java 8+ 的 Base64 编码(避免自动换行问题)
|
// 使用 Java 8+ 的 Base64 编码(避免自动换行问题)
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user