Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scan barcode from an image in gallery android

I'm creating a android project, main feature is scan barcode.

I'm tried integrate with Zxing library into my project, and it's work fine.

However, it's seems not support scan barcode from an available image in gallery of android devices.

How i can do it? or with other barcode library?

like image 512
MrSiro Avatar asked Apr 15 '15 12:04

MrSiro


People also ask

Can you scan a barcode through a picture?

The simple answer is yes - if the barcode scanner that you have has what is known as a 2D (two dimensional) imager as its scan engine. Barcode scanners come with two different types of scan engines.


1 Answers

You could use this class MultiFormatReader from ZXing library.

You have to get Gallery image in BitMap and convert it as this:

Bitmap bMap = [...];
String contents = null;

int[] intArray = new int[bMap.getWidth()*bMap.getHeight()];  
//copy pixel data from the Bitmap into the 'intArray' array  
bMap.getPixels(intArray, 0, bMap.getWidth(), 0, 0, bMap.getWidth(), bMap.getHeight());  

LuminanceSource source = new RGBLuminanceSource(bMap.getWidth(), bMap.getHeight(), intArray);
BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));

Reader reader = new MultiFormatReader();
Result result = reader.decode(bitmap);
contents = result.getText();

UPDATE1

To manipulate big image, please have a look at :

https://developer.android.com/training/articles/memory.html

https://developer.android.com/training/displaying-bitmaps/manage-memory.html

You could use this property android:largeHeap to increase heap size.

like image 52
LaurentY Avatar answered Sep 22 '22 09:09

LaurentY