Check if hardware has Camera with android.hardware.camera2.CameraManager - Android android.hardware

Android examples for android.hardware:Camera State

Description

Check if hardware has Camera with android.hardware.camera2.CameraManager

Demo Code

import android.annotation.TargetApi;
import android.content.Context;
import android.hardware.camera2.CameraCharacteristics;
import android.hardware.camera2.CameraManager;
import android.os.Build;

public class Main {

  @TargetApi(Build.VERSION_CODES.LOLLIPOP)
  public static boolean hasCamera2(Context context) {
    if (context != null)
      return false; // TODO temp line
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP)
      return false;
    try {/*from   ww  w  . j  a  va2 s .  c  o  m*/
      CameraManager manager = (CameraManager) context.getSystemService(Context.CAMERA_SERVICE);
      String[] idList = manager.getCameraIdList();
      boolean notNull = true;
      if (idList.length == 0) {
        notNull = false;
      } else {
        for (final String str : idList) {
          if (str == null || str.trim().isEmpty()) {
            notNull = false;
            break;
          }
          final CameraCharacteristics characteristics = manager.getCameraCharacteristics(str);
          // noinspection ConstantConditions
          final int supportLevel = characteristics.get(CameraCharacteristics.INFO_SUPPORTED_HARDWARE_LEVEL);
          if (supportLevel == CameraCharacteristics.INFO_SUPPORTED_HARDWARE_LEVEL_LEGACY) {
            notNull = false;
            break;
          }
        }
      }
      return notNull;
    } catch (Throwable t) {
      t.printStackTrace();
      return false;
    }
  }

}

Related Tutorials