Harish Penta
Harish Penta

Reputation: 500

Zxing Qr code scanner does not working on all Devices

When i Run the App in Asus_Z00AD(ver-5.0) the QR code Scanning is does perfectly but when i run it on any other mobile like moto-g(ver-6.0) it shows a blank screen and nothing else.. what to do.. plz suggest me..

my Scanner Activity

            package hashrail.com.aggregate.activity;

            import android.app.Activity;
            import android.content.Intent;
            import android.graphics.PointF;
            import android.os.Bundle;
            import android.support.design.widget.Snackbar;
            import android.support.v7.app.AlertDialog;
            import android.support.v7.app.AppCompatActivity;
            import android.support.v7.widget.Toolbar;
            import android.util.Log;
            import android.util.SparseArray;
            import android.view.KeyEvent;
            import android.view.MenuItem;
            import android.view.SurfaceHolder;
            import android.view.SurfaceView;
            import android.view.View;
            import android.widget.ImageView;
            import android.widget.LinearLayout;
            import android.widget.TextView;
            import android.widget.Toast;

            import com.google.zxing.Result;

            import java.io.IOException;

            import hashrail.com.aggregate.R;
            import me.dm7.barcodescanner.zxing.ZXingScannerView;


            public class BarcodeScanFragment extends AppCompatActivity implements ZXingScannerView.ResultHandler {
                Toolbar toolbar;
                LinearLayout rlpditem;
                private String codeFormat, codeContent;
                private TextView formatTxt, contentTxt;
                ImageView imgBarcode;
                LinearLayout llQRrcode;
                private ZXingScannerView mScannerView;


                public BarcodeScanFragment() {
                    // Required empty public constructor
                }

                @Override
                public void onCreate(Bundle savedInstanceState) {
                    super.onCreate(savedInstanceState);
                    setContentView(R.layout.fragment_barcode_scan);
                    toolbar = (Toolbar) findViewById(R.id.toolbar1);
                    llQRrcode = (LinearLayout) findViewById(R.id.llQRrcode);
                    formatTxt = (TextView) findViewById(R.id.scan_format);
                    contentTxt = (TextView) findViewById(R.id.scan_content);
                    imgBarcode = (ImageView) findViewById(R.id.scssan_button);

                    setSupportActionBar(toolbar);
                    getSupportActionBar().setDisplayShowHomeEnabled(true);
                    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
                    setTitle("Barcode Scanner");

                   /* imgBarcode.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {

                            setContentView(mScannerView);

                            mScannerView.startCamera();
                        }
                    });*/
                }

                @Override
                public boolean onOptionsItemSelected(MenuItem item) {

                    switch (item.getItemId()) {
                        case android.R.id.home:
                            onBackPressed();
                            return true;
                        default:
                            return super.onOptionsItemSelected(item);
                    }
                }
                public void QrScanner(View view) {
                         // Start camera
                    mScannerView = new ZXingScannerView(BarcodeScanFragment.this);   // Programmatically initialize the scanner view
                    mScannerView.setResultHandler(BarcodeScanFragment.this); // Register ourselves as a handler for scan results.
                    setContentView(mScannerView);
                    mScannerView.startCamera();
                }

               /* @Override
                public void onPause() {
                    super.onPause();
                    mScannerView.stopCamera ();   // Stop camera on pause
                }
            */
                @Override
                public void handleResult(Result rawResult) {
            // Do something with the result here

                    Log.e("handler", rawResult.getText()); // Prints scan results
                    Log.e("handler", rawResult.getBarcodeFormat().toString()); // Prints the scan format (qrcode)

                    // show the scanner result into dialog box.
                    AlertDialog.Builder builder = new AlertDialog.Builder(this);
                    builder.setTitle("Scan Result");
                    builder.setMessage(rawResult.getText());
                    AlertDialog alert1 = builder.create();
                    alert1.show();

                    // If you would like to resume scanning, call this method below:
                     mScannerView.resumeCameraPreview(this);
                }
            }

Gradle file

                apply plugin: 'com.android.application'

                android {
                    compileSdkVersion 23
                    buildToolsVersion "23.0.3"

                    defaultConfig {
                        applicationId "hashrail.com.aggregate"
                        minSdkVersion 11
                        targetSdkVersion 23
                        versionCode 1
                        versionName "1.0"
                        useLibrary 'org.apache.http.legacy'
                    }
                    buildTypes {
                        release {
                            minifyEnabled false
                            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
                        }
                    }
                }

                dependencies {
                    compile fileTree(dir: 'libs', include: ['*.jar'])
                    testCompile 'junit:junit:4.12'
                    compile 'com.android.support:appcompat-v7:23.3.0'
                    compile 'com.android.support:design:23.0.1'
                    compile 'com.android.support:cardview-v7:23.0.1'
                    compile 'com.android.support:palette-v7:23.0.1'
                    compile 'com.android.support:recyclerview-v7:23.0.1'
                    compile 'com.github.lzyzsd:circleprogress:1.1.0@aar'

                    compile 'me.dm7.barcodescanner:zxing:1.8.4'


                    //compile 'com.dlazaro66.qrcodereaderview:qrcodereaderview:1.0.0'
                    /*compile 'com.journeyapps:zxing-android-embedded:2.0.1@aar'
                    compile 'com.journeyapps:zxing-android-legacy:2.0.1@aar'
                    compile 'com.journeyapps:zxing-android-integration:2.0.1@aar'
                    compile 'com.google.zxing:core:3.0.1'*/

                }

Upvotes: 4

Views: 8169

Answers (3)

Abdul Waheed
Abdul Waheed

Reputation: 1032

This is problem in ZXingScannerView new version 1.9. User older version 1.7.2 then it will work fine on all devices.

//User this version in your build.gradle implementation 'me.dm7.barcodescanner:zxing:1.7.2'

Upvotes: 2

DeepakPanwar
DeepakPanwar

Reputation: 1389

Please check this demo.

public class ContinuousCaptureActivity extends Activity implements ZxingScannerView.TorchListener, View.OnClickListener
{
    // PICK_PHOTO_CODE is a constant integer
    public final static int PICK_IMAGE_REQUEST_CODE = 1046;
    private static final String TAG = ContinuousCaptureActivity.class.getSimpleName();
    private static final int READ_EXTERNAL_STORAGE_REQUEST_CODE = 1051;
    private ZxingScannerView barcodeView;
    private BeepManager beepManager;
    private String lastText;
    private boolean isTorchEnabled;

    private BarcodeCallback callback = new BarcodeCallback() {
        @Override
        public void barcodeResult(BarcodeResult result) {
            if (result.getText() == null || result.getText().equals(lastText)) {
                // Prevent duplicate scans
                return;
            }
            lastText = result.getText();
            barcodeView.setStatusText(result.getText());
            beepManager.playBeepSoundAndVibrate();
        }

        @Override
        public void possibleResultPoints(List<ResultPoint> resultPoints) {
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.continuous_scan);
        beepManager = new BeepManager(this);
        barcodeView = findViewById(R.id.barcode_scanner);

        barcodeView.setFullScreenScan();
        barcodeView.getCameraSettings().setAutoFocusEnabled(true);
        barcodeView.getCameraSettings().setScanInverted(true);
        barcodeView.getCameraSettings().setExposureEnabled(true);

        Collection<BarcodeFormat> formats = Arrays.asList(BarcodeFormat.QR_CODE, BarcodeFormat.CODE_39);
        barcodeView.getBarcodeView().setDecoderFactory(new DefaultDecoderFactory(formats));
        barcodeView.initializeFromIntent(getIntent());
        barcodeView.decodeContinuous(callback);
    }


    @Override
    public void onClick(View view) {

        switch (view.getId()) {
            case R.id.flashlight:

                toggleTorch();
                break;

            case R.id.galleryPicker:

                pickImage();
                break;
        }
    }

    private void pickImage() {

        if (ActivityCompat.checkSelfPermission(this, READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
            // Create intent for picking a photo from the gallery
            Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
            if (intent.resolveActivity(getPackageManager()) != null) {

                intent.setType("image/*");
                intent.putExtra("crop", "true");
                intent.putExtra("scale", true);
                intent.putExtra("aspectX", 16);
                intent.putExtra("aspectY", 9);
                startActivityForResult(intent, PICK_IMAGE_REQUEST_CODE);
            }
        } else {
            ActivityCompat.requestPermissions(
                    this,
                    new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},
                    READ_EXTERNAL_STORAGE_REQUEST_CODE);
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        switch (requestCode) {
            case READ_EXTERNAL_STORAGE_REQUEST_CODE:
                if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    // pick image after request permission success
                    pickImage();
                }
                break;
        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {

        if (requestCode == PICK_IMAGE_REQUEST_CODE) {
            if (resultCode != Activity.RESULT_OK) {
                return;
            }
            if (data != null) {
                Uri photoUri = data.getData();
                // Do something with the photo based on Uri
                Bitmap selectedBitmap = null;
                try {
                    selectedBitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), photoUri);

                    AnimatedViewFinder viewFinder = barcodeView.getViewFinder();
                    Bitmap resizedBitmap = BitMapScaler.scaleToFill(selectedBitmap, viewFinder.getWidth(), viewFinder.getWidth());
                    barcodeView.scanBitmap(resizedBitmap);

                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    private void toggleTorch() {
        if (isTorchEnabled) {
            barcodeView.setTorchOff();
        } else {
            barcodeView.setTorchOn();
        }
    }

    private void toggleTorchListener(boolean enableListener) {
        if (!ZxingUtils.hasFlash(getApplicationContext())) {
            barcodeView.getFlashlight().setVisibility(View.GONE);
        } else {
            if (true) {
                barcodeView.getFlashlight().setOnClickListener(this);
                barcodeView.setTorchListener(this);
            } else {
                barcodeView.getFlashlight().setOnClickListener(null);
                barcodeView.setTorchListener(null);
            }
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        barcodeView.resume();
        toggleTorchListener(true);
        barcodeView.getGalleryPicker().setOnClickListener(this);
    }

    @Override
    protected void onPause() {
        super.onPause();
        barcodeView.pause();
        toggleTorchListener(false);
        barcodeView.getGalleryPicker().setOnClickListener(null);
    }

    @Override
    public void onTorchOn() {
        isTorchEnabled = true;
        barcodeView.getFlashlight().setImageResource(R.drawable.vector_flash_on);
    }

    @Override
    public void onTorchOff() {
        isTorchEnabled = false;
        barcodeView.getFlashlight().setImageResource(R.drawable.vector_flash_off);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        return barcodeView.onKeyDown(keyCode, event) || super.onKeyDown(keyCode, event);
    }
}

https://github.com/tiger1990/QrScannerDemo

Upvotes: 0

Puneet Kumar
Puneet Kumar

Reputation: 306

I am using same library its working fine on version 6.0 and 7.0 devices but having some issues with lower version devices.I have also test it on moto g4 working fine on g4 and g5.

your code is totally correct i am doing the same way,Do one thing to handle it for scanning make separate activity for scanner view and get data back where you want from handleResult() method because empty white screen issue causing may be due to setcontentView overriding again for scanner view.try this way if you need code then i can also share the code...

Upvotes: 0

Related Questions