How to Read QR Codes from Images in C
IronQR允許開發者在C#中從各種圖像格式讀取QR碼,方法是使用BarcodeReader.Read方法高效解碼QR資料。
快速入門:在C#中從圖像讀取QR碼
如何從圖像中讀取QR碼
- 下載C#程式庫以從圖像中讀取QR碼
- 使用
IronDrawing導入圖像資料 - 從圖像資料建立
QrImageInput物件 - 將物件傳遞給
Read方法 - 遍歷每個檢測到的QR碼並查看其資訊
如何從不同的圖像格式中讀取QR碼?
IronQR提供從各種圖像格式讀取QR碼的內建支持。 此功能使用先進的機器學習模型,以確保在不同媒體型別中準確解碼。支持的格式包括:
- 聯合攝影專家組 (JPEG)
- 可攜式網路圖形 (PNG)
- 圖像間交換格式 (GIF)
- 標記圖像文件格式 (TIFF)
- 點陣圖圖像文件 (BMP)
- WBMP
- WebP
- 圖標 (ico)
- WMF
RawFormat(raw)
這種格式支持由IronDrawing開源庫啟用,可以高效地處理圖像。 您可以直接從數碼相機、掃描儀、移動裝置或網頁下載處理QR碼,無需格式轉換。
-
使用NuGet套件管理器安裝https://www.nuget.org/packages/IronQR
-
複製並運行這段程式碼片段。
// Import necessary IronQR and IronDrawing namespaces using IronSoftware.Drawing; using IronBarcode; public class QRCodeReader { public static void Main() { // Load an image from a file path using (var inputImage = Image.FromFile("path/to/your/image/file.webp")) { // Create a QrImageInput object from the image var qrImageInput = new QrImageInput(inputImage); // Decode the QR碼 from the image var result = BarcodeReader.Read(qrImageInput); // Iterate through each detected QR碼 and display its information foreach (var barcodeResult in result.Barcodes) { Console.WriteLine($"QR Code Data: {barcodeResult.Value}"); } } } } -
部署以在您的實時環境中測試
今天就開始在您的專案中使用IronQR,透過免費試用
注意:用您的QR碼圖像文件的實際路徑替換"path/to/your/image/file.webp"。
想知道範例圖像中的QR碼值嗎?嘗試使用程式碼片段!
為什麼IronQR支持多種圖像格式?
讀取QR碼指的是掃描並解碼儲存在QR碼中的資訊。 這通常是通過攝像頭或掃描儀以及能夠解釋QR碼資料的軟體完成的。 QR碼中的資訊可能是文字、URL、聯繫方式或其他資料形式。
IronQR的多格式支持對於現實應用至關重要,因為QR碼出現在各種背景中——從營銷素材和產品包裝到數位文件和網頁內容。 通過支持多種格式,IronQR確保開發者能構建健壯的應用程式,而無需擔心圖像格式的不相容性。 了解更多有關IronQR的讀取能力,了解這種靈活性如何改善您的開發工作流程。
何時應使用每種圖像格式?
不同的圖像格式在QR碼處理中有不同的用途:
- PNG:適合需要透明度或圖像質量至關重要的QR碼。 PNG的無損壓縮確保QR碼圖案保持清晰可讀。
- JPEG:適合包含QR碼的照片或有文件大小限制時使用。 使用較高的質量設置(80%以上),以防壓縮造成的壓縮失真影響可讀性。
- TIFF:適合用於檔案目的或企業環境中的掃描文件。
- WebP:現代格式,提供出色的壓縮和質量保留,適合網頁應用程式。
為了在任何格式中取得最佳結果,確保您的圖像保持足夠的解析度(印刷QR碼至少300 DPI)和對比度。查看我們的高級QR讀取範例以獲得格式特定的優化技巧。
如果圖像質量不好會怎樣?
IronQR包含容錯功能來處理不完美的圖像。 處理低質量圖像時,程式庫採用多種策略:
- 錯誤更正:QR碼包含錯誤更正能力(L、M、Q、H級別),即使最多30%的碼損壞也能恢復資料。
- 機器學習增強:IronQR的ML模型檢測並補償常見問題,如模糊、失真和光線不足。
- 預處理:自動圖像增強在解碼嘗試之前提高對比度和銳度。
對於具有挑戰性的情況,考慮使用自定義QR讀取模式選項來微調讀取過程:
// Example: Reading QR碼s with enhanced error correction
using IronBarcode;
public class EnhancedQRReader
{
public static void ReadPoorQualityImage()
{
// Configure reader with multiple attempts and error correction
var options = new BarcodeReaderOptions
{
Speed = ReadingSpeed.Detailed, // More thorough scanning
ExpectMultipleBarcodes = true, // Check for multiple codes
ExpectBarcodeTypes = BarcodeType.QRCode // Focus on QR碼s only
};
using (var inputImage = Image.FromFile("blurry_qr_code.jpg"))
{
var qrImageInput = new QrImageInput(inputImage);
var results = BarcodeReader.Read(qrImageInput, options);
foreach (var result in results.Barcodes)
{
Console.WriteLine($"Decoded: {result.Value}");
Console.WriteLine($"Confidence: {result.Confidence}%");
}
}
}
}
// Example: Reading QR碼s with enhanced error correction
using IronBarcode;
public class EnhancedQRReader
{
public static void ReadPoorQualityImage()
{
// Configure reader with multiple attempts and error correction
var options = new BarcodeReaderOptions
{
Speed = ReadingSpeed.Detailed, // More thorough scanning
ExpectMultipleBarcodes = true, // Check for multiple codes
ExpectBarcodeTypes = BarcodeType.QRCode // Focus on QR碼s only
};
using (var inputImage = Image.FromFile("blurry_qr_code.jpg"))
{
var qrImageInput = new QrImageInput(inputImage);
var results = BarcodeReader.Read(qrImageInput, options);
foreach (var result in results.Barcodes)
{
Console.WriteLine($"Decoded: {result.Value}");
Console.WriteLine($"Confidence: {result.Confidence}%");
}
}
}
}
Imports IronBarcode
Public Class EnhancedQRReader
Public Shared Sub ReadPoorQualityImage()
' Configure reader with multiple attempts and error correction
Dim options As New BarcodeReaderOptions With {
.Speed = ReadingSpeed.Detailed, ' More thorough scanning
.ExpectMultipleBarcodes = True, ' Check for multiple codes
.ExpectBarcodeTypes = BarcodeType.QRCode ' Focus on QR碼s only
}
Using inputImage = Image.FromFile("blurry_qr_code.jpg")
Dim qrImageInput As New QrImageInput(inputImage)
Dim results = BarcodeReader.Read(qrImageInput, options)
For Each result In results.Barcodes
Console.WriteLine($"Decoded: {result.Value}")
Console.WriteLine($"Confidence: {result.Confidence}%")
Next
End Using
End Sub
End Class
從QR碼中檢索值
大多數IronQR功能返回一個集合以支持多次檢測。 由於Value屬性。 這個範例程式碼特別選擇集合中的第一個Value。
:path=/static-assets/qr/content-code-examples/how-to/read-qr-code-image-value.cs
using System;
using System.Collections.Generic;
using System.Linq;
using IronQr;
using System.Drawing;
// Import image
var inputImage = Image.FromFile("sample.jpg");
// Load the asset into QrImageInput
QrImageInput imageInput = new QrImageInput(inputImage);
// Create a QR Reader object
QrReader reader = new QrReader();
// Read the Input an get all embedded QR Codes
IEnumerable<QrResult> results = reader.Read(imageInput);
// Display the value of the first QR code found
Console.WriteLine($"QR code value is {results.First().Value}");
Imports System
Imports System.Collections.Generic
Imports System.Linq
Imports IronQr
Imports System.Drawing
' Import image
Dim inputImage As Image = Image.FromFile("sample.jpg")
' Load the asset into QrImageInput
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 value of the first QR code found
Console.WriteLine($"QR code value is {results.First().Value}")
在圖像中檢測QR碼位置
IronQR不僅僅是簡單解碼,還能準確定位QR碼在圖像中的位置。 該定位使用標準坐標系統,其中PointF (0,0)表示圖像的左上角。 QR碼角的精確空間坐標可以通過Points[]陣列存取。
在範例中,檢測到的QR碼四個點的坐標被檢索並列印到控制台。
:path=/static-assets/qr/content-code-examples/how-to/read-qr-code-image-position.cs
using System;
using System.Collections.Generic;
using IronQr;
using System.Drawing;
using System.Linq;
// Import an image containing a QR code
var inputImage = Image.FromFile("urlQr.png");
// Load the asset into a QrImageInput object
var imageInput = new QrImageInput(inputImage);
// Create a QR Reader object
var reader = new QrReader();
// Read the input and get all embedded QR codes
IEnumerable<QrResult> results = reader.Read(imageInput);
// [TL, TR, BL, BR]
string[] labels = { "Top-Left", "Top-Right", "Bottom-Left", "Bottom-Right" };
var points = results.First().Points;
for (int i = 0; i < points.Length; i++)
{
Console.WriteLine($"{labels[i]}: {points[i].X}, {points[i].Y}");
}
Imports System
Imports System.Collections.Generic
Imports IronQr
Imports System.Drawing
Imports System.Linq
' Import an image containing a QR code
Dim inputImage As Image = Image.FromFile("urlQr.png")
' 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)
' [TL, TR, BL, BR]
Dim labels As String() = { "Top-Left", "Top-Right", "Bottom-Left", "Bottom-Right" }
Dim points = results.First().Points
For i As Integer = 0 To points.Length - 1
Console.WriteLine($"{labels(i)}: {points(i).X}, {points(i).Y}")
Next
輸入QR碼
輸出
注意系統已將每個QR碼角的精確空間坐標記錄到控制台中。
支持的QR碼型別
支持多種QR碼型別,以便建立和讀取。 IronQR為各種QR碼格式提供全面支持,以滿足多樣的應用需求。 在我們的文件中了解更多關於支持的QR格式的資訊。 以下是支持的QR碼型別:
QRCode:今天最常用的標準QR碼。 它可以儲存多達7,089個數字字元或4,296個字母數字字元,適合網站URL、聯繫資訊和其他應用。
MicroQRCode:小於標準QR碼,適合有限空間。 它可以儲存多達35個數字字元或21個字母數字字元,適合小包裝或小型印刷標籤。
RMQRCode:RMQR Code (長方形微型QR碼)是長方形版本而非正方形。 這個版本允許在長寬比上具有靈活性,適用於提供長方形空間的應用。
如何選擇合適的QR碼型別?
選擇合適的QR碼型別取決於具體的使用案例和限制條件:
-
標準QR碼:選擇它進行通用應用程式,當空間不受限制且需要最大資料容量時。 完美適合URL、WiFi憑證、vCard聯繫方式或詳細產品資訊。 查看我們的QR碼生成範例以了解實施細節。
-
微型QR碼:理想於處理小表面,如電子元件、珠寶標籤或醫療裝置。 儘管容量有限,但它是序列號、簡單URL或基本追蹤碼的完美選擇。
- RMQR碼:在可用空間具有特定尺寸限制時選擇長方形碼,如圓柱產品上的窄標籤或包裝邊緣上的拉長空間。
資料儲存限制是什麼?
了解資料容量有助於優化您的QR碼實施:
| QR碼型別 | 僅限數字 | 字母數字 | 二進制 | 漢字 |
|---|---|---|---|---|
| 標準QR | 7,089 | 4,296 | 2,953 | 1,817 |
| 微QR | 35 | 21 | 15 | 9 |
| RMQR | 可變 | 可變 | 可變 | 可變 |
考慮這些因素,計劃資料儲存時:
- 使用URL縮短工具最大化可用空間
- 用於大資料集實施資料壓縮
- 選擇適當的錯誤更正級別(較高的更正會減少容量)
對於高級實施,請探索我們的樣式QR碼生成指南,以平衡美學與資料容量。
何時應使用微型或RMQR碼?
微型和RMQR碼在特定情況下表現出色:
微QR碼最適合:
- 需要元件跟蹤的電子電路板
- 需要患者或藥物身份標識的小型醫療裝置
- 限制雕刻空間的珠寶認證
- 製造業中的小型產品標籤
RMQR碼最適合:
- 管子或管道上的窄運輸標籤
- 筆杆或工具上的拉長空間
- 橫幅式營銷材料
- 合併到現有長方形設計元素中
這是一個讀取不同QR碼型別的實用範例:
using IronBarcode;
using IronSoftware.Drawing;
public class MultiTypeQRReader
{
public static void ReadVariousQRTypes()
{
// Configure reader to handle all QR碼 types
var options = new BarcodeReaderOptions
{
ExpectBarcodeTypes = BarcodeType.QRCode |
BarcodeType.MicroQRCode |
BarcodeType.RectangularMicroQRCode
};
string[] imagePaths = {
"standard_qr.png",
"micro_qr.png",
"rectangular_qr.png"
};
foreach (var path in imagePaths)
{
using (var image = Image.FromFile(path))
{
var qrInput = new QrImageInput(image);
var results = BarcodeReader.Read(qrInput, options);
foreach (var qr in results.Barcodes)
{
Console.WriteLine($"Type: {qr.BarcodeType}");
Console.WriteLine($"Data: {qr.Value}");
Console.WriteLine($"Format: {qr.Format}");
Console.WriteLine("---");
}
}
}
}
}
using IronBarcode;
using IronSoftware.Drawing;
public class MultiTypeQRReader
{
public static void ReadVariousQRTypes()
{
// Configure reader to handle all QR碼 types
var options = new BarcodeReaderOptions
{
ExpectBarcodeTypes = BarcodeType.QRCode |
BarcodeType.MicroQRCode |
BarcodeType.RectangularMicroQRCode
};
string[] imagePaths = {
"standard_qr.png",
"micro_qr.png",
"rectangular_qr.png"
};
foreach (var path in imagePaths)
{
using (var image = Image.FromFile(path))
{
var qrInput = new QrImageInput(image);
var results = BarcodeReader.Read(qrInput, options);
foreach (var qr in results.Barcodes)
{
Console.WriteLine($"Type: {qr.BarcodeType}");
Console.WriteLine($"Data: {qr.Value}");
Console.WriteLine($"Format: {qr.Format}");
Console.WriteLine("---");
}
}
}
}
}
Imports IronBarcode
Imports IronSoftware.Drawing
Public Class MultiTypeQRReader
Public Shared Sub ReadVariousQRTypes()
' Configure reader to handle all QR碼 types
Dim options As New BarcodeReaderOptions With {
.ExpectBarcodeTypes = BarcodeType.QRCode Or
BarcodeType.MicroQRCode Or
BarcodeType.RectangularMicroQRCode
}
Dim imagePaths As String() = {
"standard_qr.png",
"micro_qr.png",
"rectangular_qr.png"
}
For Each path In imagePaths
Using image = Image.FromFile(path)
Dim qrInput As New QrImageInput(image)
Dim results = BarcodeReader.Read(qrInput, options)
For Each qr In results.Barcodes
Console.WriteLine($"Type: {qr.BarcodeType}")
Console.WriteLine($"Data: {qr.Value}")
Console.WriteLine($"Format: {qr.Format}")
Console.WriteLine("---")
Next
End Using
Next
End Sub
End Class
對於生產部署,請查看我們的NuGet軟體包指南以確保您擁有適合您平台的正確軟體包,並查看API參考以獲取所有可用方法和屬性的詳細文件。
常見問題
IronQR支持哪些圖片格式以讀取QR codes?
IronQR支持從多種圖片格式讀取QR codes,包括JPEG,PNG,GIF,TIFF,BMP,WBMP,WebP,ICO,WMF和RawFormat。這種全面的格式支持由IronDrawing提供,允許您處理來自不同來源的QR codes,而無需進行格式轉換。
如何在C#中從圖片文件中讀取QR code?
要使用IronQR讀取QR code,首先使用Image.FromFile()載入您的圖片,從載入的圖片建立QrImageInput物件,然後使用BarcodeReader.Read()來解碼QR資料。該方法返回的結果可讓您遍歷每個檢測到的QR code的資訊。
哪種技術可實現跨多媒體型別的準確QR code讀取?
IronQR使用先進的機器學習模型,以確保跨多媒體型別和圖片格式的準確QR code解碼。這種人工智慧驅動的方法有助於即使在具有挑戰性的圖片條件下也能保持高精確度。
我可以從單一圖片中讀取多個QR codes嗎?
可以,IronQR可以檢測並讀取來自單一圖片的多個QR codes。BarcodeReader.Read方法返回一個結果集合,允許您使用foreach迴圈遍歷每個檢測到的QR code以存取單獨的QR code資料。
IronQR讀取的QR codes可以儲存哪些型別的資料?
IronQR可以解碼儲存在QR codes中的各種型別的資料,包括純文字、URLs、聯絡資訊和其他形式的結構化資料。解碼的資訊可通過每個BarcodeResult物件的Value屬性存取。

