【问题标题】:How to add custom text on the bottom of QR Code image如何在二维码图像底部添加自定义文本
【发布时间】:2017-01-04 15:04:42
【问题描述】:

zxing` lib,用于在我的 spring mvc 应用程序中生成 qr 代码。他们生成代码非常好,现在我想要在 qr 图像底部的自定义文本。 比如----

我使用的库。

<dependency>
            <groupId>com.google.zxing</groupId>
            <artifactId>javase</artifactId>
            <version>2.2</version>
        </dependency>

我使用的代码

 String charset = "UTF-8"; // or "ISO-8859-1"
                                Map hintMap = new HashMap();
                                hintMap.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L);
                                createQRCode(code,
                                        dirPath ,
                                        charset, hintMap, 200, 200);
public static void createQRCode(String qrCodeData, String filePath, String charset, Map hintMap, int qrCodeheight,
            int qrCodewidth) throws WriterException, IOException {
        BitMatrix matrix = new MultiFormatWriter().encode(new String(qrCodeData.getBytes(charset), charset),
                BarcodeFormat.QR_CODE, qrCodewidth, qrCodeheight, hintMap);
        MatrixToImageWriter.writeToFile(matrix, filePath.substring(filePath.lastIndexOf('.') + 1), new File(filePath));
    }

【问题讨论】:

  • 我也遇到了同样的问题,有人找到解决办法了吗???

标签: java zxing


【解决方案1】:

您可以在生成条形码图像后手动绘制文本:

var result = qrcoder.Write(inputData);

using (var g = Graphics.FromImage(result))
using (var font = new Font(FontFamily.GenericMonospace, 12))
using (var brush = new SolidBrush(Color.Black))
using(var format = new StringFormat(){Alignment = StringAlignment.Center})
{
    int margin = 5, textHeight = 20;

    var rect = new RectangleF(margin, result.Height - textHeight,
                              result.Width - 2 * margin, textHeight);

    g.DrawString(inputData, font, brush, rect, format);
}

result.Save(tempFileName);

您可以选择自己的字体大小和字体系列,以更好地满足您的目标。

文本的位置由您绘制的矩形 (rect) 和 format.Alignment 属性决定

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-08-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-24
    • 2023-02-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多