I need to scan a QR code (play-services) but, it dosn’t work for me

  android-studio, java, qr-code

When I run the app and call for initQR() the camera opens ok but never red any code.
I need to fix this code becuase I live in a bloqueade countrie and I can’t download any other labrieries. Sorry by my english…

I add this to Manifest:

<uses-permission android:name="android.permission.CAMERA" />

And this to Gradle-app:

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    //noinspection GradleCompatible,GradleDependency
    implementation 'com.android.support:appcompat-v7:25.3.1'
    //noinspection GradleDependency
    implementation 'com.android.support:design:25.3.1'
    implementation 'com.android.support.constraint:constraint-layout:1.0.2'
    //noinspection GradleDependency,UseOfBundledGooglePlayServices
    implementation 'com.google.android.gms:play-services:10.0.1'

}

In my Main activity all this:

private CameraSource cameraSource;
private SurfaceView cameraView;
private final int MY_PERMISSIONS_REQUEST_CAMERA = 1;
private String token = "";
private String tokenanterior = "";

cameraView = (SurfaceView) findViewById(R.id.camera_view);

public void initQR() {

   BarcodeDetector barcodeDetector =
            new BarcodeDetector.Builder(this)
                    .setBarcodeFormats(Barcode.QR_CODE)
                    .build();

   
    cameraSource = new CameraSource
            .Builder(this, barcodeDetector)
            .setRequestedPreviewSize(1600, 1024)
            .setAutoFocusEnabled(true)
            .build();

    
    cameraView.getHolder().addCallback(new SurfaceHolder.Callback() {
        @Override
        public void surfaceCreated(SurfaceHolder holder) {

            
            if (ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.CAMERA)
                    != PackageManager.PERMISSION_GRANTED) {

                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                    // verificamos la version de ANdroid que sea al menos la M para mostrar
                    // el dialog de la solicitud de la camara
                    if (shouldShowRequestPermissionRationale(
                            Manifest.permission.CAMERA)) ;
                    requestPermissions(new String[]{Manifest.permission.CAMERA},
                            MY_PERMISSIONS_REQUEST_CAMERA);
                }
                return;
            } else {
                try {
                    cameraSource.start(cameraView.getHolder());
                } catch (IOException ie) {
                    Log.e("CAMERA SOURCE", ie.getMessage());
                }
            }
        }

        @Override
        public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
        }

        @Override
        public void surfaceDestroyed(SurfaceHolder holder) {
            cameraSource.stop();
        }
    });

    
    barcodeDetector.setProcessor(new Detector.Processor<Barcode>() {
        @Override
        public void release() {
        }


        @Override
        public void receiveDetections(Detector.Detections<Barcode> detections) {
            final SparseArray<Barcode> barcodes = detections.getDetectedItems();

            if (barcodes.size() > 0) {//when I debug never pass from here, always size=0

                
                token = barcodes.valueAt(0).displayValue.toString();

                if (!token.equals(tokenanterior)) {

                    tokenanterior = token;
                    aux.MostrarMensaje_largo(token);//it is like show a toast to try if works

                }
            }
        }
    });

}

I left a comment in a line tha is when I debug.

Source: Android Studio Questions

LEAVE A COMMENT