我在片段中使用扫描仪。首次尝试扫描仪工作正常,但当我移动到新的活动并返回时,则不会显示扫描仪。
public class SampleFragment extends Fragment implements ZXingScannerView.ResultHandler{
private ZXingScannerView mScannerView;
private LinearLayout qrCameraLayout;
@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());
mScannerView.setLayoutParams(new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.MATCH_PARENT));
mScannerView.setResultHandler(this);
mScannerView.startCamera();
qrCameraLayout.addView(mScannerView);
return fragmentView;
}
@Override
public void onResume`enter code here`()
{
super.onResume();
if(mScannerView==null)
mScannerView.resumeCameraPreview(this);
//after I switch to new activity and reopen, then scanner is not shown
}
@Override
public void onPause()
{
super.onPause();
mScannerView.resumeCameraPreview(this);
mScannerView=null;
}
@Override
public void handleResult(Result result) {
Toast.makeText(getActivity(),result.getText().toString(),Toast.LENGTH_LONG).show();
}
}
答案 0 :(得分:0)
尝试将onResume和onPause方法更改为此
@Override
public void onResume() {
super.onResume();
mScannerView.setResultHandler(this); // Register ourselves as a handler for scan results.
mScannerView.startCamera(); // Start camera on resume
}
@Override
public void onPause() {
super.onPause();
mScannerView.stopCamera(); // Stop camera on pause
}