/** * Get the largest possible zoom ratio (normalized to {@code 1.0f} and higher) that the camera can * support. * * <p>If the camera does not support zoom, it always returns {@code 1.0f}. * * @param params non-{@code null} camera api1 parameters * @return normalized max zoom ratio, at least {@code 1.0f} */ public static float getMaxZoomRatio(Camera.Parameters params) { if (!params.isZoomSupported()) { return 1.0f; // no zoom } List<Integer> zoomRatios = params.getZoomRatios(); // sorted smallest->largest int zoom = zoomRatios.get(zoomRatios.size() - 1); // largest zoom ratio float zoomRatio = zoom * 1.0f / ZOOM_RATIO_MULTIPLIER; // normalize to 1.0 and smaller return zoomRatio; }
public void zoom(int step) { Camera.Parameters parameters = mCamera.getParameters(); if (!parameters.isZoomSupported()) return; int zoom = parameters.getZoom() + step; if (zoom > parameters.getMaxZoom()) zoom = parameters.getMaxZoom(); else if (zoom < 0) zoom = 0; parameters.setZoom(zoom); String str = parameters.getZoomRatios().get(zoom) + "%"; zoomText.setText(str); mCamera.setParameters(parameters); }
@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); boolean useFrontCamera = getArguments().getBoolean(FRONT_CAMERA, false); camera = getCameraInstance(useFrontCamera); if (camera == null) { return; } initScreenParams(); parameters = camera.getParameters(); zoomRatios = parameters.getZoomRatios(); zoomIndex = minZoomIndex = 0; maxZoomIndex = parameters.getMaxZoom(); previewSizes = buildPreviewSizesRatioMap(parameters.getSupportedPreviewSizes()); pictureSizes = buildPictureSizesRatioMap(parameters.getSupportedPictureSizes()); List<String> sceneModes = parameters.getSupportedSceneModes(); if (sceneModes != null) { for (String mode : sceneModes) { if (mode.equals(Camera.Parameters.SCENE_MODE_HDR)) { supportedHDR = true; break; } } } // it returns false positive /*getActivity().getApplicationContext().getPackageManager() .hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH);*/ List<String> flashModes = parameters.getSupportedFlashModes(); if (flashModes == null || flashModes.size() <= 1) { /* Device has no flash */ supportedFlash = false; } else { supportedFlash = true; } if (CameraConst.DEBUG) { Timber.d("PictureSizesRatioMap:"); for (Ratio r : pictureSizes.keySet()) { Timber.d(r.toString() + ":"); for (Quality q : pictureSizes.get(r).keySet()) { Camera.Size size = pictureSizes.get(r).get(q); if (size != null) { Timber.d(q.toString() + ": " + size.width + "x" + size.height); } } } } expandParams(getArguments()); initParams(); }
private static Integer indexOfClosestZoom(Camera.Parameters parameters, double targetZoomRatio) { List<Integer> ratios = parameters.getZoomRatios(); Log.i(TAG, "Zoom ratios: " + ratios); int maxZoom = parameters.getMaxZoom(); if (ratios == null || ratios.isEmpty() || ratios.size() != maxZoom + 1) { Log.w(TAG, "Invalid zoom ratios!"); return null; } double target100 = 100.0 * targetZoomRatio; double smallestDiff = Double.POSITIVE_INFINITY; int closestIndex = 0; for (int i = 0; i < ratios.size(); i++) { double diff = Math.abs(ratios.get(i) - target100); if (diff < smallestDiff) { smallestDiff = diff; closestIndex = i; } } Log.i(TAG, "Chose zoom ratio of " + (ratios.get(closestIndex) / 100.0)); return closestIndex; }
/** * Get the available 'crop' (zoom) rectangles for this camera. * * <p>When zoom is supported, this will return a list of {@code 1 + #getMaxZoom} size, where each * crop rectangle corresponds to a zoom ratio (and is centered at the middle). * * <p>Each crop rectangle is changed to have the same aspect ratio as {@code streamSize}, by * shrinking the rectangle if necessary. * * <p>To get the reported crop region when applying a zoom to the sensor, use {@code streamSize} = * {@code activeArray size}. * * @param params non-{@code null} camera api1 parameters * @param activeArray active array dimensions, in sensor space * @param streamSize stream size dimensions, in pixels * @return a list of available zoom rectangles, sorted from least zoomed to most zoomed */ private static List<Rect> getAvailableCropRectangles( Camera.Parameters params, Rect activeArray, Size streamSize) { checkNotNull(params, "params must not be null"); checkNotNull(activeArray, "activeArray must not be null"); checkNotNull(streamSize, "streamSize must not be null"); // TODO: change all uses of Rect activeArray to Size activeArray, // since we want the crop to be active-array relative, not pixel-array relative Rect unzoomedStreamCrop = getPreviewCropRectangleUnzoomed(activeArray, streamSize); if (!params.isZoomSupported()) { // Trivial case: No zoom -> only support the full size as the crop region return new ArrayList<>(Arrays.asList(unzoomedStreamCrop)); } List<Rect> zoomCropRectangles = new ArrayList<>(params.getMaxZoom() + 1); Matrix scaleMatrix = new Matrix(); RectF scaledRect = new RectF(); for (int zoom : params.getZoomRatios()) { float shrinkRatio = ZOOM_RATIO_MULTIPLIER * 1.0f / zoom; // normalize to 1.0 and smaller // set scaledRect to unzoomedStreamCrop ParamsUtils.convertRectF(unzoomedStreamCrop, /*out*/ scaledRect); scaleMatrix.setScale( shrinkRatio, shrinkRatio, activeArray.exactCenterX(), activeArray.exactCenterY()); scaleMatrix.mapRect(scaledRect); Rect intRect = ParamsUtils.createRect(scaledRect); // Round the rect corners towards the nearest integer values zoomCropRectangles.add(intRect); } return zoomCropRectangles; }