Skip to content

Migrate CameraX snippets - #1139

Draft
barbaralaw wants to merge 5 commits into
android:mainfrom
StellarElements:barbaralaw/camerax-snippets
Draft

barbaralaw wants to merge 5 commits into
android:mainfrom
StellarElements:barbaralaw/camerax-snippets

Conversation

@barbaralaw

@barbaralaw barbaralaw commented Sep 25, 2026 •

Copy link
Copy Markdown
Contributor

Code snippets are for:

This builds on @katherinekuan's first pass (her commit is kept with its original authorship). Follow-up commits move the files into the existing :camera module (com.example.camera.snippets.camerax), switch new dependencies to the version catalog, and bring the code inside every region tag back in line with what the guide pages above print.

camera/src/main/res/layout/activity_main.xml is required for the Kotlin snippets to compile: the page code references R.layout.activity_main, R.id.previewView, and R.id.preview_view, which also forces the file name. It has no region tag; the preview page's XML layout block stays hardcoded because it prints xmlns and size attributes the compiling layout can't hide.

List of modifications:

  • All pages: Added a full stop to comments that lacked one; formatted with spotless (some wrapped lines inside regions changed indentation, and stray semicolons were removed); whole-line .../… elisions became // ... comments, or plain [START_EXCLUDE] blocks (rendered as ...) where the hidden code is needed to compile.
  • Analyze page: Region now starts at val imageAnalysis =; restored this as the lifecycle owner; the "insert your code here" comment is normalized to // Insert your code here.
  • Architecture page: Updated the custom LifecycleOwner to the current API (override val lifecycle, currentState =); getAvailableCameraInfos() → availableCameraInfos (CameraProvider is Kotlin in CameraX 1.6).
  • Camera1-to-camerax page:
    • getAvailableCameraInfos() → availableCameraInfos. getCameraSelector() is kept as printed, because the prose names CameraInfo.getCameraSelector() and CameraInfo is still a Java interface.
    • Fixed requireContext(), the *useCases spread, a stray }), return@first, Observer<Int>, missing Boolean returns, and private var inside function bodies.
    • Tap-to-focus: meteringPoint → focusPoint and result → focusResult, the names the snippet itself declares.
    • Pinch-to-zoom: SimpleOnGestureListener → ScaleGestureDetector.SimpleOnScaleGestureListener, the listener that defines onScale().
    • Take photo with CameraProvider: the page's takePicture(...) arguments are a plain exclude, so the page still shows .... The arguments, commas, and callback object are hidden together so the rendered code stays balanced.
    • The published page drops Kotlin generics in several blocks (unescaped <…> in the HTML); the migrated snippets show them: Observer<Int>, Observer<ZoomState>, Array<UseCase>, VideoCapture<Recorder>.
  • Configuration page:
    • ImageCapture.Builder example: the page's setFlashMode(...) / setTargetAspectRatio(...) placeholders are kept visible with a silent twin (page lines in a silent block comment, compiling values in a silent exclude), so the rendered code matches the page and both calls still compile.
    • MeteringPoint example: the page declares val meteringPointFactory twice. The second declaration line is kept visible with the same silent-twin approach, so the SurfaceOrientedMeteringPointFactory call still compiles without a redeclaration.
    • ?: return for a nullable; Kotlin lambda + .toFloat() in the metering snippet; fixed (x1, y1), or, and a missing ).
  • Extensions-api page: Printed imports kept as // import ... inside the region, with the real imports at the top of the file.
  • Mlkitanalyzer page: Added androidx.camera:camera-mlkit-vision and com.google.mlkit:barcode-scanning 17.3.0 to the catalog and :camera.
  • Orientation-rotation page: The two identical ImageProxy rotation blocks share one region tag; the three CameraActivity examples are each wrapped in a private object; AppCompatActivity → ComponentActivity in the three printed CameraActivity classes.
  • Preview page: Printed imports kept as // import ...; added the missing super.onCreate(savedInstanceState); AppCompatActivity → ComponentActivity in the printed MainActivity.
  • Transform-output page: Removed a stray ; and adjusted indentation and spacing (fun ...(): Matrix).
  • Video-capture page: Formatting only.
  • Build: All Activity classes in the CameraX files (hidden wrappers and the four printed classes above) extend ComponentActivity, so :camera doesn't need androidx.appcompat (consistent with Migrate Camera2 hardcoded snippets #1134). Neither page's prose names AppCompatActivity. :camera:spotlessCheck, :camera:compileDebugKotlin, and :camera:lintDebug pass.

Snippets not migrated (left hardcoded on the guide pages):

  • Gradle dependency blocks and AndroidManifest.xml entries.
  • Camera1 "before" code on camera1-to-camerax (older API shown for migration contrast).
  • Camera1-to-camerax CameraController video recording snippet, because it uses APIs removed from CameraX.
  • Orientation-rotation Exif snippets, because they use the restricted androidx.camera.core.impl.utils.Exif.
  • View Binding snippets (architecture, camera1-to-camerax, configuration seek bar, video-capture list), because the :camera module doesn't use View Binding.
  • Preview page XML layout (see above).

Documentation CL: http://cl/988523079

kkuan2011 and others added 3 commits September 29, 2026 17:04
- Update package to com.example.camera.snippets.camerax after the
  :camerax -> :camera module rename (android#1077).
- Declare camera-mlkit-vision and mlkit barcode-scanning in the version
  catalog instead of hardcoded coordinates.
- Remove lint abortOnError = false.
- Replace the tagged preview_view.xml with an untagged activity_main.xml
  supporting layout (forced by R.layout.activity_main, R.id.previewView and
  R.id.preview_view in page code).
- Restore page-exact code inside region tags (full stops, fill-in
  comments, instructional imports, placeholders).
- Fix code that does not compile against CameraX 1.6.1.
- Remove View Binding and restricted-API snippets that stay hardcoded.
- Wrap same-page class collisions in private objects.
@barbaralaw
barbaralaw force-pushed the barbaralaw/camerax-snippets branch from e85c1c2 to 73a927a Compare September 29, 2026 17:06
- Camera1-to-camerax: keep getCameraSelector() as printed (the prose
  names it and CameraInfo is a Java interface); hide the takePicture()
  arguments in a plain exclude so the page's "..." renders.
- Configuration: keep the page's setFlashMode(...) /
  setTargetAspectRatio(...) placeholders and the duplicate
  meteringPointFactory declaration visible with silent twins; restore
  the page's single useCaseGroup.
- Replace the CameraActivity.rootView extension and the context
  wrapper property with plain helpers.
- Use ComponentActivity instead of AppCompatActivity everywhere and
  drop the appcompat dependency from :camera.
- Suppress the USELESS_ELVIS warning from the page's own
  "previewView ?: return false".
- Sort mlkit-barcode-scanning in the version catalog.

This branch has not been deployed

No deployments
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