0
Answer

Java code for Generating and reading barcode images in Cloud

Jumaila Shahzad

Jumaila Shahzad

10y
1.6k
1

 

I am going to share very simple java code to generate barcode and read barcode from an image in cloud and I am using Aspose's cloud library for this known as Aspose.Barcode for Cloud API.

 

I am sharing two code snippets one is for generating barcode by using REST API in java and second one is for generating barcode by using Java SDK

 

 

Generate Barcodes using Java REST

 

First you need to set any text and supported type in the following URI to create a new barcode.

 

http://api.aspose.com/v1.1/barcode/generate?text=Text for new barcode image&type=QR

 

After building the URI,  you need to go through the following steps:

 

1.         Set App SID and App Key and sign the URI.

See section 1 of the following code and Sign URI method for more details.

2.         Send a GET request to Aspose for Cloud service.

See section 2 of the following code and ProcessCommand method for more details.

3.         Save output barcode image to disk.

See section 3 of the following code and SaveStreamToFile method for more details.

 

Below is the code to create a new barcode.

/**** Section 1 ****/

AsposeApp.setAppSID("77******-1***-4***-a***-80**********");

AsposeApp.setAppKey("********************");

 

//Build URI to generate barcode

//Type:  Codabar, Code11, Code128 and Code39Extended etc.

//Format:  PNG, JPEG and JPG etc.

String strURI = "http://api.aspose.com/v1.1/barcode/generate?text=Barcode Text Here&type=QR&format=png";

//Sign URI

String signedURI = Utils.Sign(strURI);

/**** End Section 1 ****/

/**** Section 2 ****/

InputStreamresponseStream=  Utils.ProcessCommand(signedURI, "GET");

/**** End Section 2 ****/

/**** Section 3 ****/

Folder.SaveStreamToFile("Barcode1.png", responseStream);

/**** End Section 3 ****/

responseStream.close();


Generate Barcodes using Java SDK

 

If you want to use Java SDK to create a new barcode, you can download the following SDK from Aspose for Cloud SDK for Java. In order to use the Java SDK, user need to perform following steps:

https://github.com/asposeforcloud/Aspose_Cloud_SDK_For_Java

 

1.         Set base product URI, App SID and App Key.

See section 1 of the following code.

2.         Create object of the BarCodeBuilder class and call the Save method.

See section 2 of the following code.

Below is the code that creates a new barcode.

 

/**** Section 1 ****/

 

com.aspose.cloud.common.Product.setBaseProductUri("http://api.aspose.com/v1.1");

 com.aspose.cloud.common.AsposeApp.setAppSID("77******-1***-4***-a***-80**********");

com.aspose.cloud.common.AsposeApp.setAppKey("********************");

 

/**** End Section 1 ****/

 

/**** Section 2 ****/

 

//Create an instance of BarCodeBuilder class

com.aspose.cloud.barcode.BarCodeBuilder builder = newcom.aspose.cloud.barcode.BarCodeBuilder("Pdf417

barcode generated by Aspose.BarCode for Cloud",

com.aspose.cloud.barcode.BarCodeType.Pdf417);

// Call Save() method to save the barcode to local disk

builder.Save(com.aspose.cloud.barcode.SaveLocation.Local, "Barcode1.png",

com.aspose.cloud.barcode.ImageFormat.PNG);

 

/**** End Section 2 ****/

 

I hope this code will help you understand how to create barcode API in cloud.