Failed to start qrCode Camera scanner in fragment

I have two Fragments

and in one of them I want to have a QRcode scanner (using ZXingScannerView

). However, the camera won't start and all I see is a black screen. Here is my implementation:

QRCodeReaderFragment.java

private ZXingScannerView mScannerView;
private LinearLayout qrCameraLayout;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    View fragmentView =  inflater.inflate(R.layout.fragment_qr_code_reader, container, false);

    qrCameraLayout = (LinearLayout) fragmentView.findViewById(R.id.ll_qrcamera);

    mScannerView = new ZXingScannerView(getActivity().getApplicationContext());

    mScannerView.setLayoutParams(new LinearLayout.LayoutParams(
            LinearLayout.LayoutParams.MATCH_PARENT,
            LinearLayout.LayoutParams.MATCH_PARENT));

    qrCameraLayout.addView(mScannerView);

    return fragmentView;
}

      

fragment_qr_code_reader.xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.letsguang.android.shoppingmalltenant.fragment.QrCodeReaderFragment"
    android:id="@+id/fl_qrcamera"
    >

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:id="@+id/ll_qrcamera"
        android:orientation="vertical">




    </LinearLayout>

</FrameLayout>

      

+3


source to share


2 answers


You have not configured the view. According to the documentation from https://github.com/dm77/barcodescanner you need to start / stop the camera in onResume / onPause:



@Override
public void onResume() {
    super.onResume();
    mScannerView.setResultHandler(this);
    mScannerView.startCamera();
}

@Override
public void onPause() {
    super.onPause();
    mScannerView.stopCamera();
}

      

+2


source


Scannerview should be started at onResume

and should be stopped at onPause

, it should not be started at onCreate

.



0


source







All Articles