VisionCamera

website·Indexed 34 days ago

https://visioncamera.margelo.com/

A high-performance camera library for React Native featuring frame processors, barcode scanning, and advanced camera controls. It provides APIs for managing camera sessions, capturing RAW and HDR photos, recording video, and integrating with Skia for real-time frame rendering. The ecosystem includes specialized packages such as react-native-vision-camera-barcode-scanner, react-native-vision-camera-location, react-native-vision-camera-resizer, and react-native-vision-camera-skia.

Tokens
85.7K
Snippets
543
Records
790
Agent score
62%

What's inside VisionCamera

  1. Overview of Camera Outputs

    A CameraOutput is the base class for all streams a CameraDevice can produce via a CameraSession. VisionCamera provides five core output types:

    • CameraPhotoOutput: For capturing photos.
    • CameraVideoOutput: For recording video.
    • CameraFrameOutput: For real-time frame processing.
    • CameraDepthFrameOutput: For depth data.
    • CameraPreviewOutput: For the camera viewfinder preview.
  2. Use the PhotoFile interface to access captured photo metadata

    The PhotoFile interface represents a photo captured using the capturePhotoToFile(...) method and written to a file. It provides metadata about the resulting file, including its filesystem path, dimensions, and capture properties.
    interface PhotoFile {
      containerFormat: PhotoContainerFormat;
      filePath: string;
      height: number;
      isMirrored: boolean;
      isRawPhoto: boolean;
      orientation: CameraOrientation;
      timestamp: number;
      width: number;
    }
  3. Stream camera frames using CameraFrameOutput

    The CameraFrameOutput interface allows for synchronous streaming of Frame objects from the camera, enabling real-time 'Frame Processing'. It can be initialized using either the Hooks API or the Imperative API.
    // Using Hooks API
    const frameOutput = useFrameOutput({
      pixelFormat: 'yuv',
      onFrame(frame) {
        'worklet'
        frame.dispose()
      }
    })
    
    // Using Imperative API
    const frameOutput = VisionCamera.createFrameOutput({
      targetResolution: CommonResolutions.HD_16_9,
      pixelFormat: 'yuv',
      enablePreviewSizedOutputBuffers: false,
      allowDeferredStart: true,
      enablePhysicalBufferRotation: false,
      enableCameraMatrixDelivery: false,
      dropFramesWhileBusy: true,
    })
  4. Understand and use NativeThread for Worklet Runtimes

    A NativeThread is a JavaScript handle to a native thread implementation—specifically a DispatchQueue on iOS and an Executor on Android. It is primarily used to create Worklet Runtimes that execute on that specific native thread. This is critical for streaming outputs like CameraFrameOutput to run synchronous JS functions (Frame Processors) on the same thread as the output, eliminating thread hops and synchronization overhead.

    Key constraints:

    • Execution is guaranteed to be serial, even if the underlying implementation uses a thread pool.
    • Avoid using static thread_local caches in C++ when working with NativeThread, as the underlying thread pool may reuse threads.
  5. Understand and use the Depth frame object

    A Depth frame is an in-memory buffer allowing GPU and CPU access to individual depth points. It is streamed by a CameraDepthFrameOutput or attached to a Photo (via Photo.depth) if enabled on the CameraPhotoOutput. Depth data may be computed via time-of-flight (true distance) or disparity (triangulation via multiple cameras); disparity-based frames are generally less accurate.
  6. Manage device location updates with LocationManager

    The LocationManager interface allows developers to stream real-time device location updates and access the last known location. It can be instantiated using createLocationManager() or the useLocationManager() hook. For a higher-level React API that automates permissions and subscriptions, use the useLocation() hook.