使用不同掃描模式讀取 QR Code 的方法
隨著越來越多產品走向數位化並提供全天候線上存取,讀取 QR Code 已成為例行工作。以小巧的唯讀圖樣儲存必要資料已成為標準做法,開發者需要能在不同環境中有效擷取這些資訊。
IronQR 提供 3 種 QR Code 讀取選項,各自適用於不同情境。
本文將透過清楚的範例說明三種方法。若您還沒看過先前的指南,可以先了解如何從影像讀取 QR Code,或自行產生 QR Code。
使用不同掃描模式讀取 QR Code 的方法
- 下載支援進階掃描選項的 C# 程式庫來讀取 QR Code
- 載入目標影像檔
- 傳入影像和所需的
QrScanMode,以初始化QrImageInput - 建立
QrReader類別的執行個體,再將輸入物件傳給Read方法 - 取得結果後,可讀取解碼後的
Value文字,或逐一存取Points陣列中的座標資料。
了解 QR Code掃描模式
IronQR 透過 QrScanMode 列舉提供三種掃描模式:
- 自動模式 (
QrScanMode.Auto):結合機器學習偵測和基本掃描演算法,盡可能提高準確性與可靠性 - 僅基本掃描模式 (
QrScanMode.OnlyBasicScan):當 QR Code清晰可見時,使用傳統影像處理技術進行快速、可靠的掃描 - 偵測模型模式 (
QrScanMode.OnlyDetectionModel):利用機器學習偵測 QR Code 的位置,適合擷取座標資料
讓我們詳細了解每種掃描方法。
僅基本掃描模式
僅基本掃描模式使用傳統影像處理演算法,無須使用機器學習。當 QR Code 清楚可見且位置適當時,此模式可快速有效地掃描。即使 QR Code 受損,只要三個角落的定位方塊仍可辨識,就能讀取。
using IronQr;
using IronQr.Enum;
using IronSoftware.Drawing;
using System.IO;
using System.Linq;
// Load the image
var bmp = AnyBitmap.FromFile("damaged-qr.png");
// Create scan input using Only Basic Scan mode
var input = new QrImageInput(bmp, QrScanMode.OnlyBasicScan);
// Get the result
var result = new QrReader().Read(input).FirstOrDefault();
// Save the output to a text file
File.WriteAllText("basic-scan-output.txt", result.Value);Imports IronQr
Imports IronQr.Enum
Imports IronSoftware.Drawing
Imports System.IO
Imports System.Linq
' Load the image
Dim bmp = AnyBitmap.FromFile("damaged-qr.png")
' Create scan input using Only Basic Scan mode
Dim input = New QrImageInput(bmp, QrScanMode.OnlyBasicScan)
' Get the result
Dim result = (New QrReader()).Read(input).FirstOrDefault()
' Save the output to a text file
File.WriteAllText("basic-scan-output.txt", result.Value)Imports IronQr
Imports IronQr.Enum
Imports IronSoftware.Drawing
Imports System.IO
Imports System.Linq
' Load the image
Dim bmp = AnyBitmap.FromFile("damaged-qr.png")
' Create scan input using Only Basic Scan mode
Dim input = New QrImageInput(bmp, QrScanMode.OnlyBasicScan)
' Get the result
Dim result = (New QrReader()).Read(input).FirstOrDefault()
' Save the output to a text file
File.WriteAllText("basic-scan-output.txt", result.Value)掃描輸入

已儲存的文字檔

自動掃描模式
自動掃描模式是 QrScanMode 的預設值,也是用途最廣的選項。它會自動結合機器學習偵測與傳統掃描演算法,在各種影像條件下盡可能取得最佳結果。
此範例先載入影像並啟用 Auto 模式,接著掃描影像,將找到的 QR Code 收集到清單中。最後逐一讀取 results 集合,將各個值的文字輸出到主控台(本例只有 URL)。
using System;
using IronQr;
using IronQr.Enum;
using IronSoftware.Drawing;
// Load the image file
var bmp = AnyBitmap.FromFile("cup.png");
// Create scan input using Auto Scan mode
var input = new QrImageInput(bmp, QrScanMode.Auto);
// Scan and read all QR codes
var results = new QrReader().Read(input);
// Initialize a counter to track the number of QR codes
int count = 1;
// Loop through each discovered QR code
foreach (var result in results)
{
Console.WriteLine($"QR {count}: {result.Value}");
count++;
}using System;
using IronQr;
using IronQr.Enum;
using IronSoftware.Drawing;
// Load the image file
var bmp = AnyBitmap.FromFile("cup.png");
// Create scan input using Auto Scan mode
var input = new QrImageInput(bmp, QrScanMode.Auto);
// Scan and read all QR codes
var results = new QrReader().Read(input);
// Initialize a counter to track the number of QR codes
int count = 1;
// Loop through each discovered QR code
foreach (var result in results)
{
Console.WriteLine($"QR {count}: {result.Value}");
count++;
}Imports System
Imports IronQr
Imports IronQr.Enum
Imports IronSoftware.Drawing
' Load the image file
Dim bmp = AnyBitmap.FromFile("cup.png")
' Create scan input using Auto Scan mode
Dim input = New QrImageInput(bmp, QrScanMode.Auto)
' Scan and read all QR codes
Dim results = New QrReader().Read(input)
' Initialize a counter to track the number of QR codes
Dim count As Integer = 1
' Loop through each discovered QR code
For Each result In results
Console.WriteLine($"QR {count}: {result.Value}")
count += 1
NextImports System
Imports IronQr
Imports IronQr.Enum
Imports IronSoftware.Drawing
' Load the image file
Dim bmp = AnyBitmap.FromFile("cup.png")
' Create scan input using Auto Scan mode
Dim input = New QrImageInput(bmp, QrScanMode.Auto)
' Scan and read all QR codes
Dim results = New QrReader().Read(input)
' Initialize a counter to track the number of QR codes
Dim count As Integer = 1
' Loop through each discovered QR code
For Each result In results
Console.WriteLine($"QR {count}: {result.Value}")
count += 1
Next掃描輸入

主控台輸出
透過 IronQR 掃描照片後,咖啡館網站的 URL 會被記錄下來。

偵測模型模式
偵測模型模式專門以機器學習偵測 QR Code 在影像中的位置,適用於空間分析和電腦視覺。其座標系統的原點為 (0, 0)。
一般 QR Code 掃描器使用仰賴三個定位圖樣的標準解碼演算法,而此模型會擷取 ID 的四個頂點。您可逐一存取 result 物件的 Points 集合,取得位置資料。
程式碼沿用上一個範例的杯子照片,示範如何偵測影像中的位置座標。
using System;
using IronQr;
using IronQr.Enum;
using IronSoftware.Drawing;
using System.Linq;
// Load the image
var bmp = AnyBitmap.FromFile("cup.png");
// Create scan input using Detection Model mode
var input = new QrImageInput(bmp, QrScanMode.OnlyDetectionModel);
// Read QR code
var result = new QrReader().Read(input).FirstOrDefault();
// Print position data
if (result != null)
{
if (result.Points != null)
{
foreach (var point in result.Points)
{
Console.WriteLine($"Point: X={point.X}, Y={point.Y}");
}
}
}using System;
using IronQr;
using IronQr.Enum;
using IronSoftware.Drawing;
using System.Linq;
// Load the image
var bmp = AnyBitmap.FromFile("cup.png");
// Create scan input using Detection Model mode
var input = new QrImageInput(bmp, QrScanMode.OnlyDetectionModel);
// Read QR code
var result = new QrReader().Read(input).FirstOrDefault();
// Print position data
if (result != null)
{
if (result.Points != null)
{
foreach (var point in result.Points)
{
Console.WriteLine($"Point: X={point.X}, Y={point.Y}");
}
}
}Imports System
Imports IronQr
Imports IronQr.Enum
Imports IronSoftware.Drawing
Imports System.Linq
' Load the image
Dim bmp = AnyBitmap.FromFile("cup.png")
' Create scan input using Detection Model mode
Dim input = New QrImageInput(bmp, QrScanMode.OnlyDetectionModel)
' Read QR code
Dim result = (New QrReader()).Read(input).FirstOrDefault()
' Print position data
If result IsNot Nothing Then
If result.Points IsNot Nothing Then
For Each point In result.Points
Console.WriteLine($"Point: X={point.X}, Y={point.Y}")
Next
End If
End IfImports System
Imports IronQr
Imports IronQr.Enum
Imports IronSoftware.Drawing
Imports System.Linq
' Load the image
Dim bmp = AnyBitmap.FromFile("cup.png")
' Create scan input using Detection Model mode
Dim input = New QrImageInput(bmp, QrScanMode.OnlyDetectionModel)
' Read QR code
Dim result = (New QrReader()).Read(input).FirstOrDefault()
' Print position data
If result IsNot Nothing Then
If result.Points IsNot Nothing Then
For Each point In result.Points
Console.WriteLine($"Point: X={point.X}, Y={point.Y}")
Next
End If
End If標有角點的杯子影像

標有角點的主控台輸出

掃描方法比較
功能 (QrScanMode) | 基本掃描(OnlyBasicScan) | 自動掃描(Auto) | 偵測模型(OnlyDetectionModel) |
|---|---|---|---|
| 輸出 | 解碼後的文字(Value) | 解碼後的文字(Value) | 座標(Points) |
| 演算法 | 僅使用傳統演算法 | 混合(機器學習與傳統演算法) | 僅限機器學習 |
| 重點 | 速度 | 可讀性/資料擷取 | 定位/電腦視覺 |
結論
IronQR 的三種掃描模式可靈活應對各種 QR Code 讀取情境:
- 自動掃描: 適合一般用途,盡可能提高準確性與可靠性
- 基本掃描: 可快速有效地掃描品質良好的影像
- 偵測模型: 可取得位置資訊,適合空間分析等應用
了解各種模式的優勢後,您可以依需求最佳化 QR Code 的讀取方式。IronQR 能滿足完整擷取資料、快速處理或精確定位等需求。
如需更多範例和詳細的 API 資訊,請參閱 IronQR 文件或瀏覽 GitHub 上的程式碼範例。
常見問題
IronQR中有哪些用於讀取QR碼的不同掃描模式?
IronQR提供三種掃描模式:自動掃描、僅基本掃描和檢測模型。自動掃描結合了機器學習和傳統算法以確保精度,僅基本掃描使用影像處理以確保清晰度和速度,而檢測模型則利用機器學習進行位置檢測。
IronQR中的自動掃描模式如何運作?
IronQR中的自動掃描模式結合了機器學習檢測和基本掃描算法。這是最具多功能性的選擇,在各種影像條件下提供最佳的QR碼讀取結果。
IronQR中的僅基本掃描模式的目的是什麼?
僅基本掃描模式使用傳統影像處理技術快速且有效地掃描QR碼,尤其是在它們清晰可見且位置良好的情況下。
我應該何時使用IronQR中的檢測模型模式?
檢測模型模式非常適合空間分析和電腦視覺應用,因為它利用機器學習檢測QR碼位置,並提供座標數據以進行精確定位。
IronQR的僅基本掃描模式能讀取損壞的QR碼嗎?
是的,IronQR的僅基本掃描模式可以讀取損壞的QR碼,只要三個角落方框清晰可見。
為什麼IronQR將自動掃描模式視為預設模式?
IronQR將自動掃描模式視為預設模式,因為它通過結合機器學習和基本算法來處理各種QR碼掃描場景,提供最高的可靠性。
IronQR中的每種QrScanMode提供什麼樣的輸出?
自動和僅基本掃描模式輸出解碼文本(值),而檢測模型提供檢測到的QR碼位置的座標數據(點)。
如何使用IronQR從一張圖片中讀取多個QR碼?
使用自動掃描模式,您可以從一張圖片中讀取多個QR碼,方法是掃描圖片並遍歷結果列表來存取每個發現的QR碼的文本。
檢測模型模式在其方法上有何獨特之處?
檢測模型模式專門使用機器學習來定位QR碼位置,非常適合需要關於QR碼位置詳細空間數據的應用程序。
Ahmad 是一位全端開發人員,精通 C#、Python 和 Web 技術。他對建立可擴展的軟體解決方案有著濃厚的興趣,並樂於探索如何在實際應用中實現設計與功能的完美結合。