无法在片段中启动 qrCode 相机扫描仪

Unable to start qrCode Camera scanner in fragment

我有两个 Fragments,其中一个我想有一个 QRcode 扫描器(使用 ZXingScannerView)。但是,相机没有启动,我只能看到黑屏。这是我的实现:

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>

您没有设置视图。根据 https://github.com/dm77/barcodescanner 的文档,您必须 start/stop onResume/onPause 中的相机:

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

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

Scannerview 应在 onResume 中启动,应在 onPause 中停止,不应在 onCreate 中启动。