@Override public int getOrientation(final int cameraId) { try { CameraManager manager = (CameraManager) context.getSystemService(Context.CAMERA_SERVICE); String[] cameraIds = manager.getCameraIdList(); CameraCharacteristics characteristics = manager.getCameraCharacteristics(cameraIds[cameraId]); return characteristics.get(CameraCharacteristics.SENSOR_ORIENTATION); } catch (CameraAccessException e) { // TODO handle error properly or pass it on return 0; } }
@Override public void initializeCameraManager(ConfigurationProvider configurationProvider, Context context) { super.initializeCameraManager(configurationProvider, context); this.manager = (CameraManager) context.getSystemService(Context.CAMERA_SERVICE); final WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE); final Display display = windowManager.getDefaultDisplay(); final Point size = new Point(); display.getSize(size); windowSize = new Size(size.x, size.y); try { final String[] ids = manager.getCameraIdList(); numberOfCameras = ids.length; for (String id : ids) { final CameraCharacteristics characteristics = manager.getCameraCharacteristics(id); final int orientation = characteristics.get(CameraCharacteristics.LENS_FACING); if (orientation == CameraCharacteristics.LENS_FACING_FRONT) { faceFrontCameraId = id; faceFrontCameraOrientation = characteristics.get(CameraCharacteristics.SENSOR_ORIENTATION); frontCameraCharacteristics = characteristics; } else { faceBackCameraId = id; faceBackCameraOrientation = characteristics.get(CameraCharacteristics.SENSOR_ORIENTATION); backCameraCharacteristics = characteristics; } } } catch (Exception e) { Log.e(TAG, "Error during camera init"); } }
@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; } }
Integer level = characteristics.get( CameraCharacteristics.INFO_SUPPORTED_HARDWARE_LEVEL); if (level == null || continue; Integer internal = characteristics.get(CameraCharacteristics.LENS_FACING); if (internal == null) { throw new NullPointerException("Unexpected state: LENS_FACING null"); Integer level = mCameraCharacteristics.get( CameraCharacteristics.INFO_SUPPORTED_HARDWARE_LEVEL); if (level == null || return false; Integer internal = mCameraCharacteristics.get(CameraCharacteristics.LENS_FACING); if (internal == null) { throw new NullPointerException("Unexpected state: LENS_FACING null");
Integer level = characteristics.get( CameraCharacteristics.INFO_SUPPORTED_HARDWARE_LEVEL); if (level == null || continue; Integer internal = characteristics.get(CameraCharacteristics.LENS_FACING); if (internal == null) { throw new NullPointerException("Unexpected state: LENS_FACING null"); Integer level = mCameraCharacteristics.get( CameraCharacteristics.INFO_SUPPORTED_HARDWARE_LEVEL); if (level == null || return false; Integer internal = mCameraCharacteristics.get(CameraCharacteristics.LENS_FACING); if (internal == null) { throw new NullPointerException("Unexpected state: LENS_FACING null");
/** * Updates the internal state of auto-focus to {@link #mAutoFocus}. */ void updateAutoFocus() { if (mAutoFocus) { int[] modes = mCameraCharacteristics.get( CameraCharacteristics.CONTROL_AF_AVAILABLE_MODES); // Auto focus is not supported if (modes == null || modes.length == 0 || (modes.length == 1 && modes[0] == CameraCharacteristics.CONTROL_AF_MODE_OFF)) { mAutoFocus = false; mPreviewRequestBuilder.set(CaptureRequest.CONTROL_AF_MODE, CaptureRequest.CONTROL_AF_MODE_OFF); } else { mPreviewRequestBuilder.set(CaptureRequest.CONTROL_AF_MODE, CaptureRequest.CONTROL_AF_MODE_CONTINUOUS_PICTURE); } } else { mPreviewRequestBuilder.set(CaptureRequest.CONTROL_AF_MODE, CaptureRequest.CONTROL_AF_MODE_OFF); } }
@Test public void testGetUnrecognizedKey() { assertThat(cameraCharacteristics.get(key0)).isNull(); }
/** * Updates the internal state of auto-focus to {@link #mAutoFocus}. */ void updateAutoFocus() { if (mAutoFocus) { int[] modes = mCameraCharacteristics.get( CameraCharacteristics.CONTROL_AF_AVAILABLE_MODES); // Auto focus is not supported if (modes == null || modes.length == 0 || (modes.length == 1 && modes[0] == CameraCharacteristics.CONTROL_AF_MODE_OFF)) { mAutoFocus = false; mPreviewRequestBuilder.set(CaptureRequest.CONTROL_AF_MODE, CaptureRequest.CONTROL_AF_MODE_OFF); } else { mPreviewRequestBuilder.set(CaptureRequest.CONTROL_AF_MODE, CaptureRequest.CONTROL_AF_MODE_CONTINUOUS_PICTURE); } } else { mPreviewRequestBuilder.set(CaptureRequest.CONTROL_AF_MODE, CaptureRequest.CONTROL_AF_MODE_OFF); } }
String[] cameraIds = manager.getCameraIdList(); CameraCharacteristics characteristics = manager.getCameraCharacteristics(cameraIds[cameraId]); return characteristics.get(CameraCharacteristics.SENSOR_ORIENTATION); } catch (CameraAccessException e) {
@Test public void testGetRecognizedKey() { shadowOf(cameraCharacteristics).set(key0, 1); assertThat(cameraCharacteristics.get(key0)).isEqualTo(1); } }
/** * <p>Collects some information from {@link #mCameraCharacteristics}.</p> * <p>This rewrites {@link #mPreviewSizes}, {@link #mPictureSizes}, and optionally, * {@link #mAspectRatio}.</p> */ private void collectCameraInfo() { StreamConfigurationMap map = mCameraCharacteristics.get( CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP); if (map == null) { throw new IllegalStateException("Failed to get configuration map: " + mCameraId); } mPreviewSizes.clear(); for (android.util.Size size : map.getOutputSizes(mPreview.getOutputClass())) { int width = size.getWidth(); int height = size.getHeight(); if (width <= MAX_PREVIEW_WIDTH && height <= MAX_PREVIEW_HEIGHT) { mPreviewSizes.add(new Size(width, height)); } } mPictureSizes.clear(); collectPictureSizes(mPictureSizes, map); for (AspectRatio ratio : mPreviewSizes.ratios()) { if (!mPictureSizes.ratios().contains(ratio)) { mPreviewSizes.remove(ratio); } } if (!mPreviewSizes.ratios().contains(mAspectRatio)) { mAspectRatio = mPreviewSizes.ratios().iterator().next(); } }
/** * <p>Collects some information from {@link #mCameraCharacteristics}.</p> * <p>This rewrites {@link #mPreviewSizes}, {@link #mPictureSizes}, and optionally, * {@link #mAspectRatio}.</p> */ private void collectCameraInfo() { StreamConfigurationMap map = mCameraCharacteristics.get( CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP); if (map == null) { throw new IllegalStateException("Failed to get configuration map: " + mCameraId); } mPreviewSizes.clear(); for (android.util.Size size : map.getOutputSizes(mPreview.getOutputClass())) { int width = size.getWidth(); int height = size.getHeight(); if (width <= MAX_PREVIEW_WIDTH && height <= MAX_PREVIEW_HEIGHT) { mPreviewSizes.add(new Size(width, height)); } } mPictureSizes.clear(); collectPictureSizes(mPictureSizes, map); for (AspectRatio ratio : mPreviewSizes.ratios()) { if (!mPictureSizes.ratios().contains(ratio)) { mPreviewSizes.remove(ratio); } } if (!mPreviewSizes.ratios().contains(mAspectRatio)) { mAspectRatio = mPreviewSizes.ratios().iterator().next(); } }
Integer facing = characteristics.get(CameraCharacteristics.LENS_FACING); if (facing != null && facing == CameraCharacteristics.LENS_FACING_FRONT) { continue; StreamConfigurationMap map = characteristics.get( CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP); if (map == null) { mSensorOrientation = characteristics.get(CameraCharacteristics.SENSOR_ORIENTATION); boolean swappedDimensions = false; switch (displayRotation) { Boolean available = characteristics.get(CameraCharacteristics.FLASH_INFO_AVAILABLE); mFlashSupported = available == null ? false : available;
int sensorOrientation = mCameraCharacteristics.get( CameraCharacteristics.SENSOR_ORIENTATION); captureRequestBuilder.set(CaptureRequest.JPEG_ORIENTATION,
int sensorOrientation = mCameraCharacteristics.get( CameraCharacteristics.SENSOR_ORIENTATION); captureRequestBuilder.set(CaptureRequest.JPEG_ORIENTATION,
frontCameraStreamConfigurationMap = characteristics.get(CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP); else if (currentCameraId.equals(faceBackCameraId) && backCameraStreamConfigurationMap == null) backCameraStreamConfigurationMap = characteristics.get(CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP);
String getFrontFacingCameraId(CameraManager cManager){ for(final String cameraId : cManager.getCameraIdList()){ CameraCharacteristics characteristics = cManager.getCameraCharacteristics(cameraId); int cOrientation = characteristics.get(CameraCharacteristics.LENS_FACING); if(cOrientation == CameraCharacteristics.LENS_FACING_FRONT) return cameraId; } return null; }
String backCameraId = null; CameraManager manager = (CameraManager) getSystemService(Context.CAMERA_SERVICE); for(String cameraId:manager.getCameraIdList()){ CameraCharacteristics cameraCharacteristics = manager.getCameraCharacteristics(cameraId); Integer facing = cameraCharacteristics.get(CameraCharacteristics.LENS_FACING); if(facing==CameraMetadata.LENS_FACING_BACK) { backCameraId = cameraId; break; } } Log.d(TAG, "back camera exists ? "+(backCameraId!=null)); Log.d(TAG, "back camera id :"+backCameraId);
@Override public void initializeCameraManager(CameraConfigProvider cameraConfigProvider, Context context) { super.initializeCameraManager(cameraConfigProvider, context); final WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE); final Display display = windowManager.getDefaultDisplay(); final Point size = new Point(); display.getSize(size); mWindowSize = new Size(size.x, size.y); mCameraManager = (CameraManager) context.getSystemService(Context.CAMERA_SERVICE); try { final String[] ids = mCameraManager.getCameraIdList(); mNumberOfCameras = ids.length; for (String id : ids) { final CameraCharacteristics characteristics = mCameraManager.getCameraCharacteristics(id); final int orientation = characteristics.get(CameraCharacteristics.LENS_FACING); if (orientation == CameraCharacteristics.LENS_FACING_FRONT) { mFaceFrontCameraId = id; mFaceFrontCameraOrientation = characteristics.get(CameraCharacteristics.SENSOR_ORIENTATION); mFrontCameraCharacteristics = characteristics; } else { mFaceBackCameraId = id; mFaceBackCameraOrientation = characteristics.get(CameraCharacteristics.SENSOR_ORIENTATION); mBackCameraCharacteristics = characteristics; } } } catch (Exception e) { Log.e(TAG, "Error during camera initialize"); } }
@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 notFull = true; if (idList.length == 0) { notFull = false; } else { for (final String str : idList) { if (str == null || str.trim().isEmpty()) { notFull = 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) { notFull = false; break; } } } return notFull; } catch (Throwable ignore) { return false; } }