QR code scan from image file

13,788

In the end I've found solution. Code is (originated 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'
}

Usage:

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

Related videos on Youtube

Barmaley
Author by

Barmaley

Не программист, не кодер, не девелопер, не архитектор, не тестер и вообще не айтишник. Зовите меня просто Гуру Java/Android :) Иногда изображаю крутизну: С/С++/C#/SQL/WMS/ERP Нетрадиционные познания: Schroedinger/Navier-Stokes

Updated on September 15, 2022

Comments

  • Barmaley
    Barmaley over 1 year

    Tried to use several libraries like ZXing, ZBar and their forks but didn't find way to scan barcode not from camera but from file.

    Can someone point me to right direction? Preferably I'm looking into ZXing: how to scan image from file (not from camera).

    Please.

  • Prabs
    Prabs over 6 years
    @barmley, any idea how to do it with zbar
  • Hatzen
    Hatzen almost 5 years
    Thanks, it does work for copied qrcode. But if i m trying to scan a photo it is not recognized although the image looks very good to me. Is there any chance to make it recognize real pics? Adjusting Quality or some filters or something?