Skip to content

fix(ios): focus close-range barcodes on iPhone Pro cameras#136

Draft
masanggil1986 wants to merge 1 commit into
CodingWithTashi:mainfrom
masanggil1986:fix/ios-pro-close-focus
Draft

fix(ios): focus close-range barcodes on iPhone Pro cameras#136
masanggil1986 wants to merge 1 commit into
CodingWithTashi:mainfrom
masanggil1986:fix/ios-pro-close-focus

Conversation

@masanggil1986

Copy link
Copy Markdown

Problem

On iPhone Pro models the rear wide-angle camera has a longer minimum focus distance (~15–20 cm) than the distance at which users naturally hold a barcode. The iOS scanner hard-codes AVCaptureDevice.DiscoverySession(deviceTypes: [.builtInWideAngleCamera], …), so codes held close never come into focus and can't be scanned. Non-Pro iPhones focus closer, so they're unaffected — which is why this only reproduces on Pro devices.

Fix

  • Prefer a virtual multi-camera device for the rear position (.builtInTripleCamera.builtInDualWideCamera.builtInWideAngleCamera). Virtual devices let iOS automatically switch to the ultra-wide lens for macro focus when the subject is closer than the wide lens can handle.
  • Keep the familiar ~1× framing by setting videoZoomFactor to the device's first virtualDeviceSwitchOverVideoZoomFactors value (instead of the 0.5× ultra-wide view), while still allowing the automatic macro switch for close subjects.
  • Bias autofocus toward close subjects: autoFocusRangeRestriction = .near + .continuousAutoFocus.
  • Focus is configured after startRunning() — starting a session can change the active format, which resets videoZoomFactor — and re-applied when switching back to the rear camera.

Camera selection is shared by the initial setup and the switch-camera button via preferredBackCamera(); focus configuration lives in enableCloseFocus(on:).

Backward compatibility

  • No public API change — no Dart or method-channel surface is touched, so existing callers are unaffected.
  • Every setting is capability-gated (#available(iOS 13.0, *), isAutoFocusRangeRestrictionSupported, isFocusModeSupported, empty virtualDeviceSwitchOverVideoZoomFactors), so single-lens devices (e.g. iPhone SE) keep the exact previous behavior.
  • Deployment target stays iOS 12.0; iOS-13-only APIs are behind #available.

Testing

  • ✅ Compiles cleanly — verified by building the bundled example app for the iOS simulator (Xcode 26.6).
  • Physical-device verification pending — opening as a draft. I'll test on an iPhone Pro shortly, post the results here, and mark the PR ready for review once confirmed. I noticed the README mentions iOS testing is currently a gap — I have Pro hardware available and I'm happy to help test related iOS changes as well.

The iOS scanner hard-coded the built-in wide-angle camera, whose minimum focus distance on iPhone Pro models (~15-20cm) is longer than the natural barcode scanning distance, so codes held close never come into focus.

Prefer a virtual multi-camera device (triple / dual-wide) when available so the system can switch to the ultra-wide lens for macro focus; frame it at the standard wide zoom and restrict autofocus to the near range. Focus is configured after the session starts, since starting a session can change the active format and reset videoZoomFactor. Every setting is capability-gated, so single-lens devices behave exactly as before.
@sonarqubecloud

sonarqubecloud Bot commented Jul 8, 2026

Copy link
Copy Markdown

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.

1 participant