【问题标题】:Add custom image or text to QR code generated by ZXing.Net为中兴网生成的二维码添加自定义图片或文字
【发布时间】:2015-06-25 14:58:33
【问题描述】:

我使用ZXing.Net库生成二维码图片-

在我班上名列前茅:

    [System.Runtime.InteropServices.DllImport("gdi32.dll")]
    public static extern bool DeleteObject(IntPtr hObject);

我的方法:

    protected void UpdateQRSource(String address)
    {
        QRCodeWriter qrcode = new QRCodeWriter();
        BarcodeWriter barcodeWriter = new BarcodeWriter
        {
            Format = BarcodeFormat.QR_CODE,
            Options = new EncodingOptions
            {
                Width = 300,
                Height = 300,
                Margin = 4
            }
        };

        using (Bitmap bitmap = barcodeWriter.Write(address))
        {
            IntPtr hbmp = bitmap.GetHbitmap();
            try
            {
                BitmapSource source = Imaging.CreateBitmapSourceFromHBitmap(
                    hbmp, 
                    IntPtr.Zero, 
                    Int32Rect.Empty,
                    BitmapSizeOptions.FromEmptyOptions());
                qrImage.Source = source; // set WPF image source
            }
            finally
            {
                DeleteObject(hbmp);
            }
        }
    }

请告诉我如何在二维码中间添加短文本字符串或自定义图像 - 类似于下面的Wikipedia visual QR code

更新:

在 QR 码中嵌入自定义徽标(不破坏后者!)似乎不是一项简单的任务,正如科学出版物 QR Images: Optimized Image Embedding in QR Codes 显示的那样......

但我仍然想知道是否可以生成一个二维码(如上面的源代码),然后用自定义文本或徽标覆盖它,然后通过 ZXing.Net 再次验证生成的图像。

【问题讨论】:

  • 我不知道库,但这不是文本,这是图像,你应该往这个方向看
  • 是的,确定最终输出是图像 - 但我在 ZXing 库中使用任何方法在生成的 QR 中添加自定义文本或图像。此外,我不能只是将我的图像放在 QR 上,然后只希望由于纠错(正如一些博客所建议的那样)它仍然可读 - 因为我的 QR 是动态生成的并且包含(可变)蓝牙地址 + 更多东西。
  • 我的意思是没有添加文本的方法,只有在你的二维码中添加图像的方法,就像维基百科放维基百科标志而不是文本wikipedia

标签: c# zxing qr-code


【解决方案1】:

我们开始(您可以使用任何徽标):

using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using ZXing;
using ZXing.QrCode.Internal;
using ZXing.Rendering;


namespace Test
{
    public partial class Form1 : Form
{

    private string imagePath = @"YourPath";
    private string url = @"https://en.WIKIPEDIA.ORG/";
    private int size = 400;
    public Form1()
    {
        InitializeComponent();

        pictureBox1.Image = GenerateQR(size, size, url);
        pictureBox1.Height = size;
        pictureBox1.Width = size;
        Console.WriteLine(checkQR(new Bitmap(pictureBox1.Image)));
    }

    public bool checkQR(Bitmap QrCode)
    {
        var reader = new BarcodeReader();
        var result = reader.Decode(QrCode);
        if (result == null)
            return false;
        return result.Text == url;
    }


    public Bitmap GenerateQR(int width, int height, string text)
    {
        var bw = new ZXing.BarcodeWriter();

        var encOptions = new ZXing.Common.EncodingOptions
        {
            Width = width,
            Height = height,
            Margin = 0,
            PureBarcode = false
        };

        encOptions.Hints.Add(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);

        bw.Renderer = new BitmapRenderer();
        bw.Options = encOptions;
        bw.Format = ZXing.BarcodeFormat.QR_CODE;
        Bitmap bm = bw.Write(text);
        Bitmap overlay = new Bitmap(imagePath);

        int deltaHeigth = bm.Height - overlay.Height;
        int deltaWidth = bm.Width - overlay.Width;

        Graphics g = Graphics.FromImage(bm);
        g.DrawImage(overlay, new Point(deltaWidth/2,deltaHeigth/2));

        return bm;
    }

}

结果:

还有输出:

是的

【讨论】:

  • 这是很棒的代码,谢谢 (+1) 但缺少对结果位图的验证...(即,如果 bm 仍然是有效的二维码)
  • 如果解码失败,return result.Text == url 不会抛出异常(我是 C# 新手)?写成return result != null && result.Text == url不是更好吗?
  • result.Text == url 不只是比较字符串地址吗?你不应该最好打电话给url.Equals(result.Text) 来比较字符串内容吗?
  • @AlexanderFarber you can 但这不是字符串的义务
  • @ThomasAyoub “PureBarcode”属性的目的是什么?在文档中说:“不要将内容字符串放入输出图像中。”这不是很清楚!
【解决方案2】:

由于您从 ZXing 中获得了位图,因此您可以使用标准 C# 技术来绘制文本。有关更多信息,请参阅此答案:

c# write text on bitmap

为了后代,这里有一些无耻的复制代码:

Bitmap bmp = //from ZXing;

RectangleF rectf = new RectangleF(70, 90, 90, 50);

Graphics g = Graphics.FromImage(bmp);

g.SmoothingMode = SmoothingMode.AntiAlias;
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
g.PixelOffsetMode = PixelOffsetMode.HighQuality;
g.DrawString("yourText", new Font("Tahoma",8), Brushes.Black, rectf);

g.Flush();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-02-06
    • 1970-01-01
    • 1970-01-01
    • 2017-01-04
    • 2016-05-03
    • 1970-01-01
    • 2020-02-03
    • 2014-04-25
    相关资源
    最近更新 更多