Detecting camera features with Camera2

David East
Google Developers
Published in
4 min readMar 10, 2016

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.

CameraManager manager =
(CameraManager)getSystemService(CAMERA_SERVICE);
try {
for (String cameraId : manager.getCameraIdList()) {
CameraCharacteristics chars
= manager.getCameraCharacteristics(cameraId);
// Do something with the characteristics
} catch (CameraAccessException e) {
e.printStackTrace();
}

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.

// Does the camera have a forwards facing lens?
Integer facing = chars.get(CameraCharacteristics.LENS_FACING);

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.

private void detectSelfieCamera(String cameraId) {  CameraCharacteristics chars
= manager.getCameraCharacteristics(cameraId);
// Does the camera have a forwards facing lens?
Integer facing = chars.get(CameraCharacteristics.LENS_FACING);
if (facing != null && facing ==
CameraCharacteristics.LENS_FACING_FRONT) {
// No selfies!
Context context = getApplicationContext();
CharSequence text = “No selfie for you! Turn the camera around”;
int duration = Toast.LENGTH_SHORT;

Toast toast = Toast.makeText(context, text, duration)
toast.show();
// don’t process anything for the front facing camera
continue;
} else {
// Open the rear facing camera (see github repo below)
}
}

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.

#BuildBetterApps

Follow the Android Development Patterns Collection for more!

--

--

David East
Google Developers

Developer Advocate at Google. Working on the Firebases.