camerax
Provide technical guidance for Android camera development with CameraX. Use when implementing camera features, handling asynchronous recording lifecycles, wiring low-level hardware interop using CameraX, or integrating ML Kit or Media3 effects.
- License 许可
- Apache-2.0
- 最近更新
- 2026-08-21
- Stars
- ★ 0
- Forks
- 0
01 前言 Front Matter
- name
- camerax
- license
- Complete terms in LICENSE.txt
- metadata
- { "author": "Google LLC", "version": "1.0.0", "keywords": [ "recipe", "Android", "Camera", "Camera1", "Camera2", "CameraX", "migration", "Compose", "guide", "dependencies", "PreviewView", "CameraXViewfinder", "ImageCapture", "VideoCapture", "ImageAnalysis." ], "last-updated": "2026-08-06" }
- description
- Provide technical guidance for Android camera development with CameraX. Use when implementing camera features, handling asynchronous recording lifecycles, wiring low-level hardware interop using CameraX, or integrating ML Kit or Media3 effects.
02 资源文件 14
- 许可证LICENSE.upstream11.1 KB
- 参考文档references/camera1-to-camerax.md5.5 KB
- 参考文档references/camera2-to-camerax.md5.6 KB
- 参考文档references/expert-blueprints.md2.1 KB
- 参考文档references/foldables.md3.0 KB
- 参考文档references/immutability.md1.7 KB
- 参考文档references/low-light.md4.6 KB
- 参考文档references/mlkit-spatial.md2.8 KB
- 参考文档references/modern-apis.md2.0 KB
- 参考文档references/testing.md2.6 KB
- 参考文档references/thermals.md3.1 KB
- 参考文档references/wear-os.md2.7 KB
- 参考文档references/xr.md2.4 KB
- 参考文档SOURCE.yaml702 B
03 SKILL.md 正文
This skill provides procedural guidance and standard patterns for building camera applications on Android, with a focus on CameraX, including its Camera2Interop utilities, and Media3 integrations.
Core workflows
Handling immutable API patterns
Various Android camera and media APIs, especially CameraX VideoCapture, use a fluent, immutable builder-like pattern where methods return a new instance. Failing to reassign these results in settings, such as audio, being ignored.
Pattern: Reassignment is required
<br />
// WRONG
run {
val pending = recorder.prepareRecording(context, opts)
pending.withAudioEnabled() // This returns a new instance which is ignored
val active = pending.start(exec, listener)
}
// CORRECT
run {
val pending = recorder.prepareRecording(context, opts)
.withAudioEnabled() // Chaining works
val active = pending.start(exec, listener)
}
// ALSO CORRECT
run {
var pending = recorder.prepareRecording(context, opts)
pending = pending.withAudioEnabled() // Reassignment
val active = pending.start(exec, listener)
}
<br />
See immutability for a list of affected classes.
Migrating to CameraX
When migrating legacy camera codebases to the CameraX Jetpack library:
- Camera1 to CameraX : For migrating legacy
android.hardware.Cameraimplementations, surface handling, and manual lifecycles, see the Camera1 migration guide. - Camera2 to CameraX : For migrating more recent but verbose
android.hardware.camera2implementations, session state callbacks, and interop patterns, see the Camera2 migration guide.
Comprehensive feature blueprinting
For multi-step features that involve multiple files and hardware-level wiring, follow the Structural Blueprinting approach to avoid system timeouts. Such complex features include:
- Manual controls : Break down into the
ViewModelstate, the controller layer, and theCamera2Interopwiring in the session. - RAW capture: Separate JPEG and RAW output configurations into discrete build steps.
- Custom effects : Prefer
Media3EffectorSurfaceProcessorover manual OpenGL pipelines unless absolute performance is required. - Low-light : See low-light for Night Mode and LLB guidance.
- Foldables : See foldables for handling dynamic postures and hinge states.
- XR, AR, and VR : See xr for spatial tracking, passthrough synchronization, and latency guardrails.
- Thermals and power : See thermals for managing
StreamUseCaseoptimizations andPowerManagerthermal states. - Testing and mocking : See testing for using
FakeCameraConfig, handling asynchronous lifecycles, and validating analysis pipelines. - ML Kit spatial analysis : See mlkit-spatial for coordinate mapping, rotation logic, and mirrored lens handling.
- Wear OS camera remote : See wear-os for circular UI constraints, Data Layer API syncing, and remote trigger logic.
See expert-blueprints for step-by-step guides.
API discovery
Always use higher-level abstractions instead of low-level manual wiring:
- Analysis : Use
MlKitAnalyzerinstead of manualImageAnalysis.Analyzer. - Filters and effects : Use
Media3Effectfor standard post-processing. - Multi-camera : Use
ConcurrentCameraAPIs for dual-stream setups.
See modern-apis for current recommendations.
Code quality and architectural rules
Adhere to the following Android ecosystem standard patterns when building your camera implementations:
- Testing, fakes over mocks : Avoid mocking libraries like
Mockito, especially for multi-step CameraX interfaces likeImageProxy. Build "Fakes" to verify state rather than unreliable implementation details. - Google Truth assertions : Use
assertThatover standardJUnitassertions likeassertEqualsfor improved readability. - Explicit test runners : Always define an explicit
@RunWithfor test classes to ensure the CI environment executes them correctly. - Semantic UI merging : When building custom camera controls in Compose, such as a button with an
IconandText, usesemantics { mergeDescendants = true }to ensure screen readers announce them as a single, coherent unit.
Hardware and device diversity
Camera apps run on a wide variety of hardware, from mobile phones and foldables to tablets, laptops, and even smart appliances. Have consideration for the specific hardware the app is running on.
- Form factors: Account for screen size and orientation changes on foldables and tablets.
- Multi-camera arrays: Some devices have a rear-facing camera and a front-facing camera. Other devices have multiple rear-facing cameras, such as wide-angle and telephoto lenses.
- Feature parity: Features like flash or auto-focus behave differently across hardware. For example, CameraX handles both physical flash, back, and screen-based flash, front, and both must be considered when implementing flash functionality.
Common pitfalls
- Asynchronous lifecycles : Check
isRecordingstate before attempting to stop or pause. HandleVideoRecordEvent.Startfor UI state updates, not just the initial call. - Thread safety: Camera callbacks often run on background executors. Dispatch UI updates on the main thread.
- Permission handling : Check
CAMERApermission; check forRECORD_AUDIOspecifically when enabling audio inVideoCapture.
04 同仓库更多 Skills
vitest
Vitest fast unit testing framework powered by Vite with Jest-compatible API. Use when writing tests, mocking, configuring coverage, or working with test filtering and fixtures.
test-driven-development
Use when implementing any feature or bugfix, before writing implementation code
writing-skills
Use when creating new skills, editing existing skills, or verifying skills work before deployment
slides
Create strategic HTML presentations with Chart.js, design tokens, responsive layouts, copywriting formulas, and contextual slide strategies.
shadcn
Manages shadcn components and projects — adding, searching, fixing, debugging, styling, and composing UI, including chat interfaces. Provides project context, component docs, and usage examples. Applies when working with shadcn/ui, component registries, presets, --preset codes, or any project with a components.json file. Also triggers for "shadcn init", "create an app with --preset", or "switch to --preset".
qmd
Search local markdown knowledge bases, notes, docs, and wikis with QMD. Use when users ask to find notes, retrieve documents, inspect a wiki, answer from indexed markdown, or set up QMD access.