How to Fix Barcode Orientation

Barcode orientation refers to the angle at which a barcode is printed or displayed on a product or document. It can be adjusted to various angles to fit different layout and design requirements. The most common orientation is horizontal, where the barcode is aligned from left to right, which is the standard and most widely used format. Any non-zero orientation degree poses a challenge for libraries to detect and retrieve the value. IronBarcode offers automatic orientation correction to detect any non-zero orientations for barcodes and QR codes.

Get started with IronBarcode

Start using IronBarcode in your project today with a free trial.

First Step:
green arrow pointer


Fix Barcode Orientation Example

To apply automatic orientation correction, set the AutoRotate property in BarcodeReaderOptions to true. This property is set to true by default, so you should not have to do anything. Reading any non-zero oriented barcode image should work out of the box.

Let's use the image below as our sample. Download the following 20° rotation and 45° rotation sample images.

20° Rotation
45° Rotation

Code

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

// Create an instance of BarcodeReaderOptions to configure the barcode reading process
BarcodeReaderOptions myOptionsExample = new BarcodeReaderOptions
{
    // Enable auto-rotation to allow barcodes to be read when they are not perfectly aligned
    AutoRotate = true
};

// Read the barcode from the specified image file using the configured options
var results = BarcodeReader.Read("rotate20.png", myOptionsExample);

// Check if any barcodes were detected before trying to access the results
if (results.Length > 0)
{
    // Print out the value of the first detected barcode
    Console.WriteLine(results[0].Value);
}
else
{
    // Print a message if no barcode was detected
    Console.WriteLine("No barcode detected.");
}
Imports IronBarCode
Imports System

' Create an instance of BarcodeReaderOptions to configure the barcode reading process
Private myOptionsExample As New BarcodeReaderOptions With {.AutoRotate = True}

' Read the barcode from the specified image file using the configured options
Private results = BarcodeReader.Read("rotate20.png", myOptionsExample)

' Check if any barcodes were detected before trying to access the results
If results.Length > 0 Then
	' Print out the value of the first detected barcode
	Console.WriteLine(results(0).Value)
Else
	' Print a message if no barcode was detected
	Console.WriteLine("No barcode detected.")
End If
$vbLabelText   $csharpLabel

In many cases, correcting rotation may not be sufficient, and a filter is required. Learn how to use image filters in the following article: "How to use Image Correction Filters."

Frequently Asked Questions

What is barcode orientation?

Barcode orientation refers to the angle at which a barcode is printed or displayed on a product or document. The most common orientation is horizontal, where the barcode is aligned from left to right.

How does IronBarcode handle different barcode orientations?

IronBarcode offers automatic orientation correction to detect and correct any non-zero orientations for barcodes and QR codes. This ensures accurate barcode reading regardless of the initial orientation.

How can I enable automatic orientation correction in IronBarcode?

To enable automatic orientation correction, set the AutoRotate property in BarcodeReaderOptions to true. This property is set to true by default, so it should work out of the box for most applications.

Is there a sample code to demonstrate barcode orientation correction?

Yes, the webpage provides a C# example code that demonstrates how to automatically correct the orientation of barcodes using IronBarcode. You can find it under the 'Code' section.

Are there sample images provided for testing orientation correction?

Yes, the webpage provides sample images with 20° and 45° rotations for testing barcode orientation correction. These images can be downloaded directly from the webpage.

Do I need to download a library to fix barcode orientation?

Yes, you need to download the C# library from NuGet to fix barcode orientation using IronBarcode.

What steps are involved in fixing barcode orientation using IronBarcode?

The steps include downloading the C# library, setting the AutoRotate property to true, importing the targeted barcodes and QR codes, reading them with the reading option, and retrieving the resulting barcode value.

Can image filters help with barcode reading?

Yes, in some cases, correcting rotation may not be sufficient, and a filter is required. The webpage suggests learning how to use image correction filters for better barcode reading.

Chaknith related to Code
Software Engineer
Chaknith is the Sherlock Holmes of developers. It first occurred to him he might have a future in software engineering, when he was doing code challenges for fun. His focus is on IronXL and IronBarcode, but he takes pride in helping customers with every product. Chaknith leverages his knowledge from talking directly with customers, to help further improve the products themselves. His anecdotal feedback goes beyond Jira tickets and supports product development, documentation and marketing, to improve customer’s overall experience.When he isn’t in the office, he can be found learning about machine learning, coding and hiking.