c# - Add custom image or text to QR code generated by ZXing.Net -
i use zxing.net library generate qr code image -
at top of 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 } { deleteobject(hbmp); } } }
please advise me how add short text string or custom image in middle of qr code - similar wikipedia visual qr code below:
update:
embedding custom logo in qr code (without breaking latter!) seems not trivial task scientific publication qr images: optimized image embedding in qr codes shows...
but still wonder if generate qr code (as in above source code), overlay custom text or logo, validate resulting image again zxing.net.
here go (you can use 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 output:
true
Comments
Post a Comment