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); }
public int doZoom(float scale) { synchronized (mCameraLock) { if (mCamera == null) { return 0; } int currentZoom = 0; int maxZoom; Camera.Parameters parameters = mCamera.getParameters(); if (!parameters.isZoomSupported()) { Log.w(TAG, mContext.getString(R.string.zoom_not_supported)); return currentZoom; } maxZoom = parameters.getMaxZoom(); currentZoom = parameters.getZoom() + 1; float newZoom; if (scale > 1) { newZoom = currentZoom + scale * (maxZoom / 10); } else { newZoom = currentZoom * scale; } currentZoom = Math.round(newZoom) - 1; if (currentZoom < 0) { currentZoom = 0; } else if (currentZoom > maxZoom) { currentZoom = maxZoom; } parameters.setZoom(currentZoom); mCamera.setParameters(parameters); return currentZoom; } }
protected void enableZoomControls(Camera.Parameters params) { final SharedPreferences settings = getContext().getSharedPreferences("ZoomCtl", 0); // set zoom level to previously set level if available, otherwise maxZoom final int maxZoom = params.getMaxZoom(); int currentZoom = settings.getInt("ZoomLvl", maxZoom); params.setZoom(currentZoom); if (_zoomControl == null) return; _zoomControl.setMax(maxZoom); _zoomControl.setProgress(currentZoom); _zoomControl.setOnSeekBarChangeListener( new SeekBar.OnSeekBarChangeListener() { @Override public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { Camera.Parameters params = mCamera.getParameters(); params.setZoom(progress); mCamera.setParameters(params); if (settings != null) { SharedPreferences.Editor editor = settings.edit(); editor.putInt("ZoomLvl", progress); editor.apply(); } } @Override public void onStartTrackingTouch(SeekBar seekBar) {} @Override public void onStopTrackingTouch(SeekBar seekBar) {} }); }
/** 焦点放大 */ public void zoomIn() { if (camera != null && camera.getParameters().isZoomSupported()) { Camera.Parameters parameters = camera.getParameters(); if (parameters.getZoom() >= parameters.getMaxZoom()) { return; } parameters.setZoom(parameters.getZoom() + 1); camera.setParameters(parameters); } }
public ZoomTransaction zoomTo(int level) { if (camera == null) { throw new IllegalStateException("Yes, we have no camera, we have no camera today"); } else { Camera.Parameters params = camera.getParameters(); if (level >= 0 && level <= params.getMaxZoom()) { return (new ZoomTransaction(camera, level)); } else { throw new IllegalArgumentException(String.format("Invalid zoom level: %d", level)); } } }
@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(); }
public void zoomin(float m) { if (params.isZoomSupported()) { maxZoomLevel = params.getMaxZoom(); float zoom = (float) currentZoomLevel; zoom += (m / 100f); currentZoomLevel = (int) zoom; if (currentZoomLevel > maxZoomLevel) currentZoomLevel = maxZoomLevel; if (currentZoomLevel < 1) currentZoomLevel = 1; params.setZoom(currentZoomLevel); mCamera.setParameters(params); } }
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; }
public int getMaxZoom() { Camera.Parameters parameters = mCamera.getParameters(); if (!parameters.isZoomSupported()) return 0; return parameters.getMaxZoom(); }