Uso la biblioteca ZXing.Net para generar una imagen de código QR:
En la parte superior de mi clase:
[System.Runtime.InteropServices.DllImport("gdi32.dll")] public static extern bool DeleteObject(IntPtr hObject);
Mi método:
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); } } }
Por favor, avíseme cómo agregar una cadena de texto corta o una imagen personalizada en el medio del código QR, similar al código QR visual de Wikipedia a continuación:
ACTUALIZAR:
Incrustar un logotipo personalizado en un código QR (¡sin romper este último!) parece no ser una tarea trivial, como muestra la publicación científica Imágenes QR: incrustación de imágenes optimizadas en códigos QR ...
Pero todavía me pregunto si podría generar un código QR (como en el código fuente anterior), luego superponerlo con un texto o logotipo personalizado, luego validar la imagen resultante nuevamente por ZXing.Net.
Aquí vamos (puedes usar cualquier 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; } }
El resultado:
Y la salida:
Verdadero
Dado que obtiene un mapa de bits de ZXing, puede usar técnicas estándar de C # para dibujar texto. Ver esta respuesta para más información:
c# escribir texto en mapa de bits
Para la posteridad, aquí hay un código copiado descaradamente:
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();