8000 The camera preview: thicket-camera-preview and -android (docs/17 step 2) by rleibman · Pull Request #149 · rleibman/thicket · GitHub
[go: up one dir, main page]

Skip to content

The camera preview: thicket-camera-preview and -android (docs/17 step 2) - #149

Merged
rleibman merged 6 commits into
mainfrom
feat/camera-preview
Nov 4, 2026
Merged

rleibman merged 6 commits into
mainfrom
feat/camera-preview

Conversation

@rleibman
@rleibman rleibman commented Nov 3, 2026
Copy link
Copy Markdown
Owner

Step 2 of docs/17 §17.7: a live camera preview in the app's own layout, with a still capture from it. The widget goes through the embedding point from #146. The Apple half is #148, which depends on #145; GTK comes later (§17.8.2).

What it adds

  • thicket-camera-preview (JVM, Native):
    • CameraPreview, with availability, facing: Var[Facing], running: Signal[Boolean], view(fit) (Cover by default) and capture(onResult: Option[Path] => Unit).
    • CameraPreview.unavailable(reason), whose widget shows the reason.
    • testing.FakeCameraPreview.
  • thicket-camera-preview-android: Camera2 into a TextureView, not CameraX, which is AndroidX.
    • Capture: a JPEG ImageReader on the same session, at the largest size both outputs offer, up to 1920×1080.
    • Orientation and fit: composed by hand in the view's transform.
    • When it runs: only while the view is mounted, its surface is ready, the app is in front and the permission is granted. One preview at a time.
    • Stop reasons: every close records why (testing.stopReasons).
    • Size: where the layout leaves the height open, the widget takes the frame's aspect, rather than measuring 0 px tall in a scrolling column.
  • Gallery: a Camera preview section.

Measured

Before After
checkLinux exit 0
CameraPreviewSpec 2/2, JVM and Native
GTK gallery / todo 144 / 69 148 / 69
Android gallery on s2test 95 102

The Android checks:

  1. The preview is available and running, with the back camera open.
  2. Frames keep arriving, and the picture isn't blank.
  3. A capture is a JPEG in the cache at the session's size (1280×720 here).
  4. Home closes the camera, and the preview's own lifecycle closes it first.
  5. Back in front, the camera opens again and frames arrive.
  6. Flipping to the front camera, which s2test doesn't have, says so, and nothing runs.
  7. Flipping back opens the back camera again.

checkLinux's first run failed ScaleSpec's frame budget on coreNative: 27.22 ms under load. Alone it passed 3 of 3 runs, at 2.8–3.4 ms, and the second full run passed. This PR doesn't touch core.

Falsified

  • Background stop removed: check 4 fails. The recorded stops are facing…, error 3, disconnected: Android takes the camera from a background app by itself. That's why "closed in the background" alone wasn't enough, and the check also asks which stop came first.
  • capture stubbed: check 3 fails.

Not done

🤖 Generated with Claude Code

A live camera preview in the app's own layout, through the embedding point:
- thicket-camera-preview (JVM, Native): CameraPreview (availability, facing,
  running, view(fit), capture), Facing, CameraPreview.unavailable, a fake.
- thicket-camera-preview-android: Camera2 into a TextureView, a JPEG
  ImageReader on the same session; orientation and fit by the view's
  transform; runs only while mounted, its surface ready, in front and
  permitted; one preview at a time; each close records its reason.
- Gallery: a Camera preview section. Android runs it (the gallery declares
  CAMERA; the undeclared-permission check moves to the microphone); GTK
  shows CameraPreview.unavailable; Apple has none yet (#148).
- galleryAndroid no longer depends on pickersApple/cameraApple (as #147).

Measured: checkLinux exit 0 (a first run failed ScaleSpec's frame budget
on coreNative, 27.22 ms under load; 3/3 alone at 2.8-3.4 ms; this branch
does not touch core). CameraPreviewSpec 2/2 JVM and Native. GTK gallery
148 (144 + 4), todo 69. Android gallery 102 (95 + 7) on s2test: frames,
not blank, a 1280x720 JPEG capture, Home closes the camera (by the
preview's lifecycle first), front reopens, no front camera, flip back.
platforms/Test/compile 0 errors.
Falsified: without the lifecycle's background stop the background check
fails (stops: facing..., error 3, disconnected: Android takes the camera
itself); with capture stubbed the JPEG check fails.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Copilot AI left a comment
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.

Note

Copilot was unable to complete this review at the selected tier due to model policy or plan restrictions.

🟡 Changes recommended

Approval is blocked because the review evidence identifies unresolved crash-prone null/empty handling issues in camera size selection and image acquisition, plus exception swallowing that can mask misconfiguration and lead to incorrect availability behavior.

3 open issues
What changed in this PR

This pull request implements docs/17 step 2 by introducing a cross-platform CameraPreview service API and an Android Camera2-backed live preview + still capture implementation, wiring it into the Gallery (including Android self-tests) and updating build/release/documentation metadata.

Changes:

  • Add thicket-camera-preview shared API (CameraPreview, Facing, unavailable) plus testing.FakeCameraPreview and JVM/Native tests.
  • Add thicket-camera-preview-android using Camera2 + TextureView preview and ImageReader JPEG capture, with lifecycle/permission gating and sizing/transform logic.
  • Integrate the feature into the Gallery UI/self-tests and update Android manifest/build scripts and repository docs/status/release tables.

Review notes (unresolved):

  • Critical: Guard against null/empty SCALER_STREAM_CONFIGURATION_MAP / getOutputSizes(...) results to prevent crashes; stop the preview gracefully when sizes are unavailable. (AndroidCameraPreview.scala:299)
  • Moderate: Handle possible null from ImageReader.acquireLatestImage() to avoid exceptions (including in finally image.close()). (AndroidCameraPreview.scala:367)
  • Moderate: Don’t swallow exceptions from device.permissions.status(Permission.Camera); surface or convert to an explicit unavailable state with a clear message. (AndroidCameraPreview.scala:112)
  • Moderate: Ensure capture always posts the callback on the UI thread, including busy/not-running fallback paths. (AndroidCameraPreview.scala:158)

This pull request requires changes.

File Summary
README.md Updates repo/module overview and measured test/self-test counts to include camera preview.
modules/​camera-preview/​shared/​src/​test/​scala/​thicket/​camera/​preview/​CameraPreviewSpec.scala Adds JVM/Native tests covering unavailable preview behavior and fake capture behavior.
modules/​camera-preview/​shared/​src/​main/​scala/​thicket/​camera/​preview/​testing/​FakeCameraPreview.scala Adds a controllable fake CameraPreview for tests.
modules/​camera-preview/​shared/​src/​main/​scala/​thicket/​camera/​preview/​CameraPreview.scala Defines the CameraPreview API, Facing, and CameraPreview.unavailable(...).
modules/​camera-preview/​README.md Documents module usage/semantics (running, fit, capture) and testing.
modules/​camera-preview-android/​src/​main/​scala/​thicket/​camera/​preview/​android/​AndroidCameraPreview.scala Implements Camera2 preview/capture using TextureView, sizing/transform, lifecycle/permission gating, and test hooks.
examples/​gallery/​shared/​shared/​src/​main/​scala/​example/​gallery/​StorageDemo.scala Extends gallery services wiring to optionally include CameraPreview.
examples/​gallery/​shared/​shared/​src/​main/​scala/​example/​gallery/​Gallery.scala Adds the “Camera preview” section and threads preview state for host self-tests.
examples/​gallery/​shared/​shared/​src/​main/​scala/​example/​gallery/​CameraPreviewDemo.scala Adds demo logic/state for preview availability/running, capture, and facing flips.
examples/​gallery/​README.md Documents expected gallery behavior/self-tests for camera preview across platforms.
examples/​gallery/​gtk/​src/​main/​scala/​example/​gallery/​GalleryGtk.scala Wires GTK to CameraPreview.unavailable and adds related self-test expectations.
examples/​gallery/​android/​scala/​src/​main/​scala/​example/​gallery/​android/​GallerySelfTest.scala Adds Android end-to-end self-test steps for preview frames, capture, lifecycle stop/restart, and missing front camera behavior.
examples/​gallery/​android/​scala/​src/​main/​scala/​example/​gallery/​android/​GalleryActivity.scala Supplies AndroidCameraPreview to the shared gallery services.
examples/​gallery/​android/​build.sh Packages/builds the new camera-preview artifacts into the Android gallery.
examples/​gallery/​android/​app/​src/​main/​AndroidManifest.xml Declares android.permission.CAMERA for the preview feature.
docs/​decisions.md Records the decision/progress entry for camera preview step 2 and measured/falsified notes.
docs/​17-camera-preview.md Updates camera-preview doc status/progress for step 2.
docs/​16-device-services.md Updates device-services progress notes to reflect preview availability work.
docs/​14-releasing.md Adds camera-preview artifacts to the release matrix and updates module listings.
docs/​12-component-status.md Updates component-status entries and measured counts for camera preview.
build.sbt Adds cameraPreview + cameraPreviewAndroid projects and wires them into aggregates/dependencies.

Review effort: Balanced

rleibman and others added 5 commits November 3, 2026 16:34
Refactor size selection for camera preview and JPEG output.

Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
The same two-line fix was on both sides; the one conflict is the line this
branch adds next to it, cameraPreviewAndroid, kept. galleryAndroid/compile
and platforms/Test/compile: 0 errors. Nothing else changed on main, so the
GTK (148) and Android (102) counts measured on this branch stand.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…e the build

8cadb52 (Copilot autofix) made the JPEG write a Try[Option[Path]] and added
`finishCapture(written.toOption.flatten)`, but kept the old
`finishCapture(written.toOption)` after it, which no longer type-checks
(Found Option[Option[Path]], required Option[Path]), so #149 did not compile
from 8cadb52 on. The autofix's change itself (a null image answers None) is
kept; only the stale line goes.

Measured after: checkLinux exit 0; SELFTEST=1
examples/gallery/android/build.sh on s2test, 102 checks, ALL CHECKS PASSED
(with 72ffc16's size guard and df7c129's undeclared-permission throw too).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@rleibman
rleibman commented Nov 4, 2026
Copy link
Copy Markdown
Owner Author

main (with #147) is merged in. The only conflict was the galleryAndroid dependency list, where this PR adds cameraPreviewAndroid.

The branch didn't compile after the Copilot autofix commits. 8cadb52 made the JPEG write a Try[Option[Path]] and added finishCapture(written.toOption.flatten), but left the old finishCapture(written.toOption) below it, which no longer type-checks. f5b3283 removes that one line and keeps the autofix's change (a null image now answers None). The other two autofix commits look right and are kept:

  • 72ffc16 guards against a camera with no output sizes.
  • df7c129 lets an undeclared CAMERA permission throw, as thicket-camera-android does.

Measured after:

  • checkLinux: exit 0.
  • Android gallery on s2test: 102 checks, ALL CHECKS PASSED.

One mistake of mine: my merge commit eb5c31a was pushed before I saw the compile error. That commit itself was fine; the error came from 8cadb52. I now run the build before committing.

🤖 Generated with Claude Code

@rleibman
rleibman merged commit 3b1cb78 into main Nov 4, 2026
1 of 2 checks passed
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

0