Index of every page on this site: llms.txt
All skills
android avatar

/camerax

@7f7b217
by androidandroid/skills11k stars
703
Run it yourself

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.

Use this Skill: https://skilld.dev/gh/android/skills/camerax

Nothing lands on disk. Nothing to clean up.

Fork this Skill

Edit a local copy. It keeps the author and licence.

referencesmlkit-spatial.md

≈951 tokens on demand. Your agent reads this file only when SKILL.md points to it.

When you use ML Kit for features such as face mesh, object detection, or pose detection, the most common failure point is the coordinate disparity between the analysis image and the viewfinder UI.

The mapping mindset

Dimension Analysis frame Viewfinder UI on the screen
Resolution Fixed, such as 640x480 Dynamic, such as 1080x2400
Rotation 0° for the raw buffer 90° or 270° in portrait or landscape mode
Origin Top-left of buffer at (0,0) Top-left of screen at (0,0)

Follow the implementation guide

Coordinate transformation matrix

Android provides the Viewport and UseCaseGroup APIs to calculate the transformation matrix automatically. Don't calculate aspect ratio scaling manually.

fun startBarcodeScanning(
    context: Context,
    lifecycleOwner: LifecycleOwner,
    previewView: PreviewView,
    onBarcodes: (List<Barcode>) -> Unit,
) {
    val scanner = BarcodeScanning.getClient()
    val executor = ContextCompat.getMainExecutor(context)
    val controller = LifecycleCameraController(context)

    // COORDINATE_SYSTEM_VIEW_REFERENCED: MlKitAnalyzer maps every result to
    // PreviewView coordinates, including rotation, crop, and mirroring.
    controller.setImageAnalysisAnalyzer(
        executor,
        MlKitAnalyzer(
            listOf(scanner),
            ImageAnalysis.COORDINATE_SYSTEM_VIEW_REFERENCED,
            executor,
        ) { result ->
            onBarcodes(result.getValue(scanner).orEmpty())
        },
    )
    controller.bindToLifecycle(lifecycleOwner)
    previewView.controller = controller
}
<br />

Handling the "double rotation" bug

ML Kit results, bounding boxes, are relative to the rotated buffer. If the device is in portrait, the buffer is often 480x640, landscape, but the screen is 1080x1920.

To map the coordinates, use the following workflow:

  1. Query imageProxy.imageInfo.rotationDegrees.
  2. Pass this rotation to the ML Kit InputImage.
  3. Use the MappingUtils.transformRect method to map the result Rect to the screen.

Face mesh and pose normalization

For high-precision spatial analysis, for example, "Is the user's hand at a specific screen button?", use normalized coordinates from 0.0 to 1.0.

/** Converts a landmark position in the analysis frame to screen pixels. */
fun toScreenPoint(
    landmarkX: Float,
    landmarkY: Float,
    analysisWidth: Int,
    analysisHeight: Int,
    screenWidth: Int,
    screenHeight: Int,
): Pair<Float, Float> {
    // Normalize to 0.0-1.0 first, then scale to the screen.
    val normalizedX = landmarkX / analysisWidth
    val normalizedY = landmarkY / analysisHeight
    return normalizedX * screenWidth to normalizedY * screenHeight
}
<br />

Warning : Always account for mirrored lenses . If the LENS_FACING_FRONT is used, you must flip the X-coordinate: actualX = screenWidth - screenX.

Overlays and canvas clipping

Use a custom GraphicOverlay view on top of the PreviewView.

  • Buffer lock : Ensure your GraphicOverlay clears its canvas every time a new ImageAnalysis frame is processed to prevent "ghosting" of bounding boxes.

Spatial pitfalls

  • The "stretched box" bug : Caused by assuming the Analysis Frame aspect ratio, 4:3, matches the screen aspect ratio, 21:9. Use PreviewView.SCALE_TYPE_FILL_CENTER and map coordinates accordingly.
  • Latency : If ML processing exceeds 50 ms, the bounding box trails behind the user's face.
    • Fix : Use ImageAnalysis.STRATEGY_KEEP_ONLY_LATEST to avoid queuing stale frames.
  • Sensor vs. display rotation : On some tablets, the sensor is mounted horizontally. Always query display.rotation and cameraInfo.sensorRotationDegrees.

Source: SKILL.md on GitHub

No rule matched.

skilld matched fixed text patterns in SKILL.md and file names. Patterns miss obfuscated code.

skilld run checks every file with the same patterns. It asks for approval before it loads a Skill with a behavior marked Needs approval.

No alerts6d3 checks · Risk SAFE
  • Gen Agent Trust Hub7d

    The skill provides technical guidance for Android CameraX development, covering migration from legacy APIs, handling device diversity (foldables, Wear OS, XR), and integrating with ML Kit and Media3. No security issues were detected; all code snippets and external references originate from trusted sources and follow standard platform patterns.

  • Socket6d

    No alerts

  • Snyk7d

    Risk: LOW · No issues

Signed by skilld at 7f7b217. This ties the file your Agent reads to that commit on GitHub. It does not review the instructions.

Last checked against GitHub 1 hour ago.

Activeupdated last week
Other metadata
metadata
{
  "author": "Google LLC",
  "last-updated": "2026-11-10",
  "keywords": [
    "recipe",
    "Android",
    "Camera",
    "Camera1",
    "Camera2",
    "CameraX",
    "migration",
    "Compose",
    "guide",
    "dependencies",
    "PreviewView",
    "CameraXViewfinder",
    "ImageCapture",
    "VideoCapture",
    "ImageAnalysis."
  ]
}

README badge

README badge for android/skills/camerax