日韩黑丝制服一区视频播放|日韩欧美人妻丝袜视频在线观看|九九影院一级蜜桃|亚洲中文在线导航|青草草视频在线观看|婷婷五月色伊人网站|日本一区二区在线|国产AV一二三四区毛片|正在播放久草视频|亚洲色图精品一区

分享

C#利用zxing.net操作二維碼和條形碼

 cathy001 2016-01-08

下載地址:http://zxingnet./

zxing.net是.net平臺(tái)下編解條形碼和二維碼的工具,使用非常方便。

首先下載二進(jìn)制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. {  
  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("輸入內(nèi)容不能為空!");  
  40.                 return;  
  41.             }  
  42.             Bitmap bitmap = writer.Write(textBoxText.Text);  
  43.             pictureBoxQr.Image = bitmap;  
  44.         }  
  45.     }  
  46. }  

 效果:



 將字符編碼時(shí)可以指定字符格式;默認(rèn)為ISO-8859-1英文字符集,但一般移動(dòng)設(shè)備常用UTF-8字符集編碼,

可以通過QrCodeEncodingOptions設(shè)置編碼方式。

如果要生成其他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. {  
  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("輸入內(nèi)容不能為空!");  
  40.                 return;  
  41.             }  
  42.             Bitmap bitmap = writer.Write(textBoxText.Text);  
  43.             pictureBoxQr.Image = bitmap;  
  44.         }  
  45.     }  
  46. }  

 

效果:


 輸入字符串需要符合編碼的格式,不然會(huì)報(bào)錯(cuò)。
解碼方式:
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. {  
  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)//當(dāng)拖放進(jìn)入窗體  
  26.         {  
  27.             if (e.Data.GetDataPresent(DataFormats.FileDrop))  
  28.                 e.Effect = DragDropEffects.Copy;    //顯示拷貝效應(yīng)  
  29.             else  
  30.                 e.Effect = DragDropEffects.None;    
  31.         }  
  32.     
  33.         private void Form1_DragDrop(object sender, DragEventArgs e) //當(dāng)拖放放在窗體上  
  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; //顯示解析結(jié)果  
  41.             }  
  42.         }  
  43.     }  
  44. }  
 

 

    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請遵守用戶 評(píng)論公約

    類似文章 更多