无法在片段中启动QRCODE摄像机扫描仪



我有两个Fragments,其中一个我想拥有 QRCODE scanner(使用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的文档,您必须在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中启动。

最新更新