Skip to content

Conversation

@YescoSoftware
Copy link

@YescoSoftware YescoSoftware commented Jan 9, 2026

Summary

Improves camera device enumeration on Android by properly detecting camera types (ultraWide, wideAngle, telephoto) and fixes issues with device switching and lifecycle state preservation.

Changes

Device Type Detection

  • Calculate field of view (FOV) from focal length and sensor size to accurately classify cameras
  • Classify cameras as ultraWide (FOV > 90°), telephoto (FOV < 40°), or wideAngle
  • Use actual focal lengths from camera hardware instead of hardcoded 4.25mm value
  • Works correctly on devices like Samsung that expose physical cameras as separate logical cameras

Device Switching Fixes

  • Fix currentDeviceId tracking by capturing device ID after camera binding
  • Update switchToDevice() to properly set deviceId in sessionConfig
  • Preserve isCentered flag when creating new sessionConfig in switchToDevice()
  • Fixes issue where preview would go full-screen after backgrounding/foregrounding the app after switching devices

Technical Details

  • Uses Camera2 API (CameraManager, CameraCharacteristics) for accurate device information
  • FOV calculation: fov = 2 * atan(sensorWidth / (2 * focalLength))
  • Fallback classification by focal length when sensor size unavailable
  • Each camera reports its native zoom range (1x-8x for each device)

Testing

Tested on Samsung Galaxy device with 4 cameras:

  • Camera 0: Main wide (3.98mm, ~67.5° FOV) - correctly detected as wideAngle
  • Camera 1: Front (3.59mm) - correctly detected as wideAngle
  • Camera 2: Ultra-wide (1.4mm, ~92° FOV) - correctly detected as ultraWide ✓
  • Camera 3: Front (3.59mm) - correctly detected as wideAngle

Device switching and lifecycle preservation working correctly.

Breaking Changes

None - this is backward compatible and only improves existing functionality.

Related Issues

Fixes issues with camera device detection and switching on Android devices with multiple cameras.

Summary by CodeRabbit

Release Notes

  • Bug Fixes
    • Improved camera device detection and classification for better multi-camera support.
    • Enhanced camera switching reliability across different device orientations.
    • Refined handling of physical and logical camera enumeration for more accurate device reporting.

✏️ Tip: You can customize this high-level summary in your review settings.

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Jan 9, 2026

Caution

Review failed

The pull request is closed.

📝 Walkthrough

Walkthrough

This change enhances camera device handling in CameraXView by adding device ID retrieval during camera binding, expanding device enumeration with dynamic device-type inference (ultraWide, wideAngle, telephoto) using Camera2CameraInfo and CameraCharacteristics, and updating camera switching and flipping logic to work with the enhanced device identification scheme.

Changes

Cohort / File(s) Summary
Camera Device Enumeration & Control
android/src/main/java/app/capgo/capacitor/camera/preview/CameraXView.java
Enhanced bindCameraUseCases() to retrieve and store bound camera device ID via Camera2CameraInfo. Substantially expanded getAvailableDevicesStatic() with two-step device resolution, dynamic device-type inference using CameraCharacteristics (focal length, FOV computation), and support for both logical and physical cameras with new LensInfo labeling. Updated switchToDevice() to modify session configuration instead of rebuilding selector, and modified flipCamera() to clear device ID before rebinding. Added comprehensive debug logging throughout device enumeration and switching lifecycle.

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant CameraXView
    participant CameraProvider
    participant Camera2Info as Camera2CameraInfo
    participant Characteristics as CameraCharacteristics
    participant SessionConfig

    User->>CameraXView: Enumerate available devices
    activate CameraXView
    CameraXView->>CameraProvider: getAvailableCameraInfos()
    CameraProvider-->>CameraXView: List of CameraInfo objects
    
    loop For each CameraInfo
        CameraXView->>Camera2Info: from(cameraInfo)
        Camera2Info-->>CameraXView: Camera2CameraInfo instance
        CameraXView->>Characteristics: getCameraCharacteristics()
        Characteristics-->>CameraXView: Focal lengths & physical size
        CameraXView->>CameraXView: Infer device type (ultraWide/wide/tele)
        CameraXView->>CameraXView: Compute FOV
        CameraXView->>CameraXView: Create LensInfo with type
    end
    
    loop For each physical camera
        CameraXView->>Characteristics: Get physical camera characteristics
        Characteristics-->>CameraXView: Device properties
        CameraXView->>CameraXView: Infer physical device type
        CameraXView->>CameraXView: Create physical camera entry
    end
    
    CameraXView-->>User: Device list with inferred types
    deactivate CameraXView
Loading
sequenceDiagram
    participant User
    participant CameraXView
    participant SessionConfig
    participant BindProcess as Binding Process

    User->>CameraXView: Switch to device
    activate CameraXView
    CameraXView->>SessionConfig: Update with new deviceId & position
    CameraXView->>BindProcess: Call bindCameraUseCases()
    activate BindProcess
    BindProcess->>CameraXView: Retrieve camera ID from binding
    BindProcess->>CameraXView: Store in currentDeviceId
    BindProcess-->>CameraXView: Binding complete
    deactivate BindProcess
    CameraXView-->>User: Device switched
    deactivate CameraXView
Loading

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~45 minutes

Poem

🐰 A digital lens through which we see,
Device types dance in harmony,
Focal lengths and FOV align,
Logical, physical—sublime design!
The cameras flip and bind anew,
A brighter view for all to view! 📸

✨ Finishing touches
  • 📝 Generate docstrings

📜 Recent review details

Configuration used: defaults

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 178fd87 and b53c675.

📒 Files selected for processing (1)
  • android/src/main/java/app/capgo/capacitor/camera/preview/CameraXView.java

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants