Migrate CameraX snippets - #1139
Draft
barbaralaw wants to merge 5 commits into
Draft
barbaralaw wants to merge 5 commits into
barbaralaw wants to merge 5 commits into
Conversation
- 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
force-pushed
the
barbaralaw/camerax-snippets
branch
from
September 29, 2026 17:06
e85c1c2 to
73a927a
Compare
- 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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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
:cameramodule (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.xmlis required for the Kotlin snippets to compile: the page code referencesR.layout.activity_main,R.id.previewView, andR.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 printsxmlnsand size attributes the compiling layout can't hide.List of modifications:
.../…elisions became// ...comments, or plain[START_EXCLUDE]blocks (rendered as...) where the hidden code is needed to compile.val imageAnalysis =; restoredthisas the lifecycle owner; the "insert your code here" comment is normalized to// Insert your code here.LifecycleOwnerto the current API (override val lifecycle,currentState =);getAvailableCameraInfos()→availableCameraInfos(CameraProvideris Kotlin in CameraX 1.6).getAvailableCameraInfos()→availableCameraInfos.getCameraSelector()is kept as printed, because the prose namesCameraInfo.getCameraSelector()andCameraInfois still a Java interface.requireContext(), the*useCasesspread, a stray}),return@first,Observer<Int>, missing Boolean returns, andprivate varinside function bodies.meteringPoint→focusPointandresult→focusResult, the names the snippet itself declares.SimpleOnGestureListener→ScaleGestureDetector.SimpleOnScaleGestureListener, the listener that definesonScale().CameraProvider: the page'stakePicture(...)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.<…>in the HTML); the migrated snippets show them:Observer<Int>,Observer<ZoomState>,Array<UseCase>,VideoCapture<Recorder>.ImageCapture.Builderexample: the page'ssetFlashMode(...)/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.val meteringPointFactorytwice. The second declaration line is kept visible with the same silent-twin approach, so theSurfaceOrientedMeteringPointFactorycall still compiles without a redeclaration.?: returnfor a nullable; Kotlin lambda +.toFloat()in the metering snippet; fixed(x1, y1),or, and a missing).// import ...inside the region, with the real imports at the top of the file.androidx.camera:camera-mlkit-visionandcom.google.mlkit:barcode-scanning17.3.0 to the catalog and:camera.ImageProxyrotation blocks share one region tag; the threeCameraActivityexamples are each wrapped in aprivate object;AppCompatActivity→ComponentActivityin the three printedCameraActivityclasses.// import ...; added the missingsuper.onCreate(savedInstanceState);AppCompatActivity→ComponentActivityin the printedMainActivity.;and adjusted indentation and spacing (fun ...(): Matrix).ComponentActivity, so:cameradoesn't needandroidx.appcompat(consistent with Migrate Camera2 hardcoded snippets #1134). Neither page's prose namesAppCompatActivity.:camera:spotlessCheck,:camera:compileDebugKotlin, and:camera:lintDebugpass.Snippets not migrated (left hardcoded on the guide pages):
AndroidManifest.xmlentries.CameraControllervideo recording snippet, because it uses APIs removed from CameraX.androidx.camera.core.impl.utils.Exif.:cameramodule doesn't use View Binding.Documentation CL: http://cl/988523079