Detecting camera features with Camera2

Detecting camera features with Camera2

Original link: https://medium.com/google-developers/detecting-camera-features-with-camera2-61675bb7d1bf#.ks8l788lx

My phone's camera has its own characteristics. It has flash, burst shooting, and even has a built-in noise reduction point. Developers can use these advantageous personalization features to provide better photo applications. However, not all cameras are the same, and some may lack the features mentioned above. As a developer, how do you know which features can and cannot be used? Camera2 tells you.

Why use Camera2? (Why Camera2)

Introduced in Android L, the Camera2 API is the successor to the original Camera API interface.

Camera2 is for those photo apps that require direct access to the device's camera. If the camera is just a side function of your application experience, then using the Camera Intent is sufficient. However, if your app needs to customize the camera experience, the Camera2 API is a good choice.

Camera2 inherits some of the advantages of its original interface:

  • Improved performance on new hardware.

  • The interval between pictures is faster

  • Show previews for multiple cameras

  • Apply effects and filters directly

A few other notable advantages, you can now query the API to detect the capabilities required by the camera.

Banning Selfies with Camera2

Now, let's say you're building an anti-selfie photo app. If the user tries to flip the front-facing camera, you'll send a Toast to let them know that your app doesn't have this feature.

The Camera2 API will tell you if there is a front camera, but first you need to get a list of cameras available on the device.

Step1: Get Camera (Get a Camera)

The core of the Camera2 API is the CameraManager class. You can use getCameraIdList() to get the camera's string array id through CameraManager. The id of the camera represents the camera available on the device. Using the getCameraCharactertics() method, you can pass in the id of the camera and get the settings and output parameters available to 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();
}

After getting the camera's characteristics, you are ready to query.

Step2: Query the characteristics

Once you get the CameraCharacteristics object, you can query the features available on the device. The get() method needs to pass in a CameraCharacteristic field, and then returns the value corresponding to this field.

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

Step3: Ban Selfies

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

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 completed. Any selfie will now pop up a prompt letting the user know that your app doesn't support the use of the front-facing camera.

Detecting other features

What other features can be detected using this API? A lot, there are 78 CameraCharacteristics here, so, so there's quite a bit of functionality to explore here.

  • FLASH_INFO_AVAILABLE : Does the camera have a flash?

  • REQUEST_AVAILABLE_CAPABILITIES_BURST_CAPTURE : Can the camera take high-resolution photos greater than 20fps? Then you can add burst mode.

  • JPEG_AVAILABLE_THUMBNAIL_SIZES : What JPEG thumbnail sizes are supported by the device?

Camera2 on Github

If you are ready to learn more about Camera2, the official Github example is a very good start. The examples go further, including how to display camera previews and take pictures.

Get ready to develop new camera applications using Camera2 API.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325532154&siteId=291194637