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

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

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

首先下載二進制dll文件,引入工程;

代碼:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using ZXing.QrCode;
using ZXing;
using ZXing.Common;
using ZXing.Rendering;

namespace zxingTest
{
    public partial class Form1 : Form
    {
        EncodingOptions options = null;
        BarcodeWriter writer = null;

        public Form1()
        {
            InitializeComponent();
            options = new QrCodeEncodingOptions
            {
                DisableECI = true,
                CharacterSet = "UTF-8",
                Width = pictureBoxQr.Width,
                Height = pictureBoxQr.Height
            };
            writer = new BarcodeWriter();
            writer.Format = BarcodeFormat.QR_CODE;
            writer.Options = options;
        }

        private void buttonQr_Click(object sender, EventArgs e)
        {
            if (textBoxText.Text == string.Empty)
            {
                MessageBox.Show("輸入內容不能爲空!");
                return;
            }
            Bitmap bitmap = writer.Write(textBoxText.Text);
            pictureBoxQr.Image = bitmap;
        }
    }
}

效果:



將字符編碼時可以指定字符格式;默認爲ISO-8859-1英文字符集,但一般移動設備常用UTF-8字符集編碼,

可以通過QrCodeEncodingOptions設置編碼方式。

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

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using ZXing.QrCode;
using ZXing;
using ZXing.Common;
using ZXing.Rendering;

namespace zxingTest
{
    public partial class Form1 : Form
    {
        EncodingOptions options = null;
        BarcodeWriter writer = null;

        public Form1()
        {
            InitializeComponent();
            options = new EncodingOptions
            {
                //DisableECI = true,
                //CharacterSet = "UTF-8",
                Width = pictureBoxQr.Width,
                Height = pictureBoxQr.Height
            };
            writer = new BarcodeWriter();
            writer.Format = BarcodeFormat.ITF;
            writer.Options = options;
        }

        private void buttonQr_Click(object sender, EventArgs e)
        {
            if (textBoxText.Text == string.Empty)
            {
                MessageBox.Show("輸入內容不能爲空!");
                return;
            }
            Bitmap bitmap = writer.Write(textBoxText.Text);
            pictureBoxQr.Image = bitmap;
        }
    }
}
效果:


 輸入字符串需要符合編碼的格式,不然會報錯。
解碼方式:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using ZXing.QrCode;
using ZXing;
using ZXing.Common;
using ZXing.Rendering;
  
namespace zxingTest
{
    public partial class Form1 : Form
    {
        BarcodeReader reader = null;
  
        public Form1()
        {
            InitializeComponent();
            reader = new BarcodeReader();
        }
  
        private void Form1_DragEnter(object sender, DragEventArgs e)//當拖放進入窗體
        {
            if (e.Data.GetDataPresent(DataFormats.FileDrop))
                e.Effect = DragDropEffects.Copy;    //顯示拷貝效應
            else
                e.Effect = DragDropEffects.None;  
        }
  
        private void Form1_DragDrop(object sender, DragEventArgs e) //當拖放放在窗體上
        {
            string[] fileNames = (string[])e.Data.GetData(DataFormats.FileDrop, false); //獲取文件名
            if (fileNames.Length > 0)
            {
                pictureBoxPic.Load(fileNames[0]);   //顯示圖片
                Result result = reader.Decode((Bitmap)pictureBoxPic.Image); //通過reader解碼
                textBoxText.Text = result.Text; //顯示解析結果
            }
        }
    }
}





發佈了42 篇原創文章 · 獲贊 108 · 訪問量 17萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章