This technical tip allows developers to read barcode from external image URL using Saaspose.BarCode REST API in your Java applications.Some important steps for performing this task is to build URI for reading barcode, sending the request to Saaspose server, parse and Deserializes the JSON to an object and display the value and type of all the recognized barcodes.
Sample Code for Reading Barcode from external image URL
//build URI to read barcode
//type: Codabar, Code11, Code128 and Code39Extended etc.
String strURI = "http://api.saaspose.com/v1.0/barcode/recognize?type=QR&url=http://upload.wikimedia.org/wikipedia/commons/c/ce/WikiQRCode.png";
// Send the request to Saaspose server
InputStreamresponseStream = ProcessCommand(Sign(strURI), "POST");
// Read the response
String strJSON = StreamToString(responseStream);
//Parse and Deserializes the JSON to a object.
RecognitionResponsebarcodeRecognitionResponse = gson.fromJson(strJSON,RecognitionResponse.class);
List<RecognizedBarCode> barcodes = barcodeRecognitionResponse.getBarcodes();
// Display the value and type of all the recognized barcodes
for (RecognizedBarCode barcode : barcodes)
{
System.out.println("Codetext: " + barcode.BarcodeValue() + "\nType: " + barcode.getBarcodeType());
}
\\Here is the RecognitionResponse class
public class RecognitionResponse extends BaseResponse
{
private List<RecognizedBarCode> Barcodes ;
public List<RecognizedBarCode>getBarcodes(){return Barcodes;}
}
\\Here is the RecognizedBarCode class
public class RecognizedBarCode
{
private String BarcodeType ;
private String BarcodeValue;
public String getBarcodeType(){return BarcodeType;}
public String BarcodeValue(){return BarcodeValue;}
}
\\Here is the BaseResponse class
public class BaseResponse
{
publicBaseResponse() { }
private String Code;
private String Status;
public String getCode(){return Code;}
public String getStatus(){return Status;}
public void setCode(String temCode){ Code=temCode;}
public void setStatus(String temStatus){ Status=temStatus;}
}