journeyapps / zxing-android-embedded

Barcode scanner library for Android, based on the ZXing decoder
https://journeyapps.com/
Apache License 2.0
5.73k stars 1.27k forks source link

java.lang.IllegalStateException: containerSize or previewSize is not set yet #122

Closed HectorFGarcia closed 8 years ago

HectorFGarcia commented 8 years ago

I tried this lib on four devices (4.1.2, 4.2.2, 6.0.1, 4.0.1). It works fine on three of them, but the last one (4.0.1) is getting this exception.

I was tracking down where the problem start and it comes from here:

previewSized((Size) message.obj);

That "message.obj" is null so "size" is null too. Also, before the this exception, I'm getting this warning in logcat:

 W/CameraManager: Camera rejected even safe-mode parameters! No configuration

And it comes from here:

defaultParameters = parameters.flatten();

That line provokes this exception:

 java.lang.StringIndexOutOfBoundsException: length=0; index=-1

So I don't know why this happens. I'm using barcodeScannerView.decodeContinuous(..) inside a Fragment, by the way. And as I said befote, It works fine in all devices excepts one.

P.S. While I was looking into the code, I saw this too:

  try {
        setDesiredParameters(false);
    } catch (Exception e) {
        // Failed, use safe mode
        try {
            setDesiredParameters(false);
        } catch (Exception e2) {
            // Well, darn. Give up
            Log.w(TAG, "Camera rejected even safe-mode parameters! No configuration");
        }

Shouldn't be "setDesiredParameters(true);" in the second case?

rkistner commented 8 years ago

You're right, the parameter should be true in that case.

Does the "Barcode Scanner" application from the play store work on that device?

HectorFGarcia commented 8 years ago

I'll try the app, but now I was checking the examples and "ContinuousCaptureActivity" example works.

Could be because I'm using the CompoundBarcodeView inside a Fragment instead an Activity? I don't know what I'm missing if examples works and my code don't but in other devices it does.

My code is really simple:

    barcodeScannerView = (CompoundBarcodeView) v.findViewById(R.id.barcode_scanner);
    barcodeScannerView.decodeContinuous(new BarcodeCallback() {
        @Override
        public void barcodeResult(BarcodeResult result) {
            // ...
        }

        @Override
        public void possibleResultPoints(List<ResultPoint> resultPoints) {
            // ...
        }
    });

....

 scan.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (barcodeScannerView.getVisibility() == View.GONE) {
                barcodeScannerView.resume();
                barcodeScannerView.setVisibility(View.VISIBLE);
            } else {
                barcodeScannerView.setVisibility(View.GONE);
                barcodeScannerView.pause();
            }
            //scanner.scan();
        }
    });

....

@Override
public void onResume() {
    super.onResume();
    if (barcodeScannerView.getVisibility() == View.VISIBLE)
        barcodeScannerView.resume();
}

....

<com.journeyapps.barcodescanner.CompoundBarcodeView
    android:id="@+id/barcode_scanner"
    android:layout_width="match_parent"
    android:layout_height="180dp"
    android:layout_alignParentBottom="true"
    android:layout_centerInParent="true"
    android:layout_marginBottom="70dp"
    android:visibility="gone"
    app:zxing_scanner_layout="@layout/custom_barcode_scanner"/>
rkistner commented 8 years ago

Not sure if it has an effect here, but some devices don't like it if the preview is not visible. You can try calling resume() only after the setVisibility() call. Alternatively, you can try creating a new component and add/remove it dynamically instead of doing the show/hide.

HectorFGarcia commented 8 years ago

I removed all the views stuff and still nothing. I just use this:

barcodeScannerView = (CompoundBarcodeView) v.findViewById(R.id.barcode_scanner);
barcodeScannerView.decodeContinuous(new BarcodeCallback() {
    @Override
    public void barcodeResult(BarcodeResult result) {
        // ...
    }

    @Override
    public void possibleResultPoints(List<ResultPoint> resultPoints) {
        // ...
    }
});
barcodeScannerView.resume();

And the same XML without the visibility.

Same exception :(

HectorFGarcia commented 8 years ago

Update: If I reboot the device, then the scanner works until I finish the app. At the second start, the exception is back again. In the example, scanner works all the time, so WTF.

HectorFGarcia commented 8 years ago

FINALLY!

It was all because a library wich somehow interferes with the camera. That lib is only loaded in this device, so that explains why the other devices works fine and why example works too while the app doesn't. I don't know how that library interferes the camera, apparently it's not related to it.

Sorry about wasting your time, at least I found a little bug in the process.

Great library, by the way, I tested a lot of them and this one is awesome.

yegemberdin commented 4 years ago

Hi, how the problem is solved? @HectorFGarcia @rkistner

oleksandr-riabykh commented 9 months ago

I'm not using the camera at all. only the lib is using it but has the same exception. @HectorFGarcia @rkistner so what was the solution?