Search code examples
androidqr-codebarcode-scannerimage-gallery

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?


Solution

  • 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.