Saturday 30 May 2015

Android : Extract BARCODE/QRCODE data from locally stored(SD Card) images.


Adding demo here to extract BARCODE/QRCODE data from images, images which are stored in local storage or SD card.


  • Barcode 
  • ZXing
  • Image Picker





First of all you need to download ZXing Core library from maven, below is the URL to download latest ZXing core library.


While creating this post core-3.2.0.jar was latest version. Add this jar in libs folder of your Android project.


We are picking images from gallery using intent and using "bitmap data of image"  to extract barcode/qrcode  result.


Actual code to extract data from bitmap image is given below. 


public Result[] decode(Bitmap imageBitmap) {

MultiFormatReader reader = null;
Map<DecodeHintType, Object> hints = new EnumMap<DecodeHintType, Object>(DecodeHintType.class);
/* There are different format to create barcode and qrcode, 
* while extracting we are adding all the possible format for better result. 
*/
hints.put(DecodeHintType.POSSIBLE_FORMATS, EnumSet.allOf(BarcodeFormat.class));
hints.put(DecodeHintType.TRY_HARDER, Boolean.TRUE);

reader = new MultiFormatReader();
reader.setHints(hints);

int width = imageBitmap.getWidth();
int height = imageBitmap.getHeight();
int[] pixels = new int[width * height];
imageBitmap.getPixels(pixels, 0, width, 0, 0, width, height);

RGBLuminanceSource source = new RGBLuminanceSource(width, height, pixels);
BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));

MultipleBarcodeReader multiReader = new GenericMultipleBarcodeReader(reader);
Result[] theResults = null;
try {
/* decode multiple is used so that if multiple barcode is present in single image we can get result for all of them. 
*/
theResults = multiReader.decodeMultiple(bitmap, hints);
} catch (NotFoundException e1) {
e1.printStackTrace();
}

return theResults;
}


You can download demo code from below url:




6 comments:

  1. This comment has been removed by the author.

    ReplyDelete
    Replies
    1. i test this sample code.
      i have some problem.
      that is to stop app when some picture is loaded.

      Delete
    2. Your question is not very clear but I think you are facing force stop issue when you load some image...if this is the issue then please first try with some low quality(less size) image and check if it work or not...I guess it is happening due to OutOfMemoryIssue.....if you want to integrate this in your own app you need to find some alternative to load(pick) image ..so that you can reduce image size before loading it to your app

      Delete
  2. This comment has been removed by the author.

    ReplyDelete
  3. Your blog is a breath of fresh air in the crowded online space. Your ability to articulate complex ideas in a concise and relatable manner is commendable. Well done!

    Maven Technology shines as Best Mobile App Developers in Delhi, crafting cutting-edge apps that redefine user experiences. With a keen eye for detail and a focus on user-centric design, their innovative solutions set them apart.

    For further insights, please visit us at : maventechie.com.

    ReplyDelete