Skip to content

Use takeSnapshot instead of takePhoto on Android#84951

Merged
Julesssss merged 25 commits intomainfrom
marcaaron-androidVisionCameraImprovements
Mar 31, 2026
Merged

Use takeSnapshot instead of takePhoto on Android#84951
Julesssss merged 25 commits intomainfrom
marcaaron-androidVisionCameraImprovements

Conversation

@marcaaron
Copy link
Copy Markdown
Contributor

@marcaaron marcaaron commented Mar 11, 2026

Explanation of Change

After taking a receipt photo, the transition to the confirmation page felt sluggish due to slow photo capture on Android, synchronous Onyx writes blocking navigation, and a heavy component tree rendering during the navigation animation.

This PR addresses:

Faster Android capture (captureReceipt/index.android.ts): On Android, use takeSnapshot instead of takePhoto when flash is off. takeSnapshot grabs the preview buffer directly (~16ms), avoiding the full camera capture pipeline. When flash is on, fall back to takePhoto. iOS keeps takePhoto for both cases.

iOS camera format optimization (index.native.tsx): Reorder useCameraFormat priorities so photoResolution takes precedence over videoResolution. Previously videoResolution:'max' was selecting a 5712x4284 (24.5MP) format when photos only output at 4032x3024. The new ordering selects a 4032x3024 format directly, cutting iOS capture time roughly in half (~400ms → ~200ms). Cap fps at 30 to prevent the "soap opera effect" that lower-resolution formats exhibit at higher native frame rates.

Camera quality/speed tuning (Camera.tsx): Change photoQualityBalance from "speed" to "balanced" — benchmarking showed only ~15ms difference but "balanced" applies noise reduction that produces visually better photos with smaller file sizes (2.3MB → 1.5MB for the same scene). Disable photoHdr and videoStabilizationMode per VisionCamera performance tips. Remove redundant props (enableBufferCompression, enableDepthData, enablePortraitEffectsMatteDelivery) that already match their defaults.

Deferred navigation (index.native.tsx): Use requestAnimationFrame before navigating to the confirmation step so React renders the frozen camera state (didCapturePhoto=true) before the screen transitions away.

Benchmarks (iOS, iPhone — same subject across tests)

Setting Capture Time File Size Resolution
Before (5712x4284 format, "speed") ~400ms ~2.3 MB 4032x3024
After (4032x3024 format, "balanced", fps=30) ~200ms ~1.5 MB 4032x3024

Fixed Issues

$ #84793
$ #85006

Tests

  1. Open app, tap FAB → Submit expense → take a receipt photo
  2. Verify receipt image appears clear and legible compared to production
  3. Submit the expense successfully
  4. Repeat from within a report chat (+ button → Submit expense → photo)

Repeat with track expense flow (FAB → Create expense → photo)

  1. Create a new account with no workspace
  2. Tap "Create expense" in FAB

General testing of flash

  1. Scan a receipt with flash on
  2. Scan a receipt with flash off
  3. Verify that in both cases the result is legible

Test multi-scan mode (take multiple photos)

  1. Scan a variety of receipts in multi-scan mode
  2. Verify they are legible and created with no issues.
  • Verify that no errors appear in the JS console

Offline tests

N/A

QA Steps

Same as tests

  • Verify that no errors appear in the JS console

PR Author Checklist

  • I linked the correct issue in the ### Fixed Issues section above
  • I wrote clear testing steps that cover the changes made in this PR
    • I added steps for local testing in the Tests section
    • I added steps for the expected offline behavior in the Offline steps section
    • I added steps for Staging and/or Production testing in the QA steps section
    • I added steps to cover failure scenarios (i.e. verify an input displays the correct error message if the entered data is not correct)
    • I turned off my network connection and tested it while offline to ensure it matches the expected behavior (i.e. verify the default avatar icon is displayed if app is offline)
    • I tested this PR with a High Traffic account against the staging or production API to ensure there are no regressions (e.g. long loading states that impact usability).
  • I included screenshots or videos for tests on all platforms
  • I ran the tests on all platforms & verified they passed on:
    • Android: Native
    • Android: mWeb Chrome
    • iOS: Native
    • iOS: mWeb Safari
    • MacOS: Chrome / Safari
  • I verified there are no console errors (if there's a console error not related to the PR, report it or open an issue for it to be fixed)
  • I followed proper code patterns (see Reviewing the code)
    • I verified that any callback methods that were added or modified are named for what the method does and never what callback they handle (i.e. toggleReport and not onIconClick)
    • I verified that comments were added to code that is not self explanatory
    • I verified that any new or modified comments were clear, correct English, and explained "why" the code was doing something instead of only explaining "what" the code was doing.
    • I verified any copy / text shown in the product is localized by adding it to src/languages/* files and using the translation method
      • If any non-english text was added/modified, I used JaimeGPT to get English > Spanish translation. I then posted it in #expensify-open-source and it was approved by an internal Expensify engineer. Link to Slack message:
    • I verified all numbers, amounts, dates and phone numbers shown in the product are using the localization methods
    • I verified any copy / text that was added to the app is grammatically correct in English. It adheres to proper capitalization guidelines (note: only the first word of header/labels should be capitalized), and is either coming verbatim from figma or has been approved by marketing (in order to get marketing approval, ask the Bug Zero team member to add the Waiting for copy label to the issue)
    • I verified proper file naming conventions were followed for any new files or renamed files. All non-platform specific files are named after what they export and are not named "index.js". All platform-specific files are named for the platform the code supports as outlined in the README.
    • I verified the JSDocs style guidelines (in STYLE.md) were followed
  • If a new code pattern is added I verified it was agreed to be used by multiple Expensify engineers
  • I followed the guidelines as stated in the Review Guidelines
  • I tested other components that can be impacted by my changes (i.e. if the PR modifies a shared library or component like Avatar, I verified the components using Avatar are working as expected)
  • I verified all code is DRY (the PR doesn't include any logic written more than once, with the exception of tests)
  • I verified any variables that can be defined as constants (ie. in CONST.ts or at the top of the file that uses the constant) are defined as such
  • I verified that if a function's arguments changed that all usages have also been updated correctly
  • If any new file was added I verified that:
    • The file has a description of what it does and/or why is needed at the top of the file if the code is not self explanatory
  • If a new CSS style is added I verified that:
    • A similar style doesn't already exist
    • The style can't be created with an existing StyleUtils function (i.e. StyleUtils.getBackgroundAndBorderStyle(theme.componentBG))
  • If new assets were added or existing ones were modified, I verified that:
    • The assets are optimized and compressed (for SVG files, run npm run compress-svg)
    • The assets load correctly across all supported platforms.
  • If the PR modifies code that runs when editing or sending messages, I tested and verified there is no unexpected behavior for all supported markdown - URLs, single line code, code blocks, quotes, headings, bold, strikethrough, and italic.
  • If the PR modifies a generic component, I tested and verified that those changes do not break usages of that component in the rest of the App (i.e. if a shared library or component like Avatar is modified, I verified that Avatar is working as expected in all cases)
  • If the PR modifies a component related to any of the existing Storybook stories, I tested and verified all stories for that component are still working as expected.
  • If the PR modifies a component or page that can be accessed by a direct deeplink, I verified that the code functions as expected when the deeplink is used - from a logged in and logged out account.
  • If the PR modifies the UI (e.g. new buttons, new UI components, changing the padding/spacing/sizing, moving components, etc) or modifies the form input styles:
    • I verified that all the inputs inside a form are aligned with each other.
    • I added Design label and/or tagged @Expensify/design so the design team can review the changes.
  • If a new page is added, I verified it's using the ScrollView component to make it scrollable when more elements are added to the page.
  • I added unit tests for any new feature or bug fix in this PR to help automatically prevent regressions in this user flow.
  • If the main branch was merged into this PR after a review, I tested again and verified the outcome was still expected according to the Test steps.

@github-actions
Copy link
Copy Markdown
Contributor

🚧 @marcaaron has triggered a test Expensify/App build. You can view the workflow run here.

@github-actions

This comment has been minimized.

@codecov
Copy link
Copy Markdown

codecov bot commented Mar 12, 2026

Codecov Report

✅ Changes either increased or maintained existing code coverage, great job!

Files with missing lines Coverage Δ
src/CONST/index.ts 95.16% <ø> (ø)
...epScan/components/NavigationAwareCamera/Camera.tsx 0.00% <ø> (ø)
...tep/IOURequestStepScan/captureReceipt/index.ios.ts 33.33% <33.33%> (ø)
...u/request/step/IOURequestStepScan/index.native.tsx 40.92% <60.00%> (+0.67%) ⬆️
...st/step/IOURequestStepScan/captureReceipt/index.ts 0.00% <0.00%> (ø)
...IOURequestStepScan/captureReceipt/index.android.ts 0.00% <0.00%> (ø)
... and 10 files with indirect coverage changes

@marcaaron marcaaron changed the title Use takeSnapshot to test Android performance improvements Improve shutter-to-confirmation performance Mar 12, 2026
@github-actions
Copy link
Copy Markdown
Contributor

🚧 @marcaaron has triggered a test Expensify/App build. You can view the workflow run here.

@kubabutkiewicz
Copy link
Copy Markdown
Contributor

Looks good overall, but I see 2 potential problems

  1. Isnt preloading of the participants decreasing the perf of the Scan screen? I think potentialy this can create additonal rerenders
  2. Intersection manager is deprecated from what I know maybe we can use the React 19 features for that like https://react.dev/reference/react/startTransition

@marcaaron
Copy link
Copy Markdown
Contributor Author

Isnt preloading of the participants decreasing the perf of the Scan screen?

Good question. It might be. I was thinking if we defer it until the camera is ready then by that point the user is already scanning something and the re-renders would not create a negative impact. But think I will do some more E2E timing and compare things side by side.

After testing this a bit - it does seem better on low end Android, but kind of worse on higher end iOS.

@marcaaron marcaaron changed the base branch from main to marcaaron-fixSentryDurationLogs March 12, 2026 20:22
Copy link
Copy Markdown
Contributor

@Julesssss Julesssss left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sneaky, I love it. With a noticable improvements lets do this just for Android?

After testing this a bit - it does seem better on low end Android, but kind of worse on higher end iOS.

Base automatically changed from marcaaron-fixSentryDurationLogs to main March 12, 2026 20:52
@marcaaron
Copy link
Copy Markdown
Contributor Author

Sneaky, I love it. With a noticable improvements lets do this just for Android?

Yeah, I think also we should test on a higher end Android device to see if it still helps there. I rolled back the deferred rendering idea since it absolutely made performance worse on an iPhone 16 Pro.

@marcaaron marcaaron changed the title Improve shutter-to-confirmation performance [POC / WIP] Improve shutter-to-confirmation performance Mar 13, 2026
@github-actions
Copy link
Copy Markdown
Contributor

🚧 @Julesssss has triggered a test Expensify/App build. You can view the workflow run here.

# Conflicts:
#	src/pages/iou/request/step/IOURequestStepScan/hooks/useReceiptScan.ts
#	src/pages/iou/request/step/IOURequestStepScan/index.native.tsx
@github-actions
Copy link
Copy Markdown
Contributor

🚧 @marcaaron has triggered a test Expensify/App build. You can view the workflow run here.

@github-actions
Copy link
Copy Markdown
Contributor

@marcaaron marcaaron requested a review from Julesssss March 27, 2026 19:03
Julesssss
Julesssss previously approved these changes Mar 28, 2026
Copy link
Copy Markdown
Contributor

@Julesssss Julesssss left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Testing well on Android 👍

Copy link
Copy Markdown
Contributor

@QichenZhu QichenZhu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for the last minute request, just found a small style issue.

// This cuts capture time roughly in half while maintaining the same output photo resolution.
const format = useCameraFormat(device, [{photoAspectRatio: 4 / 3}, {photoResolution: {width: 4032, height: 3024}}, {videoResolution: 'max'}]);
// Use screen dimensions for video resolution since we only need enough for the preview.
const format = useCameraFormat(device, [{photoAspectRatio: 4 / 3}, {photoResolution: {width: 4032, height: 3024}}, {videoResolution: {width: windowHeight, height: windowWidth}}]);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we make these magic numbers constants?

…onCameraImprovements

# Conflicts:
#	src/pages/iou/request/step/IOURequestStepScan/index.native.tsx
Copy link
Copy Markdown
Contributor

@QichenZhu QichenZhu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@melvin-bot melvin-bot bot requested a review from AndrewGable March 30, 2026 23:50
@melvin-bot
Copy link
Copy Markdown

melvin-bot bot commented Mar 30, 2026

@AndrewGable Please copy/paste the Reviewer Checklist from here into a new comment on this PR and complete it. If you have the K2 extension, you can simply click: [this button]

@marcaaron
Copy link
Copy Markdown
Contributor Author

marcaaron commented Mar 31, 2026

Looking into why Melvin keeps trying to assign random people to this review 😄

@Julesssss ready for you whenever 🙇

Copy link
Copy Markdown
Contributor

@kubabutkiewicz kubabutkiewicz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@Julesssss Julesssss merged commit 545edf5 into main Mar 31, 2026
37 of 39 checks passed
@Julesssss Julesssss deleted the marcaaron-androidVisionCameraImprovements branch March 31, 2026 16:20
@github-actions
Copy link
Copy Markdown
Contributor

🚧 @Julesssss has triggered a test Expensify/App build. You can view the workflow run here.

@OSBotify
Copy link
Copy Markdown
Contributor

✋ This PR was not deployed to staging yet because QA is ongoing. It will be automatically deployed to staging after the next production release.

@OSBotify
Copy link
Copy Markdown
Contributor

🚀 Deployed to staging by https://github.com/Julesssss in version: 9.3.51-0 🚀

platform result
🕸 web 🕸 success ✅
🤖 android 🤖 success ✅
🍎 iOS 🍎 success ✅

Bundle Size Analysis (Sentry):

@MelvinBot
Copy link
Copy Markdown
Contributor

No help site changes are required for this PR.

I reviewed all relevant help site articles under docs/articles/ against the changes in this PR:

Articles reviewed:

  • new-expensify/reports-and-expenses/Create-an-Expense.md — SmartScan receipt creation workflow
  • new-expensify/reports-and-expenses/Attach-and-edit-receipts-on-expenses.md — Receipt attachment/editing
  • new-expensify/reports-and-expenses/Track-Expenses.md — Expense tracking with SmartScan
  • new-expensify/concierge-ai/Expense-Assistant.md — AI-powered expense creation
  • expensify-classic/expenses/Add-an-expense.md — Classic receipt scanning

Why no changes are needed:

This PR is a pure performance optimization with no user-facing behavioral changes:

  • Uses takeSnapshot instead of takePhoto on Android (internal API swap — same user action)
  • Optimizes camera format selection on iOS (internal config — no UI change)
  • Changes photoQualityBalance from "speed" to "quality" (internal quality setting — no user-visible control)
  • Defers navigation with requestAnimationFrame (smoother transition — no workflow change)

The existing help site instructions ("tap the camera icon and snap a photo of a receipt") remain accurate. No feature names, button labels, workflows, settings, or user-facing behaviors are affected.

@mitarachim
Copy link
Copy Markdown

Deploy Blocker #86852 was identified to be related to this PR.

@OSBotify
Copy link
Copy Markdown
Contributor

OSBotify commented Apr 2, 2026

🚀 Deployed to production by https://github.com/jasperhuangg in version: 9.3.51-10 🚀

platform result
🕸 web 🕸 success ✅
🤖 android 🤖 failure ❌
🍎 iOS 🍎 success ✅

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

9 participants