camera1-to-camerax

Migrates legacy Android Camera1 and Camera2 implementations to lifecycle-aware CameraX.

3|Updated Aug 4, 2024
One-click install
npx skills add https://github.com/kabindra-shrestha/Clean-Architecture-Kotlin-Compose-Multiplatform --skill camera1-to-camerax-kabindra-shrestha
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: camera1-to-camerax
Source: https://github.com/kabindra-shrestha/Clean-Architecture-Kotlin-Compose-Multiplatform/tree/main/.agents/skills/camera/camera1-to-camerax
Command: npx skills add https://github.com/kabindra-shrestha/Clean-Architecture-Kotlin-Compose-Multiplatform --skill camera1-to-camerax-kabindra-shrestha

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve? Legacy Android camera code built on Camera1 or raw Camera2 APIs is fragile, requiring manual lifecycle handling, rotation matrix math, and device-specific workarounds. This Skill guides the migration to CameraX, the Jetpack library that handles lifecycle, rotation, and device compatibility automatically. ## Core Features & Use Cases - Dependency Setup: Adds the correct CameraX artifacts (core, camera2, lifecycle, view, compose) via Version Catalog or plain Gradle. - Legacy Code Removal: Identifies and removes android.hardware.Camera instances, SurfaceHolder callbacks, and manual lifecycle logic. - Modern Implementation: Provides complete Kotlin code for ProcessCameraProvider binding, preview (PreviewView or CameraXViewfinder), tap-to-focus, photo capture with rotation handling, and camera switching. - Use Case: An Android app with a legacy Camera1 photo capture feature needs modernization. Follow the steps to replace it with CameraX, gaining correct rotation handling and Compose support without manual matrix math. ## Quick Start Migrate my Android app's legacy Camera1 implementation to CameraX with a Compose-based preview and tap-to-focus.

Frequently Asked Questions about camera1-to-camerax

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I migrate from Camera1 to CameraX in Android?

Remove all android.hardware.Camera instances, SurfaceHolder callbacks, and manual lifecycle handling, then add CameraX dependencies and bind Preview and ImageCapture use cases to a LifecycleOwner via ProcessCameraProvider. CameraX handles rotation and lifecycle automatically.

How to show a CameraX preview in Jetpack Compose?

Use androidx.camera.compose.CameraXViewfinder with a SurfaceRequest state from the Preview use case's surface provider. Avoid wrapping PreviewView in AndroidView, which causes resizing issues; CameraXViewfinder requires CameraX 1.5.0 or higher.

What CameraX version is needed for Compose support?

CameraX 1.5.0 or higher provides the camera-compose artifact with CameraXViewfinder for Jetpack Compose. Version 1.3.0 or higher is the minimum for general interoperability with the core, camera2, lifecycle, and view artifacts.

Why does my CameraX capture pipeline lock up after taking a photo?

The capture pipeline locks when the ImageProxy is not closed in the OnImageCapturedCallback. Always call image.close() after reading the buffer, or subsequent photo captures will be blocked.

How do I implement tap-to-focus with CameraX?

Create a metering point using MeteringPointFactory (or SurfaceOrientedMeteringPointFactory in Compose) from the touch coordinates, build a FocusMeteringAction with FLAG_AF, and pass it to cameraControl.startFocusAndMetering. Do not compute focus matrices manually.