C#利用zxing.net操作二维码和条形码

下载地址:http://zxingnet.codeplex.com/

zxing.net是.net平台下编解条形码和二维码的工具,使用非常方便。

首先下载二进制dll文件,引入工程;

代码:

C#代码  复制代码  收藏代码
  1. using System; 
  2. using System.Collections.Generic; 
  3. using System.ComponentModel; 
  4. using System.Data; 
  5. using System.Drawing; 
  6. using System.Text; 
  7. using System.Windows.Forms; 
  8. using ZXing.QrCode; 
  9. using ZXing; 
  10. using ZXing.Common; 
  11. using ZXing.Rendering; 
  12.  
  13. namespace zxingTest 
  14.     public partial class Form1 : Form 
  15.     { 
  16.         EncodingOptions options = null
  17.         BarcodeWriter writer = null
  18.  
  19.         public Form1() 
  20.         { 
  21.             InitializeComponent(); 
  22.             options = new QrCodeEncodingOptions 
  23.             { 
  24.                 DisableECI = true
  25.                 CharacterSet = "UTF-8"
  26.                 Width = pictureBoxQr.Width, 
  27. Height = pictureBoxQr.Height
  28.             }; 
  29.             writer = new BarcodeWriter(); 
  30.             writer.Format = BarcodeFormat.QR_CODE; 
  31.             writer.Options = options; 
  32.         } 
  33.  
  34.         private void buttonQr_Click(object sender, EventArgs e) 
  35.         { 
  36.             if (textBoxText.Text == string.Empty) 
  37.             { 
  38.                 MessageBox.Show("输入内容不能为空!"); 
  39.                 return
  40.             } 
  41.             Bitmap bitmap = writer.Write(textBoxText.Text); 
  42.             pictureBoxQr.Image = bitmap; 
  43.         } 
  44.     } 
[c#]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.ComponentModel;  
  4. using System.Data;  
  5. using System.Drawing;  
  6. using System.Text;  
  7. using System.Windows.Forms;  
  8. using ZXing.QrCode;  
  9. using ZXing;  
  10. using ZXing.Common;  
  11. using ZXing.Rendering;  
  12.   
  13. namespace zxingTest  
  14. {  
  15.     public partial class Form1 : Form  
  16.     {  
  17.         EncodingOptions options = null;  
  18.         BarcodeWriter writer = null;  
  19.   
  20.         public Form1()  
  21.         {  
  22.             InitializeComponent();  
  23.             options = new QrCodeEncodingOptions  
  24.             {  
  25.                 DisableECI = true,  
  26.                 CharacterSet = "UTF-8",  
  27.                 Width = pictureBoxQr.Width,  
  28.                 Height = pictureBoxQr.Height  
  29.             };  
  30.             writer = new BarcodeWriter();  
  31.             writer.Format = BarcodeFormat.QR_CODE;  
  32.             writer.Options = options;  
  33.         }  
  34.   
  35.         private void buttonQr_Click(object sender, EventArgs e)  
  36.         {  
  37.             if (textBoxText.Text == string.Empty)  
  38.             {  
  39.                 MessageBox.Show("输入内容不能为空!");  
  40.                 return;  
  41.             }  
  42.             Bitmap bitmap = writer.Write(textBoxText.Text);  
  43.             pictureBoxQr.Image = bitmap;  
  44.         }  
  45.     }  
  46. }  

效果:



将字符编码时可以指定字符格式;默认为ISO-8859-1英文字符集,但一般移动设备常用UTF-8字符集编码,

可以通过QrCodeEncodingOptions设置编码方式。

如果要生成其他zxing支持的条形码,只要修改BarcodeWriter.Format就可以了。

C#代码  复制代码  收藏代码
  1. using System; 
  2. using System.Collections.Generic; 
  3. using System.ComponentModel; 
  4. using System.Data; 
  5. using System.Drawing; 
  6. using System.Text; 
  7. using System.Windows.Forms; 
  8. using ZXing.QrCode; 
  9. using ZXing; 
  10. using ZXing.Common; 
  11. using ZXing.Rendering; 
  12.  
  13. namespace zxingTest 
  14.     public partial class Form1 : Form 
  15.     { 
  16.         EncodingOptions options = null
  17.         BarcodeWriter writer = null
  18.  
  19.         public Form1() 
  20.         { 
  21.             InitializeComponent(); 
  22.             options = new EncodingOptions 
  23.             { 
  24.                 //DisableECI = true, 
  25.                 //CharacterSet = "UTF-8", 
  26.                 Width = pictureBoxQr.Width, 
  27.                 Height = pictureBoxQr.Height 
  28.             }; 
  29.             writer = new BarcodeWriter(); 
  30.             writer.Format = BarcodeFormat.ITF; 
  31.             writer.Options = options; 
  32.         } 
  33.  
  34.         private void buttonQr_Click(object sender, EventArgs e) 
  35.         { 
  36.             if (textBoxText.Text == string.Empty) 
  37.             { 
  38.                 MessageBox.Show("输入内容不能为空!"); 
  39.                 return
  40.             } 
  41.             Bitmap bitmap = writer.Write(textBoxText.Text); 
  42.             pictureBoxQr.Image = bitmap; 
  43.         } 
  44.     } 
[c#]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.ComponentModel;  
  4. using System.Data;  
  5. using System.Drawing;  
  6. using System.Text;  
  7. using System.Windows.Forms;  
  8. using ZXing.QrCode;  
  9. using ZXing;  
  10. using ZXing.Common;  
  11. using ZXing.Rendering;  
  12.   
  13. namespace zxingTest  
  14. {  
  15.     public partial class Form1 : Form  
  16.     {  
  17.         EncodingOptions options = null;  
  18.         BarcodeWriter writer = null;  
  19.   
  20.         public Form1()  
  21.         {  
  22.             InitializeComponent();  
  23.             options = new EncodingOptions  
  24.             {  
  25.                 //DisableECI = true,  
  26.                 //CharacterSet = "UTF-8",  
  27.                 Width = pictureBoxQr.Width,  
  28.                 Height = pictureBoxQr.Height  
  29.             };  
  30.             writer = new BarcodeWriter();  
  31.             writer.Format = BarcodeFormat.ITF;  
  32.             writer.Options = options;  
  33.         }  
  34.   
  35.         private void buttonQr_Click(object sender, EventArgs e)  
  36.         {  
  37.             if (textBoxText.Text == string.Empty)  
  38.             {  
  39.                 MessageBox.Show("输入内容不能为空!");  
  40.                 return;  
  41.             }  
  42.             Bitmap bitmap = writer.Write(textBoxText.Text);  
  43.             pictureBoxQr.Image = bitmap;  
  44.         }  
  45.     }  
  46. }  

效果:


输入字符串需要符合编码的格式,不然会报错。
解码方式:
C#代码  复制代码  收藏代码
  1. using System; 
  2. using System.Collections.Generic; 
  3. using System.ComponentModel; 
  4. using System.Data; 
  5. using System.Drawing; 
  6. using System.Text; 
  7. using System.Windows.Forms; 
  8. using ZXing.QrCode; 
  9. using ZXing; 
  10. using ZXing.Common; 
  11. using ZXing.Rendering; 
  12.    
  13. namespace zxingTest 
  14.     public partial class Form1 : Form 
  15.     { 
  16.         BarcodeReader reader = null
  17.    
  18.         public Form1() 
  19.         { 
  20.             InitializeComponent(); 
  21.             reader = new BarcodeReader(); 
  22.         } 
  23.    
  24.         private void Form1_DragEnter(object sender, DragEventArgs e)//当拖放进入窗体 
  25.         { 
  26.             if (e.Data.GetDataPresent(DataFormats.FileDrop)) 
  27.                 e.Effect = DragDropEffects.Copy;    //显示拷贝效应 
  28.             else 
  29.                 e.Effect = DragDropEffects.None;   
  30.         } 
  31.    
  32.         private void Form1_DragDrop(object sender, DragEventArgs e) //当拖放放在窗体上 
  33.         { 
  34.             string[] fileNames = (string[])e.Data.GetData(DataFormats.FileDrop, false); //获取文件名 
  35.             if (fileNames.Length > 0) 
  36.             { 
  37.                 pictureBoxPic.Load(fileNames[0]);   //显示图片 
  38.                 Result result = reader.Decode((Bitmap)pictureBoxPic.Image); //通过reader解码 
  39.                 textBoxText.Text = result.Text; //显示解析结果 
  40.             } 
  41.         } 
  42.     } 
[c#]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.ComponentModel;  
  4. using System.Data;  
  5. using System.Drawing;  
  6. using System.Text;  
  7. using System.Windows.Forms;  
  8. using ZXing.QrCode;  
  9. using ZXing;  
  10. using ZXing.Common;  
  11. using ZXing.Rendering;  
  12.     
  13. namespace zxingTest  
  14. {  
  15.     public partial class Form1 : Form  
  16.     {  
  17.         BarcodeReader reader = null;  
  18.     
  19.         public Form1()  
  20.         {  
  21.             InitializeComponent();  
  22.             reader = new BarcodeReader();  
  23.         }  
  24.     
  25.         private void Form1_DragEnter(object sender, DragEventArgs e)//当拖放进入窗体  
  26.         {  
  27.             if (e.Data.GetDataPresent(DataFormats.FileDrop))  
  28.                 e.Effect = DragDropEffects.Copy;    //显示拷贝效应  
  29.             else  
  30.                 e.Effect = DragDropEffects.None;    
  31.         }  
  32.     
  33.         private void Form1_DragDrop(object sender, DragEventArgs e) //当拖放放在窗体上  
  34.         {  
  35.             string[] fileNames = (string[])e.Data.GetData(DataFormats.FileDrop, false); //获取文件名  
  36.             if (fileNames.Length > 0)  
  37.             {  
  38.                 pictureBoxPic.Load(fileNames[0]);   //显示图片  
  39.                 Result result = reader.Decode((Bitmap)pictureBoxPic.Image); //通过reader解码  
  40.                 textBoxText.Text = result.Text; //显示解析结果  
  41.             }  
  42.         }  
  43.     }  
  44. }  


  • 大小: 30.9 KB
  • 大小: 29.7 KB
  • 大小: 25.7 KB
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值