Scan QR code from image file

I tried to use several libraries, such as ZXing, ZBar and their plugs, but did not find a way to scan the barcode not from the camera, but from the file.

Can someone point me in the right direction? I prefer ZXing: how to scan an image from a file (not from the camera).

you are welcome.

+4
source share
1 answer

I finally found a solution. Code (created from here ):

import com.google.zxing.*; public static String scanQRImage(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(); try { Result result = reader.decode(bitmap); contents = result.getText(); } catch (Exception e) { Log.e("QrTest", "Error decoding barcode", e); } return contents; } 

Gradle, referencing as:

 dependencies { compile 'com.google.zxing:core:3.2.1' } 

Using:

 InputStream is = new BufferedInputStream(new FileInputStream(file)); Bitmap bitmap = BitmapFactory.decodeStream(is); String decoded=scanQRImage(bitmap); Log.i("QrTest", "Decoded string="+decoded); 
+4
source

All Articles