将自定义图像或文本添加到ZXing.Net生成的QR码中

Ale*_*ber 7 c# qr-code zxing

我使用ZXing.Net库生成QR码图像 -

应用截图

在我班上的最高层:

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

我的方法:

    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);
            }
        }
    }
Run Code Online (Sandbox Code Playgroud)

请告诉我如何在QR码中间添加短文本字符串或自定义图像 - 类似于下面的维基百科视觉二维码:

维基百科

更新:

在QR码中嵌入自定义徽标(不破坏后者!)似乎不是一项微不足道的任务,因为科学出版物QR图像:QR码中的优化图像嵌入显示...

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

Tho*_*oub 13

我们走了(你可以使用任何标志):

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;
    }

}
Run Code Online (Sandbox Code Playgroud)

结果:

在此输入图像描述

并输出:

真正