Monday 29 August 2016

How to read QR Code or Barcode from Images

Reading Barcodes / QR codes from images seems to be difficult & using the below open source libraries(ZXing - Zebra Crossing) provided by Google will make it simple for any application to include this functionality.

Curious on knowing how Android Apps scan the Barcodes or QR codes from the camera?

The android apps designed to scan 1D & 2D barcodes on a high level convert the data the camera into image bytes to the below library, which in-turn return the data embedded within the bar codes.

Follow the below steps to achieve the same.



STEP 1:  Zxing had multiple modules & functionalities. However, to achive current task download the below libraries 

'zxing-core-*.*.jar' , 'zxing-javase-*.*.jar' from the maven repository.

https://mvnrepository.com/artifact/com.google.zxing


STEP 2: Add the above downloaded jars into project dependencies.


STEP 3: Below code would help achieve the task.


String code = "";
String imgPath = "ImagePath\\qrcodeImage.jpg";

//Create instance of Reader
Reader reader = new MultiFormatReader();

// Pass the image a parameter, which converts the image into binary bitmap
InputStream barCodeIS = new FileInputStream(imgPath);
BufferedImage buffImage = ImageIO.read(barCodeIS);

LuminanceSource lsrc = new BufferedImageLuminanceSource(buffImage);
BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(lsrc));

// Reader decodes the bitmap & extract the data
Result result = reader.decode(bitmap);

code = result.getText();
System.out.println("Image Barcode / QR Code : " + code);

!!!!!!!!!!!!!!!!!!! QR Code Data Decoded from the Image !!!!!!!!!!!!!!!!!!!

Sample Images Attached Below:

  1. Sample QR Code
  2. Sample Liner Barcode



References:
https://github.com/zxing/zxing


3 comments:

  1. I have been researching this topic for a long time. I have been watching various technical forums and testing a lot of lib. I think the best barcode scanner technology is still the product of CnetSDK.

    ReplyDelete
    Replies
    1. Oh! Good to know that. I'll try to use the CnetSDK.
      Thanks for the information.

      Delete
  2. I was very interested in the article , it’s quite inspiring I should admit. I like visiting your site since I always come across interesting articles like this one. Keep sharing! Regards. Read more about Software Testing Services

    ReplyDelete