Test in production without watermarks.
Works wherever you need it to.
Get 30 days of fully functional product.
Have it up and running in minutes.
Full access to our support engineering team during your product trial
Barcode scanning is an essential feature for many applications, from inventory management to retail and logistics. By integrating barcode reading into your .NET applications, you can streamline data capture, automate workflows, and improve efficiency.
IronBarcode is a powerful .NET library that makes working with barcodes simple. With this tool, you can read barcodes from images, streams, and PDF files, as well as generate QR codes. This article will show you how to integrate barcode scanning into your .NET application, focusing on creating an API or Web App integration to expose barcode scanning functionality.
IronBarcode is ideal for:
To create a barcode reader that can be exposed as a service in your application, you will integrate IronBarcode into a REST API or Web App. Below is an example of how to do this using ASP.NET Core.
If you haven’t already, download IronBarcode for your project. Make sure you have the proper license key for your intended use. Note that exposing IronBarcode’s functionality via a public API or reselling it as a standalone service requires additional licensing (SDK, OEM, or SaaS). Ensure you understand the licensing considerations before proceeding.
Once you have set up IronBarcode and installed it in your project, you can create a reusable barcode scanner class that integrates IronBarcode’s functionality and exposes it as an API endpoint.
using IronBarCode;
using System.IO;
namespace BarcodeIntegration
{
public class BarcodeScanner
{
static BarcodeScanner()
{
// Set the license key
IronBarCode.License.LicenseKey = "Your-License-Key";
}
// Read a barcode from an image file
public string ReadBarcodeFromImage(string imagePath)
{
try
{
var barcode = BarcodeReader.Read(imagePath);
return barcode?.ToString() ?? "No Barcode Found";
}
catch (Exception ex)
{
return $"Error reading barcode: {ex.Message}";
}
}
// Read a barcode from a stream (e.g., file upload or memory stream)
public string ReadBarcodeFromStream(Stream inputStream)
{
try
{
var barcode = BarcodeReader.Read(inputStream);
return barcode?.ToString() ?? "No barcode found";
}
catch (Exception ex)
{
return $"Error reading barcode: {ex.Message}";
}
}
// Read a barcode from a PDF file
public string ReadBarcodeFromPdf(string filePath)
{
try
{
var barcode = BarcodeReader.ReadPdf(filePath);
return barcode?.ToString() ?? "No barcode found";
}
catch (Exception ex)
{
return $"Error reading barcode: {ex.Message}";
}
}
}
}
using IronBarCode;
using System.IO;
namespace BarcodeIntegration
{
public class BarcodeScanner
{
static BarcodeScanner()
{
// Set the license key
IronBarCode.License.LicenseKey = "Your-License-Key";
}
// Read a barcode from an image file
public string ReadBarcodeFromImage(string imagePath)
{
try
{
var barcode = BarcodeReader.Read(imagePath);
return barcode?.ToString() ?? "No Barcode Found";
}
catch (Exception ex)
{
return $"Error reading barcode: {ex.Message}";
}
}
// Read a barcode from a stream (e.g., file upload or memory stream)
public string ReadBarcodeFromStream(Stream inputStream)
{
try
{
var barcode = BarcodeReader.Read(inputStream);
return barcode?.ToString() ?? "No barcode found";
}
catch (Exception ex)
{
return $"Error reading barcode: {ex.Message}";
}
}
// Read a barcode from a PDF file
public string ReadBarcodeFromPdf(string filePath)
{
try
{
var barcode = BarcodeReader.ReadPdf(filePath);
return barcode?.ToString() ?? "No barcode found";
}
catch (Exception ex)
{
return $"Error reading barcode: {ex.Message}";
}
}
}
}
Imports IronBarCode
Imports System.IO
Namespace BarcodeIntegration
Public Class BarcodeScanner
Shared Sub New()
' Set the license key
IronBarCode.License.LicenseKey = "Your-License-Key"
End Sub
' Read a barcode from an image file
Public Function ReadBarcodeFromImage(ByVal imagePath As String) As String
Try
Dim barcode = BarcodeReader.Read(imagePath)
Return If(barcode?.ToString(), "No Barcode Found")
Catch ex As Exception
Return $"Error reading barcode: {ex.Message}"
End Try
End Function
' Read a barcode from a stream (e.g., file upload or memory stream)
Public Function ReadBarcodeFromStream(ByVal inputStream As Stream) As String
Try
Dim barcode = BarcodeReader.Read(inputStream)
Return If(barcode?.ToString(), "No barcode found")
Catch ex As Exception
Return $"Error reading barcode: {ex.Message}"
End Try
End Function
' Read a barcode from a PDF file
Public Function ReadBarcodeFromPdf(ByVal filePath As String) As String
Try
Dim barcode = BarcodeReader.ReadPdf(filePath)
Return If(barcode?.ToString(), "No barcode found")
Catch ex As Exception
Return $"Error reading barcode: {ex.Message}"
End Try
End Function
End Class
End Namespace
This BarcodeScanner class abstracts IronBarcode’s functionality, making it easy to integrate into any .NET application. Let's look at a breakdown of the different methods included here:
Each of the following methods attempts to read a barcode from different types of input:
Each method attempts to read the barcode data and returns the barcode string or an error message if no barcode is found or an exception occurs.
To allow external applications to use your barcode scanning functionality, you can expose it as a REST API using ASP.NET Core. Below is an example of how you might do this:
using Microsoft.AspNetCore.Mvc;
using System.IO;
using Microsoft.AspNetCore.Http;
using BarcoderReader;
[ApiController]
[Route("api/barcode")]
public class BarcodeController : ControllerBase
{
private readonly BarcodeScanner _barcodeScanner;
public BarcodeController()
{
_barcodeScanner = new BarcodeScanner();
}
[HttpPost("read-from-image")]
public IActionResult ReadFromImage(IFormFile file)
{
using var stream = file.OpenReadStream();
var result = _barcodeScanner.ReadBarcodeFromStream(stream);
return Ok(new { Barcode = result });
}
}
using Microsoft.AspNetCore.Mvc;
using System.IO;
using Microsoft.AspNetCore.Http;
using BarcoderReader;
[ApiController]
[Route("api/barcode")]
public class BarcodeController : ControllerBase
{
private readonly BarcodeScanner _barcodeScanner;
public BarcodeController()
{
_barcodeScanner = new BarcodeScanner();
}
[HttpPost("read-from-image")]
public IActionResult ReadFromImage(IFormFile file)
{
using var stream = file.OpenReadStream();
var result = _barcodeScanner.ReadBarcodeFromStream(stream);
return Ok(new { Barcode = result });
}
}
Imports Microsoft.AspNetCore.Mvc
Imports System.IO
Imports Microsoft.AspNetCore.Http
Imports BarcoderReader
<ApiController>
<Route("api/barcode")>
Public Class BarcodeController
Inherits ControllerBase
Private ReadOnly _barcodeScanner As BarcodeScanner
Public Sub New()
_barcodeScanner = New BarcodeScanner()
End Sub
<HttpPost("read-from-image")>
Public Function ReadFromImage(ByVal file As IFormFile) As IActionResult
Dim stream = file.OpenReadStream()
Dim result = _barcodeScanner.ReadBarcodeFromStream(stream)
Return Ok(New With {Key .Barcode = result})
End Function
End Class
Add from PixabayUpload
or drag and drop an image here
Add image alt text
Add from PixabayUpload
or drag and drop an image here
Add image alt text
This API exposes a POST endpoint where barcode images can be uploaded, and the API will return the barcode data. This allows other systems or front-end applications to interact with your barcode scanner as a service.
To further enhance the SDK, consider adding:
public string ReadBarcodeFromImage(string imagePath)
{
try
{
BarcodeReaderOptions options = new BarcodeReaderOptions()
{
ExpectMultipleBarcodes = true,
ExpectBarcodeTypes = BarcodeEncoding.AllOneDimensional,
};
foreach (var barcode in BarcodeReader.Read(imagePath, options))
{
return barcode.ToString();
}
return "No barcode found";
}
catch (Exception ex)
{
return $"Error reading barcode: {ex.Message}";
}
}
public string ReadBarcodeFromImage(string imagePath)
{
try
{
BarcodeReaderOptions options = new BarcodeReaderOptions()
{
ExpectMultipleBarcodes = true,
ExpectBarcodeTypes = BarcodeEncoding.AllOneDimensional,
};
foreach (var barcode in BarcodeReader.Read(imagePath, options))
{
return barcode.ToString();
}
return "No barcode found";
}
catch (Exception ex)
{
return $"Error reading barcode: {ex.Message}";
}
}
Public Function ReadBarcodeFromImage(ByVal imagePath As String) As String
Try
Dim options As New BarcodeReaderOptions() With {
.ExpectMultipleBarcodes = True,
.ExpectBarcodeTypes = BarcodeEncoding.AllOneDimensional
}
For Each barcode In BarcodeReader.Read(imagePath, options)
Return barcode.ToString()
Next barcode
Return "No barcode found"
Catch ex As Exception
Return $"Error reading barcode: {ex.Message}"
End Try
End Function
Error Handling: Expand error handling and logging to provide better diagnostics for your SDK users.
Batch Processing: If you want to process multiple barcodes from a collection of images, you can create methods that take in an array of images or streams.
As mentioned earlier, the IronBarcode SDK is meant for integration into your internal applications, and exposing it through an API requires additional licensing. You must secure the necessary licensing (SDK, OEM, or SaaS) before exposing IronBarcode as part of a service, such as a public API.
Do not resell IronBarcode as a standalone SDK or expose it via a public-facing API without ensuring that your licensing covers this usage.
Experience the innovative power of IronBarcode. Try our free trial and discover seamless barcode generation, reading, and editing for your .NET applications. With advanced features, exceptional performance, and a user-friendly interface, IronBarcode is the ultimate solution for all your barcode needs. Start your free trial today and elevate your projects.