翻譯|使用教程|編輯:胡濤|2023-04-12 09:56:32.427|閱讀 188 次
概述:在本文中,我們將通過幾個簡單的步驟向您展示如何使用 C# 從圖像中讀取條形碼。您可以將本文用作開發(fā)條形碼閱讀器或掃描器應(yīng)用程序的分步指南。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關(guān)鏈接:
Aspose.BarCode for .NET 是一個功能強(qiáng)大的API,可以從任意角度生成和識別多種圖像類型的一維和二維條形碼。開發(fā)人員可以輕松添加條形碼生成和識別功能,以及在.NET應(yīng)用程序中將生成的條形碼導(dǎo)出為高質(zhì)量的圖像格式。
Aspose API支持流行文件格式處理,并允許將各類文檔導(dǎo)出或轉(zhuǎn)換為固定布局文件格式和最常用的圖像/多媒體格式。
您是否正在尋找一種以編程方式從圖像中讀取條形碼或 QR 碼的方法?如果您是開發(fā)人員,并且需要創(chuàng)建自己的條碼閱讀器應(yīng)用程序?你來對地方了。條形碼對于準(zhǔn)確跟蹤庫存和產(chǎn)品從制造到銷售點(diǎn)的交付至關(guān)重要。我們可以在.NET應(yīng)用程序中輕松檢測、識別和讀取不同類型的條形碼和二維碼。在本文中,我們將通過幾個簡單的步驟向您展示如何使用 C# 從圖像中讀取條形碼。您可以將本文用作開發(fā)條形碼閱讀器或掃描器應(yīng)用程序的分步指南。
首先,我們將了解 C# 條碼閱讀器 API,以讀取輸入圖像中可用的條碼。接下來,我們將介紹如何從圖像中檢測、識別和提取條形碼數(shù)據(jù)的步驟。您將找到詳細(xì)的步驟和代碼片段。最后,我們將提供有用的鏈接以進(jìn)一步增強(qiáng)功能。讓我們開始吧!
為了從圖像中讀取條形碼,我們將使用Aspose.BarCode for .NET API。API 允許生成、掃描和讀取范圍廣泛的條碼符號。它支持以JPEG、TIFF、PNG、BMP和GIF格式呈現(xiàn)條碼圖像。
API 提供了BarCodeReader類,可以從給定的圖像中識別 60 多種不同的條形碼類型。檢測條形碼的第一步是指定帶有條形碼的圖像的來源。這可以是文件、位圖對象或流。然后需要在DecodeType參數(shù)中指定目標(biāo)符號。我們可以通過指定DecodeType.AllSupportedTypes來查看所有不同類型的支持符號。此類的ReadBarCodes ()方法返回一個已識別條碼數(shù)組。API的BarCodeResult類存儲識別出的條碼數(shù)據(jù),如條碼類型、條碼文本、區(qū)域等參數(shù)。
API 還允許指定條形碼閱讀器應(yīng)讀取的圖像區(qū)域。這可以使用 .NET Rectangle 對象來完成,并且允許避免在默認(rèn)情況下不包含條碼的圖像區(qū)域中查找條碼的需要。
請下載 API 的 DLL或使用NuGet安裝它。
PM> Install-Package Aspose.BarCode
我們可以按照以下步驟輕松地從圖像中讀取條形碼:
以下代碼示例顯示了如何在 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# 中從位圖中讀取條形碼。
// 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 從圖像中讀取條形碼。
// 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); }
建議選擇將考慮進(jìn)行識別的目標(biāo)條碼符號體系,以最大程度地減少完成識別所需的時間并避免嘗試識別過時的條碼。
我們可以按照以下步驟指定目標(biāo)條碼類型并從圖像中讀取條碼:
以下代碼示例展示了如何使用 C# 從圖像中讀取特定類型的條形碼。
// 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# 從圖像中讀取多種類型的條形碼。
// 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 類的構(gòu)造函數(shù)中指定多種解碼類型,如下所示:
BarCodeReader reader = new BarCodeReader("C:\\Files\\BarCode\\Code39Standard.jpg", DecodeType.DataMatrix, DecodeType.QR, DecodeType.Code39Extended);
我們可以讀取DecodeTypes類中定義的一組預(yù)定義的符號體系以進(jìn)行識別。我們可以設(shè)置以下任何預(yù)定義集:
我們可以按照以下步驟指定預(yù)定義集:
以下代碼示例顯示了如何使用 C# 中預(yù)定義的一組符號來讀取條形碼。
// 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# 從圖像中讀取多個條形碼。
// 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("-------------------------"); }
我們可以按照以下步驟從圖像中讀取檢測到的條形碼的 X 和 Y 坐標(biāo):
以下代碼示例顯示如何使用 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); } }
我們可以按照以下步驟從特定區(qū)域或圖像區(qū)域讀取條形碼:
以下代碼示例展示了如何使用 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); }
我們還可以按照以下步驟從圖像的多個區(qū)域讀取條形碼:
以下代碼示例顯示如何使用 C# 從圖像的多個區(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("-------------------------"); }
以上便是如何在C#從圖像中讀取條形碼,希望能幫到您,除此之外,你有其他方面的需求,也歡迎和我們互動,或這下體驗(yàn)我們更多的產(chǎn)品~
歡迎下載|體驗(yàn)更多Aspose產(chǎn)品
本站文章除注明轉(zhuǎn)載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉(zhuǎn)載,但請務(wù)必注明出處、不得修改原文相關(guān)鏈接,如果存在內(nèi)容上的異議請郵件反饋至chenjj@fc6vip.cn