C # how to generate a QR code (including decoding)

Source: Internet
Author: User

C # how to generate a QR code (including decoding)

  1. Using System;
  2. Using System. Collections. Generic;
  3. Using System. ComponentModel;
  4. Using System. Data;
  5. Using System. Drawing;
  6. Using System. Linq;
  7. Using System. Text;
  8. Using System. Windows. Forms;
  9. Using System. Collections;
  10. Using com. google. zxing; // download from the Internet
  11. Using System. Text. RegularExpressions;
  12. Using ByteMatrix = com. google. zxing. common. ByteMatrix;
  13. Namespace QR code
  14. {
  15. Public partial class Form1: Form
  16. {
  17. Public Form1 ()
  18. {
  19. InitializeComponent ();
  20. }
  21. // Generate a barcode
  22. Private void button#click (object sender, EventArgs e)
  23. {
  24. Regex rg = new Regex ("^ [0-9] {13} $ ");
  25. If (! Rg. IsMatch (textBox1.Text ))
  26. {
  27. MessageBox. Show ("this example uses EAN_13 encoding and requires 13 digits ");
  28. Return;
  29. }
  30. Try
  31. {
  32. MultiFormatWriter mutiWriter = new com. google. zxing. MultiFormatWriter ();
  33. ByteMatrix bm = mutiWriter. encode (textBox1.Text, com. google. zxing. BarcodeFormat. EAN_13, 363,150 );
  34. Bitmap img = bm. ToBitmap ();
  35. PictureBox1.Image = img;
  36. // Automatically save the image to the current directory
  37. String filename = System. Environment. CurrentDirectory + "\ EAN_13" + DateTime. Now. Ticks. ToString () + ". jpg ";
  38. Img. Save (filename, System. Drawing. Imaging. ImageFormat. Jpeg );
  39. Label2.Text = "the image has been saved to:" + filename;
  40. }
  41. Catch (Exception ee)
  42. {
  43. MessageBox. Show (ee. Message );
  44. }
  45. }
  46. // Generate a QR code
  47. Private void button2_Click (object sender, EventArgs e)
  48. {
  49. Try
  50. {
  51. MultiFormatWriter mutiWriter = new com. google. zxing. MultiFormatWriter ();
  52. ByteMatrix bm = mutiWriter. encode (textBox1.Text, com. google. zxing. BarcodeFormat. QR_CODE, 300,300 );
  53. Bitmap img = bm. ToBitmap ();
  54. PictureBox1.Image = img;
  55. String filename = System. Environment. CurrentDirectory + "\ QR" + DateTime. Now. Ticks. ToString () + ". jpg ";
  56. Img. Save (filename, System. Drawing. Imaging. ImageFormat. Jpeg );
  57. Label2.Text = "the image has been saved to:" + filename;
  58. }
  59. Catch (Exception ee)
  60. {
  61. MessageBox. Show (ee. Message );
  62. }
  63. }
  64. // Generate a QR code with an image
  65. Private void button3_Click (object sender, EventArgs e)
  66. {
  67. Try
  68. {
  69. MultiFormatWriter mutiWriter = new com. google. zxing. MultiFormatWriter ();
  70. Hashtable hint = new Hashtable ();
  71. Hint. Add (EncodeHintType. CHARACTER_SET, "UTF-8 ");
  72. Hint. Add (EncodeHintType. ERROR_CORRECTION, com. google. zxing. qrcode. decoder. ErrorCorrectionLevel. H );
  73. ByteMatrix bm = mutiWriter. encode (textBox1.Text, com. google. zxing. BarcodeFormat. QR_CODE, 300,300, hint );
  74. Bitmap img = bm. ToBitmap ();
  75. Image middlImg = QRMiddleImg. Image;
  76. System. Drawing. Size realSize = mutiWriter. GetEncodeSize (textBox1.Text, com. google. zxing. BarcodeFormat. QR_CODE, 300,300 );
  77. // Calculate the size and position of the inserted Image
  78. Int middleImgW = Math. Min (int) (realSize. Width/3.5), middlImg. Width );
  79. Int middleImgH = Math. Min (int) (realSize. Height/3.5), middlImg. Height );
  80. Int middleImgL = (img. Width-middleImgW)/2;
  81. Int middleImgT = (img. Height-middleImgH)/2;
  82. // Convert img to bmp format. Otherwise, the Graphics object cannot be created later.
  83. Bitmap BMP img = new Bitmap (img. Width, img. Height, System. Drawing. Imaging. PixelFormat. Format32bppArgb );
  84. Using (Graphics g = Graphics. FromImage (bmp img ))
  85. {
  86. G. InterpolationMode = System. Drawing. Drawing2D. InterpolationMode. HighQualityBicubic;
  87. G. SmoothingMode = System. Drawing. Drawing2D. SmoothingMode. HighQuality;
  88. G. CompositingQuality = System. Drawing. Drawing2D. CompositingQuality. HighQuality;
  89. G. DrawImage (img, 0, 0 );
  90. }
  91. // Insert an image into the QR code
  92. System. Drawing. Graphics MyGraphic = System. Drawing. Graphics. FromImage (bmp img );
  93. // White background
  94. MyGraphic. FillRectangle (Brushes. White, middleImgL, middleImgT, middleImgW, middleImgH );
  95. MyGraphic. DrawImage (middlImg, middleImgL, middleImgT, middleImgW, middleImgH );
  96. PictureBox1.Image = BMP;
  97. // Automatically save the image to the current directory
  98. String filename = System. Environment. CurrentDirectory + "\ QR" + DateTime. Now. Ticks. ToString () + ". jpg ";
  99. Bmp img. Save (filename, System. Drawing. Imaging. ImageFormat. Jpeg );
  100. Label2.Text = "the image has been saved to:" + filename;
  101. }
  102. Catch (Exception ee)
  103. {
  104. MessageBox. Show (ee. Message );
  105. }
  106. }
  107. String opFilePath = "";
  108. // Select the intermediate image to be generated
  109. Private void pictureBox2_Click (object sender, EventArgs e)
  110. {
  111. OpenFileDialog1.Filter = "Image File | *. bmp; *. jpg; *. png; *. ico ";
  112. If (openFileDialog1.ShowDialog () = DialogResult. OK)
  113. {
  114. OpFilePath = openFileDialog1.FileName;
  115. Image img = Image. FromFile (opFilePath );
  116. QRMiddleImg. Image = img;
  117. }
  118. }
  119. // Select the image to decode
  120. Private void button4_Click (object sender, EventArgs e)
  121. {
  122. OpenFileDialog1.Filter = "Image File | *. bmp; *. jpg; *. png; *. ico ";
  123. If (openFileDialog1.ShowDialog () = DialogResult. OK)
  124. {
  125. OpFilePath = openFileDialog1.FileName;
  126. PictureBox2.ImageLocation = opFilePath;
  127. }
  128. }
  129. // Decodes the QR code
  130. Private void button5_Click (object sender, EventArgs e)
  131. {
  132. MultiFormatReader mutiReader = new com. google. zxing. MultiFormatReader ();
  133. Bitmap img = (Bitmap) Bitmap. FromFile (opFilePath );
  134. If (img = null)
  135. Return;
  136. LuminanceSource ls = new RGBLuminanceSource (img, img. Width, img. Height );
  137. BinaryBitmap bb = new BinaryBitmap (new com. google. zxing. common. HybridBinarizer (ls ));
  138. // Note that it must be a Utf-8 code
  139. Hashtable hints = new Hashtable ();
  140. Hints. Add (EncodeHintType. CHARACTER_SET, "UTF-8 ");
  141. Result r = mutiReader. decode (bb, hints );
  142. RichTextBox1.Text = r. Text;
  143. }
  144. }
  145. }

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.