ANKUR KOTWAL: The Camera2 API in Android L
gives you immense control of the camera.
I'm Ankur Kotwal, a developer advocate at Google,
and I'd like to show you how amazing these APIs are.
But first, let's look at the Kit Kat camera API.
It really was designed for the straightforward point and shoot
camera apps, and because of this,
there are limited ways to access streaming image data.
The API also doesn't support any manual capture control methods.
In order to overcome these limitations
and give developers the capability
to build awesome camera apps, we've
introduced a completely new set of APIs
under the Android.hardware.Camera2
package.
Let's take a look.
These new APIs are designed as a pipeline,
instead of a one way stream.
It is now possible to preconfigure output surfaces
and attach them to the camera.
Each surface can be a destination
for a stream of image buffers, and you can pass a request
through the capture session.
Each request will be converted into one result object,
and the destination surface will be filled with image buffers.
It is possible to pass multiple requests simultaneously,
and these requests will be processed asynchronously.
Let's look at what it takes to build a simple camera
app using these new APIs.
First, you need to detect camera hardware.
You can do so by enumerating the existing cameras on a device,
and get the detailed information off the camera
by using the CameraManager service.
You can then use a camera by calling the open camera method
with an ID and a callback list.
When a device is ready, the onOpened method
is invoked with a camera device instance.
This instant is a representation of a single camera.
Before submitting requests, you need
to define a set of buffer output targets
and connect them to the camera device.
This is done by initiating the camera capture session.
For example, you can use TextureView for previewing,
MediaRecorder for recording a video,
or the RenderScriptAllocation for YUV processing.
When you create a surface, the size of the surface
is important.
The camera device only supports a certain type of size,
and therefore you must set the valid size for each surface.
First, we get the available stream configurations
that the camera supports by the scalar stream configuration
map.
The size should be one of the results
from the getOutputSizes method.
For this example, we're just going to grab the first one,
but in a real world scenario, you
may want to look through this array
and pick the appropriate size.
After creating the necessary surface,
you can call the createCaptureSession method.
Be patient, as creating a session can require
several hundred milliseconds.
Once the session is ready, you can deal with the requests.
Based on your needs, you can create a request builder
by using one of the pre-defined templates.
Then you can build the capture request
by calling the build method in the build up.
Here, TEMPLATE_PREVIEW is used for the preview request,
and TEMPLATE_STILL_CAPTURE is used for the JPEG capture
request.
So now, you have both a session and a request,
and there are two options to submit requests.
Capture is to capture one image using a given capture request.
On the other hand, setRepeatingRequest
is for capturing images continuously
by using a given request again and again until you explicitly
stop it.
Both capture and setRepeatingRequests
have overloads that take a list of capture requests.
CaptureBurst for capture, and setRepeatingBurst
for set repeating requests.
However, before submitting the capture request,
you might need to adjust the camera
settings as per your needs.
It is possible to modify any properties of the builder
before building a request.
These can be things like white balance mode, effects,
and even flash settings.
There are some really great effects
that you can apply, like negatives, and sepia,
and solarize, and more.
You can interact with exposure and focus settings precisely
using CONTROL_AF_TRIGGER and CONTROL_AE_PRECAPTURE_TRIGGER.
Be careful though.
If you forget to set focus properly,
you can end up with an image that's a little bit blurry.
Whenever you submit requests, you
can pass a capture listener callback
to keep track of capture progress.
For example, you can use onCaptureStarted
for playing a shutter sound or for playing a capture
animation.
And after a capture request is processed,
onCaptureCompleted will be called with a total capture
request result instance.
At this moment, TotalCaptureResult
contains the final configuration of the capture process,
and the destination surfaces will soon
be sent the final captured data.
You can access the output image through the surface,
and then it's up to you on how to use that image.
In a basic camera app, you could get the bytes buffer
of the image from the image reader,
and stored it on to the SD card.
So this is the new Camera2 API.
It is designed as a very flexible and unified pipeline
for all camera related tasks, and it also
increases the ability of applications
to control the camera subsystem.
You can discover more details about the Camera2 API
and useful samples at developer.Android.com
Be sure to check out the Android Developer
Preview for more new APIs.
Thank you.