如何在 C# 中讀取 QR code 類型

This article was translated from English: Does it need improvement?
Translated
View the article in English

在執行階段識別任何掃描 QR 碼的格式。請參閱 QrResult.QrType 以偵測符號系統,並針對各種輸入來源建立具類型意識的處理邏輯。

當應用程式接受來自多個來源的 QR 碼時,其格式未必總是可預測的。 物流平台可能會收到來自運送標籤的標準 QR 碼,以及來自產品標籤的緊湊型 Micro QR 碼。 文件處理系統可能會掃描 PDF 檔案中嵌入的條碼,以及印製在實體媒體上的條碼。 讀取 QrResult.QrType 可讓應用程式掌握偵測到的格式,從而能夠驗證輸入內容、將資料路由至正確的處理程序,或記錄不支援的格式以供審查。

本指南將示範如何使用 IronQR程式庫,從掃描結果中擷取 QR 碼格式。 尚未掃描過 QR 碼的開發人員,請先參閱《從圖片讀取 QR 碼》指南。

快速入門:掃描 QR 碼類型

載入圖片,使用 QrReader 進行掃描,並取得偵測到的格式。

  1. 使用NuGet套件管理器安裝https://www.nuget.org/packages/IronQR

    PM > Install-Package IronQR
  2. 複製並運行這段程式碼。

    var input = new QrImageInput("qr-code.png");
    var results = new QrReader().Read(input);
    Console.WriteLine(results.First().QrType);
  3. 部署到您的生產環境進行測試

    今天就在您的專案中開始使用免費試用IronQR

    arrow pointer

如何讀取 QR 碼類型?

要讀取 QR 碼的類型,請將圖片載入 QrImageInput,傳遞給 QrReader.Read(),並在返回的 QrResult 上存取 QrType。 此屬性會傳回一個 QrEncoding 枚舉值,用以識別偵測到的符號系統。

輸入

下方的 QR 碼編碼了 https://ironsoftware.com,掃描後將提取其類型。

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}")
$vbLabelText   $csharpLabel

QrType 會傳回 QrEncoding 枚舉值,例如 QrEncoding.MicroQRCodeQrEncoding.RMQRCode。 這使得該內容可直接用於 switch 語句中,無需進行解析或字串比對。

輸出

顯示偵測到的 QR 碼類型的主控台輸出

如何根據 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
$vbLabelText   $csharpLabel

每個 case 對應一個特定的 QrEncoding 值。 default 分支會處理任何未明確處理的格式,因此即使遇到意外輸入,迴圈也不會靜默失敗。 隨著應用程式需要支援更多格式,請新增更多範例。

輸出

顯示每個偵測到的 QR 碼之格式導向路由結果的控制台輸出

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 功能集

Curtis Chau
技術作家

Curtis Chau 擁有卡爾頓大學計算機科學學士學位,專注於前端開發,擅長於 Node.js、TypeScript、JavaScript 和 React。Curtis 熱衷於創建直觀且美觀的用戶界面,喜歡使用現代框架並打造結構良好、視覺吸引人的手冊。

除了開發之外,Curtis 對物聯網 (IoT) 有著濃厚的興趣,探索將硬體和軟體結合的創新方式。在閒暇時間,他喜愛遊戲並構建 Discord 機器人,結合科技與創意的樂趣。

準備好開始了嗎?
Nuget 下載 63,625 | 版本: 2026.4 剛剛發布
Still Scrolling Icon

還在捲動嗎?

想要快速證明? PM > Install-Package IronQR
執行範例 觀看您的 URL 變成 QR code。