dlazaro66 / QRCodeReaderView

Modification of ZXING Barcode Scanner project for easy Android QR-Code detection and AR purposes
1.9k stars 492 forks source link

Using QRCodeReaderView in Fragments #7

Open carlosharlens opened 9 years ago

carlosharlens commented 9 years ago

Hi,

I just use this lib on a fragment. it runs like a charm but, once container fragment lost focus, it is imposible to get qrCodeReaderView working again. Dont know why stops reading.

Please, help!.

best regards!

dlazaro66 commented 9 years ago

Hi Carlos,

I'll check it when I have some free time. Did you try in other different devices?

Thanks, best regards.

carlosharlens commented 9 years ago

Yes I did. non results.

I cant understand why not?. because fragments get destroyed and recreated each time.

HaoCherHong commented 9 years ago

Hello guys, I got the same problem here and I've found a possible solution for you. I guess you called qrCodeReaderView.getCameraManager().stopPreview() somewhere after your fragment paused. If you did so, in some case after stop previewing the PreviewCallback implementation will be detached from camera. so the following line can fixed it magically, add them before where you call startPreview() : qrCodeReaderView.getCameraManager().getCamera().setPreviewCallback(qrCodeReaderView);

@dlazaro66 I guess you can create a method in the ReaderView class to do startPreview() and stopPreview() and added the line above into it :)

hope this help you!

ekzee commented 8 years ago

Got this issue as well on Samsung S3 by blocking and unblocking screen (Im using version 2.0.1). After screen unblocked, onPreviewFrame stops being called. Adding mCameraManager.setPreviewCallback(this); in startCamera method of the QRCodeReaderView fixed the issue.

xiejinpeng007 commented 7 years ago

I encountered the same issue , and I found that the mCameraManager is private filed , how can I do that?

I solve this by using Reflection.

    // invoke this method before startCamera();
    private void resetPreViewCallback() {
        try {
            Field field = QRCodeReaderView.class.getDeclaredField("mCameraManager");
            field.setAccessible(true);
            CameraManager cameraManager = (CameraManager) (field.get(binding.qrCodeReaderView));
            cameraManager.setPreviewCallback(
                    (data, camera) -> binding.qrCodeReaderView.onPreviewFrame(data, camera));

        } catch (NoSuchFieldException | IllegalAccessException | SecurityException e) {
            e.printStackTrace();
        }
    }
ekzee commented 7 years ago

Just copy QRCodeReaderView class to your project and fix it there