Repository navigation
The camera preview: thicket-camera-preview and -android (docs/17 step 2) - #149
Conversation
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>
There was a problem hiding this comment.
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-previewshared API (CameraPreview,Facing,unavailable) plustesting.FakeCameraPreviewand JVM/Native tests. - Add
thicket-camera-preview-androidusing Camera2 +TextureViewpreview andImageReaderJPEG 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
nullfromImageReader.acquireLatestImage()to avoid exceptions (including infinally 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
capturealways 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
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>
|
The branch didn't compile after the Copilot autofix commits.
Measured after:
One mistake of mine: my merge commit 🤖 Generated with Claude Code |


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, withavailability,facing: Var[Facing],running: Signal[Boolean],view(fit)(Coverby default) andcapture(onResult: Option[Path] => Unit).CameraPreview.unavailable(reason), whose widget shows the reason.testing.FakeCameraPreview.thicket-camera-preview-android: Camera2 into aTextureView, not CameraX, which is AndroidX.ImageReaderon the same session, at the largest size both outputs offer, up to 1920×1080.testing.stopReasons).CAMERA, whichinstall -ggrants, so the Device section's "an undeclared permission throws" check moved from the camera to the microphone.CameraPreview.unavailable.Measured
checkLinuxCameraPreviewSpecs2testThe Android checks:
s2testdoesn't have, says so, and nothing runs.checkLinux's first run failedScaleSpec's frame budget oncoreNative: 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 touchcore.Falsified
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.capturestubbed: check 3 fails.Not done
NeedsPermissionisn't exercised on a device.install -ggrants the permission, and revoking it kills the app.build.sbtfix. If build.sbt: galleryAndroid does not depend on pickersApple or cameraApple #147 merges first, the change is identical on both sides.🤖 Generated with Claude Code