首页 > 代码库 > 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.   
  10. using System.Collections;  
  11. using com.google.zxing;//需要从网上下载  
  12. using System.Text.RegularExpressions;  
  13. using ByteMatrix = com.google.zxing.common.ByteMatrix;  
  14.   
  15. namespace 二维码  
  16. {  
  17.     public partial class Form1 : Form  
  18.     {  
  19.         public Form1()  
  20.         {  
  21.             InitializeComponent();  
  22.         }  
  23.         //生成条形码  
  24.         private void button1_Click(object sender, EventArgs e)  
  25.         {  
  26.             Regex rg = new Regex("^[0-9]{13}$");  
  27.             if (!rg.IsMatch(textBox1.Text))  
  28.             {  
  29.                 MessageBox.Show("本例子采用EAN_13编码,需要输入13位数字");  
  30.                 return;  
  31.             }  
  32.   
  33.             try  
  34.             {  
  35.                 MultiFormatWriter mutiWriter = new com.google.zxing.MultiFormatWriter();  
  36.                 ByteMatrix bm = mutiWriter.encode(textBox1.Text, com.google.zxing.BarcodeFormat.EAN_13, 363, 150);  
  37.                 Bitmap img = bm.ToBitmap();  
  38.                 pictureBox1.Image = img;  
  39.   
  40.                 //自动保存图片到当前目录  
  41.                 string filename = System.Environment.CurrentDirectory + "\\EAN_13" + DateTime.Now.Ticks.ToString() + ".jpg";  
  42.                 img.Save(filename, System.Drawing.Imaging.ImageFormat.Jpeg);  
  43.                 label2.Text = "图片已保存到:" + filename;  
  44.             }  
  45.             catch (Exception ee)  
  46.             {   
  47.                 MessageBox.Show(ee.Message);  
  48.             }  
  49.         }  
  50.   
  51.         //生成二维码  
  52.         private void button2_Click(object sender, EventArgs e)  
  53.         {  
  54.             try  
  55.             {  
  56.                 MultiFormatWriter mutiWriter = new com.google.zxing.MultiFormatWriter();  
  57.                 ByteMatrix bm = mutiWriter.encode(textBox1.Text, com.google.zxing.BarcodeFormat.QR_CODE, 300, 300);  
  58.                 Bitmap img = bm.ToBitmap();  
  59.                 pictureBox1.Image = img;  
  60.                 string filename = System.Environment.CurrentDirectory + "\\QR" + DateTime.Now.Ticks.ToString() + ".jpg";  
  61.                 img.Save(filename, System.Drawing.Imaging.ImageFormat.Jpeg);  
  62.                 label2.Text = "图片已保存到:" + filename;  
  63.   
  64.             }  
  65.             catch (Exception ee)  
  66.             {   
  67.                 MessageBox.Show(ee.Message);   
  68.             }  
  69.         }  
  70.   
  71.         //生成带图片的二维码  
  72.         private void button3_Click(object sender, EventArgs e)  
  73.         {  
  74.             try  
  75.             {  
  76.                 MultiFormatWriter mutiWriter = new com.google.zxing.MultiFormatWriter();  
  77.                 Hashtable hint = new Hashtable();  
  78.                 hint.Add(EncodeHintType.CHARACTER_SET, "UTF-8");  
  79.                 hint.Add(EncodeHintType.ERROR_CORRECTION, com.google.zxing.qrcode.decoder.ErrorCorrectionLevel.H);  
  80.                 ByteMatrix bm = mutiWriter.encode(textBox1.Text, com.google.zxing.BarcodeFormat.QR_CODE, 300, 300, hint);  
  81.                 Bitmap img = bm.ToBitmap();  
  82.                 Image middlImg = QRMiddleImg.Image;  
  83.                 System.Drawing.Size realSize = mutiWriter.GetEncodeSize(textBox1.Text, com.google.zxing.BarcodeFormat.QR_CODE, 300, 300);  
  84.                 //计算插入图片的大小和位置  
  85.                 int middleImgW = Math.Min((int)(realSize.Width / 3.5), middlImg.Width);  
  86.                 int middleImgH = Math.Min((int)(realSize.Height / 3.5), middlImg.Height);  
  87.                 int middleImgL = (img.Width - middleImgW) / 2;  
  88.                 int middleImgT = (img.Height - middleImgH) / 2;  
  89.   
  90.                 //将img转换成bmp格式,否则后面无法创建 Graphics对象  
  91.                 Bitmap bmpimg = new Bitmap(img.Width, img.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb);  
  92.                 using (Graphics g = Graphics.FromImage(bmpimg))  
  93.                 {  
  94.                     g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;  
  95.                     g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;  
  96.                     g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;  
  97.                     g.DrawImage(img, 0, 0);  
  98.                 }  
  99.   
  100.                 //在二维码中插入图片  
  101.                 System.Drawing.Graphics MyGraphic = System.Drawing.Graphics.FromImage(bmpimg);  
  102.                 //白底  
  103.                 MyGraphic.FillRectangle(Brushes.White, middleImgL, middleImgT, middleImgW, middleImgH);  
  104.                 MyGraphic.DrawImage(middlImg, middleImgL, middleImgT, middleImgW, middleImgH);  
  105.   
  106.                 pictureBox1.Image = bmpimg;  
  107.   
  108.                 //自动保存图片到当前目录  
  109.                 string filename = System.Environment.CurrentDirectory + "\\QR" + DateTime.Now.Ticks.ToString() + ".jpg";  
  110.                 bmpimg.Save(filename, System.Drawing.Imaging.ImageFormat.Jpeg);  
  111.                 label2.Text = "图片已保存到:" + filename;  
  112.   
  113.             }  
  114.             catch (Exception ee)  
  115.             {   
  116.                 MessageBox.Show(ee.Message);   
  117.             }  
  118.         }  
  119.   
  120.         string opFilePath = "";  
  121.         //选择要生成的中间图片  
  122.         private void pictureBox2_Click(object sender, EventArgs e)  
  123.         {  
  124.             openFileDialog1.Filter = "图片文件|*.bmp;*.jpg;*.png;*.ico";  
  125.             if (openFileDialog1.ShowDialog() == DialogResult.OK)  
  126.             {  
  127.                 opFilePath = openFileDialog1.FileName;  
  128.                 Image img = Image.FromFile(opFilePath);  
  129.                 QRMiddleImg.Image = img;  
  130.             }  
  131.         }  
  132.   
  133.         //选择要解码的图片  
  134.         private void button4_Click(object sender, EventArgs e)  
  135.         {  
  136.             openFileDialog1.Filter = "图片文件|*.bmp;*.jpg;*.png;*.ico";  
  137.             if (openFileDialog1.ShowDialog() == DialogResult.OK)  
  138.             {  
  139.                 opFilePath = openFileDialog1.FileName;  
  140.                 pictureBox2.ImageLocation = opFilePath;  
  141.             }  
  142.         }  
  143.   
  144.         //解码二维码  
  145.         private void button5_Click(object sender, EventArgs e)  
  146.         {  
  147.             MultiFormatReader mutiReader = new com.google.zxing.MultiFormatReader();  
  148.             Bitmap img = (Bitmap)Bitmap.FromFile(opFilePath);  
  149.             if (img == null)  
  150.                 return;  
  151.             LuminanceSource ls = new RGBLuminanceSource(img, img.Width, img.Height);  
  152.             BinaryBitmap bb = new BinaryBitmap(new com.google.zxing.common.HybridBinarizer(ls));  
  153.             //注意  必须是Utf-8编码  
  154.             Hashtable hints = new Hashtable();  
  155.             hints.Add(EncodeHintType.CHARACTER_SET, "UTF-8");  
  156.             Result r = mutiReader.decode(bb, hints);  
  157.             richTextBox1.Text = r.Text;  
  158.         }  
  159.     }  
  160. }  

C#如何生成二维码详解(含解码)