Java Code Examples for android.hardware.camera2.CameraManager#getCameraCharacteristics()

The following examples show how to use android.hardware.camera2.CameraManager#getCameraCharacteristics() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CameraWrapper.java    From DeviceConnect-Android with MIT License 6 votes vote down vote up
private static Integer choiceAutoFocusMode(final Context context,
                                           final CameraManager cameraManager,
                                           final String cameraId) throws CameraAccessException {
    PackageManager pkgMgr = context.getPackageManager();
    if (!pkgMgr.hasSystemFeature(PackageManager.FEATURE_CAMERA_AUTOFOCUS)) {
        return null;
    }

    CameraCharacteristics characteristics = cameraManager.getCameraCharacteristics(cameraId);
    int[] afModes = characteristics.get(CameraCharacteristics.CONTROL_AF_AVAILABLE_MODES);
    if (afModes == null) {
        return null;
    }
    for (int i = 0; i < afModes.length; i++) {
        if (afModes[i] == CameraMetadata.CONTROL_AF_MODE_CONTINUOUS_PICTURE) {
            return afModes[i];
        }
    }
    return null;
}
 
Example 2
Source File: CameraWrapper.java    From DeviceConnect-Android with MIT License 6 votes vote down vote up
private static Integer choiceAutoExposureMode(final CameraManager cameraManager,
                                              final String cameraId) throws CameraAccessException {
    CameraCharacteristics characteristics = cameraManager.getCameraCharacteristics(cameraId);
    int[] aeModes = characteristics.get(CameraCharacteristics.CONTROL_AE_AVAILABLE_MODES);
    if (aeModes == null) {
        return null;
    }
    for (int i = 0; i < aeModes.length; i++) {
        if (aeModes[i] == CameraMetadata.CONTROL_AE_MODE_ON_AUTO_FLASH) {
            return aeModes[i];
        }
    }
    for (int i = 0; i < aeModes.length; i++) {
        if (aeModes[i] == CameraMetadata.CONTROL_AE_MODE_ON) {
            return aeModes[i];
        }
    }
    return null;
}
 
Example 3
Source File: Camera2Helper.java    From DeviceConnect-Android with MIT License 6 votes vote down vote up
/**
 * カメラID に対応したカメラデバイスがサポートしている写真サイズのリストを取得します.
 *
 * @param cameraManager カメラマネージャ
 * @param cameraId カメラID
 * @return サポートしている写真サイズのリスト
 */
@NonNull
static List<Size> getSupportedPictureSizes(final CameraManager cameraManager, final String cameraId) {
    List<Size> pictureSizes = new ArrayList<>();
    try {
        CameraCharacteristics characteristics = cameraManager.getCameraCharacteristics(cameraId);
        StreamConfigurationMap map = characteristics.get(CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP);
        if(map != null) {
            pictureSizes = Arrays.asList(map.getOutputSizes(ImageFormat.JPEG));
            Collections.sort(pictureSizes, SizeComparator);
        }
    } catch (CameraAccessException e) {
        // ignore.
    }
    return pictureSizes;
}
 
Example 4
Source File: Camera2Utils.java    From libcommon with Apache License 2.0 6 votes vote down vote up
/**
 * 指定した条件に合う解像度を選択する
 * @param manager
 * @param cameraId
 * @param targetFace
 * @param width
 * @param height
 * @param degrees
 * @return
 * @throws CameraAccessException
 */
public static CameraConst.CameraInfo chooseOptimalSize(
	@NonNull final CameraManager manager,
	final String cameraId, @CameraConst.FaceType final int targetFace,
	final int width, final int height, final int degrees)
		throws CameraAccessException {

	if (DEBUG) Log.v(TAG,
		String.format("chooseOptimalSize:Size(%dx%d),targetFace=%d,degrees=%d",
			width, height, targetFace, degrees));

	final CameraCharacteristics characteristics
		= manager.getCameraCharacteristics(cameraId);
	final StreamConfigurationMap map = characteristics.get(
			CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP);
	final Size previewSize
		= chooseOptimalSize(characteristics, map, width, height, degrees);
	final int orientation = characteristics.get(CameraCharacteristics.SENSOR_ORIENTATION);
	if (!TextUtils.isEmpty(cameraId) && (previewSize != null)) {
		return new CameraConst.CameraInfo(cameraId, targetFace, orientation,
			previewSize.getWidth(), previewSize.getHeight());
	}
	return null;
}
 
Example 5
Source File: Camera2Enumerator.java    From webrtc_android with MIT License 6 votes vote down vote up
/**
 * Checks if API is supported and all cameras have better than legacy support.
 */
public static boolean isSupported(Context context) {
  if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
    return false;
  }

  CameraManager cameraManager = (CameraManager) context.getSystemService(Context.CAMERA_SERVICE);
  try {
    String[] cameraIds = cameraManager.getCameraIdList();
    for (String id : cameraIds) {
      CameraCharacteristics characteristics = cameraManager.getCameraCharacteristics(id);
      if (characteristics.get(CameraCharacteristics.INFO_SUPPORTED_HARDWARE_LEVEL)
          == CameraCharacteristics.INFO_SUPPORTED_HARDWARE_LEVEL_LEGACY) {
        return false;
      }
    }
  } catch (AndroidException e) {
    Logging.e(TAG, "Camera access exception: " + e);
    return false;
  }
  return true;
}
 
Example 6
Source File: Camera2Helper.java    From DeviceConnect-Android with MIT License 6 votes vote down vote up
/**
 * カメラID に対応したカメラデバイスがサポートしているプレビューサイズのリストを取得します.
 *
 * @param cameraManager カメラマネージャ
 * @param cameraId カメラID
 * @return サポートしているプレビューサイズのリスト
 */
@NonNull
static List<Size> getSupportedPreviewSizes(final CameraManager cameraManager, final String cameraId) {
    List<Size> previewSizes = new ArrayList<>();
    try {
        CameraCharacteristics characteristics = cameraManager.getCameraCharacteristics(cameraId);
        StreamConfigurationMap map = characteristics.get(CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP);
        if (map != null) {
            previewSizes = Arrays.asList(map.getOutputSizes(SurfaceTexture.class));
            Collections.sort(previewSizes, SizeComparator);
        }
    } catch (CameraAccessException e) {
        // ignore.
    }
    return previewSizes;
}
 
Example 7
Source File: BaseCameraActivity.java    From fritz-examples with MIT License 5 votes vote down vote up
private String chooseCamera() {
    final CameraManager manager = (CameraManager) getSystemService(Context.CAMERA_SERVICE);
    try {
        for (final String cameraId : manager.getCameraIdList()) {
            final CameraCharacteristics characteristics = manager.getCameraCharacteristics(cameraId);

            // We don't use a front facing camera in this sample.
            final Integer facing = characteristics.get(CameraCharacteristics.LENS_FACING);
            if (facing != null && facing == CameraCharacteristics.LENS_FACING_FRONT) {
                continue;
            }

            final StreamConfigurationMap map =
                    characteristics.get(CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP);

            if (map == null) {
                continue;
            }

            // Fallback to camera1 API for internal cameras that don't have full support.
            // This should help with legacy situations where using the camera2 API causes
            // distorted or otherwise broken previews.
            useCamera2API = (facing == CameraCharacteristics.LENS_FACING_EXTERNAL)
                    || isHardwareLevelSupported(characteristics,
                    CameraCharacteristics.INFO_SUPPORTED_HARDWARE_LEVEL_FULL);
            Log.i(TAG, "Camera API lv2?: " + useCamera2API);
            return cameraId;
        }
    } catch (CameraAccessException e) {
        Log.e(TAG, "Not allowed to access camera: " + e);
    }

    return null;
}
 
Example 8
Source File: CameraActivity.java    From next18-ai-in-motion with Apache License 2.0 5 votes vote down vote up
private String chooseCamera() {
    final CameraManager manager = (CameraManager) getSystemService(Context.CAMERA_SERVICE);
    try {
        for (final String cameraId : manager.getCameraIdList()) {
            final CameraCharacteristics characteristics = manager.getCameraCharacteristics(cameraId);

            // We don't use a front facing camera in this sample.
            final Integer facing = characteristics.get(CameraCharacteristics.LENS_FACING);
            if (facing != null && facing == CameraCharacteristics.LENS_FACING_FRONT) {
                continue;
            }

            final StreamConfigurationMap map =
                    characteristics.get(CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP);

            if (map == null) {
                continue;
            }

            // Fallback to camera1 API for internal cameras that don't have full support.
            // This should help with legacy situations where using the camera2 API causes
            // distorted or otherwise broken previews.
            useCamera2API = (facing == CameraCharacteristics.LENS_FACING_EXTERNAL)
                    || isHardwareLevelSupported(characteristics,
                    CameraCharacteristics.INFO_SUPPORTED_HARDWARE_LEVEL_FULL);
            return cameraId;
        }
    } catch (CameraAccessException e) {
        Log.e("CameraAccessException", "Not allowed to access camera" + e.toString());
    }

    return null;
}
 
Example 9
Source File: CameraHelper.java    From sandriosCamera with MIT License 5 votes vote down vote up
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public static boolean hasCamera2(Context context) {
    if (context == null) return false;
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) return false;
    try {
        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);

                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 ignore) {
        return false;
    }
}
 
Example 10
Source File: CameraActivity.java    From dbclf with Apache License 2.0 5 votes vote down vote up
protected String chooseCamera() {
    final CameraManager manager = (CameraManager) getSystemService(Context.CAMERA_SERVICE);
    try {
        for (final String cameraId : manager.getCameraIdList()) {
            final CameraCharacteristics characteristics = manager.getCameraCharacteristics(cameraId);

            // We don't use a front facing camera in this sample.
            final Integer facing = characteristics.get(CameraCharacteristics.LENS_FACING);
            if (facing != null && facing == CameraCharacteristics.LENS_FACING_FRONT) {
                continue;
            }

            final StreamConfigurationMap map =
                    characteristics.get(CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP);

            if (map == null) {
                continue;
            }

            // Fallback to camera1 API for internal cameras that don't have full support.
            // This should help with legacy situations where using the camera2 API causes
            // distorted or otherwise broken previews.
            useCamera2API = (facing == CameraCharacteristics.LENS_FACING_EXTERNAL)
                    || isHardwareLevelSupported(characteristics,
                    CameraCharacteristics.INFO_SUPPORTED_HARDWARE_LEVEL_FULL);
            return cameraId;
        }
    } catch (CameraAccessException ignored) {
    }

    return null;
}
 
Example 11
Source File: ScanQRActivity.java    From ScreenCapture with MIT License 5 votes vote down vote up
private boolean isHardwareLevelSupported(int requiredLevel) {
    boolean res = false;
    if (mCameraId == null)
        return res;
    try {
        CameraManager manager = (CameraManager) getSystemService(Context.CAMERA_SERVICE);
        CameraCharacteristics cameraCharacteristics = manager.getCameraCharacteristics(mCameraId);

        int deviceLevel = cameraCharacteristics.get(CameraCharacteristics.INFO_SUPPORTED_HARDWARE_LEVEL);
        switch (deviceLevel) {
            case CameraCharacteristics.INFO_SUPPORTED_HARDWARE_LEVEL_3:
                Log.d(TAG, "Camera support level: INFO_SUPPORTED_HARDWARE_LEVEL_3");
                break;
            case CameraCharacteristics.INFO_SUPPORTED_HARDWARE_LEVEL_FULL:
                Log.d(TAG, "Camera support level: INFO_SUPPORTED_HARDWARE_LEVEL_FULL");
                break;
            case CameraCharacteristics.INFO_SUPPORTED_HARDWARE_LEVEL_LEGACY:
                Log.d(TAG, "Camera support level: INFO_SUPPORTED_HARDWARE_LEVEL_LEGACY");
                break;
            case CameraCharacteristics.INFO_SUPPORTED_HARDWARE_LEVEL_LIMITED:
                Log.d(TAG, "Camera support level: INFO_SUPPORTED_HARDWARE_LEVEL_LIMITED");
                break;
            default:
                Log.d(TAG, "Unknown INFO_SUPPORTED_HARDWARE_LEVEL: " + deviceLevel);
                break;
        }


        if (deviceLevel == CameraCharacteristics.INFO_SUPPORTED_HARDWARE_LEVEL_LEGACY) {
            res = requiredLevel == deviceLevel;
        } else {
            // deviceLevel is not LEGACY, can use numerical sort
            res = requiredLevel <= deviceLevel;
        }

    } catch (Exception e) {
        Log.e(TAG, "isHardwareLevelSupported Error", e);
    }
    return res;
}
 
Example 12
Source File: Camera2WrapperManager.java    From DeviceConnect-Android with MIT License 5 votes vote down vote up
/**
 * サポートしているカメラのタイプ一覧を取得します.
 *
 * @param context コンテキスト
 * @return カメラのタイプ一覧
 */
public static List<Integer> supportCameraIds(Context context) {
    CameraManager manager = (CameraManager) context.getSystemService(Context.CAMERA_SERVICE);
    if (manager == null) {
        throw new UnsupportedOperationException("Not supported a Camera.");
    }

    List<Integer> list = new ArrayList<>();
    try {
        for (String cameraId : manager.getCameraIdList()) {
            CameraCharacteristics characteristics = manager.getCameraCharacteristics(cameraId);
            Integer facing = characteristics.get(CameraCharacteristics.LENS_FACING);
            if (facing != null) {
                switch (facing) {
                    case CameraCharacteristics.LENS_FACING_BACK:
                        list.add(CameraCharacteristics.LENS_FACING_BACK);
                        break;
                    case CameraCharacteristics.LENS_FACING_FRONT:
                        list.add(CameraCharacteristics.LENS_FACING_FRONT);
                        break;
                    default:
                        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                            list.add(CameraCharacteristics.LENS_FACING_EXTERNAL);
                        }
                        break;
                }
            }
        }
    } catch (CameraAccessException e) {
        // ignore.
    }
    return list;
}
 
Example 13
Source File: VideoCaptureCamera2.java    From 365browser with Apache License 2.0 5 votes vote down vote up
private static CameraCharacteristics getCameraCharacteristics(int id) {
    final CameraManager manager =
            (CameraManager) ContextUtils.getApplicationContext().getSystemService(
                    Context.CAMERA_SERVICE);
    try {
        return manager.getCameraCharacteristics(Integer.toString(id));
    } catch (CameraAccessException ex) {
        Log.e(TAG, "getCameraCharacteristics: ", ex);
    }
    return null;
}
 
Example 14
Source File: Camera2Helper.java    From DeviceConnect-Android with MIT License 5 votes vote down vote up
/**
 * 指定された facing に対応するカメラIDを取得します.
 * <p>
 * facing に対応したカメラが発見できない場合には null を返却します。
 * </p>
 * @param cameraManager カメラマネージャ
 * @param facing カメラの向き
 * @return カメラID
 * @throws CameraAccessException カメラの操作に失敗した場合に発生
 */
public static String getCameraId(final CameraManager cameraManager, final int facing) throws CameraAccessException {
    for (String cameraId : cameraManager.getCameraIdList()) {
        CameraCharacteristics characteristics = cameraManager.getCameraCharacteristics(cameraId);
        Integer supportFacing = characteristics.get(CameraCharacteristics.LENS_FACING);
        if (supportFacing != null && supportFacing == facing) {
            return cameraId;
        }
    }
    return null;
}
 
Example 15
Source File: CameraActivity.java    From tensorflow-classifier-android with Apache License 2.0 5 votes vote down vote up
private String chooseCamera() {
  final CameraManager manager = (CameraManager) getSystemService(Context.CAMERA_SERVICE);
  try {
    for (final String cameraId : manager.getCameraIdList()) {
      final CameraCharacteristics characteristics = manager.getCameraCharacteristics(cameraId);

      // We don't use a front facing camera in this sample.
      final Integer facing = characteristics.get(CameraCharacteristics.LENS_FACING);
      if (facing != null && facing == CameraCharacteristics.LENS_FACING_FRONT) {
        continue;
      }

      final StreamConfigurationMap map =
          characteristics.get(CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP);

      if (map == null) {
        continue;
      }

      useCamera2API = isHardwareLevelSupported(characteristics,
          CameraCharacteristics.INFO_SUPPORTED_HARDWARE_LEVEL_FULL);
      LOGGER.i("Camera API lv2?: %s", useCamera2API);
      return cameraId;
    }
  } catch (CameraAccessException e) {
    LOGGER.e(e, "Not allowed to access camera");
  }

  return null;
}
 
Example 16
Source File: Camera2Source.java    From Camera2Vision with Apache License 2.0 5 votes vote down vote up
public boolean isCamera2Native() {
    try {
        if (ContextCompat.checkSelfPermission(mContext, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {return false;}
        manager = (CameraManager) mContext.getSystemService(Context.CAMERA_SERVICE);
        mCameraId = manager.getCameraIdList()[mFacing];
        CameraCharacteristics characteristics = manager.getCameraCharacteristics(mCameraId);
        //CHECK CAMERA HARDWARE LEVEL. IF CAMERA2 IS NOT NATIVELY SUPPORTED, GO BACK TO CAMERA1
        Integer deviceLevel = characteristics.get(CameraCharacteristics.INFO_SUPPORTED_HARDWARE_LEVEL);
        return deviceLevel != null && (deviceLevel != CameraCharacteristics.INFO_SUPPORTED_HARDWARE_LEVEL_LEGACY);
    }
    catch (CameraAccessException ex) {return false;}
    catch (NullPointerException e) {return false;}
    catch (ArrayIndexOutOfBoundsException ez) {return false;}
}
 
Example 17
Source File: RotationHelper.java    From FastBarcodeScanner with Apache License 2.0 5 votes vote down vote up
private static boolean isFrontFacing(Activity activity, String cameraId)
{
    try {
        CameraManager manager = (CameraManager) activity.getSystemService(Context.CAMERA_SERVICE);
        CameraCharacteristics characteristics = manager.getCameraCharacteristics(cameraId);
        int facing = characteristics.get(CameraCharacteristics.LENS_FACING);
        return facing == CameraCharacteristics.LENS_FACING_FRONT;
    } catch (android.hardware.camera2.CameraAccessException e) {
        Log.e(TAG, "CameraAccessException");
        throw new UnsupportedOperationException("CameraAccessException");
    }
}
 
Example 18
Source File: Camera2ApiManager.java    From rtmp-rtsp-stream-client-java with Apache License 2.0 4 votes vote down vote up
@Nullable
private CameraCharacteristics getCharacteristicsForFacing(CameraManager cameraManager,
    CameraHelper.Facing facing) throws CameraAccessException {
  String cameraId = getCameraIdForFacing(cameraManager, facing);
  return cameraId != null ? cameraManager.getCameraCharacteristics(cameraId) : null;
}
 
Example 19
Source File: Camera2PortabilityTest.java    From Camera2 with Apache License 2.0 4 votes vote down vote up
private CameraCharacteristics buildFrameworkCharacteristics() throws CameraAccessException {
    CameraManager manager = (CameraManager) mContext.getSystemService(Context.CAMERA_SERVICE);
    String id = manager.getCameraIdList()[0];
    return manager.getCameraCharacteristics(id);
}
 
Example 20
Source File: Camera2RawFragment.java    From android-Camera2Raw with Apache License 2.0 4 votes vote down vote up
/**
 * Sets up state related to camera that is needed before opening a {@link CameraDevice}.
 */
private boolean setUpCameraOutputs() {
    Activity activity = getActivity();
    CameraManager manager = (CameraManager) activity.getSystemService(Context.CAMERA_SERVICE);
    if (manager == null) {
        ErrorDialog.buildErrorDialog("This device doesn't support Camera2 API.").
                show(getFragmentManager(), "dialog");
        return false;
    }
    try {
        // Find a CameraDevice that supports RAW captures, and configure state.
        for (String cameraId : manager.getCameraIdList()) {
            CameraCharacteristics characteristics
                    = manager.getCameraCharacteristics(cameraId);

            // We only use a camera that supports RAW in this sample.
            if (!contains(characteristics.get(
                            CameraCharacteristics.REQUEST_AVAILABLE_CAPABILITIES),
                    CameraCharacteristics.REQUEST_AVAILABLE_CAPABILITIES_RAW)) {
                continue;
            }

            StreamConfigurationMap map = characteristics.get(
                    CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP);

            // For still image captures, we use the largest available size.
            Size largestJpeg = Collections.max(
                    Arrays.asList(map.getOutputSizes(ImageFormat.JPEG)),
                    new CompareSizesByArea());

            Size largestRaw = Collections.max(
                    Arrays.asList(map.getOutputSizes(ImageFormat.RAW_SENSOR)),
                    new CompareSizesByArea());

            synchronized (mCameraStateLock) {
                // Set up ImageReaders for JPEG and RAW outputs.  Place these in a reference
                // counted wrapper to ensure they are only closed when all background tasks
                // using them are finished.
                if (mJpegImageReader == null || mJpegImageReader.getAndRetain() == null) {
                    mJpegImageReader = new RefCountedAutoCloseable<>(
                            ImageReader.newInstance(largestJpeg.getWidth(),
                                    largestJpeg.getHeight(), ImageFormat.JPEG, /*maxImages*/5));
                }
                mJpegImageReader.get().setOnImageAvailableListener(
                        mOnJpegImageAvailableListener, mBackgroundHandler);

                if (mRawImageReader == null || mRawImageReader.getAndRetain() == null) {
                    mRawImageReader = new RefCountedAutoCloseable<>(
                            ImageReader.newInstance(largestRaw.getWidth(),
                                    largestRaw.getHeight(), ImageFormat.RAW_SENSOR, /*maxImages*/ 5));
                }
                mRawImageReader.get().setOnImageAvailableListener(
                        mOnRawImageAvailableListener, mBackgroundHandler);

                mCharacteristics = characteristics;
                mCameraId = cameraId;
            }
            return true;
        }
    } catch (CameraAccessException e) {
        e.printStackTrace();
    }

    // If we found no suitable cameras for capturing RAW, warn the user.
    ErrorDialog.buildErrorDialog("This device doesn't support capturing RAW photos").
            show(getFragmentManager(), "dialog");
    return false;
}