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 :(