Does CameraSource.stop() require to be called from UI Thread?

醉酒当歌 提交于 2019-12-02 18:06:54

问题


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.


回答1:


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!