如何在C#中讀取QR碼型別
在運行時識別任何掃描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碼的型別,將圖像載入到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}")
QrEncoding.RMQRCode。 這使得可以直接在switch語句中使用,無需解析或字串比較。
輸出
我如何根據QR碼格式路由處理?
當應用程式從多個來源接收QR碼時,並非每個輸入都會是相同格式。 在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
每個QrEncoding值。 QrType分支會捕獲任何未明確處理的格式,這樣環路在遇到意外輸入時不會默默失敗。 隨著應用程式需要支持其他格式,新增更多情況。
輸出
QrResult.QrType 返回什麼?
QrEncoding QrResult上,標識掃描器檢測到的符號。 在QrReader.Read()期間會自動填充,無需額外的配置。 新增QrEncoding值。
| 值 | 描述 |
|---|---|
QrEncoding.QRCode |
標準QR碼,最常用於所有行業的格式 |
QrEncoding.MicroQRCode |
設計用於有限列印區域的小表面的緊湊型變體 |
QrEncoding.RMQRCode |
為窄長形標籤形狀優化的矩形Micro QR碼(rMQR) |
QrType是唯讀的,反映了掃描器在圖像中檢測到的內容。 其值不取決於QR碼是如何生成的。
QrType的常見使用案例是什麼?
- 物流和運輸:檢測標籤上是否帶有標準QR碼或緊湊型Micro QR碼並將其路由到正確的解析管道。
- 文件處理:驗證掃描的文件是否包含預期格式,然後再提取其值以進行記錄匹配。
- 多格式自助服務機:在單一站點接受不同的QR格式並將每個解析到相應的處理器,無需手動干預。
- 審核與合規:將符號型別與解碼數值一起記錄,以建立輸入格式跨批次的可驗證記錄。
- 品質保證:驗證生成的QR碼是否可以按照預期型別掃描回來,確認輸出符合規範。
常見問題
QrResult.QrType在IronQR中的用途是什麼?
QrResult.QrType在IronQR中的用途是檢測掃描QR碼的符號學,使您的C#應用程式能夠進行智能的格式化處理。
我如何使用IronQR讀取QR碼型別?
要使用IronQR讀取QR碼型別,您可以在QrImageInput上使用QrReader.Read()方法。這將使您能存取QrResult.QrType,該屬性提供有關掃描的特定QR碼型別的資訊。
為什麼檢測QR碼型別在C#應用程式中很重要?
檢測QR碼型別在C#應用程式中很重要,因為它使開發人員能夠基於QR碼格式智能地處理,確保適當的處理和資料提取。
IronQR能夠處理不同的QR碼符號學嗎?
是的,IronQR可以通過使用QrResult.QrType準確地識別和處理各種QR碼型別來處理不同的QR碼符號學。
QrReader.Read()在掃描QR碼中的角色是什麼?
QrReader.Read()是IronQR中的一種方法,用於從QrImageInput掃描QR碼,使您能夠檢索QR碼型別和其他相關資訊以進行進一步處理。
使用IronQR可以處理多種QR碼型別嗎?
是的,IronQR允許您通過利用QrResult.QrType屬性來識別和處理各種QR碼型別,以處理多種QR碼型別。
在C#中使用IronQR讀取QR碼有何益處?
在C#中使用IronQR讀取QR碼的益處包括準確檢測QR碼型別、智能的格式化處理、以及與C#應用程式的無縫整合。
IronQR如何提高QR碼處理效率?
IronQR通過提供像QrReader.Read()和QrResult.QrType這樣的工具,提高了QR碼處理效率,這些工具簡化了在C#專案中對不同QR碼型別的識別和處理。

