stud91
stud91

Reputation: 1854

Unable 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 doesn't start and all i can 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>

Upvotes: 4

Views: 3038

Answers (2)

ssv
ssv

Reputation: 1

Scannerview should be started in onResume and should be stopped in onPause, it should not be started in onCreate.

Upvotes: 0

dev.ice
dev.ice

Reputation: 81

You didn't setup the view. According to the documentation from https://github.com/dm77/barcodescanner you've got 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();
}

Upvotes: 4

Related Questions