How to Output Data Formats

by Hairil Hasyimi Bin Omar

Instead of simply reading the barcode and printing the values in the console, IronBarcode offers much more. It provides several output formats that pave the way for users to further process the read results. These formats include properties such as barcode image, barcode type, BinaryValue, coordinates, height, width, page number, barcode, page orientation, text, and value.

Users can manipulate these properties further within the program. Let's explore how to use these properties and the use cases where they can be helpful.

Get started with IronBarcode

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

First Step:
green arrow pointer


Output Formats and Use Cases

BarcodeResult stores various useful properties. These properties are listed below:

  • BarcodeImage
  • BarcodeType
  • BinaryValue
  • Coordinates, Height & Width
  • PageNumber
  • Barcode and PageOrientation
  • Text & Value

Barcode Image

Once IronBarcode perform reading process on an image, the barcodes found in the image will be stored in BarcodeResult as BarcodeImage property of type AnyBitmap. The BarcodeImage property store the found barcode images. Users can retrieve this object to further process the image or save it as a permanent copy. This provides an efficient way and ease of use by eliminating the need to write additional codes to extract the barcode images from an image.

Let us look at the code snippet below that demonstrates a possible use case for this output format

:path=/static-assets/barcode/content-code-examples/how-to/output-data-formats-BarcodeImage.cs
using IronBarCode;
using IronSoftware.Drawing;
using System.Collections.Generic;

// Read barcode from PDF file
BarcodeResults result = BarcodeReader.ReadPdf("test.pdf");

// Create list for barcodes
List<AnyBitmap> barcodeList = new List<AnyBitmap>();

foreach (BarcodeResult barcode in result)
{
    barcodeList.Add(barcode.BarcodeImage);
}

// Create multi-page TIFF
AnyBitmap.CreateMultiFrameTiff(barcodeList).SaveAs("barcodeImages.tif");
Imports IronBarCode
Imports IronSoftware.Drawing
Imports System.Collections.Generic

' Read barcode from PDF file
Private result As BarcodeResults = BarcodeReader.ReadPdf("test.pdf")

' Create list for barcodes
Private barcodeList As New List(Of AnyBitmap)()

For Each barcode As BarcodeResult In result
	barcodeList.Add(barcode.BarcodeImage)
Next barcode

' Create multi-page TIFF
AnyBitmap.CreateMultiFrameTiff(barcodeList).SaveAs("barcodeImages.tif")
VB   C#

The code snippet above illustrates one of the use cases for this output format. Specifically, it's designed to create a multipage TIFF image from barcodes detected within a PDF document. First, we scan or detect barcodes in the sample PDF. Then, we create an AnyBitmap list where we store the information from the BarcodeImage property. Finally, we utilize this list to generate a multi-page TIFF using the CreateMultiFrameTiff method.

Please note
BarcodeImage property from BarcodeResult only store the images of barcodes found during read and not the whole input image itself.

Barcode Type

This property help users in determining what type of barcode is present in the input image or document. However, the limitation to this feature is that the barcode type inside the image must be those that are supported and can be read by IronBarcode. To know more on supported barcode types in IronBarcode, users can refer to this article.

Code snippet below demonstrate on how users can retrieve the barcode values as well the barcode type in an image by printing the values on console.

:path=/static-assets/barcode/content-code-examples/how-to/output-data-formats-BarcodeType.cs
using IronBarCode;
using System;

// Read barcode from PNG
BarcodeResults result = BarcodeReader.Read("bc3.png");

// Output barcode type to console
foreach (BarcodeResult barcode in result)
{
    Console.WriteLine("The barcode value is " + barcode.ToString() + " and the barcode type is " + barcode.BarcodeType);
}
Imports IronBarCode
Imports System

' Read barcode from PNG
Private result As BarcodeResults = BarcodeReader.Read("bc3.png")

' Output barcode type to console
For Each barcode As BarcodeResult In result
	Console.WriteLine("The barcode value is " & barcode.ToString() & " and the barcode type is " & barcode.BarcodeType)
Next barcode
VB   C#

From the code snippet above, we performed barcode read by calling BarcodeReader.Read() method on the input image. This returns BarcodeResults object that stores all the BarcodeResult from reading all barcodes available in the image. Next, we iterate through the BarcodeResults object to retrieve the BarcodeResult and get the barcode value and Barcode Type printed to the console.

Binary Value

Using IronBarcode, users also get to retrieve the byte array of the barcode value by retrieving the BinaryValue property from the BarcodeResult object. This allows the users to manipulate the barcode value further inside the program.

Code snippet below demonstrates one of the use case of retrieving the barcode value as binary data

:path=/static-assets/barcode/content-code-examples/how-to/output-data-formats-BinaryValue.cs
using IronBarCode;

// Read barcode from PNG
BarcodeResults result = BarcodeReader.Read("multiple-barcodes.png");

int i = 1;
foreach (BarcodeResult barcode in result)
{
    var binaryValue = barcode.BinaryValue;
    var barcodeType = IronBarCode.BarcodeEncoding.QRCode;

    // Create QR code
    GeneratedBarcode generatedBarcode = BarcodeWriter.CreateBarcode(binaryValue, barcodeType);

    // Export QR code
    generatedBarcode.SaveAsPng($"qrFromBinary{i}.png");
    i++;
}
Imports IronBarCode

' Read barcode from PNG
Private result As BarcodeResults = BarcodeReader.Read("multiple-barcodes.png")

Private i As Integer = 1
For Each barcode As BarcodeResult In result
	Dim binaryValue = barcode.BinaryValue
	Dim barcodeType = IronBarCode.BarcodeEncoding.QRCode

	' Create QR code
	Dim generatedBarcode As GeneratedBarcode = BarcodeWriter.CreateBarcode(binaryValue, barcodeType)

	' Export QR code
	generatedBarcode.SaveAsPng($"qrFromBinary{i}.png")
	i += 1
Next barcode
VB   C#

Observing the code snippet above, we've created a straightforward program that essentially transforms multiple barcodes within an image into separate new images of QR codes. Initially, we scan the barcodes in the sample PNG image. Once we've detected these barcodes, we iterate through them, access the BinaryValue property, and then use it to create new QR code barcodes.

Barcode Coordinates, Height & Width

Another property of the BarcodeResult object that users can access is the barcode's coordinates, including X1, Y1, and X2, Y2, as well as its Height and Width within an image file or document. These properties prove to be very useful when users need to retrieve information about the barcode's location and dimensions. Let's use an illustration to highlight the barcode's location and dimensions.

:path=/static-assets/barcode/content-code-examples/how-to/output-data-formats-height-width.cs
using IronBarCode;
using IronSoftware.Drawing;
using System.Linq;

// Read barcode from PNG
BarcodeResults result = BarcodeReader.Read("multiple-barcodes.png");

AnyBitmap bitmap = AnyBitmap.FromFile("multiple-barcodes.png");

foreach (BarcodeResult barcode in result)
{
    PointF[] barcodePoints = barcode.Points;

    float x1 = barcodePoints.Select(b => b.X).Min();
    float y1 = barcodePoints.Select(b => b.Y).Min();

    Rectangle rectangle = new Rectangle((int)x1, (int)y1, (int)barcode.Width!, (int)barcode.Height!);

    bitmap = bitmap.Redact(rectangle, Color.Magenta);

    // Save the image
    bitmap.SaveAs("redacted.png", AnyBitmap.ImageFormat.Png);
}
Imports System
Imports IronBarCode
Imports IronSoftware.Drawing
Imports System.Linq

' Read barcode from PNG
Private result As BarcodeResults = BarcodeReader.Read("multiple-barcodes.png")

Private bitmap As AnyBitmap = AnyBitmap.FromFile("multiple-barcodes.png")

For Each barcode As BarcodeResult In result
	Dim barcodePoints() As PointF = barcode.Points

	Dim x1 As Single = barcodePoints.Select(Function(b) b.X).Min()
	Dim y1 As Single = barcodePoints.Select(Function(b) b.Y).Min()

'INSTANT VB TODO TASK: There is no VB equivalent to the C# 'null-forgiving operator':
'ORIGINAL LINE: Rectangle rectangle = new Rectangle((int)x1, (int)y1, (int)barcode.Width!, (int)barcode.Height!);
	Dim rectangle As New Rectangle(CInt(Math.Truncate(x1)), CInt(Math.Truncate(y1)), CInt(barcode.Width), CInt(barcode.Height))

	bitmap = bitmap.Redact(rectangle, Color.Magenta)

	' Save the image
	bitmap.SaveAs("redacted.png", AnyBitmap.ImageFormat.Png)
Next barcode
VB   C#
Sample input before redaction
Redacted image

The code snippet above used to redact multiple barcodes found in an image file. To achieve this, we used combination of 2 libraries, which are IronBarcode and IronDrawing. To get BarcodeResult object and extract the properties from it, we firstly read the barcodes available in an image file using BarcodeReader.Read() method. At the same time, the input image file also need to be converted into AnyBitmap object in order to use and apply the redaction method on the image. Once we have the BarcodeResults object, we can apply loop and iterate through it to get the X1, Y1, Width, and Height of every barcodes available in the image and use them as CropRectangle properties in the AnyBitmap.Redact() method.

Page Number

Users can also retrieve the page number at which the barcode was found. This is a helpful feature for users who seek to use multipage document that contains multiple barcodes, and need to know the location of the barcodes found in the document for further processing.

Let's look at the code snippet below:

:path=/static-assets/barcode/content-code-examples/how-to/output-data-formats-page-number.cs
using IronBarCode;
using System;

// Read barcode from PDF
BarcodeResults result = BarcodeReader.ReadPdf("test.pdf");

// Output page number to console
foreach (BarcodeResult barcode in result)
{
    Console.WriteLine("The barcode value " + barcode.ToString() + " is found on page number " + barcode.PageNumber);
}
Imports IronBarCode
Imports System

' Read barcode from PDF
Private result As BarcodeResults = BarcodeReader.ReadPdf("test.pdf")

' Output page number to console
For Each barcode As BarcodeResult In result
	Console.WriteLine("The barcode value " & barcode.ToString() & " is found on page number " & barcode.PageNumber)
Next barcode
VB   C#

The simple code snippet above demonstrates on one use case where users need the program to return the value of barcodes found in a multipage PDF document, as well as its page numbers. The code snippet above used BarcodeReader.ReadPdf() method to read the barcodes inside a multipage PDF document, which returned BarcodeResults object that stored every BarcodeResult found in the document. Apply loop and iterate through each item in the object to retrieve the value of the barcodes and the page number where the barcodes were found. Apart from this use case, users can also use this property to debug if all barcodes in a document were able to be read.

Please note
The value returned from this property is 1-Based which means the first page is always one and not zero

Barcode rotation and Page Orientation

Using IronBarcode, users are also able to retrieve information on the barcode orientation as well as the page orientation where the barcode was found. To extract these 2 information, users can retrieve from Rotation and PageOrientation properties from BarcodeResult object. Rotation will return an integer that represents the angle of rotation of the barcode found. However, please mind that

Let's look at the code snippet below:

:path=/static-assets/barcode/content-code-examples/how-to/output-data-formats-orientation.cs
using IronBarCode;
using System;

// Read barcode from PDF
BarcodeResults result = BarcodeReader.ReadPdf("test.pdf");

// Output page orientation and rotation to console
foreach (BarcodeResult barcode in result)
{
    Console.WriteLine(barcode.Value);
    Console.WriteLine(barcode.PageOrientation);
    Console.WriteLine(barcode.Rotation);
}
Imports IronBarCode
Imports System

' Read barcode from PDF
Private result As BarcodeResults = BarcodeReader.ReadPdf("test.pdf")

' Output page orientation and rotation to console
For Each barcode As BarcodeResult In result
	Console.WriteLine(barcode.Value)
	Console.WriteLine(barcode.PageOrientation)
	Console.WriteLine(barcode.Rotation)
Next barcode
VB   C#

The simple code snippet above was run with the sample PDF input attached to prove that users can retrieve the page orientation and barcode rotation by getting the value of BarcodeResult.PageOrientation and BarcodeResult.Rotation respectively. This feature is useful mainly for debugging purpose.

Please note
IronBarcode can only read barcodes of rotation 0, 90, 180 and 270 degrees. IronBarcode will not return any value if the barcode has a rotation value other than mentioned. PageOrientation will return a PageOrientation object, which consist of Portrait or Landscape.

Text and Value

Of course the main property that users will want to retrieve when using IronBarcode, is to know it's value and text. These 2 properties are often used interchangeably and will return the same value. Apart from that, users can also use BarcodeResult.ToString() method to achieve the same result. Code snippet below demonstrates:

:path=/static-assets/barcode/content-code-examples/how-to/output-data-formats-text-value.cs
using IronBarCode;
using System;

// Read barcode from PDF
BarcodeResults result = BarcodeReader.ReadPdf("barcodestamped3.pdf");

// Output text value to console
foreach (BarcodeResult barcode in result)
{
    Console.WriteLine(barcode.Value);
    Console.WriteLine(barcode.Text);
    Console.WriteLine(barcode.ToString());
}
Imports IronBarCode
Imports System

' Read barcode from PDF
Private result As BarcodeResults = BarcodeReader.ReadPdf("barcodestamped3.pdf")

' Output text value to console
For Each barcode As BarcodeResult In result
	Console.WriteLine(barcode.Value)
	Console.WriteLine(barcode.Text)
	Console.WriteLine(barcode.ToString())
Next barcode
VB   C#

From the code snippet above, users only need to use a few lines of code to read barcode in an image using IronBarcode. After iterating through the BarcodeResults returned by BarcodeReader.Read() method, we output to the console the result of getting the value and text properties, as well as calling BarcodeResult.ToString() method to show that all these returned the same value.

In a nutshell, IronBarcode is a perfect API for users to perform multiple operations regarding barcodes, and not only limited to writing and decoding barcodes. With various output data formats supported, users can do so much more with the BarcodeResult object returned by IronBarcode.

Hairil related to Text and Value

Hairil Hasyimi Bin Omar

Software Engineer

Like all great engineers, Hairil is an avid learner. He’s refining his knowledge of C#, Python, and Java, using that knowledge to add value to team members across Iron Software. Hairil joined the Iron Software team from Universiti Teknologi MARA in Malaysia, where he graduated with a Bachelor's degree in Chemical and Process Engineering.