Camerax

android/skills/camera/camerax

作者 android42dc2270e960Complete terms in LICENSE.txt7.7K 個星標收錄於 2026年10月8日更新於 2026年10月8日儲存庫13 天前更新

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.

AI 產生的概覽

提供使用 CameraX 建置 Android 相機應用程式的技術指引與模式,涵蓋移轉和 Media3 整合。

功能
此技能提供以 CameraX 為核心的 Android 相機開發技術指引與標準模式,包含 Camera2Interop 工具與 Media3 整合。內容涵蓋不可變建構器模式、從 Camera1 與 Camera2 移轉、手動控制與 RAW 拍攝等功能藍圖,以及低光源、摺疊式裝置、XR、散熱、測試、ML Kit 空間分析和 Wear OS 遙控。另列出 API 探索建議、程式碼品質規則、硬體多樣性考量與常見陷阱。產出為書面技術指引與參考資料,而非程式碼。
適用情境
適用於實作 Android 相機功能、處理非同步錄製生命週期、使用 CameraX 進行低階硬體互通,或整合 ML Kit 與 Media3 特效時。也適合將舊版 Camera1 或 Camera2 程式碼移轉至 CameraX 的團隊。
執行需求
無需指令碼或執行階段相依性,僅為說明文件與參考資料。除讀取隨附的參考檔案外,代理不需要憑證或網路存取。

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

kotlin
// WRONGrun {  val pending = recorder.prepareRecording(context, opts)  pending.withAudioEnabled() // This returns a new instance which is ignored  val active = pending.start(exec, listener)}
// CORRECTrun {  val pending = recorder.prepareRecording(context, opts)      .withAudioEnabled() // Chaining works  val active = pending.start(exec, listener)}
// ALSO CORRECTrun {  var pending = recorder.prepareRecording(context, opts)  pending = pending.withAudioEnabled() // Reassignment  val active = pending.start(exec, listener)}
<br />

See immutability [blocked] 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.Camera implementations, surface handling, and manual lifecycles, see the Camera1 migration guide [blocked].
  • Camera2 to CameraX : For migrating more recent but verbose android.hardware.camera2 implementations, session state callbacks, and interop patterns, see the Camera2 migration guide [blocked].

Comprehensive feature blueprinting

For multi-step features that involve multiple files and hardware-level wiring, follow the Structural Blueprinting [blocked] approach to avoid system timeouts. Such complex features include:

  • Manual controls : Break down into the ViewModel state, the controller layer, and the Camera2Interop wiring in the session.
  • RAW capture: Separate JPEG and RAW output configurations into discrete build steps.
  • Custom effects : Prefer Media3Effect or SurfaceProcessor over manual OpenGL pipelines unless absolute performance is required.
  • Low-light : See low-light [blocked] for Night Mode and LLB guidance.
  • Foldables : See foldables [blocked] for handling dynamic postures and hinge states.
  • XR, AR, and VR : See xr [blocked] for spatial tracking, passthrough synchronization, and latency guardrails.
  • Thermals and power : See thermals [blocked] for managing StreamUseCase optimizations and PowerManager thermal states.
  • Testing and mocking : See testing [blocked] for using FakeCameraConfig, handling asynchronous lifecycles, and validating analysis pipelines.
  • ML Kit spatial analysis : See mlkit-spatial [blocked] for coordinate mapping, rotation logic, and mirrored lens handling.
  • Wear OS camera remote : See wear-os [blocked] for circular UI constraints, Data Layer API syncing, and remote trigger logic.

See expert-blueprints [blocked] for step-by-step guides.

API discovery

Always use higher-level abstractions instead of low-level manual wiring:

  • Analysis : Use MlKitAnalyzer instead of manual ImageAnalysis.Analyzer.
  • Filters and effects : Use Media3Effect for standard post-processing.
  • Multi-camera : Use ConcurrentCamera APIs for dual-stream setups.

See modern-apis [blocked] 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 like ImageProxy. Build "Fakes" to verify state rather than unreliable implementation details.
  • Google Truth assertions : Use assertThat over standard JUnit assertions like assertEquals for improved readability.
  • Explicit test runners : Always define an explicit @RunWith for 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 Icon and Text, use semantics { 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 isRecording state before attempting to stop or pause. Handle VideoRecordEvent.Start for 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 CAMERA permission; check for RECORD_AUDIO specifically when enabling audio in VideoCapture.

來源與署名

來源:android/skills位於camera/camerax提交42dc227

授權條款: Complete terms in LICENSE.txt

內容歸原作者所有。SourceWeft 從公開儲存庫中收錄這些內容。

檢舉或申請下架

更多來自 android/skills 的技能

Wear Compose M3

android

指導建置、更新與移轉 Wear OS Compose Material3 應用程式,並提供標準元件範例與版本規則。

Software Development7.7K13 天前更新

Display Glasses With Jetpack Compose Glimmer

android

Provides guidelines for developing projected Android XR apps for display glasses using the Jetpack Compose Glimmer UI toolkit. This skill covers foundational Glimmer design principles, workflows for implementing Jetpack Compose Glimmer, and interaction models for the glasses form factor. Use this skill to build an Android XR Augmented Experience app with Jetpack Compose Glimmer that adheres to the Glimmer design system for optimized glasses styling.

待分類7.7K13 天前更新

Play Billing Library Version Upgrade

android

引導將 Android 專案從舊版 Google Play 帳務程式庫升級到最新穩定版本。

Software Development7.7K13 天前更新

Leanback To Compose Tv Migration

android

Provides instructions and architectural patterns for migrating Android TV applications from legacy Leanback UI Toolkit, Android Views, or Support Fragments to Jetpack Compose for TV (androidx.tv). Use this skill for Leanback to Compose migrations, including browse screen, settings screen, authentication screen, login screen, or video playback screen migrations, or when replacing BrowseSupportFragment, LeanbackSettingsFragment, PreferenceFragment, BaseLeanbackPreferenceFragmentCompat, VideoSupportFragment, GuidedStepSupportFragment, SearchSupportFragment, VerticalGridSupportFragment, Presenter, ArrayObjectAdapter, or CursorMapper with modern Compose equivalents, implementing immersive carousels with focus memory, Media3 video playback with PlayerSurface, or custom 10-foot hero layouts.

待分類7.7K13 天前更新

Testing Setup

android

分析 Android 專案的測試設定,並安裝測試框架、測試基礎設施,以及單元、UI、截圖與端對端測試。

Software Development7.7K13 天前更新

Android Profiler

android

引導 Android 效能分析:錄製系統追蹤、堆積轉儲與方法記錄,並以 Perfetto SQL 進行分析。

Software Development7.7K13 天前更新