Search code examples
androidandroid-fragmentsandroid-cameraqr-codezxing

View stays black when using multiple DecoratedBarcodeViews


I am trying to use a QR Code reader in multiple tabs. After having problems with Google Vision API i tried to switch to zxing. First i tried to use the library zxing-android-embedded.

I tried their tabbed sample which contains a barcodereader and a cameraview. If I replace the cameraview with an additional barcodereader the view in the first tab stays black.

black cameraview

I used two ScanFragments in the SectionsPagerAdapter in TabbedScanning.java:

@Override
public android.support.v4.app.Fragment getItem(int position) {
    if(position == 0) {
        return ScanFragment.newInstance();
    } else {
        return ScanFragment.newInstance();
    }
}

After switching tab or changing screen orientation everything works fine but before the first tab stays black.

working cameraview

I also found the following error in the logfile which i don't know how i can resolve this.

02-22 09:37:42.164 20935-20974/example.zxing E/CameraInstance: Failed to configure camera
    java.lang.RuntimeException: getParameters failed (empty parameters)
    at android.hardware.Camera.native_getParameters(Native Method)
    at android.hardware.Camera.getParameters(Camera.java:3099)
    at com.journeyapps.barcodescanner.camera.CameraManager.setParameters(CameraManager.java:379)
    at com.journeyapps.barcodescanner.camera.CameraManager.configure(CameraManager.java:159)
    at com.journeyapps.barcodescanner.camera.CameraInstance$4.run(CameraInstance.java:203)
    at android.os.Handler.handleCallback(Handler.java:836)
    at android.os.Handler.dispatchMessage(Handler.java:103)
    at android.os.Looper.loop(Looper.java:203)
    at android.os.HandlerThread.run(HandlerThread.java:61)

What can i do that the view doesn't stay black and shows a working cameraview?


Solution

  • I guess I have found a workaround for that problem:

    in setUserVisibleHint you have to assign a local field with the current visibility of the fragment.

    private boolean isVisibleToUser = false;
    
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        this.isVisibleToUser = isVisibleToUser;
        ...
    

    and check this value in onResume

    @Override
    public void onResume() {
        super.onResume();
        if (isVisibleToUser)
            barcodeView.resume();
    }
    

    I have tested this now on 2 different devices and haven't encountered any other problem after changing the screen orientation or minimizing the app.