最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

C#實(shí)現(xiàn)條形碼識(shí)別的解決方案分享

 更新時(shí)間:2023年07月24日 10:10:18   作者:二次元攻城獅  
主流的識(shí)別庫(kù)主要有ZXing.NET和ZBar,OpenCV 4.0后加入了QR碼檢測(cè)和解碼功能,所以本文主要和大家分享了使用ZBar進(jì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)版,按自己的需求選擇版本:

識(shí)別庫(kù)使用方法參考:C#使用zxing,zbar,thoughtworkQRcode解析二維碼。

擴(kuò)展:開(kāi)源掃碼軟件

推薦一個(gè)C# WPF 原生開(kāi)發(fā)的在電腦上識(shí)別條碼的工具軟件QrCodeScanner,功能如下:

支持四種模式:截圖識(shí)別 + 攝像頭識(shí)別 + 本地圖片識(shí)別 + 作為掃描槍使用

支持ZbarZxing兩種主流引擎

支持多碼同掃

支持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)文章

最新評(píng)論

拜泉县| 肥城市| 凤冈县| 察哈| 青神县| 吴忠市| 张家界市| 定日县| 阳春市| 锡林浩特市| 乌兰浩特市| 东乡| 昔阳县| 大竹县| 库尔勒市| 渭源县| 山阳县| 开原市| 青浦区| 军事| 临夏县| 黑山县| 泾源县| 郁南县| 临朐县| 桦南县| 方山县| 昌吉市| 台安县| 老河口市| 阿克陶县| 本溪市| 马龙县| 馆陶县| 赤水市| 临安市| 黔西县| 大兴区| 茂名市| 彭山县| 大竹县|