C # 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; // you need to download using System from the Internet. text. regularExpressions; using ByteMatrix = com. google. zxing. common. byteMatrix; namespace QR code {public partial class Form1: Form {public Form1 () {Initializ EComponent () ;}// generate the 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 = "image saved to:" + filename;} catch (Exception ee) {MessageBox. show (ee. message) ;}}// generate the 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 = "image 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); // calculates 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 Bitmap bmp = 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 the image System into the QR code. 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 saves 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 = "image saved to:" + filename;} catch (Exception ee) {MessageBox. show (ee. message) ;}} string opFilePath = ""; // select the 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 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 the Utf-8 code Hashtable hints = new Hashtable (); hints. add (EncodeHintType. CHARACTER_SET, "UTF-8"); Result r = mutiReader. decode (bb, hints); richTextBox1.Text = r. text ;}}}