|
@@ -13,6 +13,11 @@ import org.springframework.util.LinkedMultiValueMap;
|
|
|
import org.springframework.util.MultiValueMap;
|
|
|
import org.springframework.web.client.RestTemplate;
|
|
|
|
|
|
+import javax.imageio.ImageIO;
|
|
|
+import java.awt.*;
|
|
|
+import java.awt.image.BufferedImage;
|
|
|
+import java.io.ByteArrayInputStream;
|
|
|
+import java.io.ByteArrayOutputStream;
|
|
|
import java.io.File;
|
|
|
import java.nio.charset.StandardCharsets;
|
|
|
import java.util.Base64;
|
|
@@ -113,6 +118,15 @@ public class WeChatMiniProgramUtil {
|
|
|
return response;
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 生成小程序码并在底部加上指定文字
|
|
|
+ */
|
|
|
+ public String generateQrCodeWithText(String scene, String page, String footerText) throws Exception {
|
|
|
+ byte[] qrCodeBytes = generateUnlimitedQrCode(scene, page);
|
|
|
+ return addTextToQrCode(qrCodeBytes, footerText, "微软雅黑", 18, Color.BLACK);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
/**
|
|
|
* 生成不限制数量的小程序码(返回base64)
|
|
|
*/
|
|
@@ -157,6 +171,64 @@ public class WeChatMiniProgramUtil {
|
|
|
return body;
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 在小程序码图片底部添加指定文字(带白色背景)
|
|
|
+ *
|
|
|
+ * @param imageBytes 原始图片字节流
|
|
|
+ * @param text 要添加的文字
|
|
|
+ * @param fontName 字体名称,如 "微软雅黑"
|
|
|
+ * @param fontSize 字号大小
|
|
|
+ * @param color 文字颜色
|
|
|
+ * @return 处理后的小程序码图片(Base64)
|
|
|
+ */
|
|
|
+ public String addTextToQrCode(byte[] imageBytes, String text, String fontName, int fontSize, Color color) throws Exception {
|
|
|
+ // 加载原始图片
|
|
|
+ BufferedImage originalImage = ImageIO.read(new ByteArrayInputStream(imageBytes));
|
|
|
+
|
|
|
+ // 底部留白高度
|
|
|
+ int bottomPadding = 40;
|
|
|
+
|
|
|
+ // 创建新图片,扩展高度以容纳文字和白底背景
|
|
|
+ BufferedImage newImage = new BufferedImage(
|
|
|
+ originalImage.getWidth(),
|
|
|
+ originalImage.getHeight() + bottomPadding,
|
|
|
+ BufferedImage.TYPE_INT_RGB // 使用 RGB 模式确保背景非透明
|
|
|
+ );
|
|
|
+
|
|
|
+ Graphics2D g = newImage.createGraphics();
|
|
|
+
|
|
|
+ // 抗锯齿设置
|
|
|
+ g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
|
|
|
+
|
|
|
+ // 绘制原图
|
|
|
+ g.drawImage(originalImage, 0, 0, null);
|
|
|
+
|
|
|
+ // 设置字体样式和颜色
|
|
|
+ Font font = new Font(fontName, Font.BOLD, fontSize);
|
|
|
+ g.setFont(font);
|
|
|
+ g.setColor(color);
|
|
|
+
|
|
|
+ // 填充一个白色矩形作为文字背景
|
|
|
+ g.setColor(Color.WHITE); // 白色背景
|
|
|
+ g.fillRect(0, originalImage.getHeight(), originalImage.getWidth(), bottomPadding);
|
|
|
+
|
|
|
+ // 绘制黑色文字
|
|
|
+ g.setColor(color); // 设置文字颜色,如 Color.BLACK
|
|
|
+ FontMetrics metrics = g.getFontMetrics();
|
|
|
+ int x = (originalImage.getWidth() - metrics.stringWidth(text)) / 2;
|
|
|
+ int y = originalImage.getHeight() + bottomPadding - 10;
|
|
|
+
|
|
|
+ // 绘制文字
|
|
|
+ g.drawString(text, x, y);
|
|
|
+
|
|
|
+ g.dispose();
|
|
|
+
|
|
|
+ // 输出为 Base64
|
|
|
+ ByteArrayOutputStream os = new ByteArrayOutputStream();
|
|
|
+ ImageIO.write(newImage, "PNG", os);
|
|
|
+ return Base64.getEncoder().encodeToString(os.toByteArray());
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* 获取用户 session_key(用于解密用户信息)
|
|
|
*/
|