程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> C#如何生成二維碼詳解(含解碼)

C#如何生成二維碼詳解(含解碼)

編輯:C#入門知識

C#如何生成二維碼詳解(含解碼)



  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;//需要從網上下載
  11. using System.Text.RegularExpressions;
  12. using ByteMatrix = com.google.zxing.common.ByteMatrix;
  13. namespace 二維碼
  14. {
  15. public partial class Form1 : Form
  16. {
  17. public Form1()
  18. {
  19. InitializeComponent();
  20. }
  21. //生成條形碼
  22. private void button1_Click(object sender, EventArgs e)
  23. {
  24. Regex rg = new Regex("^[0-9]{13}$");
  25. if (!rg.IsMatch(textBox1.Text))
  26. {
  27. MessageBox.Show("本例子采用EAN_13編碼,需要輸入13位數字");
  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. //自動保存圖片到當前目錄
  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 = "圖片已保存到:" + filename;
  40. }
  41. catch (Exception ee)
  42. {
  43. MessageBox.Show(ee.Message);
  44. }
  45. }
  46. //生成二維碼
  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 = "圖片已保存到:" + filename;
  58. }
  59. catch (Exception ee)
  60. {
  61. MessageBox.Show(ee.Message);
  62. }
  63. }
  64. //生成帶圖片的二維碼
  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. //計算插入圖片的大小和位置
  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. //將img轉換成bmp格式,否則後面無法創建 Graphics對象
  83. Bitmap bmpimg = new Bitmap(img.Width, img.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
  84. using (Graphics g = Graphics.FromImage(bmpimg))
  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. //在二維碼中插入圖片
  92. System.Drawing.Graphics MyGraphic = System.Drawing.Graphics.FromImage(bmpimg);
  93. //白底
  94. MyGraphic.FillRectangle(Brushes.White, middleImgL, middleImgT, middleImgW, middleImgH);
  95. MyGraphic.DrawImage(middlImg, middleImgL, middleImgT, middleImgW, middleImgH);
  96. pictureBox1.Image = bmpimg;
  97. //自動保存圖片到當前目錄
  98. string filename = System.Environment.CurrentDirectory + "\\QR" + DateTime.Now.Ticks.ToString() + ".jpg";
  99. bmpimg.Save(filename, System.Drawing.Imaging.ImageFormat.Jpeg);
  100. label2.Text = "圖片已保存到:" + filename;
  101. }
  102. catch (Exception ee)
  103. {
  104. MessageBox.Show(ee.Message);
  105. }
  106. }
  107. string opFilePath = "";
  108. //選擇要生成的中間圖片
  109. private void pictureBox2_Click(object sender, EventArgs e)
  110. {
  111. openFileDialog1.Filter = "圖片文件|*.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. //選擇要解碼的圖片
  120. private void button4_Click(object sender, EventArgs e)
  121. {
  122. openFileDialog1.Filter = "圖片文件|*.bmp;*.jpg;*.png;*.ico";
  123. if (openFileDialog1.ShowDialog() == DialogResult.OK)
  124. {
  125. opFilePath = openFileDialog1.FileName;
  126. pictureBox2.ImageLocation = opFilePath;
  127. }
  128. }
  129. //解碼二維碼
  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. //注意 必須是Utf-8編碼
  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. }

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved