Search code examples
javaandroidcamerabarcodezxing

The old library zxing is not running in my app


What's wrong with this code? I am running the following code in MainActivity but I get only an empty app:

package com.example.testbar;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import com.google.zxing.Result;
import android.util.Log;
import me.dm7.barcodescanner.zxing.ZXingScannerView;

public class MainActivity extends AppCompatActivity implements ZXingScannerView.ResultHandler {
    private ZXingScannerView mScannerView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //setContentView(R.layout.activity_main);
        // Programmatically initialize the scanner view
        mScannerView = new ZXingScannerView(this);
        setContentView(mScannerView);
    }
    @Override
    public void handleResult(Result rawResult) {
        // Do something with the result here
        Log.v("TAG", rawResult.getText()); // Prints scan results
        // Prints the scan format (qrcode, pdf417 etc.)
        Log.v("TAG", rawResult.getBarcodeFormat().toString());
        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);
    }

    @Override
    public void onResume() {
        super.onResume();
        // Register ourselves as a handler for scan results.
        mScannerView.setResultHandler(this);
        // Start camera on resume
        mScannerView.startCamera();
    }

    @Override
    public void onPause() {
        super.onPause();
        // Stop camera on pause
        mScannerView.stopCamera();
    }

}

I've added the permissions in the AndroidManifest.xml file:

<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera"/>
<uses-feature android:name="android.hardware.camera.autofocus"/>

The code builds without errors in Android Studio but I only get an empty app... and no camera when the app runs.


Solution

  • You should follow Android's documentation for checking and requesting permissions. https://developer.android.com/training/permissions/requesting

    I have modified your code and this should be a working sample.

    public class MainActivity extends AppCompatActivity implements ZXingScannerView.ResultHandler {
        private ZXingScannerView mScannerView;
        private static final int MY_PERMISSIONS_REQUEST_CAMERA = 101;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            //setContentView(R.layout.activity_main);
            // Programmatically initialize the scanner view
            mScannerView = new ZXingScannerView(this);
            setContentView(mScannerView);
        }
    
        @Override
        public void handleResult(Result rawResult) {
            // Do something with the result here
            Log.v("TAG", rawResult.getText()); // Prints scan results
            // Prints the scan format (qrcode, pdf417 etc.)
            Log.v("TAG", rawResult.getBarcodeFormat().toString());
            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);
        }
    
        @Override
        public void onResume() {
            super.onResume();
            // Register ourselves as a handler for scan results.
            mScannerView.setResultHandler(this);
            // Start camera on resume
    
            if (hasPermission(Manifest.permission.CAMERA)) {
                startCamera();
            }
        }
    
        @Override
        public void onPause() {
            super.onPause();
            // Stop camera on pause
            mScannerView.stopCamera();
        }
    
        private void startCamera() {
            mScannerView.startCamera();
        }
    
        private Boolean hasPermission(String permission) {
            if (ContextCompat.checkSelfPermission(this, permission) != PackageManager.PERMISSION_GRANTED) {
                // you need to request the permission
                Log.d("TAG", "User hasn't granted permission.");
    
                // No explanation needed for camera. request the permission
                ActivityCompat.requestPermissions(this,
                        new String[]{permission},
                        MY_PERMISSIONS_REQUEST_CAMERA);
            }
            else {
                Log.d("TAG",  "User already granted permission.");
                return true;
            }
            return false;
        }
    
        @Override
        public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
            switch (requestCode) {
                case MY_PERMISSIONS_REQUEST_CAMERA:
                    // If request is cancelled, the result arrays are empty.
                    if (grantResults.length > 0
                            && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                        startCamera();
                    } else {
                        Log.d("TAG", "Permission denied by user...");
                    }
                    break;
            }
        }
    }