DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Sheraz has posted 65 posts at DZone. View Full User Profile

Read & Recognize Barcode from External Image URL in Java Applications

11.16.2012
| 4304 views |
  • submit to reddit
//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
            InputStream responseStream = ProcessCommand(Sign(strURI), "POST");
            // Read the response
            String strJSON = StreamToString(responseStream);
            //Parse and Deserializes the JSON to a object. 
            RecognitionResponse barcodeRecognitionResponse = 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 
	    {
	        public BaseResponse() { }
	        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;}
	    }

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.