首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在ZXing中将二维码版本从3改为10

如何在ZXing中将二维码版本从3改为10
EN

Stack Overflow用户
提问于 2015-05-06 22:50:24
回答 1查看 3.1K关注 0票数 1

我已经创建了一个应用程序,它为我生成了一个带有ZXing库的二维码图像,但它使用的是版本3的二维码,我想知道是否可以将其更改为版本10……我是ZXing库的新手……

代码如下:

代码语言:javascript
复制
public void CreateQRImage(string inputData)
{
    if (radioRH.Checked)
    {
        if (inputData.Trim() == String.Empty)
        {
            System.Windows.Forms.MessageBox.Show("Data must not be empty.");
        }

        BarcodeWriter qrcoder = new ZXing.BarcodeWriter
        {
            Format = BarcodeFormat.QR_CODE,
            Options = new ZXing.QrCode.QrCodeEncodingOptions
            {
                ErrorCorrection = ZXing.QrCode.Internal.ErrorCorrectionLevel.H,
                Height = 250,
                Width = 250
            }
        };

        string tempFileName = System.IO.Path.GetTempPath() + inputData + ".png";

        Image image;
        String data = inputData;
        var result = qrcoder.Write(inputData);
        image = new Bitmap(result);
        image.Save(tempFileName);

        System.Diagnostics.Process.Start(tempFileName);

        var textRes = qrcoder.Write(inputData);

        int textWidth = 230, textHeight = 20;
        // creating new bitmap having imcreased width
        var img = new Bitmap(textRes.Width + textWidth, textRes.Height);

        using (var g = Graphics.FromImage(img))
        using (var font = new Font(FontFamily.GenericMonospace, 14))
        using (var brush = new SolidBrush(Color.Black))
        using (var bgBrush = new SolidBrush(Color.White))
        using (var format = new StringFormat() { Alignment = StringAlignment.Near })
        {
            // filling background with white color
            g.FillRectangle(bgBrush, 0, 0, img.Width, img.Height);
            // drawing the generated image over new one
            g.DrawImage(textRes, new Point(0, 0));
            // drawing text
            g.DrawString(inputData, font, brush, textRes.Width, (result.Height - textHeight) / 2, format);
        }

        img.Save(tempFileName);
    }

我希望二维码图像从这个(版本3)更改

到这个(版本10)

EN

回答 1

Stack Overflow用户

发布于 2019-04-15 12:51:36

我知道这是一个老问题,但我最近也遇到了同样的问题。

我的公司要求我生成版本4的二维码(33 * 33),但Zxing默认使用更低的版本。

这在网上几乎没有记录,但有一种方法可以迫使Zxing生成特定版本的二维码。在您的代码示例中,只需在格式选项中添加"QrVersion = 10“:

代码语言:javascript
复制
    BarcodeWriter qrcoder = new ZXing.BarcodeWriter
    {
        Format = BarcodeFormat.QR_CODE,
        Options = new ZXing.QrCode.QrCodeEncodingOptions
        {
            ErrorCorrection = ZXing.QrCode.Internal.ErrorCorrectionLevel.H,
            Height = 250,
            Width = 250,
            QrVersion = 10
        }
    };

这将产生一个版本10的二维码。

**注意:如果您设置了较低的QrVersion值(例如4),然后尝试在二维码中放入大量数据,则会抛出错误。

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30080243

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档