/** * <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())) { mPreviewSizes.add(new Size(size.getWidth(), size.getHeight())); } mPictureSizes.clear(); collectPictureSizes(mPictureSizes, map); if (!mPreviewSizes.ratios().contains(mAspectRatio)) { mAspectRatio = mPreviewSizes.ratios().iterator().next(); } }
@Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_camera_v2); CameraManager cameraManager = (CameraManager) getSystemService(CAMERA_SERVICE); try { for (String cameraId : cameraManager.getCameraIdList()) { CameraCharacteristics characteristics = cameraManager.getCameraCharacteristics(cameraId); Integer facing = characteristics.get(CameraCharacteristics.LENS_FACING); if (facing != null && facing.equals(CameraCharacteristics.LENS_FACING_FRONT)) { } // Do something with the characteristics } } catch (CameraAccessException e) { e.printStackTrace(); } }
public void open() { try { CameraManager manager = (CameraManager) mActivity.getSystemService(Context.CAMERA_SERVICE); for (String cameraId : manager.getCameraIdList()) { CameraCharacteristics characteristics = manager.getCameraCharacteristics(cameraId); if (characteristics.get(CameraCharacteristics.LENS_FACING) == CameraCharacteristics.LENS_FACING_BACK) { StreamConfigurationMap map = characteristics.get(CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP); mCameraSize = map.getOutputSizes(SurfaceTexture.class)[0]; HandlerThread thread = new HandlerThread("OpenCamera"); thread.start(); Handler backgroundHandler = new Handler(thread.getLooper()); manager.openCamera(cameraId, mCameraDeviceCallback, null); // カメラの物理的な情報を得る mCameraCharacteristics = manager.getCameraCharacteristics( cameraId ); return; } } } catch (CameraAccessException e) { e.printStackTrace(); } }
/** * 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); } }
@Override public void onStopTrackingTouch(SeekBar seekBar) { switch (seekBar.getId()) { case R.id.iso: { Range<Integer> range = mCameraCharacteristics.get(CameraCharacteristics.SENSOR_INFO_SENSITIVITY_RANGE); int max = range.getUpper(); int min = range.getLower(); isoValue = ((seekBar.getProgress() * (max - min)) / 100 + min); createCameraPreviewSession_ISO(); break; } case R.id.focus: { float distance = mCameraCharacteristics.get(CameraCharacteristics.LENS_INFO_MINIMUM_FOCUS_DISTANCE); focusValue = seekBar.getProgress() * distance / 100; createCameraPreviewSession_FOCUS(); break; } default: break; } }
/** * Find out if we need to swap dimensions to get the preview size relative to sensor coordinate. * @param activity - The associated activity from which the camera is loaded. * @param characteristics - CameraCharacteristics corresponding to the current started cameradevice * @return swappedDimensions - A boolean value indicating if the the dimensions need to be swapped. */ public static boolean needSwappedDimensions(Activity activity, CameraCharacteristics characteristics) { int displayRotation = activity.getWindowManager().getDefaultDisplay().getRotation(); //noinspection ConstantConditions int mSensorOrientation = characteristics.get(CameraCharacteristics.SENSOR_ORIENTATION); boolean swappedDimensions = false; switch (displayRotation) { case Surface.ROTATION_0: case Surface.ROTATION_90: if (mSensorOrientation == 90 || mSensorOrientation == 270) { swappedDimensions = true; } break; case Surface.ROTATION_180: case Surface.ROTATION_270: if (mSensorOrientation == 0 || mSensorOrientation == 180) { swappedDimensions = true; } break; default: Log.e(TAG, "Display rotation is invalid: " + displayRotation); } return swappedDimensions; }
@Override protected String createCamera(int deviceDegrees) throws CameraException { try { for (String camId : mCamManager.getCameraIdList()) { CameraCharacteristics characteristics = mCamManager.getCameraCharacteristics(camId); Integer facing = characteristics.get(CameraCharacteristics.LENS_FACING); if (facing == CameraCharacteristics.LENS_FACING_FRONT) { Log.d(TAG, "front-facing mCamera found: " + camId); return camId; } } } catch (CameraAccessException e) { throw new CameraException(e); } throw new CameraException(mActivity.getString(R.string.frontCameraMissing)); }
@Override protected String getCameraInfo() { String camStr = mContext.getString(R.string.camApi2) + "\n"; CameraManager camManager = (CameraManager) mContext.getSystemService(Context.CAMERA_SERVICE); try { for (String camId : camManager.getCameraIdList()) { camStr += mContext.getString(R.string.cameraId, camId) + ": "; CameraCharacteristics characteristics = camManager.getCameraCharacteristics(camId); Boolean hasFlash = characteristics.get(CameraCharacteristics.FLASH_INFO_AVAILABLE); Integer facing = characteristics.get(CameraCharacteristics.LENS_FACING); camStr += (hasFlash ? mContext.getString(R.string.has) : mContext.getString(R.string.no)) + " " + mContext.getString(R.string.flash) + ", "; camStr += (facing == CameraCharacteristics.LENS_FACING_BACK ? mContext.getString(R.string.backFacing) : mContext.getString(R.string.frontFacing)) + "\n"; } } catch (CameraAccessException e) { camStr = mActivity.getString(R.string.failedAccessCamera) + ":" + e.getMessage(); } return camStr.trim(); }
public FlashController(Context ctx, CameraManager cameraManager, ServerConnection serverConnection) throws CameraAccessException, IllegalAccessException { mCtx = ctx; camManager = cameraManager; mServerConnection = serverConnection; EventBus.getDefault().register(this); for (String camId : camManager.getCameraIdList()) { CameraCharacteristics characteristics = camManager.getCameraCharacteristics(camId); Boolean hasFlash = characteristics.get(CameraCharacteristics.FLASH_INFO_AVAILABLE); Integer facing = characteristics.get(CameraCharacteristics.LENS_FACING); if (facing == CameraCharacteristics.LENS_FACING_BACK && hasFlash) { torchId = camId; break; } } if (torchId == null) { throw new IllegalAccessException(ctx.getString(R.string.couldNotFindBackFlash)); } }
@Override public void collectRatioSizes() { ratioSizeList.clear(); CameraCharacteristics characteristics; StreamConfigurationMap map = null; try { characteristics = ((CameraManager) getContext().getSystemService(Context.CAMERA_SERVICE)).getCameraCharacteristics(Integer.toString(Integer.parseInt(getCameraId()))); map = characteristics.get(CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP); } catch (CameraAccessException e) { e.printStackTrace(); } Size[] outputSizes = map.getOutputSizes(SurfaceTexture.class); if (outputSizes != null) { List<Double> ratioList = new ArrayList<>(); for (Size size : outputSizes) { double ratio = (double) size.getWidth() / (double) size.getHeight(); if (!ratioList.contains(ratio)) { ratioList.add(ratio); ratioSizeList.add(new AspectRatio(ratio, size.getWidth(), size.getHeight())); } } } }
/** * Rotation need to transform from the camera sensor orientation to the device's current * orientation. * * @param c the {@link CameraCharacteristics} to query for the camera sensor * orientation. * @param deviceOrientation the current device orientation relative to the native device * orientation. * @return the total rotation from the sensor orientation to the current device orientation. */ private static int sensorToDeviceRotation(CameraCharacteristics c, int deviceOrientation) { int sensorOrientation = c.get(CameraCharacteristics.SENSOR_ORIENTATION); // Get device orientation in degrees deviceOrientation = ORIENTATIONS.get(deviceOrientation); // Reverse device orientation for front-facing cameras if (c.get(CameraCharacteristics.LENS_FACING) == CameraCharacteristics.LENS_FACING_FRONT) { deviceOrientation = -deviceOrientation; } // Calculate desired JPEG orientation relative to camera orientation to make // the image upright relative to the device orientation return (sensorOrientation + deviceOrientation + 360) % 360; }
@NonNull private <T> T getOrThrow(@NonNull String cameraId, @NonNull CameraCharacteristics.Key<T> key) { try { CameraCharacteristics characteristics = manager.getCameraCharacteristics(cameraId); T value = characteristics.get(key); if (value == null) { throw new CameraException("could not determine camera characteristic " + key.getName() + " from camera " + cameraId); } else { return value; } } catch (CameraAccessException e) { throw new CameraException(e); } }
private void logSupportedHardwareLevel(@NonNull String cameraId) { int hardwareLevel = cameraCharacteristicsHelper.getSupportedHardwareLevel(cameraId); String hardwareLevelName = String.valueOf(hardwareLevel); // for level 3 and higher switch (hardwareLevel) { case CameraCharacteristics.INFO_SUPPORTED_HARDWARE_LEVEL_LEGACY: hardwareLevelName = "legacy"; break; case CameraCharacteristics.INFO_SUPPORTED_HARDWARE_LEVEL_LIMITED: hardwareLevelName = "limited"; break; case CameraCharacteristics.INFO_SUPPORTED_HARDWARE_LEVEL_FULL: hardwareLevelName = "full"; break; } log.d("using camera with supported hardware level: %s", hardwareLevelName); }
@Before public void setUp() throws Exception { surfaces = asList(surface1, surface2); when(manager.getCameraIdList()).thenReturn(new String[]{CAMERA_ID_1, CAMERA_ID_2}); when(characteristicsHelper.getLensFacing(CAMERA_ID_1)).thenReturn(CameraCharacteristics.LENS_FACING_BACK); when(characteristicsHelper.getLensFacing(CAMERA_ID_2)).thenReturn(CameraCharacteristics.LENS_FACING_FRONT); when(characteristicsHelper.getSensorOrientation(CAMERA_ID_1)).thenReturn(SENSOR_ROTATION); when(characteristicsHelper.getPreviewOutputSizes(CAMERA_ID_1)).thenReturn( new Size[]{PREVIEWSIZE_1, PREVIEWSIZE_640x480, PREVIEWSIZE_2}); when(camera.getId()).thenReturn(CAMERA_ID_1); when(camera.createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW)).thenReturn(captureRequestBuilder); when(textureView.getSurfaceTexture()).thenReturn(surfaceTexture); }
/** * Rotation need to transform from the camera sensor orientation to the device's current * orientation. * * @param c the {@link CameraCharacteristics} to query for the camera sensor * orientation. * @param deviceOrientation the current device orientation relative to the native device * orientation. * @return the total rotation from the sensor orientation to the current device orientation. */ public static int sensorToDeviceRotation(CameraCharacteristics c, int deviceOrientation) { int sensorOrientation = c.get(CameraCharacteristics.SENSOR_ORIENTATION); // Get device orientation in degrees deviceOrientation = ORIENTATIONS.get(deviceOrientation); // Reverse device orientation for front-facing cameras if (c.get(CameraCharacteristics.LENS_FACING) == CameraCharacteristics.LENS_FACING_FRONT) { deviceOrientation = -deviceOrientation; } // Calculate desired JPEG orientation relative to camera orientation to make // the image upright relative to the device orientation return (sensorOrientation + deviceOrientation + 360) % 360; }
private void start() { checkIsOnCameraThread(); Logging.d(TAG, "start"); try { cameraCharacteristics = cameraManager.getCameraCharacteristics(cameraId); } catch (final CameraAccessException e) { reportError("getCameraCharacteristics(): " + e.getMessage()); return; } cameraOrientation = cameraCharacteristics.get(CameraCharacteristics.SENSOR_ORIENTATION); isCameraFrontFacing = cameraCharacteristics.get(CameraCharacteristics.LENS_FACING) == CameraMetadata.LENS_FACING_FRONT; findCaptureFormat(); openCamera(); }
private void findCaptureFormat() { checkIsOnCameraThread(); Range<Integer>[] fpsRanges = cameraCharacteristics.get(CameraCharacteristics.CONTROL_AE_AVAILABLE_TARGET_FPS_RANGES); fpsUnitFactor = Camera2Enumerator.getFpsUnitFactor(fpsRanges); List<CaptureFormat.FramerateRange> framerateRanges = Camera2Enumerator.convertFramerates(fpsRanges, fpsUnitFactor); List<Size> sizes = Camera2Enumerator.getSupportedSizes(cameraCharacteristics); Logging.d(TAG, "Available preview sizes: " + sizes); Logging.d(TAG, "Available fps ranges: " + framerateRanges); if (framerateRanges.isEmpty() || sizes.isEmpty()) { reportError("No supported capture formats."); return; } final CaptureFormat.FramerateRange bestFpsRange = CameraEnumerationAndroid.getClosestSupportedFramerateRange(framerateRanges, framerate); final Size bestSize = CameraEnumerationAndroid.getClosestSupportedSize(sizes, width, height); CameraEnumerationAndroid.reportCameraResolution(camera2ResolutionHistogram, bestSize); captureFormat = new CaptureFormat(bestSize.width, bestSize.height, bestFpsRange); Logging.d(TAG, "Using capture format: " + captureFormat); }
/** * 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; } } // On Android OS pre 4.4.2, a class will not load because of VerifyError if it contains a // catch statement with an Exception from a newer API, even if the code is never executed. // https://code.google.com/p/android/issues/detail?id=209129 } catch (/* CameraAccessException */ AndroidException e) { Logging.e(TAG, "Camera access exception: " + e); return false; } return true; }
public void toggleFacing() { if (facingSelected == CameraCharacteristics.LENS_FACING_FRONT) { facingSelected = CameraCharacteristics.LENS_FACING_BACK; toggleButton.setImageResource(R.drawable.ic_camera_rear); } else { facingSelected = CameraCharacteristics.LENS_FACING_FRONT; toggleButton.setImageResource(R.drawable.ic_camera_front); } toggleButton.invalidate(); closeCamera(); if (textureView.isAvailable()) { openCamera(textureView.getWidth(), textureView.getHeight()); } else { textureView.setSurfaceTextureListener(surfaceTextureListener); } }
private String getCameraId() { if (mCameraId == null) { try { String[] ids = mCameraManager.getCameraIdList(); for (String id : ids) { CameraCharacteristics c = mCameraManager.getCameraCharacteristics(id); Boolean flashAvailable = c.get(CameraCharacteristics.FLASH_INFO_AVAILABLE); Integer lensFacing = c.get(CameraCharacteristics.LENS_FACING); if (flashAvailable != null && flashAvailable && lensFacing != null && lensFacing == CameraCharacteristics.LENS_FACING_BACK) { mCameraId = id; } } if (DEBUG) Log.d(TAG, "getCameraId: " + mCameraId); } catch (Exception e) { e.printStackTrace(); mTorchStatus = TORCH_STATUS_ERROR; broadcastStatus(); stopSelf(); } } return mCameraId; }
private void findCaptureFormat() { checkIsOnCameraThread(); Range<Integer>[] fpsRanges = cameraCharacteristics.get(CameraCharacteristics.CONTROL_AE_AVAILABLE_TARGET_FPS_RANGES); fpsUnitFactor = Camera2Enumerator.getFpsUnitFactor(fpsRanges); List<CaptureFormat.FramerateRange> framerateRanges = Camera2Enumerator.convertFramerates(fpsRanges, fpsUnitFactor); List<Size> sizes = Camera2Enumerator.getSupportedSizes(cameraCharacteristics); Logging.d(TAG, "Available preview sizes: " + sizes); Logging.d(TAG, "Available fps ranges: " + framerateRanges); if (framerateRanges.isEmpty() || sizes.isEmpty()) { reportError("No supported capture formats."); return; } final CaptureFormat.FramerateRange bestFpsRange = CameraEnumerationAndroid.getClosestSupportedFramerateRange(framerateRanges, framerate); final Size bestSize = CameraEnumerationAndroid.getClosestSupportedSize(sizes, width, height); //CameraEnumerationAndroid.reportCameraResolution(camera2ResolutionHistogram, bestSize); captureFormat = new CaptureFormat(bestSize.width, bestSize.height, bestFpsRange); Logging.d(TAG, "Using capture format: " + captureFormat); }
private void startPreview() { try { CameraCharacteristics characteristics = cameraManager.getCameraCharacteristics(cameraId); StreamConfigurationMap configMap = characteristics.get(CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP); Size previewSize = Util.getPreferredPreviewSize( configMap.getOutputSizes(ImageFormat.JPEG),textureView.getWidth(), textureView.getHeight()); surfaceTexture.setDefaultBufferSize(previewSize.getWidth(),previewSize.getHeight()); Surface surface = new Surface(surfaceTexture); captureBuilder = cameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW); captureBuilder.addTarget(surface); cameraDevice.createCaptureSession(Arrays.asList(surface),captureSessionCallback,backgroundHandler); } catch (CameraAccessException e) { e.printStackTrace(); } }
@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; } }
private boolean isHardwareLevelSupported( CameraCharacteristics characteristics, int requiredLevel) { int deviceLevel = characteristics.get(CameraCharacteristics.INFO_SUPPORTED_HARDWARE_LEVEL); if (deviceLevel == CameraCharacteristics.INFO_SUPPORTED_HARDWARE_LEVEL_LEGACY) { return requiredLevel == deviceLevel; } // deviceLevel is not LEGACY, can use numerical sort return requiredLevel <= deviceLevel; }
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; }
/** * Configure the surfaceview and RS processing. */ private void configureSurfaces() { // Find a good size for output - largest 16:9 aspect ratio that's less than 720p final int MAX_WIDTH = 1280; final float TARGET_ASPECT = 16.f / 9.f; final float ASPECT_TOLERANCE = 0.1f; StreamConfigurationMap configs = mCameraInfo.get(CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP); Size[] outputSizes = configs.getOutputSizes(SurfaceHolder.class); Size outputSize = outputSizes[0]; float outputAspect = (float) outputSize.getWidth() / outputSize.getHeight(); for (Size candidateSize : outputSizes) { if (candidateSize.getWidth() > MAX_WIDTH) continue; float candidateAspect = (float) candidateSize.getWidth() / candidateSize.getHeight(); boolean goodCandidateAspect = Math.abs(candidateAspect - TARGET_ASPECT) < ASPECT_TOLERANCE; boolean goodOutputAspect = Math.abs(outputAspect - TARGET_ASPECT) < ASPECT_TOLERANCE; if ((goodCandidateAspect && !goodOutputAspect) || candidateSize.getWidth() > outputSize.getWidth()) { outputSize = candidateSize; outputAspect = candidateAspect; } } Log.i(TAG, "Resolution chosen: " + outputSize); setupProcessing(outputSize); // this will trigger onSurfaceChanged() getViewfinderSurfaceView().getHolder() .setFixedSize(outputSize.getWidth(), outputSize.getHeight()); getViewfinderSurfaceView().setAspectRatio(outputAspect); }
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;} }
@Override protected int getSensorOrientation() { try { CameraCharacteristics characteristics = mCamManager.getCameraCharacteristics(String.valueOf(mCameraId)); return characteristics.get(CameraCharacteristics.SENSOR_ORIENTATION); } catch (CameraAccessException e) { Log.e(TAG, "Couldn't find out camera sensor orientation"); } return 0; }
private String getCameraId() throws CameraAccessException { int camera = frontFacingCameraActive ? CameraCharacteristics.LENS_FACING_FRONT : CameraCharacteristics.LENS_FACING_BACK; for (String cameraId : cameraManager.getCameraIdList()) { CameraCharacteristics cameraCharacteristics = cameraManager.getCameraCharacteristics(cameraId); if (cameraCharacteristics.get(CameraCharacteristics.LENS_FACING) == camera) { return cameraId; } } return null; }
private void triggerFocusArea(float x, float y) { CameraManager manager = (CameraManager) getActivity().getSystemService(Context.CAMERA_SERVICE); try { CameraCharacteristics characteristics = manager.getCameraCharacteristics(mCameraId); Integer sensorOrientation = characteristics.get( CameraCharacteristics.SENSOR_ORIENTATION); sensorOrientation = sensorOrientation == null ? 0 : sensorOrientation; Rect cropRegion = AutoFocusHelper.cropRegionForZoom(characteristics, 1f); mAERegions = AutoFocusHelper.aeRegionsForNormalizedCoord(x, y, cropRegion, sensorOrientation); mAFRegions = AutoFocusHelper.afRegionsForNormalizedCoord(x, y, cropRegion, sensorOrientation); // Step 1: Request single frame CONTROL_AF_TRIGGER_START. CaptureRequest.Builder builder; builder = mCameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW); builder.addTarget(mPreviewSurface); builder.set(CaptureRequest.CONTROL_MODE, CameraMetadata.CONTROL_MODE_AUTO); mControlAFMode = AutoFocusMode.AUTO; builder.set(CaptureRequest.CONTROL_AF_MODE, mControlAFMode.switchToCamera2FocusMode()); builder.set(CaptureRequest.CONTROL_AF_TRIGGER, CaptureRequest.CONTROL_AF_TRIGGER_START); mCaptureSession.capture(builder.build(), mPreCaptureCallback, mBackgroundHandler); // Step 2: Call repeatingPreview to update mControlAFMode. sendRepeatPreviewRequest(); resumeContinuousAFAfterDelay(DELAY_TIME_RESUME_CONTINUOUS_AF); } catch (CameraAccessException ex) { Log.e(TAG, "Could not execute preview request.", ex); } }
private ImageSaver(Image image, File file, CaptureResult result, CameraCharacteristics characteristics, Context context, RefCountedAutoCloseable<ImageReader> reader) { mImage = image; mFile = file; mCaptureResult = result; mCharacteristics = characteristics; mContext = context; mReader = reader; }
public static int getJPEGOrientation(int rotation, CameraCharacteristics cameraCharacteristics) { Integer sensorOrientation = cameraCharacteristics.get(CameraCharacteristics.SENSOR_ORIENTATION); sensorOrientation = sensorOrientation == null ? 0 : sensorOrientation; // Sensor orientation is 90 for most devices, or 270 for some devices (eg. Nexus 5X) // We have to take that into account and rotate JPEG properly. // For devices with orientation of 90, we simply return our mapping from ORIENTATIONS. // For devices with orientation of 270, we need to rotate the JPEG 180 degrees. return (ORIENTATIONS.get(rotation) + sensorOrientation + 270) % 360; }
/** * Calculates sensor crop region for a zoom level (zoom >= 1.0). * * @return Crop region. */ public static Rect cropRegionForZoom(CameraCharacteristics characteristics, float zoom) { Rect sensor = characteristics.get(CameraCharacteristics.SENSOR_INFO_ACTIVE_ARRAY_SIZE); int xCenter = sensor.width() / 2; int yCenter = sensor.height() / 2; int xDelta = (int) (0.5f * sensor.width() / zoom); int yDelta = (int) (0.5f * sensor.height() / zoom); return new Rect(xCenter - xDelta, yCenter - yDelta, xCenter + xDelta, yCenter + yDelta); }