KMP Vision Camera

License Maven Central Version Issues Pull Requests codecov CodeFactor

This project is a Kotlin Multiplatform port of React Native VisionCamera (V5) by Marc Rousavy / Margelo. It keeps the same concepts and the same API, with Kotlin coroutines instead of Promises and Compose instead of React: if you know VisionCamera, you already know KMP Vision Camera. It is not affiliated with, nor endorsed by, Margelo or the VisionCamera authors.

KMP Vision Camera is a high-performance camera library for Kotlin Multiplatform (Android and iOS). It gives access to the full power of the native camera APIs (CameraX and Camera2 on Android, AVFoundation on iOS) through a single Kotlin API, from a simple preview to advanced session configuration, photo, video and real-time frame processing.

Features

  • VisionCamera’s API in Kotlin – VisionCamera, CameraDevice, CameraSession, outputs, constraints, CameraController… the same names and concepts, so the VisionCamera documentation and knowledge apply.
  • Native engines – CameraX (with Camera2 interop) on Android, AVFoundation on iOS, with all devices and their capabilities exposed: physical lenses, formats, FPS ranges, HDR, zoom, torch, exposure…
  • Constraints – describe what you want (60 FPS, HDR, stabilization, resolution priority…), and the session negotiates the closest configuration the device supports.
  • Frame processing – a real-time Frame stream on a dedicated thread, with planes, CPU pixel buffers and zero-copy native buffers (CVPixelBufferRef on iOS, AHardwareBuffer* on Android), plus depth frames, a frame renderer and a frame converter.
  • Barcode scanning – the barcode module ports VisionCamera’s barcode scanner, with the same API on both platforms: ML Kit on Android, Apple Vision on iOS (nothing to add to your app).
  • Location – the location module ports VisionCamera’s location: a LocationManager (Google Play Services on Android, CoreLocation on iOS) and location tags in photos and videos.
  • Frame resizer – the resizer module resizes and converts frames to RGB tensors for ML models on the GPU, with Metal on iOS and Vulkan on Android.
  • Coroutines first – every asynchronous operation is a suspend function.
  • Compose optional – the engine has no Compose dependency and works with Android Views, UIKit or SwiftUI; the compose module adds Camera, NativePreviewView and remember* helpers.

Status

Early development. What works today:

  • Permissions (camera and microphone)
  • Camera devices: listing, default devices, capabilities, formats, physical devices
  • Camera sessions: configuration with connections and constraints, start/stop, lifecycle, interruptions
  • Camera controller: zoom, torch, exposure bias, low-light boost, focus and metering, manual controls
  • Preview output and preview views (Android View, iOS UIView, Compose), tap-to-focus and pinch-to-zoom
  • Photo output
  • Video output
  • Photo preview image and preview snapshots
  • Frame output (frame processing) with native buffers, frame renderer and frame converter
  • Depth output, object output (iOS) and output synchronizer (iOS)
  • Automatic output orientation (device or interface), subject area changes
  • Barcode scanner (barcode, barcode-compose): frames, images, camera output and CodeScanner
  • Location (location, location-compose): LocationManager, rememberLocation, location tags in photos and videos
  • Resizer (resizer, resizer-compose): GPU resize and RGB conversion of frames for ML models (Metal, Vulkan)
  • Skia

Modules

Module Description
core The camera engine: devices, sessions, controller, preview, photo, video, frame, depth and object outputs
compose Compose Multiplatform integration: Camera, NativePreviewView, NativeFrameRendererView, remember*
barcode Barcode scanning on frames, images or as a camera output
barcode-compose Compose integration of barcode: CodeScanner, rememberBarcodeScanner, rememberBarcodeScannerOutput
location Device location (LocationManager) to embed in photos and videos
location-compose Compose integration of location: rememberLocation, rememberLocationManager
resizer GPU frame resizer and RGB converter for ML models, with Metal and Vulkan
resizer-compose Compose integration of resizer: rememberResizer

Installation

Add the dependencies to your build.gradle.kts, in the commonMain source set:

dependencies {
    implementation("me.nathanfallet.visioncamera:core:0.1.0")
    // Optional, for Compose Multiplatform:
    implementation("me.nathanfallet.visioncamera:compose:0.1.0")
    // Optional, for barcode scanning:
    implementation("me.nathanfallet.visioncamera:barcode:0.1.0")
    // Optional, for barcode scanning with Compose Multiplatform:
    implementation("me.nathanfallet.visioncamera:barcode-compose:0.1.0")
    // Optional, for location tags in photos and videos (and its Compose helpers):
    implementation("me.nathanfallet.visioncamera:location:0.1.0")
    implementation("me.nathanfallet.visioncamera:location-compose:0.1.0")
    // Optional, for resizing frames on the GPU (and its Compose helper):
    implementation("me.nathanfallet.visioncamera:resizer:0.1.0")
    implementation("me.nathanfallet.visioncamera:resizer-compose:0.1.0")
}

On Android, declare the permissions you need in your AndroidManifest.xml:

<uses-permission android:name="android.permission.CAMERA" />
<!-- Only if you record audio -->
<uses-permission android:name="android.permission.RECORD_AUDIO" />

On iOS, add NSCameraUsageDescription (and NSMicrophoneUsageDescription if you record audio) to your Info.plist. For location tags, add ACCESS_FINE_LOCATION and ACCESS_COARSE_LOCATION on Android, and NSLocationWhenInUseUsageDescription on iOS.

Usage

Compose

@Composable
fun CameraScreen() {
    val permission = rememberCameraPermission()
    LaunchedEffect(permission.hasPermission) {
        if (!permission.hasPermission && permission.canRequestPermission) permission.requestPermission()
    }
    if (!permission.hasPermission) return

    Camera(
        device = TargetCameraPosition.BACK,
        isActive = true,
        modifier = Modifier.fillMaxSize(),
    )
}

Or, with more control over the device and the outputs:

val device = rememberCameraDevice(TargetCameraPosition.BACK)
val previewOutput = rememberPreviewOutput()
val controller = rememberCamera(
    isActive = true,
    device = device ?: TargetCameraPosition.BACK,
    outputs = listOf(previewOutput),
    constraints = listOf(FPSConstraint(60.0)),
)
NativePreviewView(previewOutput, Modifier.fillMaxSize())

Frame processing

val frameOutput = rememberFrameOutput(FrameOutputOptions(pixelFormat = TargetVideoPixelFormat.YUV)) { frame ->
    frame.use {
        // Runs on the output's thread, for every frame
        println("${it.width}x${it.height}, ${it.getPlanes().size} planes")
    }
}
Camera(device = TargetCameraPosition.BACK, isActive = true, outputs = listOf(frameOutput))

Barcode scanning

CodeScanner(
    isActive = true,
    barcodeFormats = listOf(TargetBarcodeFormat.QR_CODE, TargetBarcodeFormat.EAN_13),
    onBarcodeScanned = { barcodes -> barcodes.forEach { println(it.rawValue) } },
    onError = { println(it) },
    modifier = Modifier.fillMaxSize(),
)

Or, as an output of your own Camera (or with rememberBarcodeScanner in a frame processor):

val barcodeOutput = rememberBarcodeScannerOutput(
    barcodeFormats = listOf(TargetBarcodeFormat.ALL_FORMATS),
    onBarcodeScanned = { barcodes -> barcodes.forEach { println("${it.format}: ${it.rawValue}") } },
    onError = { println(it) },
)
Camera(device = TargetCameraPosition.BACK, isActive = true, outputs = listOf(barcodeOutput))

Location

val location = rememberLocation()
LaunchedEffect(location.hasPermission) {
    if (!location.hasPermission) location.requestPermission()
}

// Embed it in photos and videos
val photo = photoOutput.capturePhoto(CapturePhotoSettings(location = location.currentLocation))

Resizing frames for ML models

val resizer = rememberResizer(
    ResizerOptions(
        width = 192,
        height = 192,
        channelOrder = ChannelOrder.RGB,
        dataType = DataType.FLOAT32,
        scaleMode = ScaleMode.COVER,
        pixelLayout = PixelLayout.PLANAR,
    ),
).resizer
val frameOutput = rememberFrameOutput(FrameOutputOptions(pixelFormat = TargetVideoPixelFormat.YUV)) { frame ->
    frame.use {
        resizer?.resize(it)?.use { resized ->
            val pixels = resized.getPixelBuffer() // 192x192 RGB float32, planar
        }
    }
}

Imperative API

The engine has no Compose dependency, and can be used from any Kotlin code (Android Views, UIKit, SwiftUI…):

if (VisionCamera.cameraPermissionStatus != PermissionStatus.AUTHORIZED) {
    VisionCamera.requestCameraPermission()
}

val deviceFactory = VisionCamera.createDeviceFactory()
val device = getCameraDevice(deviceFactory, TargetCameraPosition.BACK)!!
val previewOutput = VisionCamera.createPreviewOutput()

val session = VisionCamera.createCameraSession(enableMultiCam = false)
val (controller) = session.configure(
    listOf(
        CameraSessionConnection(
            input = device,
            outputs = listOf(CameraOutputConfiguration(previewOutput)),
            constraints = listOf(FPSConstraint(60.0)),
        ),
    ),
)
session.start()
controller.setZoom(2.0)

// Display it in a PreviewView (an Android View, or a UIView on iOS)
previewView.previewOutput = previewOutput

Documentation

Guides are available at visioncamera.nathanfallet.me. Since the API mirrors VisionCamera’s, the VisionCamera documentation is also a great resource.

Sample app

The sample module contains a demo app for Android (sample-android) and iOS (sample-ios): preview with tap-to-focus and pinch-to-zoom, photo, video recording, live frame statistics, and QR code scanning on iOS.

Credits

KMP Vision Camera is a port of React Native VisionCamera, licensed under the MIT license, Copyright (c) 2025 Marc Rousavy. Its architecture, API design and native implementation are the foundation of this project. Its license notice is reproduced in NOTICE.

License

KMP Vision Camera is licensed under the Apache License 2.0.


This site uses Just the Docs, a documentation theme for Jekyll.