feat: add zoom ratio API (setZoomRatio) to ICameraProvider#566
feat: add zoom ratio API (setZoomRatio) to ICameraProvider#566
Conversation
…mera1Provider - ICameraProvider: add isZoomSupported(), getMinZoomRatio(), getMaxZoomRatio(), and setZoomRatio(float) with default no-op implementations so existing code requires no changes - CameraXProvider: implement via CameraControl.setZoomRatio() and CameraInfo.getZoomState(); always supported - Camera1Provider: implement by mapping float ratio to the nearest Camera.Parameters integer zoom index; guarded by isZoomSupported() Closes #529
|
Note Reviews pausedIt looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the Use the following commands to manage reviews:
Use the checkboxes below for quick actions:
WalkthroughAdds floating-point zoom controls to the camera provider API: new methods to query support and min/max ratios and to set a zoom ratio. Camera1 maps float ratios to discrete Camera.Parameters zoom indices; CameraX uses ZoomState and CameraControl to apply linear zoom. Changes
Sequence Diagram(s)sequenceDiagram
participant Client
participant Camera1Provider
participant CameraParameters as "Camera.Parameters"
Client->>Camera1Provider: setZoomRatio(ratio)
Camera1Provider->>CameraParameters: getZoomRatios() / getMaxZoom()
Camera1Provider->>Camera1Provider: compute nearest discrete index
Camera1Provider->>CameraParameters: setZoom(index)
CameraParameters-->>Camera1Provider: success / exception
Camera1Provider-->>Client: return / log error
sequenceDiagram
participant Client
participant CameraXProvider
participant ZoomState
participant CameraControl
Client->>CameraXProvider: setZoomRatio(ratio)
CameraXProvider->>ZoomState: getMinMaxZoom()
CameraXProvider->>CameraXProvider: clamp ratio to [min,max]
CameraXProvider->>CameraControl: setLinearZoom(clampedRatio)
CameraControl-->>CameraXProvider: onComplete callback
CameraXProvider-->>Client: return / log warning if unavailable
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Poem
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Pull request overview
Adds a zoom ratio API to the ICameraProvider abstraction so callers can control zoom consistently across both CameraX and Camera1 backends (addresses #529).
Changes:
- Adds default (no-op) zoom methods to
ICameraProvider:isZoomSupported,getMinZoomRatio,getMaxZoomRatio,setZoomRatio. - Implements zoom support in
CameraXProviderusingZoomStateandCameraControl.setZoomRatio(...). - Implements zoom support in
Camera1Providerby mapping a float ratio to the closest discreteParameters.setZoom(int)index.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
library/src/main/java/org/wysaid/camera/ICameraProvider.java |
Introduces the new zoom-related interface methods with default implementations to avoid breaking existing implementors. |
library/src/main/java/org/wysaid/camera/CameraXProvider.java |
Wires zoom to CameraX ZoomState/CameraControl. |
library/src/main/java/org/wysaid/camera/Camera1Provider.java |
Adds Camera1 zoom support via Parameters.isZoomSupported(), getZoomRatios(), and discrete zoom index selection. |
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@library/src/main/java/org/wysaid/camera/CameraXProvider.java`:
- Around line 367-375: Clamp the incoming ratio in setZoomRatio before passing
to CameraControl: if mCamera is null return as before; otherwise compute a
clamped value using getMinZoomRatio() and getMaxZoomRatio() and call
mCamera.getCameraControl().setZoomRatio(clampedRatio) (still using
ContextCompat.getMainExecutor(mContext) and the existing listener). This ensures
setZoomRatio honors the ICameraProvider contract and avoids passing out-of-range
values to CameraControl.
ℹ️ Review info
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (3)
library/src/main/java/org/wysaid/camera/Camera1Provider.javalibrary/src/main/java/org/wysaid/camera/CameraXProvider.javalibrary/src/main/java/org/wysaid/camera/ICameraProvider.java
…ZoomRatio - isZoomSupported() now returns false when camera is not yet bound or ZoomState is unavailable, giving callers a reliable capability check - setZoomRatio() clamps the incoming ratio to [min, max] before passing to CameraControl, honoring the ICameraProvider contract - observe the returned ListenableFuture to log errors instead of silently discarding failures
…s in Camera1Provider
There was a problem hiding this comment.
Actionable comments posted: 2
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@library/src/main/java/org/wysaid/camera/Camera1Provider.java`:
- Around line 166-183: In setZoomRatio, guard against invalid ratio inputs and
null/empty zoomRatios: validate the float ratio passed into
Camera1Provider.setZoomRatio (reject or clamp NaN and Infinity and ensure it
falls within a sensible range, e.g., 0.0–1.0 or derived from parameters) and
check that params.getZoomRatios() is not null and not empty before using size()
or iterating; if the list is empty or zoom is unsupported, return early. Also
ensure you handle the case where CameraInstance.getInstance().getParams()
returns null (early return) and only compute bestIndex when zoomRatios is
present. Use the existing symbols (CameraInstance.getInstance().getParams(),
params.isZoomSupported(), params.getZoomRatios(), and the bestIndex loop) to
locate and apply these guards.
- Around line 159-163: The code in Camera1Provider indexes
params.getZoomRatios() without validating the list; add defensive checks in the
method that fetches zoom ratio: retrieve List<Integer> ratios from
Camera.Parameters, return 1.0f if ratios is null or empty, and if ratios.size()
<= params.getMaxZoom() use a safe index (e.g., Math.min(params.getMaxZoom(),
ratios.size() - 1)) to pick the ratio; keep the rest of the logic (divide by
100.0f) the same so vendor-broken implementations won't crash when calling
params.getMaxZoom().
- getMaxZoomRatio: guard against null/empty getZoomRatios() list and use Math.min to avoid ArrayIndexOutOfBoundsException on vendor devices where list size < getMaxZoom() + 1 - setZoomRatio: reject NaN/Infinity/non-positive ratio early; guard against null/empty zoomRatios list; use long arithmetic to avoid int overflow in diff computation Addresses coderabbitai review comments (Critical + Major).
Eliminate repeated CameraInstance.getInstance().getParams() calls (5 sites) and the duplicated zoom-support guard (params == null || !isZoomSupported) by introducing two private helpers: - getParams(): shorthand wrapper, no behavioral change - getZoomParams(): returns params only when camera is open and zoom is supported; used by isZoomSupported, getMaxZoomRatio, setZoomRatio Pure structural cleanup, zero logic change.
- Rename CHANGE.md -> CHANGELOG.md (standard Keep a Changelog convention) - Rewrite in standard Markdown format (## [version] — date headings) - Fill in missing history gaps between v2.5.0 (2018) and v3.1.1 (2026): - v3.1.2, v3.1.1, v2.7.0-alpha with full feature/fix/perf/chore entries - Source: GitHub Releases, merged PRs (#510–#566), and git log - Add [Unreleased] section tracking master commits not yet tagged - Fix copyright year range in LICENSE: 2017 -> 2017-2026
Summary
Adds a zoom ratio API to the
ICameraProviderabstraction layer, enabling callers to control optical/digital zoom on both the CameraX and Camera1 backends.Closes #529
Changes
ICameraProvider— new interface methods (all withdefaultno-ops, zero breaking changes)boolean isZoomSupported()float getMinZoomRatio()float getMaxZoomRatio()void setZoomRatio(float ratio)CameraXProviderisZoomSupported()→ alwaystrue(CameraX always supports zoom)getMin/MaxZoomRatio()→ reads fromCameraInfo.getZoomState()setZoomRatio()→ callsCameraControl.setZoomRatio()(async, fire-and-forget)Camera1ProviderisZoomSupported()→ delegates toCamera.Parameters.isZoomSupported()getMaxZoomRatio()→ readsgetZoomRatios().get(getMaxZoom()) / 100fsetZoomRatio()→ maps float ratio to the closest discrete integer zoom index viaParameters.setZoom(int); slight rounding is expected (Camera1 only exposes discrete steps)Backward Compatibility
All four methods use
defaultimplementations, so any customICameraProviderthat does not override them compiles and runs without change.How to verify
Summary by CodeRabbit