基于C#生成條形碼操作知識(shí)匯總附源碼下載
1. 介紹
1.1 條形碼
條形碼(barcode):是將寬度不等的多個(gè)黑條和空白,按照一定的編碼規(guī)則排列,用以表達(dá)一組信息的圖形標(biāo)識(shí)符。
1.2 條形碼分類(lèi)
可分為一維條形碼和二維條形碼:
一維條形碼:只是在一個(gè)方向(一般是水平方向)表達(dá)信息,而在垂直方向則不表達(dá)任何信息。
二維條形碼:在水平和垂直方向的二維空間存儲(chǔ)信息的條形碼。
1.3 第三方類(lèi)庫(kù):ZXing.Net
1.3.1 說(shuō)明
ZXing 是一個(gè)可生成和讀取 1D/2D(1維/2維) 條形碼的開(kāi)源類(lèi)庫(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 等等。
1.3.2 下載地址
Java 版本:https://github.com/zxing/zxing
ZXing.Net 版本:http://zxingnet.codeplex.com/
2. 一維碼操作
2.1 介紹
一維條形碼:只是在一個(gè)方向(一般是水平方向)表達(dá)信息,而在垂直方向則不表達(dá)任何信息。
常用碼制:EAN碼、39碼、交叉25碼、UPC碼、128碼、93碼,ISBN碼及Codabar(庫(kù)德巴碼)等。
國(guó)內(nèi)推行使用的是EAN商品條形碼,可分為EAN-13(標(biāo)準(zhǔn)版)和EAN-8(縮短版)兩種。
例圖:
2.2 生成一維碼
以生成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 + "\\EAN_13-" + this.ContentTxt.Text + ".jpg"; img.Save(filePath, System.Drawing.Imaging.ImageFormat.Jpeg);
2.3 讀取一維碼
以讀取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); }
3. 二維碼操作
3.1 介紹
二維碼:在水平和垂直方向的二維空間存儲(chǔ)信息的條形碼。
常用碼制:PDF417、QR Code、Code 49、Code 16K、Code One等。
例圖:
3.2 生成二維碼
以生成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è)置周?chē)瞻走吘? // 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 + "\\QR-" + this.ContentTxt.Text + ".jpg"; img.Save(filePath, System.Drawing.Imaging.ImageFormat.Jpeg);
3.3 讀取二維碼
以讀取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); }
3.4 生成帶Logo的二維碼
二維碼帶有校驗(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è)置周?chē)瞻走吘? // 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 + "\\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 + "\\QR-" + this.ContentTxt.Text + ".jpg"; img.Save(filePath, System.Drawing.Imaging.ImageFormat.Jpeg);
4. 源碼下載
4.1 運(yùn)行圖
4.2 下載地址
百度網(wǎng)盤(pán):http://pan.baidu.com/s/1qWRJMAo
CSDN:http://download.csdn.net/detail/polk6/9383226
上一篇:詳解C#編程中一維數(shù)組與多維數(shù)組的使用
欄 目:C#教程
下一篇:AnyChat的視頻會(huì)議程序?qū)嵗斀?/a>
本文標(biāo)題:基于C#生成條形碼操作知識(shí)匯總附源碼下載
本文地址:http://mengdiqiu.com.cn/a1/C_jiaocheng/6757.html
您可能感興趣的文章
- 01-10C#獲取動(dòng)態(tài)生成的CheckBox值
- 01-10基于C#實(shí)現(xiàn)簡(jiǎn)單離線注冊(cè)碼生成與驗(yàn)證
- 01-10C#基于UDP實(shí)現(xiàn)的P2P語(yǔ)音聊天工具
- 01-10C#實(shí)現(xiàn)基于加減按鈕形式控制系統(tǒng)音量及靜音的方法
- 01-10C#基于WebBrowser獲取cookie的實(shí)現(xiàn)方法
- 01-10C#基于委托實(shí)現(xiàn)多線程之間操作的方法
- 01-10基于C#對(duì)用戶密碼使用MD5加密與解密
- 01-10基于C#實(shí)現(xiàn)簡(jiǎn)單的隨機(jī)抽獎(jiǎng)小程序
- 01-10C#基于cookie實(shí)現(xiàn)的購(gòu)物車(chē)功能
- 01-10基于C#實(shí)現(xiàn)12306的動(dòng)態(tài)驗(yàn)證碼變成靜態(tài)驗(yàn)證碼的方法


閱讀排行
- 1C語(yǔ)言 while語(yǔ)句的用法詳解
- 2java 實(shí)現(xiàn)簡(jiǎn)單圣誕樹(shù)的示例代碼(圣誕
- 3利用C語(yǔ)言實(shí)現(xiàn)“百馬百擔(dān)”問(wèn)題方法
- 4C語(yǔ)言中計(jì)算正弦的相關(guān)函數(shù)總結(jié)
- 5c語(yǔ)言計(jì)算三角形面積代碼
- 6什么是 WSH(腳本宿主)的詳細(xì)解釋
- 7C++ 中隨機(jī)函數(shù)random函數(shù)的使用方法
- 8正則表達(dá)式匹配各種特殊字符
- 9C語(yǔ)言十進(jìn)制轉(zhuǎn)二進(jìn)制代碼實(shí)例
- 10C語(yǔ)言查找數(shù)組里數(shù)字重復(fù)次數(shù)的方法
本欄相關(guān)
- 01-10C#通過(guò)反射獲取當(dāng)前工程中所有窗體并
- 01-10關(guān)于ASP網(wǎng)頁(yè)無(wú)法打開(kāi)的解決方案
- 01-10WinForm限制窗體不能移到屏幕外的方法
- 01-10WinForm繪制圓角的方法
- 01-10C#實(shí)現(xiàn)txt定位指定行完整實(shí)例
- 01-10WinForm實(shí)現(xiàn)仿視頻 器左下角滾動(dòng)新
- 01-10C#停止線程的方法
- 01-10C#實(shí)現(xiàn)清空回收站的方法
- 01-10C#通過(guò)重寫(xiě)Panel改變邊框顏色與寬度的
- 01-10C#實(shí)現(xiàn)讀取注冊(cè)表監(jiān)控當(dāng)前操作系統(tǒng)已
隨機(jī)閱讀
- 04-02jquery與jsp,用jquery
- 08-05dedecms(織夢(mèng))副欄目數(shù)量限制代碼修改
- 01-10delphi制作wav文件的方法
- 08-05DEDE織夢(mèng)data目錄下的sessions文件夾有什
- 01-10使用C語(yǔ)言求解撲克牌的順子及n個(gè)骰子
- 01-11Mac OSX 打開(kāi)原生自帶讀寫(xiě)NTFS功能(圖文
- 01-10C#中split用法實(shí)例總結(jié)
- 08-05織夢(mèng)dedecms什么時(shí)候用欄目交叉功能?
- 01-11ajax實(shí)現(xiàn)頁(yè)面的局部加載
- 01-10SublimeText編譯C開(kāi)發(fā)環(huán)境設(shè)置