|
@@ -5,14 +5,20 @@ 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.QRCodeWriter;
|
|
|
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
|
|
|
|
|
|
+import javax.imageio.ImageIO;
|
|
|
+import java.awt.*;
|
|
|
+import java.awt.image.BufferedImage;
|
|
|
+import java.io.ByteArrayOutputStream;
|
|
|
import java.io.IOException;
|
|
|
import java.util.HashMap;
|
|
|
import java.util.Map;
|
|
|
|
|
|
public class QRCodeUtils {
|
|
|
|
|
|
+
|
|
|
/**
|
|
|
* 生成二维码
|
|
|
*
|
|
@@ -47,6 +53,77 @@ public class QRCodeUtils {
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
+ * 生成带有底部文字的二维码
|
|
|
+ *
|
|
|
+ * @param content 二维码的内容
|
|
|
+ * @param text 底部文字
|
|
|
+ * @throws IOException
|
|
|
+ */
|
|
|
+ public static byte[] createCodeWithText(String content, String text) throws IOException {
|
|
|
+ // 二维码的宽高
|
|
|
+ int width = 300;
|
|
|
+ int height = 300;
|
|
|
+
|
|
|
+ // 其他参数,如字符集编码
|
|
|
+ Map<EncodeHintType, Object> hints = new HashMap<>();
|
|
|
+ hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
|
|
|
+ hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
|
|
|
+ hints.put(EncodeHintType.MARGIN, 0);
|
|
|
+
|
|
|
+ BitMatrix bitMatrix = null;
|
|
|
+ try {
|
|
|
+ // 生成二维码矩阵
|
|
|
+ QRCodeWriter writer = new QRCodeWriter();
|
|
|
+ bitMatrix = writer.encode(content, BarcodeFormat.QR_CODE, width, height, hints);
|
|
|
+ } catch (WriterException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+
|
|
|
+ // 创建图像并绘制二维码
|
|
|
+ int imgWidth = width;
|
|
|
+ int imgHeight = height + 30; // 增加额外空间用于文本
|
|
|
+ BufferedImage image = new BufferedImage(imgWidth, imgHeight, BufferedImage.TYPE_INT_RGB);
|
|
|
+ Graphics2D graphics = image.createGraphics();
|
|
|
+
|
|
|
+ // 设置背景色
|
|
|
+ graphics.setColor(Color.WHITE);
|
|
|
+ graphics.fillRect(0, 0, imgWidth, imgHeight);
|
|
|
+
|
|
|
+ // 绘制二维码
|
|
|
+ graphics.setColor(Color.BLACK);
|
|
|
+ for (int x = 0; x < width; x++) {
|
|
|
+ for (int y = 0; y < height; y++) {
|
|
|
+ if (bitMatrix.get(x, y)) {
|
|
|
+ graphics.fillRect(x, y, 1, 1);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 添加底部文字,使用合适的字体
|
|
|
+ graphics.setColor(Color.BLACK);
|
|
|
+ // 使用支持中文的字体
|
|
|
+ Font font = new Font("Microsoft YaHei", Font.BOLD, 16);
|
|
|
+ graphics.setFont(font);
|
|
|
+ FontMetrics metrics = graphics.getFontMetrics(font);
|
|
|
+ int textWidth = metrics.stringWidth(text);
|
|
|
+ // 在底部居中绘制文本
|
|
|
+ graphics.drawString(text, (imgWidth - textWidth) / 2, height + 20);
|
|
|
+
|
|
|
+ graphics.dispose();
|
|
|
+
|
|
|
+ // 保存图像
|
|
|
+ try {
|
|
|
+ ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
|
|
|
+// ImageIO.write(image, "png", new File(filePath));
|
|
|
+ ImageIO.write(image, "png", outputStream);
|
|
|
+ return outputStream.toByteArray();
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
* 删除生成的二维码周围的白边,根据审美决定是否删除
|
|
|
*
|
|
|
* @param matrix BitMatrix对象
|