Create Barcode

Barcodes are widely used around the world for the necessary data collection. A barcode is an encoded image used to represent data in a visual and machine-readable form. As a developer, you came across a number of scenarios, where you need to create and read barcodes in your application. In this article I will share a RESTful solution, Aspose.BarCode Cloud, for working with the barcode in your application. It eliminates the requirement to install and maintain a separate software to create and read barcodes.

Aspose.BarCode Cloud is a platform independent barcode generation and recognition solution. It enables developers to create and read 1D (Linear), 2D and postal barcodes of 60+ symbologies that can be integrated universally into any workflow, html page, web application and desktop application. While creating new barcodes you can set barcode configurations, such as, barcode bar height, its dimensions, barcode image format and so on. The API generate and recognize barcode images in a variety of image formats: JPEG, PNG, GIF, BMP, TIFF, EMF, WMF, EXIF and ICON.

I’m using Aspose.BarCode Cloud SDK for .NET in this article. However, Aspose.BarCode Cloud also provides SDKs of all popular programming languages via GitHub and external Package Managers, so you can directly implement Aspose.BarCode Cloud in your favorite platform directly, with ease.

Here we go, we will follow these steps:

How to create the barcode

Step 1

First thing first, sign up with aspose.cloud to get your AppSID and AppKey

Step 2

Create a new project in Visual Studio and install Aspose.BarCode SDK for .NET package from NuGet

PM> Install-Package Aspose.BarCode-Cloud

Step 3

Copy paste the following code in the new project to create a barcode. I have used GetBarGenerate API method in this example, it creates the barcode with specified settings and returns the result in the response stream. I have created 2D barcode(datamatrix) in this example, you can create any barcode from the supported barcode types.

// Get App Key and App SID from https://dashboard.aspose.cloud/
// Instantiate Aspose BarCode Cloud API SDK
var barCodeApi = new BarCodeApi(AppKey, AppSid);
// Set Filename of image
String datafolder = "C:/Temp/";
// Set Filename of image
String name = "sample-barcode";
// Set Text to encode inside barcode
String text = "Aspose.BarCode";
// Set Barcode Symbology
String type = "datamatrix";
// Set Barcode Image Format
String format = "PNG";
// Sets if checksum will be added to barcode image.
String enableChecksum = null;
// Set optional params (if any)
float? resolutionX = null;
float? resolutionY = null;
float? dimensionX = null;
float? dimensionY = null;
try
{
// Invoke Aspose.BarCode Cloud SDK API to create barcode and returns result as stream in the response
using (Stream response = barCodeApi.BarCodeGetBarCodeGenerate(new BarCodeGetBarCodeGenerateRequest(text, type, format, resolutionX, resolutionY, dimensionX, dimensionY, enableChecksum)))
// Download generated barcode from api response
using (FileStream stream = File.Create(datafolder + name + "." + format))
{
response.CopyTo(stream);
}
Console.WriteLine("Generate Barcode to Local File, Done!");
}
catch (Exception ex)
{
Console.WriteLine("error:" + ex.Message + "\n" + ex.StackTrace);
}

Step 4

Run the code, it will save the barcode image as PNG image format. You can use response stream as per your use case.

How to read the barcode

Repeat the above steps 1-2, if required.

Step 3

Copy paste the following code in the new project to read the barcode image from local drive. It recognizes the barcode image and return the barcode type and value.

// Get App Key and App SID from https://dashboard.aspose.cloud/
// Instantiate Aspose BarCode Cloud API SDK
var barCodeApi = new BarCodeApi(AppKey, AppSid);
// Set Filename of image
String datafolder = "C:/Temp/";
// Set input file name
String name = "sample-barcode.png";
// The barcode type. If this parameter is empty, autodetection of all supported types is used.
String type = "";
// Set mode for checksum validation during recognition
String checksumValidation = "";
// Set if FNC symbol stripping should be performed
bool stripFnc = false;
// Set recognition of rotated barcode
int rotationAngle = 0;
// Set the image file url
String url = null;
Stream file = System.IO.File.OpenRead(datafolder + name);
try
{
// Invoke Aspose.BarCode Cloud SDK API to read barcode from local file
var apiResponse = barCodeApi.BarCodePostBarCodeRecognizeFromUrlorContent( new BarCodePostBarCodeRecognizeFromUrlorContentRequest(type, checksumValidation, stripFnc, rotationAngle, url, file));
if (apiResponse != null)
{
foreach (var barcode in apiResponse.BarCodes)
{
Console.WriteLine("Codetext: " + barcode.BarCodeValue + "\nType: " + barcode.BarCodeType);
}
Console.WriteLine("Read Barcode from Local File, Done!");
}
}
catch (Exception ex)
{
Console.WriteLine("error:" + ex.Message + "\n" + ex.StackTrace);
}

Step 4

Run the code, the API reads all the barcode images from provided image and return the details in the response.

And that’s it. You see how easy is it to create a new barcode or read a barcode image with few lines of code.

What’s Next

In case you still haven’t met the API and haven’t worked with it, free trial is right here waiting for you to give it a try and explore the power of the Aspose.BarCode Cloud API. All you need is to sign up with the aspose.cloud and visit our developer resources for a quick start: