Reputation: 345
I implemented QR code scanner with result handler. It is scanning well. But, after first scanning is complete, camera gets stuck. How to implement QR-scanning code so that camera continues scanning without getting stuck?
My code looks like this:
public class SimpleScannerFragment extends Fragment implements ZXingScannerView.ResultHandler {
private ZXingScannerView mScannerView;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.scannerview, null);
mScannerView = (ZXingScannerView) v.findViewById(R.id.scanner_view);
return v;
}
@Override
public void onResume() {
super.onResume();
mScannerView.startCamera();
mScannerView.setResultHandler(this);
}
@Override
public void handleResult(Result rawResult) {
ParsedResult parserdResult = ResultParser.parseResult(rawResult);
Toast.makeText(getActivity(), "Contents = " + rawResult.getText() + ", Format = " + rawResult.getBarcodeFormat().toString(), Toast.LENGTH_SHORT).show();
}
@Override
public void onPause() {
super.onPause();
mScannerView.stopCamera();
}
}
Upvotes: 3
Views: 4028
Reputation: 59
@viper, the best approach which worked for me all the way.
in the onPostExecute
start the camera again and resumecamerapreview.
if you display a dialog in the handleResult or onPostExecute, Dmitri's answer might not work very well.
if (scannerView != null){
scannerView.startCamera();
scannerView.setAutoFocus(true); //not necessary
scannerView.resumeCameraPreview(ScanActivity.this);
}
this starts the camera and refreshes it
Upvotes: 1
Reputation: 1
resumeCameraPreview(resultHandler: ZXingScannerView.ResultHandler)
startCamera()
2 lines of code will make scanner start camera and resume again then continuously scan after scan
Upvotes: 0
Reputation: 2299
You must resume the camera after pause camera
Test this code
public class SimpleScannerFragment extends Fragment implements ZXingScannerView.ResultHandler {
private ZXingScannerView mScannerView;.
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.scannerview, null);
mScannerView = (ZXingScannerView) v.findViewById(R.id.scanner_view);
return v;
}
@Override
public void onStart() {
super.onStart();
mScannerView.startCamera();
mScannerView.setResultHandler(this);
}
@Override
public void handleResult(Result rawResult) {
ParsedResult parserdResult = ResultParser.parseResult(rawResult);
Toast.makeText(getActivity(), "Contents = " + rawResult.getText() + ", Format = " + rawResult.getBarcodeFormat().toString(), Toast.LENGTH_SHORT).show();
mScannerView.resumeCameraPreview(this);
}
@Override
public void onStop() {
mScannerView.stopCamera();
super.onStop();
}
}
Upvotes: 0
Reputation: 1474
According to manual(https://github.com/dm77/barcodescanner) just resume your cammera in handler:
@Override
public void handleResult(Result rawResult) {
ParsedResult parserdResult = ResultParser.parseResult(rawResult);
Toast.makeText(getActivity(), "Contents = " + rawResult.getText() + ", Format = " + rawResult.getBarcodeFormat().toString(), Toast.LENGTH_SHORT).show();
mScannerView.resumeCameraPreview(this);
}
Upvotes: 6