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