如何在 C# 中讀取 QR code 類型
在執行階段識別任何掃描 QR 碼的格式。請參閱 QrResult.QrType 以偵測符號系統,並針對各種輸入來源建立具類型意識的處理邏輯。
當應用程式接受來自多個來源的 QR 碼時,其格式未必總是可預測的。 物流平台可能會收到來自運送標籤的標準 QR 碼,以及來自產品標籤的緊湊型 Micro QR 碼。 文件處理系統可能會掃描 PDF 檔案中嵌入的條碼,以及印製在實體媒體上的條碼。 讀取 QrResult.QrType 可讓應用程式掌握偵測到的格式,從而能夠驗證輸入內容、將資料路由至正確的處理程序,或記錄不支援的格式以供審查。
本指南將示範如何使用 IronQR程式庫,從掃描結果中擷取 QR 碼格式。 尚未掃描過 QR 碼的開發人員,請先參閱《從圖片讀取 QR 碼》指南。
快速入門:掃描 QR 碼類型
載入圖片,使用 QrReader 進行掃描,並取得偵測到的格式。
最小工作流程(5 個步驟)
- 下載 IronQR C# 程式庫以讀取各種 QR 碼
- 載入圖片並將其封裝在 `QrImageInput` 中
- 建立 `QrReader` 實例,並傳入輸入值呼叫 `Read` 方法
- 透過 `QrResult.QrType` 存取偵測到的格式
- 使用 `QrEncoding` 的 `switch` 語法,根據格式路由處理流程
如何讀取 QR 碼類型?
要讀取 QR 碼的類型,請將圖片載入 QrImageInput,傳遞給 QrReader.Read(),並在返回的 QrResult 上存取 QrType。 此屬性會傳回一個 QrEncoding 枚舉值,用以識別偵測到的符號系統。
輸入
下方的 QR 碼編碼了 https://ironsoftware.com,掃描後將提取其類型。
:path=/static-assets/qr/content-code-examples/how-to/read-qr-code-type.cs
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}")
QrType 會傳回 QrEncoding 枚舉值,例如 QrEncoding.MicroQRCode 或 QrEncoding.RMQRCode。 這使得該內容可直接用於 switch 語句中,無需進行解析或字串比對。
輸出
如何根據 QR 碼格式進行處理路由?
當應用程式從多個來源接收 QR 碼時,並非所有輸入的格式都相同。 使用 switch 處理 QrResult.QrType,根據其 QrEncoding 值,將每個偵測到的代碼路由至正確的處理程序。 此設計可將格式專屬邏輯隔離,並使新增格式分支變得直觀簡便。
使用上方的相同輸入 QR 碼:
:path=/static-assets/qr/content-code-examples/how-to/read-qr-code-type-all.cs
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
Next
每個 case 對應一個特定的 QrEncoding 值。 default 分支會處理任何未明確處理的格式,因此即使遇到意外輸入,迴圈也不會靜默失敗。 隨著應用程式需要支援更多格式,請新增更多範例。
輸出
QrResult.QrType 會傳回什麼?
QrType 是每個 QrResult 上的 QrEncoding 枚舉屬性,用於識別掃描器偵測到的條碼符號。 此內容會在 QrReader.Read() 期間自動填入,無需額外設定。 請在 switch 中加入 using IronQr.Enum; 以直接使用 QrEncoding 的值。
| 價值 | 說明 |
|---|---|
QrEncoding.QRCode |
標準 QR 碼,各產業中最常用的格式 |
QrEncoding.MicroQRCode |
專為印刷面積有限的小型表面設計的緊湊型版本 |
QrEncoding.RMQRCode |
針對狹長型標籤形狀優化的矩形微型 QR 碼 (rMQR) |
QrType 為唯讀內容,反映掃描器在圖像中偵測到的資訊。 其價值並不取決於 QR 碼的生成方式。
QrType 的常見應用情境有哪些?
- 物流與運送:偵測標籤上是否載有標準 QR 碼或緊湊型 Micro QR 碼,並將其分別導向正確的解析流程。
- 文件處理:在提取資料以進行記錄比對之前,驗證掃描文件是否符合預期格式。
- 多格式資訊站:單一站點即可接收不同格式的 QR 碼,並將其自動分派至相應的處理程序,無需人工干預。
- 稽核與合規:將符號類型與解碼值一併記錄,以建立跨批次輸入格式的可驗證紀錄。
- 品質保證:驗證生成的 QR 碼掃描後是否顯示為預期類型,以確認輸出符合規格。
有關偵測 QR 碼類型後讀取資料的更多資訊,請參閱《讀取 QR 碼值指南》及完整的 IronQR 功能集。

