2

I need an independent QR scanner in my app, so I've added the zxing library to my Android app - I've followed this link - I added the dependencies in build.gradle, and this is how I call the method:

  btnScanQR.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            new IntentIntegrator(MainScreenActivity.this).initiateScan(); // `this` is the current Activity
        }
    });

// Get the results:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
    if(result != null) {
        if(result.getContents() == null) {
            Toast.makeText(this, "Cancelled", Toast.LENGTH_LONG).show();
        } else {
            Toast.makeText(this, "Scanned: " + result.getContents(), Toast.LENGTH_LONG).show();
        }
    } else {
        super.onActivityResult(requestCode, resultCode, data);
    }
}

So it's basically exactly like on their github page, so there shouldn't be any problems, right?

However, I get this error when I try to open the scanner through my app, and I can't seem to find any solutions online to this exact problem:

    CameraInstance: Configuring camera
    W/CameraManager: Failed to set rotation.
    W/CameraManager: Camera rejected even safe-mode parameters! No configuration
    E/CameraInstance: Failed to configure camera

java.lang.NullPointerException: Attempt to invoke virtual method 'android.hardware.Camera$Parameters android.hardware.Camera.getParameters()' on a null object reference
                                                                      at com.journeyapps.barcodescanner.camera.CameraManager.setParameters(CameraManager.java:353)
                                                                      at com.journeyapps.barcodescanner.camera.CameraManager.configure(CameraManager.java:139)
                                                                      at com.journeyapps.barcodescanner.camera.CameraInstance$4.run(CameraInstance.java:171)
                                                                       at android.os.Handler.handleCallback(Handler.java:739)
                                                                       at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                       at android.os.Looper.loop(Looper.java:148)
                                                                       at android.os.HandlerThread.run(HandlerThread.java:61)
    D/CameraPreview: pause()
    D/CameraInstance: Closing camera
    D/CameraPreview: pause()

What is the cause of this (is it because it fails to rotate?) and how could I fix this? I've tried changing the orientation like it says on zxings github page with this:

<activity
    android:name="com.journeyapps.barcodescanner.CaptureActivity"
    android:screenOrientation="fullSensor"
    tools:replace="screenOrientation" />


IntentIntegrator integrator = new IntentIntegrator(this);
integrator.setOrientationLocked(false);
integrator.initiateScan();

But I keep getting the same error! Please help :(

eee
  • 179
  • 1
  • 9
  • check this https://github.com/dm77/barcodescanner – dindinii Jun 07 '16 at 17:04
  • 1
    I had the same problem. If you insert this code: `MobileBarcodeScanner.Initialize(Application);` in _onCreate_ in the _MainActivity.cs_ you will be able to access the camera without exceptions. – Adriano Dec 14 '16 at 14:04
  • MobileBarcodeScanner.Initialize(Application); Worked for me thank you @Adriano. somr threads say ZXing.Net.Mobile.Forms.Android.Platform.Init(); works, but did not work for me version 2.4.1 – gcoleman0828 Dec 31 '18 at 15:52

1 Answers1

1

Following two reasons for this error: 1) You have not initialized camera:

mcamera = mcamera.open(); 

2) You have not given permissions in androidmanifest.xml file.

 <uses-permission android:name="android.permission.CAMERA"/>
dindinii
  • 645
  • 4
  • 7