Android camera: Threads? Which should do what

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.

    链接地址: http://www.djcxy.com/p/26130.html

    上一篇: 坏的Alloc与200GB内存可用C ++

    下一篇: Android相机:线程? 哪个应该做什么