是否需要从UI线程调用CameraSource.stop()? [英] Does CameraSource.stop() require to be called from UI Thread?

查看:90
本文介绍了是否需要从UI线程调用CameraSource.stop()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用从Mobile Vision使用Google条码扫描器API的Android应用程序.该应用程序的目的是检测条形码,然后根据与条形码关联的数据采取一些措施.

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.

一旦检测到第一个条形码,我想停止摄像机源,以使检测不再继续.当我尝试在receiveDetections(Detector.Detections<Barcode> detections)回调中执行cameraSource.stop()时,线程被阻塞,并且logcat中有很多日志输出.由于此回调未在UI线程中执行,因此UI保持未阻塞状态.我尝试在UI线程中执行cameraSource.stop(),效果很好.

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.

我尝试从UI线程以及另一个线程中调用cameraSource.start(),两者都可以正常工作.

I have tried calling cameraSource.start() from UI thread as well as another thread and both work fine.

现在,在文档中找不到与cameraSource交互应来自UI线程或工作线程的任何地方.我无法弄清楚为什么从另一个线程调用时此操作将失败的逻辑.

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.

推荐答案

不必从UI线程调用CameraSource.stop(),但是由于实现细节,不应从执行receiveDetections的线程中调用它. stop()代码等待该线程完成,因此以这种方式调用它将创建死锁.

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.

这篇关于是否需要从UI线程调用CameraSource.stop()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆