Credntia / MVBarcodeReader

A Barcode scanner library for Android. Uses the Google Play Services' mobile vision api for barcode detection.
Apache License 2.0
68 stars 22 forks source link

Aspect ration of the barcodeFragment is not set correctly when used in FragmentDialog #15

Closed roostaamir closed 6 years ago

roostaamir commented 7 years ago

Hi again and thank you again for this library I am trying to create a dialog in the activity in which the BarcodeFragment lives. I thought that my only option (probably) is a DialogFragment So I created one like this:

public class BarcodeScannerDialog extends DialogFragment {

        private int[] formats = { Barcode.EAN_13, Barcode.EAN_8 };
        private BarcodeCaptureFragment barcodeCaptureFragment;
        private BarcodeCaptureFragment.BarcodeScanningListener scanningListener;
        private Method initiateCameraMethod = null;

        @Nullable
        @Override
        public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
            prepareBarcodeScanner();
            getChildFragmentManager().beginTransaction().replace(R.id.linearLayout_camera_root, barcodeCaptureFragment).commit();
            return inflater.inflate(R.layout.dialog_barcode_scanner, container, false);
        }

        private void prepareBarcodeScanner() {
            barcodeCaptureFragment = BarcodeCaptureFragment.instantiate(
                    MVBarcodeScanner.ScanningMode.SINGLE_AUTO, formats);
            scanningListener = new BarcodeCaptureFragment.BarcodeScanningListener() {
                @Override
                public void onBarcodeScanned(Barcode barcode) {
                     //do something with the barcode
                }

                @Override
                public void onBarcodesScanned(List<Barcode> barcodes) {

                }

                @Override
                public void onBarcodeScanningFailed(String reason) {

                }
            };
            barcodeCaptureFragment.setListener(scanningListener);
        }
    }

this is the dialog's layout file:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="match_parent"
              android:layout_height="match_parent">

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/linearLayout_camera_root"/>

</LinearLayout>

The problem is that the camera viewfinder will get either expanded out of the dialog(galaxy s8+), or it will be too small for the whole dialog that a white bar is still visible above the camera viewfinder. Is there a way or a suggestion you can give me to achieve my goal?

iamMehedi commented 7 years ago

I think the issue here is that you are embedding the fragment within another fragment (so it's a child fragment) and somehow it's failing to calculate the right aspect fit for the fragment's size. I'll look into it when I can manage some time. If you are trying to solve it yourself then look into onLayout method of the CameraSourcePreview.java file. The issue is most probably in that function.

roostaamir commented 7 years ago

This is not the same issue as issue #16 that you closed. With this issue, the camera preview is out of bounds, and not stretched. With issue #16, the camera preview is not out of bounds but it is stretched. In that issue, when the activity is paused and then resumed again, the camera preview will be fixed. Please reopen the issue. Could you reopen issue #16 and help me with that one too?

iamMehedi commented 6 years ago

@roostaamir can you pleas test the ScannerDialog in the issue_15_16 branch (just tap the Scan with Dialog button) and let me know if you can still reproduce the issue?

iamMehedi commented 6 years ago

closing as it seems to be solved. Reopen if you still face this issue.