Detecting camera features with Camera2

David East
Mar 10, 2016 · 4 min read

My phone’s camera has a personality of it’s own. It has a flash, it faces backwards, it can take burst shots, and it even has built-in noise reduction. Developers can take distinct advantage of these personality traits to provide better photo apps. But, not all cameras are created equal, and some may be lacking the features above. As a developer, how do you know what features you can and cannot use? Just ask Camera2.

Why Camera2?

The Camera2 API was introduced in Android L and the successor to the original Camera API.

Camera2 is for powerful photos apps who need direct access to the device’s cameras. If the camera is just an accessory to your app’s experience, then the Camera Intent is probably the way to go. But, if your app needs a custom camera experience then the Camera2 API is the right choice.

Camera2 has some serious benefits of its predecessor.

  • Improved performance on newer hardware
  • Take images at faster intervals
  • Show previews from multiple cameras
  • Apply effects and filters directly

In additional to all of the awesome benefits, you can now query the API to detect needed features from the camera.

Banning Selfies with Camera2

Now let’s say you’re building an anti-selfie photo app. If the user tries to flip to the front camera, you’ll send them a Toast letting them know your app will have none of that.

The Camera2 API can tell you if the front-facing camera exists, but first you’ll need to get the list of cameras available on the device.

Step 1: Get a Camera

The center-piece of the Camera2 API is the CameraManager class. Using CameraManager you can get a string array of camera ids using getCameraIdList(). The camera ids represent the cameras available on the device. Using the method getCameraCharactertics(), you can pass in the camera id and get the available settings and output parameters for the device.

With the characteristics for the camera in hand, you’re ready to query.

Step 2: Query the characteristics

Once you have the CameraCharacteristics object, you’re able to query the available characteristics on the device. The get() method expects a CameraCharacteristic field, and it will return to you the value of the field.

In the example above the LENS_FACING field is used to get the current direction of the camera. With this knowledge, you can now ban all selfies.

Step 3: Ban Selfies

The facing variable is just an integer. How do you know what direction the camera is actually facing? The integer represents a CameraMetadata constant. Using the LENS_FACING_FRONT constant, you can detect if the lens faces forwards.

Mission accomplished. Now any selfie taker will get a little toast, letting them know your app isn’t for their front facing camera.

Detecting other features

What else can you detect with this API? A lot. There are 78 CameraCharacteristics, so there’s plenty to explore.

Camera2 on Github

If you’re ready to give Camera2 a spin, the official Github sample is a great place to start. The sample goes even further, covering how to display a camera preview and take pictures.

Get ready for a new dawn of camera apps using the Camera2 API.


Follow the Android Development Patterns Collection for more!

Google Developers

Engineering and technology articles for developers, written…

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store