I am working on an android app that uses Google Barcode Scanner API from Mobile Vision. The purpose of app is to detect barcode and then take some action based on the data associated with barcode.
As soon as first barcode is detected, I want to stop the camera source so that the detection shouldn't continue. When I try to execute cameraSource.stop()
inside receiveDetections(Detector.Detections<Barcode> detections)
callback, the thread gets blocked and there is a lot of log output in logcat. Since this callback is not executed in UI thread, UI remains unblocked. I tried executing cameraSource.stop()
in UI thread and it worked fine.
I have tried calling cameraSource.start()
from UI thread as well as another thread and both work fine.
Now I couldn't find it anywhere in the documentation that interactions with cameraSource should be from UI thread or worker thread. I couldn't figure out the logic for why this would fail when called from another thread.
It's not necessary to call CameraSource.stop() from the UI thread, but due to an implementation detail it should not be called from the thread that executes receiveDetections. The stop() code waits for this thread to finish, so calling it this way would create a deadlock.
来源:https://stackoverflow.com/questions/41004388/does-camerasource-stop-require-to-be-called-from-ui-thread