Android Camera: Themes? What should do what

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

The only thing I found in the official documentation is that camera.open() should be placed in its own stream.

What about:

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

It does not indicate which stream they need. Should they run on the main thread (ui thread)? Or can I choose?

Why am I trying to figure this out? camera.startPreview() when launched in the main thread, causes my application to tremble / lag for a short period of time, this greatly affects my application, because it is placed inside the viewPager, and I do not want the camera to always view (which will not cause a delay, but will require system resources).

Any ideas?

+7
java android multithreading camera
source share
1 answer

The documentation for Camera states that the class is not thread safe and should not be called from multiple threads at the same time (I suppose if you are not doing your own synchronization).

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

From the link (my selection):

This class is not thread safe, and is intended to be used from a single event stream . Most lengthy operations (preview, focus, photo capture, etc.) occur asynchronously and call back if necessary. Callbacks will be called in the event from which the open (int) stream was called. These class methods should never be called from multiple threads at the same time.

From the help of the open(int) method:

Callbacks to other methods are delivered to the thread's event loop called open (). If this thread does not have an event loop, then callbacks are passed to the main event loop of the application. If there is no main application event loop, callbacks will not be delivered.

Caution: on some devices this method may take a long time. It is best to call this method from a workflow (possibly using AsyncTask) to avoid blocking the main thread of the application's user interface.

The required thread 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.

+3
source share

All Articles