本篇介紹通過C#生成和讀取一維碼、二維碼的操作。
1. 介紹:介紹條形碼、條形碼的分類以及ZXing.Net類庫(kù)。
2. 一維碼操作:包含對(duì)一維碼的生成、讀取操作。
3. 二維碼操作:包含對(duì)二維碼的生成、讀取操作,以及生成帶Logo的二維碼。
4. 源碼下載:展示運(yùn)行圖及源碼下載。
條形碼(barcode):是將寬度不等的多個(gè)黑條和空白,按照一定的編碼規(guī)則排列,用以表達(dá)一組信息的圖形標(biāo)識(shí)符。
可分為一維條形碼和二維條形碼:
一維條形碼:只是在一個(gè)方向(一般是水平方向)表達(dá)信息,而在垂直方向則不表達(dá)任何信息。
二維條形碼:在水平和垂直方向的二維空間存儲(chǔ)信息的條形碼。
ZXing 是一個(gè)可生成和讀取 1D/2D(1維/2維) 條形碼的開源類庫(kù)。原先是java版本,后由第三方衍生了支持QT、C++、.Net等版本。
.Net版本支持的平臺(tái):.Net 2.0, 3.5 and 4.0、Silverlight 4 and 5、Windows Phone 7.0, 7.1 and 8.0、Windows CE、Unity3D、Xamarin.Android 等等。
Java 版本:https://github.com/zxing/zxing
ZXing.Net 版本:http://zxingnet.codeplex.com/
一維條形碼:只是在一個(gè)方向(一般是水平方向)表達(dá)信息,而在垂直方向則不表達(dá)任何信息。
常用碼制:EAN碼、39碼、交叉25碼、UPC碼、128碼、93碼,ISBN碼及Codabar(庫(kù)德巴碼)等。
國(guó)內(nèi)推行使用的是EAN商品條形碼,可分為EAN-13(標(biāo)準(zhǔn)版)和EAN-8(縮短版)兩種。
例圖:
以生成EAN-13碼制為例:
// 1.設(shè)置條形碼規(guī)格EncodingOptions encodeOption = new EncodingOptions();encodeOption.Height = 130; // 必須制定高度、寬度encodeOption.Width = 240; // 2.生成條形碼圖片并保存ZXing.BarcodeWriter wr = new BarcodeWriter();wr.Options = encodeOption;wr.Format = BarcodeFormat.EAN_13; // 條形碼規(guī)格:EAN13規(guī)格:12(無(wú)校驗(yàn)位)或13位數(shù)字Bitmap img = wr.Write(this.ContentTxt.Text); // 生成圖片string filePath = System.AppDomain.CurrentDomain.BaseDirectory + "http://EAN_13-" + this.ContentTxt.Text + ".jpg";img.Save(filePath, System.Drawing.Imaging.ImageFormat.Jpeg);
以讀取EAN-13碼制的圖片為例:
// 1.設(shè)置讀取條形碼規(guī)格DecodingOptions decodeOption = new DecodingOptions();decodeOption.PossibleFormats = new List<BarcodeFormat>() { BarcodeFormat.EAN_13,};// 2.進(jìn)行讀取操作ZXing.BarcodeReader br = new BarcodeReader();br.Options = decodeOption;ZXing.Result rs = br.Decode(this.barCodeImg.Image as Bitmap);if (rs == null){ this.ContentTxt.Text = "讀取失敗"; MessageBox.Show("讀取失敗");}else{ this.ContentTxt.Text = rs.Text; MessageBox.Show("讀取成功,內(nèi)容:" + rs.Text);}
二維碼:在水平和垂直方向的二維空間存儲(chǔ)信息的條形碼。
常用碼制:PDF417、QR Code、Code 49、Code 16K、Code One等。
例圖:
以生成QR碼制為例:
// 1.設(shè)置QR二維碼的規(guī)格ZXing.QrCode.QrCodeEncodingOptions qrEncodeOption = new ZXing.QrCode.QrCodeEncodingOptions();qrEncodeOption.CharacterSet = "UTF-8"; // 設(shè)置編碼格式,否則讀取'中文'亂碼qrEncodeOption.Height = 200;qrEncodeOption.Width = 200;qrEncodeOption.Margin = 1; // 設(shè)置周圍空白邊距// 2.生成條形碼圖片并保存ZXing.BarcodeWriter wr = new BarcodeWriter();wr.Format = BarcodeFormat.QR_CODE; // 二維碼wr.Options = qrEncodeOption;Bitmap img = wr.Write(this.ContentTxt.Text);string filePath = System.AppDomain.CurrentDomain.BaseDirectory + "http://QR-" + this.ContentTxt.Text + ".jpg";img.Save(filePath, System.Drawing.Imaging.ImageFormat.Jpeg);
以讀取QR碼制的圖片為例:
// 1.設(shè)置讀取條形碼規(guī)格DecodingOptions decodeOption = new DecodingOptions();decodeOption.PossibleFormats = new List<BarcodeFormat>() { BarcodeFormat.QR_CODE,;// 2.進(jìn)行讀取操作ZXing.BarcodeReader br = new BarcodeReader();br.Options = decodeOption;ZXing.Result rs = br.Decode(this.barCodeImg.Image as Bitmap);if (rs == null){ this.ContentTxt.Text = "讀取失敗"; MessageBox.Show("讀取失敗");}else{ this.ContentTxt.Text = rs.Text; MessageBox.Show("讀取成功,內(nèi)容:" + rs.Text);}
二維碼帶有校驗(yàn)功能,故可以在中間區(qū)域展示一定尺寸的圖片。
例圖:
代碼:
// 1.設(shè)置QR二維碼的規(guī)格ZXing.QrCode.QrCodeEncodingOptions qrEncodeOption = new ZXing.QrCode.QrCodeEncodingOptions();qrEncodeOption.CharacterSet = "UTF-8"; // 設(shè)置編碼格式,否則讀取'中文'亂碼qrEncodeOption.Height = 200;qrEncodeOption.Width = 200;qrEncodeOption.Margin = 1; // 設(shè)置周圍空白邊距// 2.生成條形碼圖片ZXing.BarcodeWriter wr = new BarcodeWriter();wr.Format = BarcodeFormat.QR_CODE; // 二維碼wr.Options = qrEncodeOption;Bitmap img = wr.Write(this.ContentTxt.Text);// 3.在二維碼的Bitmap對(duì)象上繪制logo圖片Bitmap logoImg = Bitmap.FromFile(System.AppDomain.CurrentDomain.BaseDirectory + "http://logo.jpg") as Bitmap;Graphics g = Graphics.FromImage(img);Rectangle logoRec = new Rectangle(); // 設(shè)置logo圖片的大小和繪制位置logoRec.Width = img.Width / 6;logoRec.Height = img.Height / 6;logoRec.X = img.Width / 2 - logoRec.Width / 2; // 中心點(diǎn)logoRec.Y = img.Height / 2 - logoRec.Height / 2;g.DrawImage(logoImg, logoRec);// 4.保存繪制后的圖片string filePath = System.AppDomain.CurrentDomain.BaseDirectory + "http://QR-" + this.ContentTxt.Text + ".jpg";img.Save(filePath, System.Drawing.Imaging.ImageFormat.Jpeg);
百度網(wǎng)盤:http://pan.baidu.com/s/1qWRJMAo
CSDN:http://download.csdn.net/detail/polk6/9383226
==================================系列文章==========================================
本篇文章:3.3 C# 條形碼操作【源碼下載】
C#文章導(dǎo)航
新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注