Monday, November 4, 2013

Read a Bar code from WebDriver

In my previous post i have shown you how we can read a test on an image either in a web site location or in your hard drive. Using the same techniques lets see how we can read a bar code with "OCR" class.  But this time we have to use a special method, and that is "OCR().recognizeBarcode()". Lets see how we can do this. 

below is the bar code image that i'm going to read from my HD


Simply download this image and save it in your HD and run below code. Make sure to change the image path according to your image location. 

import java.awt.image.BufferedImage;  
import java.awt.image.RenderedImage;  
import java.io.File;  
import javax.imageio.ImageIO;  
import com.asprise.util.ocr.OCR;  

public class Test2 { 
public static void main(String[] args) throws Exception {

//Creates a new object and assign the image location 
BufferedImage image = ImageIO.read(new File("D:\\TEST\\Bar_code.jpg"));  

//Creates a string variable and assign the bar code values 
String s = new OCR().recognizeBarcode(image);

//Creates a string variable and assign other character values
String imageText = new OCR().recognizeCharacters((RenderedImage) image);   

//Prints the bar code values
System.out.println("Text From Image : \n"+s);    

//Prints the other character values 
System.out.println("Text From Image : \n"+ imageText);    

//Prints the length of the texts  
System.out.println("Length of total text : \n"+ imageText.length());     
 
 
}


}


The output of the program is : 

Text From Image : 
123456789012
Text From Image : 

Asprise OCR
Speed. Accuracy.

Length of total text : 

33


6 comments:

  1. Thank you.
    Very useful code sample for reading barcode from WebDriver.

    ReplyDelete
    Replies
    1. I am getting,
      import of com.asprise.util.ocr.OCR cannot be resolved error, may we know how did u resolve this ?
      Please share the maven dependency or library which u have used

      Delete
  2. Hi ,..I tried using the code. It is asking for the OCR class file.
    I tried to download the javaocr-plugins-distribution-1.0-bin jars and added to the build path. But no luck.
    Could you please help me out.

    ReplyDelete
  3. same issue here . can some guide to get com.asprise.util.ocr.OCR jar file

    ReplyDelete
  4. same issue here . can some guide to get com.asprise.util.ocr.OCR jar file

    ReplyDelete
  5. same issue here . can some guide to get com.asprise.util.ocr.OCR jar file

    ReplyDelete