Skip to content Skip to sidebar Skip to footer

How To Scan Qr Code From Image(not From Camera)

My Question is: How to get a image file from sd card onto the application Finally how to decode the barcode image file and store the decoded result as a string or any suitable dat

Solution 1:

I wanted to test it out before posting it so it took me a while, I'm also using ZXing right now so this comes handy for me as well:

First of course, read the image from the gallery (this can be in your activity):

IntentpickIntent=newIntent(Intent.ACTION_PICK);
        pickIntent.setDataAndType( android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*");

        startActivityForResult(pickIntent, 111);

After that, just get the image uri on the activity result and then ZXing will do the magic:

@OverrideprotectedvoidonActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        switch (requestCode) {
            //the case is because you might be handling multiple request codes herecase111:
                if(data == null || data.getData()==null) {
                    Log.e("TAG", "The uri is null, probably the user cancelled the image selection process using the back button.");
                    return;
                }
                Uriuri= data.getData();
                try
                {
                    InputStreaminputStream= getContentResolver().openInputStream(uri);
                    Bitmapbitmap= BitmapFactory.decodeStream(inputStream);
                    if (bitmap == null)
                    {
                        Log.e("TAG", "uri is not a bitmap," + uri.toString());
                        return;
                    }
                    intwidth= bitmap.getWidth(), height = bitmap.getHeight();
                    int[] pixels = newint[width * height];
                    bitmap.getPixels(pixels, 0, width, 0, 0, width, height);
                    bitmap.recycle();
                    bitmap = null;
                    RGBLuminanceSourcesource=newRGBLuminanceSource(width, height, pixels);
                    BinaryBitmapbBitmap=newBinaryBitmap(newHybridBinarizer(source));
                    MultiFormatReaderreader=newMultiFormatReader();
                    try
                    {
                        Resultresult= reader.decode(bBitmap);
                        Toast.makeText(this, "The content of the QR image is: " + result.getText(), Toast.LENGTH_SHORT).show();
                    }
                    catch (NotFoundException e)
                    {
                        Log.e("TAG", "decode exception", e);
                    }
                }
                catch (FileNotFoundException e)
                {
                    Log.e("TAG", "can not open file" + uri.toString(), e);
                }
                break;
        }
    }

I tested this and it works, cheers.

Post a Comment for "How To Scan Qr Code From Image(not From Camera)"