如何在 C# 中讀取 QR 碼類型
在運行時識別任何掃描的QR Code格式。閱讀QrResult.QrType以檢測符號並構建類型識別的處理邏輯,用於不同來源的輸入。
當應用程式接受來自多個來源的 QR 碼時,其格式未必總是可預測的。 物流平台可能會收到來自運送標籤的標準 QR 碼,以及來自產品標籤的緊湊型 Micro QR 碼。 文件處理系統可能會掃描 PDF 檔案中嵌入的條碼,以及印製在實體媒體上的條碼。 閱讀QrResult.QrType讓應用程式能夠知道檢測出的是哪種格式,使得可以驗證輸入、將數據路由到正確的處理程序,或記錄不支持的格式供審查。
本指南演示如何使用IronQR程式庫從掃描結果中檢索QR Code格式。 尚未掃描QR Code的開發者應先從從圖片讀取QR Code指南開始。
快速入門:掃描 QR 碼類型載入圖像,用QrReader掃描,並訪問檢測的格式。
-
1使用 IronQR 安裝 NuGet Package Manager
-
2複製並運行這段程式碼。
var input = new QrImageInput(AnyBitmap.FromFile("qr-code.png")); var results = new QrReader().Read(input); Console.WriteLine(results.First().QrType);Dim input = New QrImageInput(AnyBitmap.FromFile("qr-code.png")) Dim results = (New QrReader()).Read(input) Console.WriteLine(results.First().QrType)Dim input = New QrImageInput(AnyBitmap.FromFile("qr-code.png")) Dim results = (New QrReader()).Read(input) Console.WriteLine(results.First().QrType) -
3部署到您的生產環境進行測試
立即在您的項目中開始使用 IronQR,並使用 免費試用
最小工作流程(5 個步驟)
- 下載IronQR C#程式庫以讀取QR Code類型
- 載入圖片並將其封裝在
QrImageInput中 - 建立
QrReader實例,並傳入輸入值呼叫Read方法 - 透過
QrResult.QrType存取偵測到的格式 - 使用
QrEncoding的switch語法,根據格式路由處理流程
如何讀取 QR 碼類型?
要讀取 QR Code 的類型,請將影像載入 QrImageInput 中,將其傳遞給 QrReader.Read(),然後在返回的 QrResult 中存取 QrType。 該屬性返回一個QrEncoding列舉值,以識別檢測到的符號。
輸入
下面的QR Code編碼了**https://ironsoftware.com**,將被掃描以擷取其類型。

using IronQr;
using System.Drawing;
using System.Linq;
// Import an image containing a QR code
var inputImage = Image.FromFile("sample.jpg");
// Load the asset into a QrImageInput object
QrImageInput imageInput = new QrImageInput(inputImage);
// Create a QR Reader object
QrReader reader = new QrReader();
// Read the input and get all embedded QR codes
IEnumerable<QrResult> results = reader.Read(imageInput);
// Display the type of the first QR code found
Console.WriteLine($"The QR type is {results.First().QrType}");using IronQr;
using System.Drawing;
using System.Linq;
// Import an image containing a QR code
var inputImage = Image.FromFile("sample.jpg");
// Load the asset into a QrImageInput object
QrImageInput imageInput = new QrImageInput(inputImage);
// Create a QR Reader object
QrReader reader = new QrReader();
// Read the input and get all embedded QR codes
IEnumerable<QrResult> results = reader.Read(imageInput);
// Display the type of the first QR code found
Console.WriteLine($"The QR type is {results.First().QrType}");Imports IronQr
Imports System.Drawing
Imports System.Linq
' Import an image containing a QR code
Dim inputImage As Image = Image.FromFile("sample.jpg")
' Load the asset into a QrImageInput object
Dim imageInput As New QrImageInput(inputImage)
' Create a QR Reader object
Dim reader As New QrReader()
' Read the input and get all embedded QR codes
Dim results As IEnumerable(Of QrResult) = reader.Read(imageInput)
' Display the type of the first QR code found
Console.WriteLine($"The QR type is {results.First().QrType}")Imports IronQr
Imports System.Drawing
Imports System.Linq
' Import an image containing a QR code
Dim inputImage As Image = Image.FromFile("sample.jpg")
' Load the asset into a QrImageInput object
Dim imageInput As New QrImageInput(inputImage)
' Create a QR Reader object
Dim reader As New QrReader()
' Read the input and get all embedded QR codes
Dim results As IEnumerable(Of QrResult) = reader.Read(imageInput)
' Display the type of the first QR code found
Console.WriteLine($"The QR type is {results.First().QrType}")QrType 返回一個 QrEncoding 枚舉值,如 QrEncoding.QRCode、QrEncoding.MicroQRCode 或 QrEncoding.RMQRCode。 這使得可以直接安全地在switch語句中使用,而無需解析或字串比較。
輸出

如何根據 QR 碼格式進行處理路由?
當應用程式從多個來源接收 QR 碼時,並非所有輸入的格式都相同。 在 QrResult.QrType 上使用 switch 來根據每個檢測到的代碼的 QrEncoding 值將其路由到正確的處理程序。 此設計可將格式專屬邏輯隔離,並使新增格式分支變得直觀簡便。
使用上方的相同輸入 QR 碼:
using IronQr;
using IronQr.Enum;
using System.Drawing;
// Import an image containing QR codes
var inputImage = Image.FromFile("sample.jpg");
// Load the asset into a QrImageInput object
QrImageInput imageInput = new QrImageInput(inputImage);
// Create a QR Reader object
QrReader reader = new QrReader();
// Read all embedded QR codes from the image
IEnumerable<QrResult> results = reader.Read(imageInput);
// Route processing based on the detected QR code format
foreach (QrResult result in results)
{
switch (result.QrType)
{
case QrEncoding.QRCode:
Console.WriteLine($"Standard QR Code: {result.Value}");
break;
case QrEncoding.MicroQRCode:
Console.WriteLine($"Micro QR Code: {result.Value}");
break;
case QrEncoding.RMQRCode:
Console.WriteLine($"RMQR Code: {result.Value}");
break;
default:
Console.WriteLine($"Other format ({result.QrType}): {result.Value}");
break;
}
}using IronQr;
using IronQr.Enum;
using System.Drawing;
// Import an image containing QR codes
var inputImage = Image.FromFile("sample.jpg");
// Load the asset into a QrImageInput object
QrImageInput imageInput = new QrImageInput(inputImage);
// Create a QR Reader object
QrReader reader = new QrReader();
// Read all embedded QR codes from the image
IEnumerable<QrResult> results = reader.Read(imageInput);
// Route processing based on the detected QR code format
foreach (QrResult result in results)
{
switch (result.QrType)
{
case QrEncoding.QRCode:
Console.WriteLine($"Standard QR Code: {result.Value}");
break;
case QrEncoding.MicroQRCode:
Console.WriteLine($"Micro QR Code: {result.Value}");
break;
case QrEncoding.RMQRCode:
Console.WriteLine($"RMQR Code: {result.Value}");
break;
default:
Console.WriteLine($"Other format ({result.QrType}): {result.Value}");
break;
}
}Imports IronQr
Imports IronQr.Enum
Imports System.Drawing
' Import an image containing QR codes
Dim inputImage As Image = Image.FromFile("sample.jpg")
' Load the asset into a QrImageInput object
Dim imageInput As New QrImageInput(inputImage)
' Create a QR Reader object
Dim reader As New QrReader()
' Read all embedded QR codes from the image
Dim results As IEnumerable(Of QrResult) = reader.Read(imageInput)
' Route processing based on the detected QR code format
For Each result As QrResult In results
Select Case result.QrType
Case QrEncoding.QRCode
Console.WriteLine($"Standard QR Code: {result.Value}")
Case QrEncoding.MicroQRCode
Console.WriteLine($"Micro QR Code: {result.Value}")
Case QrEncoding.RMQRCode
Console.WriteLine($"RMQR Code: {result.Value}")
Case Else
Console.WriteLine($"Other format ({result.QrType}): {result.Value}")
End Select
NextImports IronQr
Imports IronQr.Enum
Imports System.Drawing
' Import an image containing QR codes
Dim inputImage As Image = Image.FromFile("sample.jpg")
' Load the asset into a QrImageInput object
Dim imageInput As New QrImageInput(inputImage)
' Create a QR Reader object
Dim reader As New QrReader()
' Read all embedded QR codes from the image
Dim results As IEnumerable(Of QrResult) = reader.Read(imageInput)
' Route processing based on the detected QR code format
For Each result As QrResult In results
Select Case result.QrType
Case QrEncoding.QRCode
Console.WriteLine($"Standard QR Code: {result.Value}")
Case QrEncoding.MicroQRCode
Console.WriteLine($"Micro QR Code: {result.Value}")
Case QrEncoding.RMQRCode
Console.WriteLine($"RMQR Code: {result.Value}")
Case Else
Console.WriteLine($"Other format ({result.QrType}): {result.Value}")
End Select
Next每個 case 都針對特定的 QrEncoding 值。 QrType分支檔捕獲任何未明確處理的格式,以確保對意外輸入不會靜默失敗。 隨著應用程式需要支援更多格式,請新增更多範例。
輸出

QrResult.QrType 返回什麼?
QrResult.QrType 是每個 QrResult 上的一個 QrEncoding enum 屬性,用於識別掃描儀檢測到的符號學。 它在QrReader.Read()過程中自動填充,無需其他配置。 添加 using IronQr.Enum; 以便直接在 switch 中使用 QrEncoding 值。
| 價值 | 說明 |
|---|---|
QrEncoding.QRCode | 標準 QR 碼,各產業中最常用的格式 |
QrEncoding.MicroQRCode | 專為印刷面積有限的小型表面設計的緊湊型版本 |
QrEncoding.RMQRCode | 針對狹長型標籤形狀優化的矩形微型 QR 碼 (rMQR) |
QrType是只讀的,反映了掃描儀在圖像中檢測到的內容。 其價值並不取決於 QR 碼的生成方式。
QrType的常見用例是什麼?
- 物流與運送:偵測標籤上是否載有標準 QR 碼或緊湊型 Micro QR 碼,並將其分別導向正確的解析流程。
- 文件處理:在提取資料以進行記錄比對之前,驗證掃描文件是否符合預期格式。
- 多格式資訊站:單一站點即可接收不同格式的 QR 碼,並將其自動分派至相應的處理程序,無需人工干預。
- 稽核與合規:將符號類型與解碼值一併記錄,以建立跨批次輸入格式的可驗證紀錄。
- 品質保證:驗證生成的 QR 碼掃描後是否顯示為預期類型,以確認輸出符合規格。
對於在檢測類型後讀取QR Code資料的更多內容,請參閱讀取 QR Code 值指南和完整的IronQR功能集。
常見問題
如何在C#中讀取QR code的類型?
您可以使用IronQR程式庫在C#中讀取QR code的類型。將圖像載入到QrImageInput中,傳遞給QrReader.Read(),然後訪問返回的QrResult上的QrType以識別QR code的格式。
QrResult.QrType返回什麼?
QrResult.QrType返回一個QrEncoding枚舉值,用於識別檢測到的符號,例如QRCode、MicroQRCode或RMQRCode。此屬性在使用IronQR的掃描過程中會自動填充。
如何根據QR code格式路由處理?
要根據QR code格式路由處理,請在QrResult.QrType上使用switch語句。這允許您處理特定的QrEncoding值,如QRCode、MicroQRCode或RMQRCode,將每個值導向正確的處理邏輯。
檢測QR code類型的常見用例是什麼?
常見用例包括物流和運輸以正確路由QR類型,文件處理以驗證預期格式,多格式自動服務站以處理各種輸入,以及審計/合規性以記錄符號類型。
QrResult.QrType需要任何其他配置嗎?
不需要任何額外配置。在使用IronQR的QrReader.Read()時,QrResult.QrType會自動填充,直接訪問檢測到的QrEncoding值。
如何驗證QR code是否掃描為預期的類型?
使用IronQR掃描QR code,並檢查QrResult.QrType值以確認是否與預期的QrEncoding類型匹配,確保生成的QR code正確。
我能用IronQR處理意外的QR code格式嗎?
是的,您可以通過在QrResult.QrType上的switch語句中使用預設情況來處理意外的格式。這確保任何未處理的格式都會被捕獲並記錄以供審查。
不同的QrEncoding值意味著什麼?
不同的QrEncoding值指定了在QR code中檢測到的符號類型。例如,QRCode是一種標準格式,MicroQRCode適合緊湊表面,而RMQRCode則優化用於細長標籤。
QrResult.QrType如何有助於QR code驗證?
QrResult.QrType通過確認掃描的QR code在處理其數據之前與預期的格式匹配,從而確保數據的完整性和兼容性。
Curtis Chau 擁有卡爾頓大學計算機科學學士學位,專注於前端開發,擅長於 Node.js、TypeScript、JavaScript 和 React。Curtis 熱衷於創建直觀且美觀的用戶界面,喜歡使用現代框架並打造結構良好、視覺吸引人的手冊。