How to Fix Barcode Orientation in C# | IronBarcode

如何在 C# 中修复条形码方向</#35;

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

IronBarcode 通过其内置的 AutoRotate 功能自动校正条形码方向,该功能可检测并读取任意角度的条形码,无需手动旋转图像,确保即使面对倾斜或旋转的图像也能准确读取条形码。

条码方向是指条码在产品或文件上的打印或显示角度。 可以调整到不同的角度以适应各种布局和设计要求。 最常见的方向是水平方向,条码从左至右对齐,这是标准且最广泛使用的格式。 任何非零的方向度数对库检测和获取值来说都是一个挑战。 IronBarcode 提供自动方向校正,检测任何非零方向的条码和二维码。

快速入门:一行代码实现图像自动旋转校正

调整方向非常简单:只需一行代码,利用 IronBarcode 的 AutoRotate 选项(默认启用),即使图像被旋转,也能准确读取 BarCode。

  1. 使用 NuGet 包管理器安装 https://www.nuget.org/packages/BarCode

    PM > Install-Package BarCode
  2. 复制并运行这段代码。

    var result = IronBarCode.BarcodeReader.Read("rotatedImage.png", new IronBarCode.BarcodeReaderOptions { AutoRotate = true });
  3. 部署到您的生产环境中进行测试

    通过免费试用立即在您的项目中开始使用IronBarcode

    arrow pointer

如何修复应用程序中的 BarCode 方向?

若要应用自动方向校正,请将 AutoRotate 中的 BarcodeReaderOptions 属性设置为 true。 默认情况下,该属性设置为 true,因此您无需进行任何操作。 读取任何非零方向的条码图像应开箱即用。

AutoRotate 功能在处理各种 BARCODE 格式(包括 QR 码、数据矩阵和传统线性 BARCODE)时尤为实用。 无论您是从图像中读取条形码,还是从 PDF 文档中扫描,方向校正都能确保可靠的结果。

让我们使用下面的图像作为示例。 Download the following 20° rotation and 45° rotation sample images.

Barcode rotated 20 degrees clockwise showing vertical stripes at an angle for orientation testing
Barcode rotated 45 degrees showing diagonal orientation requiring correction

实现自动旋转需要哪些代码?

:path=/static-assets/barcode/content-code-examples/how-to/image-orientation-correct-autorotate.cs
using IronBarCode;
using System;

BarcodeReaderOptions myOptionsExample = new BarcodeReaderOptions()
{
    // Turn on auto rotation in ML detection
    AutoRotate = true,
};

var results = BarcodeReader.Read("rotate20.png", myOptionsExample);

// Print out the value
Console.WriteLine(results[0].Value);
Imports IronBarCode
Imports System

Private myOptionsExample As New BarcodeReaderOptions() With {.AutoRotate = True}

Private results = BarcodeReader.Read("rotate20.png", myOptionsExample)

' Print out the value
Console.WriteLine(results(0).Value)
$vbLabelText   $csharpLabel

AutoRotate 功能利用先进的机器学习算法自动检测 BARCODE 的方向。 在处理单张图像中的多个条形码或处理不同方向的成批图像时,这一点尤为重要。

使用不同的旋转角度

IronBarcode 的方向校正功能可无缝处理各种旋转角度。 下面是一个演示以不同旋转角度读取 BarCode 的示例:

using IronBarCode;
using System;
using System.Collections.Generic;

// Process multiple rotated images
var rotatedImages = new List<string> { "rotate20.png", "rotate45.png", "rotate90.png" };
var options = new BarcodeReaderOptions
{
    AutoRotate = true,
    // Combine with other reading optimizations
    Speed = ReadingSpeed.Balanced,
    ExpectMultipleBarcodes = false
};

foreach (var imagePath in rotatedImages)
{
    var results = BarcodeReader.Read(imagePath, options);
    if (results.Length > 0)
    {
        Console.WriteLine($"Image: {imagePath} - Barcode Value: {results[0].Value}");
        Console.WriteLine($"Barcode Type: {results[0].BarcodeType}");
        Console.WriteLine($"Rotation Applied: {results[0].WasRotated}");
    }
}
using IronBarCode;
using System;
using System.Collections.Generic;

// Process multiple rotated images
var rotatedImages = new List<string> { "rotate20.png", "rotate45.png", "rotate90.png" };
var options = new BarcodeReaderOptions
{
    AutoRotate = true,
    // Combine with other reading optimizations
    Speed = ReadingSpeed.Balanced,
    ExpectMultipleBarcodes = false
};

foreach (var imagePath in rotatedImages)
{
    var results = BarcodeReader.Read(imagePath, options);
    if (results.Length > 0)
    {
        Console.WriteLine($"Image: {imagePath} - Barcode Value: {results[0].Value}");
        Console.WriteLine($"Barcode Type: {results[0].BarcodeType}");
        Console.WriteLine($"Rotation Applied: {results[0].WasRotated}");
    }
}
Imports IronBarCode
Imports System
Imports System.Collections.Generic

' Process multiple rotated images
Dim rotatedImages As New List(Of String) From {"rotate20.png", "rotate45.png", "rotate90.png"}
Dim options As New BarcodeReaderOptions With {
    .AutoRotate = True,
    ' Combine with other reading optimizations
    .Speed = ReadingSpeed.Balanced,
    .ExpectMultipleBarcodes = False
}

For Each imagePath In rotatedImages
    Dim results = BarcodeReader.Read(imagePath, options)
    If results.Length > 0 Then
        Console.WriteLine($"Image: {imagePath} - Barcode Value: {results(0).Value}")
        Console.WriteLine($"Barcode Type: {results(0).BarcodeType}")
        Console.WriteLine($"Rotation Applied: {results(0).WasRotated}")
    End If
Next
$vbLabelText   $csharpLabel

性能考虑

虽然 AutoRotate 默认处于启用状态,但了解其对性能的影响有助于优化您的 BarCode 读取工作流程。 该功能可与 IronBarcode 的读取速度选项有效配合,使您能够根据应用程序的需求在准确性和性能之间取得平衡。

对于需要高速处理的应用程序,您可以将 AutoRotate 与其他优化技术结合使用:

var fastReadOptions = new BarcodeReaderOptions
{
    AutoRotate = true,
    Speed = ReadingSpeed.Faster,
    // Specify expected barcode types to improve performance
    ExpectBarcodeTypes = BarcodeEncoding.QRCode | BarcodeEncoding.Code128,
    // Define crop region if barcode location is predictable
    CropArea = new System.Drawing.Rectangle(100, 100, 300, 300)
};
var fastReadOptions = new BarcodeReaderOptions
{
    AutoRotate = true,
    Speed = ReadingSpeed.Faster,
    // Specify expected barcode types to improve performance
    ExpectBarcodeTypes = BarcodeEncoding.QRCode | BarcodeEncoding.Code128,
    // Define crop region if barcode location is predictable
    CropArea = new System.Drawing.Rectangle(100, 100, 300, 300)
};
Dim fastReadOptions As New BarcodeReaderOptions With {
    .AutoRotate = True,
    .Speed = ReadingSpeed.Faster,
    ' Specify expected barcode types to improve performance
    .ExpectBarcodeTypes = BarcodeEncoding.QRCode Or BarcodeEncoding.Code128,
    ' Define crop region if barcode location is predictable
    .CropArea = New System.Drawing.Rectangle(100, 100, 300, 300)
}
$vbLabelText   $csharpLabel

集成图像校正功能

AutoRotate 可与 IronBarcode 的图像校正滤镜无缝配合使用。 在处理质量较差的图片时,如果图片还被旋转过,您可以进行多重校正:

var advancedOptions = new BarcodeReaderOptions
{
    AutoRotate = true,
    // Apply additional image corrections
    ImageFilters = new ImageFilterCollection
    {
        new AdaptiveThresholdFilter(),
        new BrightnessFilter(1.2f),
        new ContrastFilter(1.5f)
    }
};

var results = BarcodeReader.Read("low-quality-rotated-barcode.jpg", advancedOptions);
var advancedOptions = new BarcodeReaderOptions
{
    AutoRotate = true,
    // Apply additional image corrections
    ImageFilters = new ImageFilterCollection
    {
        new AdaptiveThresholdFilter(),
        new BrightnessFilter(1.2f),
        new ContrastFilter(1.5f)
    }
};

var results = BarcodeReader.Read("low-quality-rotated-barcode.jpg", advancedOptions);
Imports System

Dim advancedOptions As New BarcodeReaderOptions With {
    .AutoRotate = True,
    ' Apply additional image corrections
    .ImageFilters = New ImageFilterCollection From {
        New AdaptiveThresholdFilter(),
        New BrightnessFilter(1.2F),
        New ContrastFilter(1.5F)
    }
}

Dim results = BarcodeReader.Read("low-quality-rotated-barcode.jpg", advancedOptions)
$vbLabelText   $csharpLabel

方向校正的最佳实践

  1. 默认行为:由于 AutoRotate 默认处于启用状态,除非您之前已将其禁用或希望确保其处于活动状态,否则通常无需显式设置。

2.与裁剪区域相结合:使用裁剪区域提高性能时,请确保裁剪区域足够大,以容纳旋转后的条形码。

  1. 多线程处理AutoRotate 具有线程安全特性,可与异步及多线程操作良好配合,因此非常适合用于大规模 BARCODE 处理应用。

  2. 格式相关注意事项:虽然 AutoRotate 支持所有受支持的 BARCODE 格式,但某些格式(如 PDF417 和 Data Matrix)可能需要使用额外的格式特定选项。

在许多情况下,纠正旋转可能不够,还需要使用滤镜。 在以下文章中了解如何使用图像过滤器:"如何使用图像校正过滤器"。

常见问题解答

如何修复 C# 应用程序中旋转的 BarCode 图像?

IronBarcode 使用其内置的 AutoRotate 功能自动修复旋转的条形码图像。只需在 BarcodeReaderOptions(条码阅读器选项)中将 AutoRotate 设置为 true(默认情况下已启用),该库即可检测并读取任意角度的条码,而无需手动旋转。

哪些 BarCode 方向可以自动纠正?

IronBarcode 的自动旋转(AutoRotate)功能可检测并纠正任何非零度方向,包括 20°、45°、90°、180° 和 270°旋转。该功能适用于各种条码格式,包括 QR 码、数据矩阵和传统线性条码。

我需要编写特殊代码来处理倾斜条形码吗?

无需特殊代码。IronBarcode 的自动旋转(AutoRotate)属性默认已启用,因此方向校正功能开箱即用。您只需一行代码: var result = IronBarCode.BarcodeReader.Read("rotatedImage.png");

方向校正可以与 PDF 文档一起使用吗?

是的,IronBarcode 的自动旋转功能可在扫描 PDF 文档以及图像中的条形码时无缝工作。无论源格式如何,其方向校正功能都能确保可靠的结果。

自动方位检测采用的是什么技术?

IronBarcode 使用先进的机器学习算法自动检测条码方向。这种智能方法可确保准确读取条形码,即使是倾斜或旋转的图像,也无需人工干预。

在条形码操作中使用IronBarcode的好处是什么?

IronBarcode提供了易于集成、对多种条形码格式的支持、高质量的图像生成和强大的读取能力等好处,使其成为C#条形码操作的综合工具。

IronBarcode是否提供支持自定义条形码外观的功能?

是的,IronBarcode为条形码外观提供广泛的自定义选项,包括颜色、大小和文本注释,允许运您将条形码设计成符合您特定的设计要求。

IronBarcode如何帮助提高业务流程的效率?

IronBarcode通过快速准确的条码生成和读取提高了业务流程效率,减少了手动数据输入错误,并改善了库存和资产跟踪。

在项目中实现IronBarcode需要哪些编程技能?

了解C#编程的基础知识就足以在项目中实现IronBarcode,因为它提供了简单的方法和全面的文档来指导开发人员。

IronBarcode适合小项目和大型企业应用吗?

IronBarcode被设计为可扩展且多功能,适合需要强大条码解决方案的小项目和大型企业应用。

Curtis Chau
技术作家

Curtis Chau 拥有卡尔顿大学的计算机科学学士学位,专注于前端开发,精通 Node.js、TypeScript、JavaScript 和 React。他热衷于打造直观且美观的用户界面,喜欢使用现代框架并创建结构良好、视觉吸引力强的手册。

除了开发之外,Curtis 对物联网 (IoT) 有浓厚的兴趣,探索将硬件和软件集成的新方法。在空闲时间,他喜欢玩游戏和构建 Discord 机器人,将他对技术的热爱与创造力相结合。

准备开始了吗?
Nuget 下载 2,240,258 | 版本: 2026.5 just released
Still Scrolling Icon

还在滚动吗?

想快速获得证据? PM > Install-Package BarCode
运行示例 观看您的字符串变成 BarCode。