Android相机:线程?哪个应该做什么 [英] Android camera: Threads? Which should do what

查看:646
本文介绍了Android相机:线程?哪个应该做什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我在官方文档中发现的唯一的东西是 camera.open()应该放在自己的线程中。



如何:




  • camera.startPreview()

  • camera.stopPreview )

  • camera.release()



它没有说明他们需要什么线程。它们必须在主线程(ui线程)上运行吗?或者我可以自由选择吗?



为什么我想弄清楚这一点? camera.startPreview()当在主线程上运行导致我的应用程序抖动/滞后一段时间,这严重影响我的应用程序,因为它被放在一个viewPager和我不希望相机总是预览(这将导致没有滞后,但占用系统资源)。



任何想法?

解决方案

相机声明该类不是线程安全的,并且应该不是一次从多个线程调用(我想,除非你执行自己的同步)。



它表示回调将传递给调用的线程



从引用(强调我):


这个类不是线程安全的,意味着从一个事件线程使用。大多数长时间运行的操作(预览,焦点,照片捕获等)异步发生,并根据需要调用回调。回调将被调用从事件线程打开(int)被调用。


open(int)方法引用:


来自其他方法的回调传递到调用open()的线程的事件循环。如果这个线程没有事件循环,那么回调被传递到主应用程序事件循环。如果没有主应用程序事件循环,则不会传递回调。


注意:在某些设备上,此方法可能需要很长时间完成。最好从工作线程(可能使用AsyncTask)调用此方法,以避免阻塞主应用程序UI线程。



是您用来调用 open(int)的线程。

因此,为了回答你的问题,是的,你可以相对自由地选择,但你必须保持一致。


I am trying to figure out which threads should do what in Android.

The only thing I have found stated in the official documentation is that camera.open() should be put into its own thread.

What about:

  • camera.startPreview()
  • camera.stopPreview()
  • camera.release()

It doesn't state which thread they need. Must they be run on the main thread (ui thread)? Or am I free to choose?

Why am I trying to figure this out? camera.startPreview() when run on the main thread is causing my app to jitter/lag for a short period of time, this heavily affects my application as it is put inside a viewPager and I do not wish to have the camera to always preview (which would cause no lag, but takes up system resources).

Any ideas?

解决方案

The documentation for Camera states that the class is not thread safe and should not be called from multiple threads at once (I suppose, unless you are performing your own synchronization).

It says that the callbacks will be delivered to the thread that makes the call to open

From the reference (emphasis mine):

This class is not thread-safe, and is meant for use from one event thread. Most long-running operations (preview, focus, photo capture, etc) happen asynchronously and invoke callbacks as necessary. Callbacks will be invoked on the event thread open(int) was called from. This class's methods must never be called from multiple threads at once.

From the open(int) method reference:

Callbacks from other methods are delivered to the event loop of the thread which called open(). If this thread has no event loop, then callbacks are delivered to the main application event loop. If there is no main application event loop, callbacks are not delivered.

Caution: On some devices, this method may take a long time to complete. It is best to call this method from a worker thread (possibly using AsyncTask) to avoid blocking the main application UI thread.

The thread it needs is the one you use to call open(int).

So to answer your question, yes you are relatively free to choose, but you must remain consistent.

这篇关于Android相机:线程?哪个应该做什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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