How to Fix Barcode Orientation in C# | IronBarcode

How to Fix Barcode Orientation

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

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

as-heading:2(快速入门:单行代码自动旋转图像校正)

您可以这样简单地纠正方向:使用 IronBarcode 的 AutoRotate 选项(默认启用)单行代码,即使图像旋转也能准确读取条码。

Nuget IconGet started making PDFs with NuGet now:

  1. Install IronBarcode with NuGet Package Manager

    PM > Install-Package BarCode

  2. Copy and run this code snippet.

    var result = IronBarCode.BarcodeReader.Read("rotatedImage.png", new IronBarCode.BarcodeReaderOptions { AutoRotate = true });
  3. Deploy to test on your live environment

    Start using IronBarcode in your project today with a free trial
    arrow pointer
class="hsg-featured-snippet">

最小化工作流程(5个步骤)

  1. 下载 C# 库来修复条码方向
  2. AutoRotate 属性设置为 true
  3. 导入目标条码和二维码
  4. 使用读取选项读取条码和二维码
  5. 检索所得条码值

修正条码方向示例

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

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

class="competitors-section__wrapper-even-1">
20° Rotation
45° Rotation

代码

: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

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

常见问题解答

什么是条形码方向及其重要性是什么?

条形码方向指的是条形码显示或打印在产品或文档上的角度。正确的方向对于准确的条形码检测和读取至关重要,因为非标准方向会使这些过程复杂化。

我如何在 .NET C# 中校正条形码方向?

您可以通过使用 IronBarcode 的自动方向校正功能在 .NET C# 中校正条形码方向。这涉及将 BarcodeReaderOptions 中的 AutoRotate 属性设置为 true,该属性默认启用。

修复条形码方向的步骤是什么?

要使用 C# 库修复条形码方向,请从 NuGet 下载 IronBarcode,将 AutoRotate 属性设置为 true,导入条形码和二维码,使用库读取它们,并获取结果值。

我可以看到如何实现自动方向校正的示例吗?

可以,文章提供了一个 C# 示例,以展示如何使用 IronBarcode 实现自动方向校正。它涉及在条形码读取选项中将 AutoRotate 属性设置为 true。

是否有可用于测试条形码方向校正的资源?

是的,文章提供了 20° 和 45° 旋转的样本图片,用于测试条形码方向校正。这些图片可以直接从网页下载以供练习。

如果校正条形码方向不够,我该怎么办?

如果仅靠校正方向还不够,您可能需要应用图像滤镜。文章建议学习如何使用图像校正滤镜以增强条形码读取能力。

为什么我需要下载 C# 库来校正条形码方向?

下载像 IronBarcode 这样的 C# 库是实现条形码方向校正所必需的,因为它提供了处理各种条形码方向所需的工具和选项,例如 AutoRotate 功能。

设置 AutoRotate 属性如何帮助读取条形码?

在 IronBarcode 的 BarcodeReaderOptions 中将 AutoRotate 属性设置为 true,可实现自动方向校正,确保条形码无论初始方向如何都能被正确读取。

Chaknith Bin
软件工程师
Chaknith 在 IronXL 和 IronBarcode 工作。他在 C# 和 .NET 方面有着深厚的专业知识,帮助改进软件并支持客户。他从用户互动中获得的见解有助于更好的产品、文档和整体体验。
准备开始了吗?
Nuget 下载 1,935,276 | 版本: 2025.11 刚刚发布