中兴网生成的二维码中添加自定义图片或文字 [英] Add custom image or text to QR code generated by ZXing.Net
问题描述
我使用如下:
更新:
在 QR 码中嵌入自定义徽标(不破坏后者!)似乎不是一项微不足道的任务,因为科学出版物 QR 图像:优化图像嵌入 QR 码 显示...
但我还是想知道我是否可以生成一个二维码(如上面的源代码),然后用自定义文本或徽标覆盖它,然后通过 ZXing.Net 再次验证生成的图像.
我们开始吧(您可以使用任何徽标):
using System.Collections.Generic;使用 System.Drawing;使用 System.Windows.Forms;使用ZXing;使用 ZXing.QrCode.Internal;使用 ZXing.Rendering;命名空间测试{公共部分类 Form1 :表单{私有字符串 imagePath = @"YourPath";私人字符串 url = @"https://en.WIKIPEDIA.ORG/";私有整数大小 = 400;公共 Form1(){初始化组件();pictureBox1.Image = GenerateQR(size, size, url);图片框1.高度=大小;pictureBox1.Width = 大小;Console.WriteLine(checkQR(new Bitmap(pictureBox1.Image)));}public bool checkQR(位图二维码){var reader = new BarcodeReader();var 结果 = reader.Decode(QrCode);如果(结果 == 空)返回假;返回 result.Text == url;}public Bitmap GenerateQR(int width, int height, string text){var bw = new ZXing.BarcodeWriter();var encOptions = new ZXing.Common.EncodingOptions{宽度 = 宽度,高度 = 高度,保证金 = 0,PureBarcode = 假};encOptions.Hints.Add(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);bw.Renderer = new BitmapRenderer();bw.Options = encOptions;bw.Format = ZXing.BarcodeFormat.QR_CODE;位图 bm = bw.Write(text);位图叠加 = 新位图 (imagePath);int deltaHeigth = bm.Height - overlay.Height;int deltaWidth = bm.Width - overlay.Width;图形 g = Graphics.FromImage(bm);g.DrawImage(overlay, new Point(deltaWidth/2,deltaHeigth/2));返回 bm;}}
结果:
和输出:
<块引用>正确
I use ZXing.Net library to generate a QR code image -
At the top of my class:
[System.Runtime.InteropServices.DllImport("gdi32.dll")]
public static extern bool DeleteObject(IntPtr hObject);
My method:
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);
}
}
}
Please advise me how to add short text string or a custom image in the middle of the QR code - similar to the Wikipedia visual QR code below:
UPDATE:
Embedding custom logo in QR code (without breaking the latter!) seems to be not a trivial task as the scientific publication QR Images: Optimized Image Embedding in QR Codes shows...
But I still wonder if I could generate a QR code (as in the above source code), then overlay it with a custom text or logo, then validate the resulting image again by ZXing.Net.
Here we go (you can use any logo):
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;
}
}
The result:
And the output:
True
这篇关于中兴网生成的二维码中添加自定义图片或文字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!