Aspose.BarCode for .NET?是一個(gè)功能強(qiáng)大的API,可以從任意角度生成和識(shí)別多種圖像類(lèi)型的一維和二維條形碼。開(kāi)發(fā)人員可以輕松添加條形碼生成和識(shí)別功能,以及在.NET應(yīng)用程序中將生成的條形碼導(dǎo)出為高質(zhì)量的圖像格式。
Aspose API支持流行文件格式處理,并允許將各類(lèi)文檔導(dǎo)出或轉(zhuǎn)換為固定布局文件格式和最常用的圖像/多媒體格式。
Aspose.BarCode 最新下載(qun:767755948)https://www.evget.com/product/576/download
您是否正在尋找一種以編程方式從圖像中讀取條形碼或 QR 碼的方法?如果您是開(kāi)發(fā)人員,并且需要?jiǎng)?chuàng)建自己的條碼閱讀器應(yīng)用程序?你來(lái)對(duì)地方了。條形碼對(duì)于準(zhǔn)確跟蹤庫(kù)存和產(chǎn)品從制造到銷(xiāo)售點(diǎn)的交付至關(guān)重要。我們可以在.NET應(yīng)用程序中輕松檢測(cè)、識(shí)別和讀取不同類(lèi)型的條形碼和二維碼。在本文中,我們將通過(guò)幾個(gè)簡(jiǎn)單的步驟向您展示如何使用 C# 從圖像中讀取條形碼。您可以將本文用作開(kāi)發(fā)條形碼閱讀器或掃描器應(yīng)用程序的分步指南。
首先,我們將了解 C# 條碼閱讀器 API,以讀取輸入圖像中可用的條碼。接下來(lái),我們將介紹如何從圖像中檢測(cè)、識(shí)別和提取條形碼數(shù)據(jù)的步驟。您將找到詳細(xì)的步驟和代碼片段。最后,我們將提供有用的鏈接以進(jìn)一步增強(qiáng)功能。讓我們開(kāi)始吧!
用于從圖像讀取條形碼的 C# API
為了從圖像中讀取條形碼,我們將使用Aspose.BarCode for .NET API。API 允許生成、掃描和讀取范圍廣泛的條碼符號(hào)。它支持以JPEG、TIFF、PNG、BMP和GIF格式呈現(xiàn)條碼圖像。
API 提供了BarCodeReader類(lèi),可以從給定的圖像中識(shí)別 60 多種不同的條形碼類(lèi)型。檢測(cè)條形碼的第一步是指定帶有條形碼的圖像的來(lái)源。這可以是文件、位圖對(duì)象或流。然后需要在DecodeType參數(shù)中指定目標(biāo)符號(hào)。我們可以通過(guò)指定DecodeType.AllSupportedTypes來(lái)查看所有不同類(lèi)型的支持符號(hào)。此類(lèi)的ReadBarCodes ()方法返回一個(gè)已識(shí)別條碼數(shù)組。API的BarCodeResult類(lèi)存儲(chǔ)識(shí)別出的條碼數(shù)據(jù),如條碼類(lèi)型、條碼文本、區(qū)域等參數(shù)。
API 還允許指定條形碼閱讀器應(yīng)讀取的圖像區(qū)域。這可以使用 .NET Rectangle 對(duì)象來(lái)完成,并且允許避免在默認(rèn)情況下不包含條碼的圖像區(qū)域中查找條碼的需要。
請(qǐng)下載 API 的 DLL或使用NuGet安裝它。
PM> Install-Package Aspose.BarCode
使用 C# 從圖像中讀取條形碼
我們可以按照以下步驟輕松地從圖像中讀取條形碼:
- 首先,使用BarCodeReader類(lèi)加載圖像。
- 調(diào)用readBarCodes()方法,在BarCodeResult類(lèi)對(duì)象中獲取識(shí)別結(jié)果。
- 最后,遍歷結(jié)果并顯示條形碼的類(lèi)型和文本。
以下代碼示例顯示了如何在 C# 中從圖像中讀取條形碼。
// This code example demonstrates how to read barcode from an image file. // Initialize barcode reader BarCodeReader reader = new BarCodeReader("C:\\Files\\BarCode\\Sample.png"); // Read barcode and show results foreach(BarCodeResult result in reader.ReadBarCodes()) { Console.Out.WriteLine("CodeText: " + result.CodeText); Console.Out.WriteLine("Symbology type: " + result.CodeType); }
C#從位圖中讀取條碼
我們可以按照以下步驟輕松地從圖像中讀取條形碼:
- 首先,使用Bitmap類(lèi)加載圖像。
- 接下來(lái),使用Bitmap對(duì)象創(chuàng)建BarCodeReader類(lèi)的實(shí)例。
- 調(diào)用ReadBarCodes()方法,在BarCodeResult類(lèi)對(duì)象中獲取識(shí)別結(jié)果。
- 最后,遍歷結(jié)果并顯示條形碼的類(lèi)型和文本。
以下代碼示例顯示了如何在 C# 中從位圖中讀取條形碼。
// This code example demonstrates how to read barcode from bitmap. // Load image in Bitmap Bitmap bmp = new Bitmap("C:\\Files\\BarCode\\Code128.jpg"); // Initialize Barcode reader BarCodeReader reader = new BarCodeReader(bmp); // Read all barcodes in the provided area foreach (BarCodeResult result in reader.ReadBarCodes()) { Console.Out.WriteLine("CodeText: " + result.CodeText); Console.Out.WriteLine("Symbology type: " + result.CodeType); }
在 C# 中使用 Stream 從圖像中讀取條碼
我們還可以使用文件流加載條碼圖像并按照以下步驟讀取條碼:
- 首先,使用FileStream類(lèi)加載圖像。
- 接下來(lái),使用流對(duì)象創(chuàng)建BarCodeReader類(lèi)的實(shí)例。
- 調(diào)用ReadBarCodes()方法,在BarCodeResult類(lèi)對(duì)象中獲取識(shí)別結(jié)果。
- 最后,遍歷結(jié)果并顯示條形碼的類(lèi)型和文本。
以下代碼示例展示了如何在 C# 中使用 Stream 從圖像中讀取條形碼。
// This code example demonstrates how to read barcode from an image using file stream. // Load image Stream stream = new FileStream("C:\\Files\\BarCode\\MultipleBarcodes.jpeg", FileMode.Open, FileAccess.Read); // Initialize Barcode reader BarCodeReader reader = new BarCodeReader(stream); // Read all barcodes in the provided area foreach (BarCodeResult result in reader.ReadBarCodes()) { Console.Out.WriteLine("CodeText: " + result.CodeText); Console.Out.WriteLine("Symbology type: " + result.CodeType); }
C#從圖像中讀取特定類(lèi)型的條碼
建議選擇將考慮進(jìn)行識(shí)別的目標(biāo)條碼符號(hào)體系,以最大程度地減少完成識(shí)別所需的時(shí)間并避免嘗試識(shí)別過(guò)時(shí)的條碼。
我們可以按照以下步驟指定目標(biāo)條碼類(lèi)型并從圖像中讀取條碼:
- 首先,使用BarCodeReader類(lèi)加載圖像。
- 接下來(lái),設(shè)置條形碼解碼類(lèi)型,例如Code39Standard。
- 之后,使用ReadBarCodes()方法在BarCodeResult類(lèi)對(duì)象中獲取識(shí)別結(jié)果。
- 最后,遍歷結(jié)果并顯示條形碼的類(lèi)型和文本。
以下代碼示例展示了如何使用 C# 從圖像中讀取特定類(lèi)型的條形碼。
// This code example demonstrates how to read barcode of a specific decode type from an image. // Initialize barcode reader BarCodeReader reader = new BarCodeReader("C:\\Files\\BarCode\\Code39Standard.jpg", DecodeType.Code39Standard); // Read barcode of type Code39Extended foreach (BarCodeResult result in reader.ReadBarCodes()) { Console.Out.WriteLine("CodeText: " + result.CodeText); Console.Out.WriteLine("Symbology type: " + result.CodeType); }
C#讀取圖片中多種類(lèi)型的條碼
我們還可以按照以下步驟指定多種條形碼類(lèi)型:
- 首先,使用BarCodeReader類(lèi)加載圖像。
- 接下來(lái),使用SetBarCodeReadType()方法設(shè)置條碼解碼類(lèi)型。
- 之后,使用ReadBarCodes()方法在BarCodeResult類(lèi)對(duì)象中獲取識(shí)別結(jié)果。
- 最后,遍歷結(jié)果并顯示條形碼的類(lèi)型和文本。
以下代碼示例展示了如何使用 C# 從圖像中讀取多種類(lèi)型的條形碼。
// This code example demonstrates how to read barcode of multiple decode types from an image. // Initialize barcode reader BarCodeReader reader = new BarCodeReader("C:\\Files\\BarCode\\MultipleBarcodes.png"); reader.SetBarCodeReadType(DecodeType.DataMatrix, DecodeType.QR, DecodeType.Code39Extended); // Read barcodes foreach (BarCodeResult result in reader.ReadBarCodes()) { Console.Out.WriteLine("CodeText: " + result.CodeText); Console.Out.WriteLine("Symbology type: " + result.CodeType); Console.Out.WriteLine("-------------------------"); }
我們還可以在 BarCodeReader 類(lèi)的構(gòu)造函數(shù)中指定多種解碼類(lèi)型,如下所示:
BarCodeReader reader = new BarCodeReader("C:\\Files\\BarCode\\Code39Standard.jpg", DecodeType.DataMatrix, DecodeType.QR, DecodeType.Code39Extended);
在 C# 中從圖像中讀取預(yù)定義的條碼類(lèi)型集
我們可以讀取DecodeTypes類(lèi)中定義的一組預(yù)定義的符號(hào)體系以進(jìn)行識(shí)別。我們可以設(shè)置以下任何預(yù)定義集:
- AllSupportedTypes - 所有支持的條形碼類(lèi)型
- Types1D - 所有支持的一維符號(hào)
- Types2D - 所有支持的二維符號(hào)
- PostalTypes - 所有受支持的郵政符號(hào)體系,主要由郵政服務(wù)使用
- MostCommonTypes - 定義了一組最廣泛使用的條碼標(biāo)準(zhǔn)
我們可以按照以下步驟指定預(yù)定義集:
- 首先,首先,使用BarCodeReader類(lèi)加載圖像。
- 接下來(lái),在BarCodeReader構(gòu)造函數(shù)或SetBarCodeReadType方法中設(shè)置條碼解碼類(lèi)型,例如DecodeType.Types2D。
- 之后,使用ReadBarCodes()方法在BarCodeResult類(lèi)對(duì)象中獲取識(shí)別結(jié)果。
- 最后,遍歷結(jié)果并顯示條形碼的類(lèi)型和文本。
以下代碼示例顯示了如何使用 C# 中預(yù)定義的一組符號(hào)來(lái)讀取條形碼。
// This code example demonstrates how to read a barcode using predefined set of symbologies. // Initialize barcode reader BarCodeReader reader = new BarCodeReader("C:\\Files\\BarCode\\MultipleBarcodes.png", DecodeType.Types1D); // Read barcode and show results foreach (BarCodeResult result in reader.ReadBarCodes()) { Console.Out.WriteLine("CodeText: " + result.CodeText); Console.Out.WriteLine("Symbology type: " + result.CodeType); Console.Out.WriteLine("-------------------------"); }
在 C# 中從圖像中讀取多個(gè)條形碼
我們還可以按照以下步驟從圖像中讀取所有可用的條形碼:
- 首先,使用BarCodeReader類(lèi)加載圖像。
- 接下來(lái),將條形碼解碼類(lèi)型設(shè)置為ALL_SUPPORTED_TYPES。
- 之后,使用ReadBarCodes()方法在BarCodeResult類(lèi)對(duì)象中獲取識(shí)別結(jié)果。
- 最后,遍歷結(jié)果并顯示條形碼的類(lèi)型和文本。
以下代碼示例顯示了如何使用 C# 從圖像中讀取多個(gè)條形碼。
// This code example demonstrates how to read barcode multiple barcodes from an image. // Initialize barcode reader BarCodeReader reader = new BarCodeReader("C:\\Files\\BarCode\\MultipleBarcodes.png", DecodeType.AllSupportedTypes); // Read all types of barcode available on the input image foreach (BarCodeResult result in reader.ReadBarCodes()) { Console.Out.WriteLine("CodeText: " + result.CodeText); Console.Out.WriteLine("Symbology type: " + result.CodeType); Console.Out.WriteLine("-------------------------"); }
使用 C# 獲取條形碼的 X 和 Y 坐標(biāo)
我們可以按照以下步驟從圖像中讀取檢測(cè)到的條形碼的 X 和 Y 坐標(biāo):
- 首先,使用BarCodeReader類(lèi)加載圖像。
- 接下來(lái),使用ReadBarCodes()方法在BarCodeResult類(lèi)對(duì)象中獲取識(shí)別結(jié)果。
- 然后,遍歷結(jié)果并檢查Region是否不為空。
- 之后,使用Points獲取限制條形碼區(qū)域的 Points 數(shù)組。
- 最后,顯示條形碼的 X 和 Y 點(diǎn)。
以下代碼示例顯示如何使用 C# 從圖像中獲取條形碼的 X 和 Y 坐標(biāo)點(diǎn)。
// This code example demonstrates how to read X & Y region point of barcodes from an image. // Initialize barcode reader BarCodeReader reader = new BarCodeReader("C:\\Files\\BarCode\\Code39Standard.jpg", DecodeType.AllSupportedTypes); // Read barcode foreach (BarCodeResult result in reader.ReadBarCodes()) { if (result.Region != null) { // Display x and y coordinates of all the barcodes detected Point[] point = result.Region.Points; Console.Out.WriteLine("Top left coordinates: X = " + point[0].X + ", Y = " + point[0].Y); Console.Out.WriteLine("Bottom left coordinates: X = " + point[1].X + ", Y = " + point[1].Y); Console.Out.WriteLine("Bottom right coordinates: X = " + point[2].X + ", Y = " + point[2].Y); Console.Out.WriteLine("Top right coordinates: X = " + point[3].X + ", Y = " + point[3].Y); } }
在 C# 中從圖像的特定區(qū)域讀取條形碼
我們可以按照以下步驟從特定區(qū)域或圖像區(qū)域讀取條形碼:
- 首先,使用位圖方法讀取圖像文件的緩沖區(qū)數(shù)據(jù)。
- 接下來(lái),創(chuàng)建BarCodeReader類(lèi)的一個(gè)實(shí)例。它以圖像、矩形區(qū)域和解碼類(lèi)型作為參數(shù)。
- 之后,使用ReadBarCodes()方法在BarCodeResult類(lèi)對(duì)象中獲取識(shí)別結(jié)果。
- 最后,遍歷結(jié)果并顯示條形碼的類(lèi)型和文本。
以下代碼示例展示了如何使用 C# 從圖像的特定區(qū)域讀取條形碼。
// This code example demonstrates how to read barcode from specific region of an image. // Load image Bitmap img = new Bitmap("C:\\Files\\BarCode\\MultipleBarcodes.jpeg"); // Create an instance of BarCodeReader class // and specify an area to look for the barcode BarCodeReader reader = new BarCodeReader(img, new Rectangle(0, 0, 400, 200)); // Read all barcodes in the provided area foreach (BarCodeResult result in reader.ReadBarCodes()) { Console.Out.WriteLine("CodeText: " + result.CodeText); Console.Out.WriteLine("Symbology type: " + result.CodeType); }
在 C# 中從圖像的多個(gè)區(qū)域讀取條形碼
我們還可以按照以下步驟從圖像的多個(gè)區(qū)域讀取條形碼:
- 首先,使用Bitmap類(lèi)加載圖像。
- 接下來(lái),使用 Rectangle 類(lèi)定義矩形。
- 然后,創(chuàng)建BarCodeReader類(lèi)的實(shí)例。
- 同時(shí),使用圖像對(duì)象和矩形作為參數(shù)調(diào)用SetBarCodeImage() 。
- 或者,使用SetBarCodeReadType()方法設(shè)置條碼讀取類(lèi)型。
- 之后,使用ReadBarCodes()方法在BarCodeResult類(lèi)對(duì)象中獲取識(shí)別結(jié)果。
- 最后,遍歷結(jié)果并顯示條形碼的類(lèi)型和文本。
以下代碼示例顯示如何使用 C# 從圖像的多個(gè)區(qū)域讀取條形碼。
// This code example demonstrates how to read barcode from specific region of an image. // Load image in Bitmap Bitmap bmp = new Bitmap("C:\\Files\\BarCode\\MultipleBarcodes.png"); // Rectangle of a 2D barcode in the source image Rectangle rect2D = new Rectangle(0, 0, 400, 200); // Rectangle of Code128 barcode in the source image Rectangle rectCode128 = new Rectangle(450, 100, 600, 180); // Initialize Barcode reader BarCodeReader reader = new BarCodeReader(); reader.SetBarCodeImage(bmp, new Rectangle[] { rect2D, rectCode128 }); reader.SetBarCodeReadType(DecodeType.AllSupportedTypes); // Read all barcodes in the provided area foreach (BarCodeResult result in reader.ReadBarCodes()) { Console.Out.WriteLine("CodeText: " + result.CodeText); Console.Out.WriteLine("Symbology type: " + result.CodeType); Console.Out.WriteLine("-------------------------"); }
文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-424191.html
以上便是如何在C#從圖像中讀取條形碼,希望能幫到您,除此之外,你有其他方面的需求,也歡迎和我們互動(dòng),或這下體驗(yàn)我們更多的產(chǎn)品~文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-424191.html
到了這里,關(guān)于條碼控件Aspose.BarCode入門(mén)教程(8):C#從圖像中讀取條形碼的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!