C # how to generate a QR code (including decoding)
- Using System;
- Using System. Collections. Generic;
- Using System. ComponentModel;
- Using System. Data;
- Using System. Drawing;
- Using System. Linq;
- Using System. Text;
- Using System. Windows. Forms;
-
- Using System. Collections;
- Using com. google. zxing; // download from the Internet
- Using System. Text. RegularExpressions;
- Using ByteMatrix = com. google. zxing. common. ByteMatrix;
-
- Namespace QR code
- {
- Public partial class Form1: Form
- {
- Public Form1 ()
- {
- InitializeComponent ();
- }
- // Generate a barcode
- Private void button#click (object sender, EventArgs e)
- {
- Regex rg = new Regex ("^ [0-9] {13} $ ");
- If (! Rg. IsMatch (textBox1.Text ))
- {
- MessageBox. Show ("this example uses EAN_13 encoding and requires 13 digits ");
- Return;
- }
-
- Try
- {
- MultiFormatWriter mutiWriter = new com. google. zxing. MultiFormatWriter ();
- ByteMatrix bm = mutiWriter. encode (textBox1.Text, com. google. zxing. BarcodeFormat. EAN_13, 363,150 );
- Bitmap img = bm. ToBitmap ();
- PictureBox1.Image = img;
-
- // Automatically save the image to the current directory
- String filename = System. Environment. CurrentDirectory + "\ EAN_13" + DateTime. Now. Ticks. ToString () + ". jpg ";
- Img. Save (filename, System. Drawing. Imaging. ImageFormat. Jpeg );
- Label2.Text = "the image has been saved to:" + filename;
- }
- Catch (Exception ee)
- {
- MessageBox. Show (ee. Message );
- }
- }
-
- // Generate a QR code
- Private void button2_Click (object sender, EventArgs e)
- {
- Try
- {
- MultiFormatWriter mutiWriter = new com. google. zxing. MultiFormatWriter ();
- ByteMatrix bm = mutiWriter. encode (textBox1.Text, com. google. zxing. BarcodeFormat. QR_CODE, 300,300 );
- Bitmap img = bm. ToBitmap ();
- PictureBox1.Image = img;
- String filename = System. Environment. CurrentDirectory + "\ QR" + DateTime. Now. Ticks. ToString () + ". jpg ";
- Img. Save (filename, System. Drawing. Imaging. ImageFormat. Jpeg );
- Label2.Text = "the image has been saved to:" + filename;
-
- }
- Catch (Exception ee)
- {
- MessageBox. Show (ee. Message );
- }
- }
-
- // Generate a QR code with an image
- Private void button3_Click (object sender, EventArgs e)
- {
- Try
- {
- MultiFormatWriter mutiWriter = new com. google. zxing. MultiFormatWriter ();
- Hashtable hint = new Hashtable ();
- Hint. Add (EncodeHintType. CHARACTER_SET, "UTF-8 ");
- Hint. Add (EncodeHintType. ERROR_CORRECTION, com. google. zxing. qrcode. decoder. ErrorCorrectionLevel. H );
- ByteMatrix bm = mutiWriter. encode (textBox1.Text, com. google. zxing. BarcodeFormat. QR_CODE, 300,300, hint );
- Bitmap img = bm. ToBitmap ();
- Image middlImg = QRMiddleImg. Image;
- System. Drawing. Size realSize = mutiWriter. GetEncodeSize (textBox1.Text, com. google. zxing. BarcodeFormat. QR_CODE, 300,300 );
- // Calculate the size and position of the inserted Image
- Int middleImgW = Math. Min (int) (realSize. Width/3.5), middlImg. Width );
- Int middleImgH = Math. Min (int) (realSize. Height/3.5), middlImg. Height );
- Int middleImgL = (img. Width-middleImgW)/2;
- Int middleImgT = (img. Height-middleImgH)/2;
-
- // Convert img to bmp format. Otherwise, the Graphics object cannot be created later.
- Bitmap BMP img = new Bitmap (img. Width, img. Height, System. Drawing. Imaging. PixelFormat. Format32bppArgb );
- Using (Graphics g = Graphics. FromImage (bmp img ))
- {
- G. InterpolationMode = System. Drawing. Drawing2D. InterpolationMode. HighQualityBicubic;
- G. SmoothingMode = System. Drawing. Drawing2D. SmoothingMode. HighQuality;
- G. CompositingQuality = System. Drawing. Drawing2D. CompositingQuality. HighQuality;
- G. DrawImage (img, 0, 0 );
- }
-
- // Insert an image into the QR code
- System. Drawing. Graphics MyGraphic = System. Drawing. Graphics. FromImage (bmp img );
- // White background
- MyGraphic. FillRectangle (Brushes. White, middleImgL, middleImgT, middleImgW, middleImgH );
- MyGraphic. DrawImage (middlImg, middleImgL, middleImgT, middleImgW, middleImgH );
-
- PictureBox1.Image = BMP;
-
- // Automatically save the image to the current directory
- String filename = System. Environment. CurrentDirectory + "\ QR" + DateTime. Now. Ticks. ToString () + ". jpg ";
- Bmp img. Save (filename, System. Drawing. Imaging. ImageFormat. Jpeg );
- Label2.Text = "the image has been saved to:" + filename;
-
- }
- Catch (Exception ee)
- {
- MessageBox. Show (ee. Message );
- }
- }
-
- String opFilePath = "";
- // Select the intermediate image to be generated
- Private void pictureBox2_Click (object sender, EventArgs e)
- {
- OpenFileDialog1.Filter = "Image File | *. bmp; *. jpg; *. png; *. ico ";
- If (openFileDialog1.ShowDialog () = DialogResult. OK)
- {
- OpFilePath = openFileDialog1.FileName;
- Image img = Image. FromFile (opFilePath );
- QRMiddleImg. Image = img;
- }
- }
-
- // Select the image to decode
- Private void button4_Click (object sender, EventArgs e)
- {
- OpenFileDialog1.Filter = "Image File | *. bmp; *. jpg; *. png; *. ico ";
- If (openFileDialog1.ShowDialog () = DialogResult. OK)
- {
- OpFilePath = openFileDialog1.FileName;
- PictureBox2.ImageLocation = opFilePath;
- }
- }
-
- // Decodes the QR code
- Private void button5_Click (object sender, EventArgs e)
- {
- MultiFormatReader mutiReader = new com. google. zxing. MultiFormatReader ();
- Bitmap img = (Bitmap) Bitmap. FromFile (opFilePath );
- If (img = null)
- Return;
- LuminanceSource ls = new RGBLuminanceSource (img, img. Width, img. Height );
- BinaryBitmap bb = new BinaryBitmap (new com. google. zxing. common. HybridBinarizer (ls ));
- // Note that it must be a Utf-8 code
- Hashtable hints = new Hashtable ();
- Hints. Add (EncodeHintType. CHARACTER_SET, "UTF-8 ");
- Result r = mutiReader. decode (bb, hints );
- RichTextBox1.Text = r. Text;
- }
- }
- }