-1

I have a feature request. The current flow is for the user to scan a code (not a QR code, not sure what it is, zxing will scan it), then scan the test card.

The client has asked for me allow the user to import the test from the library. So we need to be able to scan the code off an image.

Is it possible to do this in zxing or am I forced to use the camera / feature is not possible?

Thanks!

StarWind0
  • 1,554
  • 2
  • 17
  • 46
  • 1
    This post may point you in the right direction: http://stackoverflow.com/questions/32134072/qr-code-scan-from-image-file – Dr. Nitpick Jul 18 '16 at 01:19
  • Yeah coworker sent that to me after I posted. I tested it on a bunch of photos, fails on everything sadly. – StarWind0 Jul 18 '16 at 01:38
  • :/ That is unfortunate. I haven't tried running the code so perhaps it is broken but one thing to note is that if the quality of the barcode images are not good enough for the library to be able to read it then it will fail to scan. That is one of the major downsides of trying to read a static image for a barcode vs depending on the library to pick out the barcode in a stream of live images from the camera. – Dr. Nitpick Jul 18 '16 at 01:55

1 Answers1

0

Here is my solution. I had to downsize the image, and inver the colors for it to work with zxing. I might add a convert to gray scale, but not today..

 public static String scanDataMatrixImage(Bitmap bitmap) {
    bitmap = doInvert(bitmap);

    double scaling = getScaling(bitmap);
    Bitmap resized;
    if(scaling>0) {
         resized = Bitmap.createScaledBitmap(bitmap, (int) (bitmap.getWidth() * scaling), (int) (bitmap.getHeight() * scaling), true);
    }
    else{
         resized = bitmap;
    }

    String contents = null;

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

    LuminanceSource source = new RGBLuminanceSource(resized.getWidth(), resized.getHeight(), intArray);
    BinaryBitmap binaryBitmap = new BinaryBitmap(new HybridBinarizer(source));


    MultiFormatReader reader = new MultiFormatReader();

    try

    {
        Result result = reader.decode(binaryBitmap);
        contents = result.getText();
    } catch (
            Exception e
            )

    {
        Log.e("QrTest", "Error decoding barcode", e);
    }

    return contents;
}


 private static double getScaling(Bitmap bitmap){
    int width = bitmap.getWidth();
    int height = bitmap.getHeight();
    int smallest = width;
    if(smallest > height){
        smallest = height;
    }
    double ratio = 200.0/smallest;
    return ratio;
}

public static Bitmap doInvert(Bitmap src) {
    // create new bitmap with the same settings as source bitmap
    Bitmap bmOut = Bitmap.createBitmap(src.getWidth(), src.getHeight(), src.getConfig());
    // color info
    int A, R, G, B;
    int pixelColor;
    // image size
    int height = src.getHeight();
    int width = src.getWidth();

    // scan through every pixel
    for (int y = 0; y < height; y++)
    {
        for (int x = 0; x < width; x++)
        {
            // get one pixel
            pixelColor = src.getPixel(x, y);
            // saving alpha channel
            A = Color.alpha(pixelColor);
            // inverting byte for each R/G/B channel
            R = 255 - Color.red(pixelColor);
            G = 255 - Color.green(pixelColor);
            B = 255 - Color.blue(pixelColor);
            // set newly-inverted pixel to output image
            bmOut.setPixel(x, y, Color.argb(A, R, G, B));
        }
    }

    // return final bitmap
    return bmOut;
}
StarWind0
  • 1,554
  • 2
  • 17
  • 46