C#實(shí)現(xiàn)條形碼識(shí)別的解決方案分享
簡(jiǎn)介
主流的識(shí)別庫(kù)主要有ZXing.NET和ZBar,OpenCV 4.0后加入了QR碼檢測(cè)和解碼功能。本文使用的是ZBar,同等條件下ZBar識(shí)別率更高,圖片和部分代碼參考在C#中使用ZBar識(shí)別條形碼。
使用ZBar
通過(guò)NuGet安裝ZBar,必須使用1.0.0版本,最新的1.0.2版本無(wú)法自動(dòng)生成相關(guān)的dll并且使用不了1.0.0版的dll庫(kù)默認(rèn)支持netcoreapp3.1,在.NET6環(huán)境下也能正常使用,正常情況下輸出目錄會(huì)自動(dòng)生成lib文件夾和dll文件。
注:ZBar 1.0.0在x86平臺(tái)下可正常運(yùn)行,但Debug會(huì)報(bào)錯(cuò),建議使用x64或AnyCPU。
條碼識(shí)別:
/// <summary>
/// 條碼識(shí)別
/// </summary>
static List<ZBar.Symbol> ScanBarCode(string filename)
{
var bitmap = (Bitmap)Image.FromFile(filename);
bitmap = MakeGrayscale3(bitmap);
List<ZBar.Symbol> result = new List<ZBar.Symbol>();
using (var scanner = new ZBar.ImageScanner())
{
var symbols = scanner.Scan(bitmap);
if (symbols != null && symbols.Count > 0)
{
result.AddRange(symbols);
}
}
return result;
}
/// <summary>
/// 處理圖片灰度
/// </summary>
static Bitmap MakeGrayscale3(Bitmap original)
{
//create a blank bitmap the same size as original
Bitmap newBitmap = new Bitmap(original.Width, original.Height);
//get a graphics object from the new image
Graphics g = Graphics.FromImage(newBitmap);
//create the grayscale ColorMatrix
System.Drawing.Imaging.ColorMatrix colorMatrix = new System.Drawing.Imaging.ColorMatrix(
new float[][]
{
new float[] {.3f, .3f, .3f, 0, 0},
new float[] {.59f, .59f, .59f, 0, 0},
new float[] {.11f, .11f, .11f, 0, 0},
new float[] {0, 0, 0, 1, 0},
new float[] {0, 0, 0, 0, 1}
});
//create some image attributes
ImageAttributes attributes = new ImageAttributes();
//set the color matrix attribute
attributes.SetColorMatrix(colorMatrix);
//draw the original image on the new image
//using the grayscale color matrix
g.DrawImage(original, new Rectangle(0, 0, original.Width, original.Height),
0, 0, original.Width, original.Height, GraphicsUnit.Pixel, attributes);
//dispose the Graphics object
g.Dispose();
return newBitmap;
}使用方法:
Console.WriteLine(ZBar.ZBar.Version);
var symbols = ScanBarCode("426301-20160127111209879-611759974.jpg");
string result = string.Empty;
symbols.ForEach(s => Console.WriteLine($"條碼類型:{s.Type} 條碼內(nèi)容:{s.Data} 條碼質(zhì)量:{s.Quality}"));
Console.ReadKey();擴(kuò)展:其它條碼識(shí)別庫(kù)
在C#平臺(tái)下還有一個(gè)ThoughtWorks.QRCode庫(kù)也支持條碼解析,具體效果還沒(méi)有測(cè)試。原始代碼最后的版本是在2015年,后面的版本只是將庫(kù)做了個(gè)標(biāo)準(zhǔn)版,按自己的需求選擇版本:
- ThoughtWorks.QRCode:原始版本,最后更新時(shí)間2015年。
- ThoughtWorks.QRCode.Standard(推薦使用):netstandard2.0版本,修復(fù)bug,增加了自動(dòng)QRCodeVersion功能。
- ThoughtWorks.QRCode.Core:netstandard2.0版本,功能未修復(fù)變更。
識(shí)別庫(kù)使用方法參考:C#使用zxing,zbar,thoughtworkQRcode解析二維碼。
擴(kuò)展:開(kāi)源掃碼軟件
推薦一個(gè)C# WPF 原生開(kāi)發(fā)的在電腦上識(shí)別條碼的工具軟件QrCodeScanner,功能如下:
支持四種模式:截圖識(shí)別 + 攝像頭識(shí)別 + 本地圖片識(shí)別 + 作為掃描槍使用
支持Zbar和Zxing兩種主流引擎
支持多碼同掃
支持Material Design繽紛主題色與暗黑模式
獨(dú)創(chuàng)的掃描槍模

到此這篇關(guān)于C#實(shí)現(xiàn)條形碼識(shí)別的解決方案分享的文章就介紹到這了,更多相關(guān)C#條形碼識(shí)別內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Visual Studio關(guān)于C#項(xiàng)目Dll的引用多種方式(圖文詳解)
本文通過(guò)圖文并茂的形式給大家展示Visual Studio關(guān)于C#項(xiàng)目Dll的引用幾種方式 ,感興趣的朋友跟隨小編一起看看吧2024-08-08
C#實(shí)現(xiàn)網(wǎng)頁(yè)截圖功能
這篇文章主要介紹了C#實(shí)現(xiàn)網(wǎng)頁(yè)截圖功能,是非常實(shí)用的一個(gè)功能,需要的朋友可以參考下2014-08-08
C#數(shù)據(jù)類型實(shí)現(xiàn)背包、隊(duì)列和棧
本文詳細(xì)講解了C#數(shù)據(jù)結(jié)構(gòu)類型,并實(shí)現(xiàn)背包、隊(duì)列和棧的方法,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-04-04
C#函數(shù)式編程中的遞歸調(diào)用之尾遞歸詳解
這篇文章主要介紹了C#函數(shù)式編程中的遞歸調(diào)用詳解,本文講解了什么是尾遞歸、尾遞歸的多種方式、尾遞歸的代碼實(shí)例等內(nèi)容,需要的朋友可以參考下2015-01-01
C#畫(huà)筆使用復(fù)合數(shù)組繪制單個(gè)矩形的方法
這篇文章主要介紹了C#畫(huà)筆使用復(fù)合數(shù)組繪制單個(gè)矩形的方法,涉及C#使用畫(huà)筆繪制圖形的相關(guān)技巧,需要的朋友可以參考下2015-06-06
C#強(qiáng)制轉(zhuǎn)換和嘗試轉(zhuǎn)換的方法
這篇文章主要為大家詳細(xì)介紹了C#強(qiáng)制轉(zhuǎn)換和嘗試轉(zhuǎn)換的方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-09-09

