### Install VisionCamera with bun Source: https://visioncamera.margelo.com/docs Install the react-native-vision-camera library using bun. ```bash bun add react-native-vision-camera ``` -------------------------------- ### Install VisionCamera with yarn Source: https://visioncamera.margelo.com/docs Install the react-native-vision-camera library using yarn. ```bash yarn add react-native-vision-camera ``` -------------------------------- ### Install VisionCamera with npm Source: https://visioncamera.margelo.com/docs Install the react-native-vision-camera library using npm. ```bash npm install react-native-vision-camera ``` -------------------------------- ### Install VisionCamera with pnpm Source: https://visioncamera.margelo.com/docs Install the react-native-vision-camera library using pnpm. ```bash pnpm add react-native-vision-camera ``` -------------------------------- ### Start Video Recording Source: https://visioncamera.margelo.com/docs/video-output This snippet demonstrates how to start a video recording using the `Recorder.startRecording` method. It includes callbacks for when the recording finishes or encounters an error. ```javascript await recorder.startRecording( (path) => console.log(`Recording finished!`), (error) => console.error(`Recording error!`, error) ) ``` -------------------------------- ### Imperative Camera Session Configuration Source: https://visioncamera.margelo.com/docs/devices Create and configure a CameraSession imperatively. This involves getting a default device, configuring its inputs and outputs, and starting the session. ```javascript const session = await VisionCamera.createCameraSession(false) const device = await getDefaultCameraDevice('back') await session.configure([ { input: device, outputs: [], constraints: [] } ], {}) await session.start() ``` -------------------------------- ### Imperative CameraSession Creation and Configuration Source: https://visioncamera.margelo.com/docs/camera-session Demonstrates how to manually create a CameraSession, configure its inputs and outputs, and start it. ```APIDOC ## Creating a CameraSession (Imperative) ### Description Manually create a `CameraSession` instance, configure its input and output connections, and then start the session. ### Method `VisionCamera.createCameraSession(isMultiCam: boolean): Promise ` ### Parameters #### Path Parameters - **isMultiCam** (boolean) - Required - Whether to create a multi-camera session. ### Endpoint `VisionCamera.createCameraSession` ### Request Example ```javascript const device = ... // Obtain a camera device const isMultiCam = false const session = await VisionCamera.createCameraSession(isMultiCam) await session.configure([ { input: device, outputs: [], constraints: [] } ], {{}}) await session.start() ``` ### Response #### Success Response (200) - **session** (CameraSession) - The created CameraSession instance. ### Related Methods - `session.configure(connections: Connection[], options: CameraSessionConfiguration): Promise` - `session.start(): Promise` ``` -------------------------------- ### Install VisionCamera Dependencies with bun Source: https://visioncamera.margelo.com/docs Install react-native-nitro-modules and react-native-nitro-image, which VisionCamera depends on, using bun. ```bash bun add react-native-nitro-modules react-native-nitro-image ``` -------------------------------- ### Handle Camera Output Readiness Imperatively Source: https://visioncamera.margelo.com/docs/camera-outputs Execute code after starting the camera session to ensure the output is configured and ready for use. ```javascript const session = await VisionCamera.createCameraSession(false) const device = await getDefaultCameraDevice('back') const output = ... await session.configure([ { input: device, outputs: [ { output: output, mirrorMode: 'auto' } ], constraints: [] } ], {}) await session.start() output.doSomething() ``` -------------------------------- ### Get and Monitor External Camera Device (Imperative) Source: https://visioncamera.margelo.com/docs/devices Get an external camera device imperatively and set up a listener to monitor for changes (plugged in/out). This allows dynamically updating the selected device. ```javascript const devices = getAllCameraDevices() let device = getCameraDevice(devices, "external") addOnCameraDevicesChangedListener((d) => { device = getCameraDevice(d, "external") }) ``` -------------------------------- ### Install VisionCamera Dependencies with npm Source: https://visioncamera.margelo.com/docs Install react-native-nitro-modules and react-native-nitro-image, which VisionCamera depends on, using npm. ```bash npm install react-native-nitro-modules react-native-nitro-image ``` -------------------------------- ### Start Recording with Callbacks Source: https://visioncamera.margelo.com/docs/recorder Initiate a video recording and provide callbacks for completion, errors, pausing, and resuming. The path and reason for finishing are passed to the completion callback. ```javascript const recorder = ... await recorder.startRecording( (path, reason) => console.log(`Recording finished! ${reason}`), (error) => console.error(`Recording error!`, error), () => console.log(`Recording paused!`), () => console.log(`Recording resumed!`) ) ``` -------------------------------- ### Install VisionCamera Dependencies with pnpm Source: https://visioncamera.margelo.com/docs Install react-native-nitro-modules and react-native-nitro-image, which VisionCamera depends on, using pnpm. ```bash pnpm add react-native-nitro-modules react-native-nitro-image ``` -------------------------------- ### Basic Camera View Setup Source: https://visioncamera.margelo.com/docs/camera-view Renders a basic camera view using the back camera device. Ensure you have a CameraDevice available. ```javascript function App() { const device = useCameraDevice('back') return ( ) } ``` -------------------------------- ### Configure Multi-Camera Session with Inputs and Outputs Source: https://visioncamera.margelo.com/docs/multi-camera Set up a multi-camera session by configuring inputs (camera devices) and their corresponding outputs (previews, photos). This example shows how to set up both front and back cameras with different output configurations. ```javascript const frontPreviewOutput = VisionCamera.createPreviewOutput() const frontPhotoOutput = VisionCamera.createPhotoOutput({}) const backPreviewOutput = VisionCamera.createPreviewOutput() const backPhotoOutput = VisionCamera.createPhotoOutput({}) const [frontController, backController] = await session.configure([ // Front Camera { input: frontDevice, outputs: [ { output: frontPreviewOutput, mirrorMode: 'on' }, { output: frontPhotoOutput, mirrorMode: 'off' }, ], constraints: [] }, // Back Camera { input: backDevice, outputs: [ { output: backPreviewOutput, mirrorMode: 'off' }, { output: backPhotoOutput, mirrorMode: 'off' }, ], constraints: [] } ]) await session.start() ``` -------------------------------- ### Create Camera Session Imperatively Source: https://visioncamera.margelo.com/docs/camera-session Manually create a `CameraSession` using `VisionCamera.createCameraSession`. You must explicitly configure, start, and stop the session. Ensure most configuration is done before starting the session to avoid performance issues. ```javascript const device = ... const isMultiCam = false const session = await VisionCamera.createCameraSession(isMultiCam) await session.configure([ { input: device, outputs: [], constraints: [] } ], {}) await session.start() ``` -------------------------------- ### Install VisionCamera Dependencies with yarn Source: https://visioncamera.margelo.com/docs Install react-native-nitro-modules and react-native-nitro-image, which VisionCamera depends on, using yarn. ```bash yarn add react-native-nitro-modules react-native-nitro-image ``` -------------------------------- ### Create Camera Session and Video Output (Imperative) Source: https://visioncamera.margelo.com/docs/video-output This imperative approach shows how to create a camera session, get a default camera device, and configure a video output. It's useful for more complex or manual camera control scenarios. ```javascript const session = await VisionCamera.createCameraSession(false) const device = await getDefaultCameraDevice('back') const videoOutput = VisionCamera.createVideoOutput({ /* options */ }) await session.configure([ { input: device, outputs: [ { output: videoOutput, mirrorMode: 'auto' } ], constraints: [] } ], {}) await session.start() ``` -------------------------------- ### Create Camera Session with View Source: https://visioncamera.margelo.com/docs/camera-session Use the `` view for a declarative way to create and manage a camera session. Set `isActive` to true to start the session and specify the `device`. ```javascript function App() { return ( ) } ``` -------------------------------- ### Get All Available Camera Devices (Imperative) Source: https://visioncamera.margelo.com/docs/devices Use this imperative function to get all available camera devices on the system. This is useful if you want to filter camera devices yourself. It also sets up a listener for device changes. ```javascript let devices = getAllCameraDevices() addOnCameraDevicesChangedListener((d) => { devices = d }) ``` -------------------------------- ### Get Available Camera Extensions (Imperative) Source: https://visioncamera.margelo.com/docs/camera-extensions Call `getSupportedExtensions` to asynchronously fetch all available camera extensions for a specific camera device. ```javascript const device = ... const extensions = await getSupportedExtensions(device) ``` -------------------------------- ### Create Camera Preview Output Imperatively Source: https://visioncamera.margelo.com/docs/preview-output Imperatively create a `CameraPreviewOutput` using `VisionCamera.createPreviewOutput()` and configure it within a `createCameraSession`. This method is suitable for more advanced or custom setups. ```javascript const session = await VisionCamera.createCameraSession(false) const device = await getDefaultCameraDevice('back') const previewOutput = VisionCamera.createPreviewOutput() await session.configure([ { input: device, outputs: [ { output: previewOutput, mirrorMode: 'auto' } ], constraints: [] } ], {}) await session.start() ``` -------------------------------- ### Set Camera Constraints with FPS Priority (Imperative) Source: https://visioncamera.margelo.com/docs/constraints Configure camera session constraints imperatively using `createCameraSession`. This example prioritizes FPS. ```javascript const session = await VisionCamera.createCameraSession(false) const videoOutput = createVideoOutput({ targetResolution: CommonResolutions.UHD_16_9 }) const device = await getDefaultCameraDevice('back') await session.configure([ { input: device, outputs: [videoOutput], constraints: [ { fps: 60 }, { resolutionBias: videoOutput } ] } ], {}) await session.start() ``` -------------------------------- ### Start Focus Metering Action with Controller Source: https://visioncamera.margelo.com/docs/tap-to-focus Initiate a focus metering action to a specific `MeteringPoint` using the `CameraController.focusTo` method. ```typescript const controller = ... const meteringPoint = ... await controller.focusTo(meteringPoint) ``` -------------------------------- ### Get All Available Camera Devices (Hook) Source: https://visioncamera.margelo.com/docs/devices Use this hook to get all available camera devices on the system. This is useful if you want to filter camera devices yourself. ```javascript const devices = useCameraDevices() ``` -------------------------------- ### Create and Use Object Output with useCamera Hook (Alternative Structure) Source: https://visioncamera.margelo.com/docs/object-output An alternative structure for using the `useCamera` hook with an object output. This example initializes the camera configuration separately. ```javascript function App() { const device = useCameraDevice('back') const objectOutput = useObjectOutput({ types: ['qr'], onObjectsScanned(objects) { console.log(`Scanned ${objects.length} objects!`) } }) const camera = useCamera({ isActive: true, device: device, outputs: [objectOutput], }) } ``` -------------------------------- ### Create Depth Frame Output with useCamera Hook and useCamera Hook Source: https://visioncamera.margelo.com/docs/depth-output This example demonstrates creating a depth frame output and configuring the camera using both `useCameraDevice` and `useCamera` hooks. Remember to dispose of depth frames after use. ```javascript function App() { const device = useCameraDevice('back') const depthOutput = useDepthOutput({ // ...options onDepth(depth) { 'worklet' console.log(`Received ${depth.width}x${depth.height} Depth!`) depth.dispose() } }) const camera = useCamera({ isActive: true, device: device, outputs: [depthOutput], }) } ``` -------------------------------- ### Install Pods for iOS Source: https://visioncamera.margelo.com/docs After running 'npx expo run:ios', rebuild Pods for your iOS project. ```bash npx pod-install ``` -------------------------------- ### Start a Focus Metering Action using CameraRef Source: https://visioncamera.margelo.com/docs/tap-to-focus Initiates a focus metering action by converting view coordinates to camera coordinates and calling `CameraRef.focusTo`. ```APIDOC ## Start a Focus Metering Action using CameraRef ### Description This example demonstrates how to use the `useRef` hook to get a `CameraRef` and then call the `focusTo` method with tap coordinates. ### Method Signature `camera.current.focusTo({ x: viewX, y: viewY })` ### Request Example ```javascript function App() { const camera = useRef(null) const onTap = async ({ viewX, viewY }) => { await camera.current.focusTo({ x: viewX, y: viewY }) } return ( ) } ``` ``` -------------------------------- ### Rebuild Native Mods for Expo Source: https://visioncamera.margelo.com/docs After installing dependencies, run 'npx expo prebuild' to compile the mods for Expo projects. ```bash npx expo prebuild ``` -------------------------------- ### Start Focus Metering Action with MeteringPoint Source: https://visioncamera.margelo.com/docs/tap-to-focus Initiates a focus metering action to a specific `MeteringPoint` using `CameraController.focusTo`. ```APIDOC ## Start Focus Metering Action with MeteringPoint ### Description This example shows how to use an existing `CameraController` and `MeteringPoint` to perform a focus metering action. ### Method Signature `controller.focusTo(meteringPoint)` ### Parameters - **meteringPoint** (`MeteringPoint`) - The point to focus on. ### Request Example ```javascript const controller = ... const meteringPoint = ... await controller.focusTo(meteringPoint) ``` ``` -------------------------------- ### Set Camera Constraints with Resolution Bias (Imperative) Source: https://visioncamera.margelo.com/docs/constraints Configure camera session constraints imperatively using `createCameraSession`. This example prioritizes resolution bias. ```javascript const session = await VisionCamera.createCameraSession(false) const videoOutput = createVideoOutput({ targetResolution: CommonResolutions.UHD_16_9 }) const device = await getDefaultCameraDevice('back') await session.configure([ { input: device, outputs: [videoOutput], constraints: [ { resolutionBias: videoOutput }, { fps: 60 } ] } ], {}) await session.start() ``` -------------------------------- ### Create Camera and Video Output (React Component) Source: https://visioncamera.margelo.com/docs/video-output This snippet shows how to set up a camera device and a video output within a React component using hooks. It's used for basic camera and video capture setup. ```javascript function App() { const device = useCameraDevice('back') const videoOutput = useVideoOutput({ /* options */ }) return ( ) } ``` -------------------------------- ### Create and Use Frame Output with useCamera Hook Source: https://visioncamera.margelo.com/docs/frame-output This example demonstrates using the `useCamera` hook in conjunction with `useFrameOutput` to stream and process camera frames. Remember to dispose of frames after processing. ```javascript function App() { const device = useCameraDevice('back') const frameOutput = useFrameOutput({ // ...options onFrame(frame) { 'worklet' console.log(`Received ${frame.width}x${frame.height} Frame!`) frame.dispose() } }) const camera = useCamera({ isActive: true, device: device, outputs: [frameOutput], }) } ``` -------------------------------- ### Example of Negotiated Camera Capabilities Source: https://visioncamera.margelo.com/docs/constraints Illustrates how VisionCamera might select capabilities based on constraints. In this case, FPS is prioritized over resolution. ```plaintext - 4k @ 30FPS <-- resolution matches, but FPS doesn't - and FPS is more important - 1080p @ 60FPS <-- will be selected; matches 60 FPS and has best resolution match - 720p @ 60FPS <-- FPS matches, but we can get better resolution - 480p @ 120FPS <-- FPS matches, but we can get better resolution ``` -------------------------------- ### Imperative CameraSession with FPS Constraint Source: https://visioncamera.margelo.com/docs/constraints Configure an imperative CameraSession with an FPS constraint. This allows for fine-grained control over camera setup and negotiation. ```javascript const session = await VisionCamera.createCameraSession(false) const videoOutput = createVideoOutput({ targetResolution: CommonResolutions.UHD_16_9 }) const device = await getDefaultCameraDevice('back') await session.configure([ { input: device, outputs: [videoOutput], constraints: [ { fps: 99999 } ] } ], {}) await session.start() ``` -------------------------------- ### MLKit Image Orientation and Mirroring Source: https://visioncamera.margelo.com/docs/a-frame This example shows how to set the MLImage orientation based on the Frame's orientation and mirroring properties, which is crucial for correct image processing in native code. ```swift let frame = ... let mlImage = MLImage(sampleBuffer: frame.sampleBuffer) switch frame.orientation { case .up: mlImage.orientation = frame.isMirrored ? .portrait : .portraitMirrored case .down: // ... ``` -------------------------------- ### Starting Zoom Animation Source: https://visioncamera.margelo.com/docs/zooming Initiate a zoom animation to a specified zoom level using the `startZoomAnimation` method on the `CameraController`. The second argument controls the duration or speed of the animation. ```APIDOC ## Starting Zoom Animation ### Description Initiate a zoom animation to a specified zoom level using the `startZoomAnimation` method on the `CameraController`. The second argument controls the duration or speed of the animation. ### Method ```javascript await controller.startZoomAnimation(zoom, duration) ``` ``` -------------------------------- ### Initialize Resizer with createResizer Function Source: https://visioncamera.margelo.com/docs/resizer Alternatively, use the createResizer function to asynchronously initialize the resizer. This is useful when you need to await the resizer setup before using it. ```typescript import { createResizer } from 'react-native-vision-camera-resizer' const resizer = await createResizer({ width: 128, height: 128, channelOrder: 'rgb', dataType: 'float32', pixelLayout: 'planar', }) ``` -------------------------------- ### Get Specific Camera Device Imperatively Source: https://visioncamera.margelo.com/docs/devices Use this imperative function to get a specific camera device that matches your criteria, such as a triple-camera setup. This loads faster than a bigger virtual camera and supports most resolutions. ```javascript const devices = getAllCameraDevices() const device = getCameraDevice(devices, "back", { physicalDevices: ['ultra-wide-angle', 'wide-angle', 'telephoto'] }) ``` -------------------------------- ### Imperative Frame Output Configuration with createCameraSession Source: https://visioncamera.margelo.com/docs/frame-output Configure and start a camera session imperatively using `VisionCamera.createCameraSession`. This approach allows for more granular control over camera inputs and outputs, including frame outputs and their configurations. ```javascript const session = await VisionCamera.createCameraSession(false) const device = await getDefaultCameraDevice('back') const frameOutput = VisionCamera.createFrameOutput({ /* options */ }) const workletRuntime = createWorkletRuntimeForThread(frameOutput.thread) scheduleOnRuntime(workletRuntime, () => { 'worklet' frameOutput.setOnFrameCallback((frame) => { console.log(`Received ${frame.width}x${frame.height} Frame!`) frame.dispose() }) }) await session.configure([ { input: device, outputs: [ { output: frameOutput, mirrorMode: 'auto' } ], constraints: [] } ], {}) await session.start() ``` -------------------------------- ### Start Focus Metering Action with Camera View Source: https://visioncamera.margelo.com/docs/tap-to-focus Use the `` view to automatically convert view points to camera coordinates and perform a focus metering action. ```typescript function App() { const camera = useRef(null) const onTap = async ({ viewX, viewY }) => { await camera.current.focusTo({ x: viewX, y: viewY }) } return ( ) } ``` -------------------------------- ### Start Zoom Animation Source: https://visioncamera.margelo.com/docs/zooming Initiate a smooth zoom animation to a specified zoom level using `CameraController.startZoomAnimation(...)`. The second argument controls the animation duration. ```javascript const controller = ... const zoom = ... await controller.startZoomAnimation(zoom, 2) ``` -------------------------------- ### Set Camera Constraints with FPS Priority (Hook) Source: https://visioncamera.margelo.com/docs/constraints Configure camera constraints using the `constraints` option within the `useCamera` hook. This example prioritizes FPS. ```javascript function App() { const videoOutput = useVideoOutput({ targetResolution: CommonResolutions.UHD_16_9 }) const device = useCameraDevice('back') const camera = useCamera({ isActive: true, device: device, outputs: [videoOutput], constraints: [ { fps: 60 }, { resolutionBias: videoOutput } ] }) return ... } ``` -------------------------------- ### Get Location with LocationManager Source: https://visioncamera.margelo.com/docs/location Instantiate `LocationManager` imperatively to manage location updates. This method involves requesting permission, starting updates, and listening for changes. ```javascript const locationManager = createLocationManager({ /* options */ }) if (locationManager.locationPermissionStatus !== 'authorized') { const hasPermission = await locationManager.requestLocationPermission() if (!hasPermission) return } await locationManager.startUpdating() locationManager.addOnLocationChangedListener((location) => { console.log(location) }) ``` -------------------------------- ### Imperative Object Output Configuration with CameraSession Source: https://visioncamera.margelo.com/docs/object-output This snippet shows how to imperatively create and configure a camera session with an object output using the `VisionCamera` API. This approach is suitable for more complex setups or when not using React hooks. ```javascript const session = await VisionCamera.createCameraSession(false) const device = await getDefaultCameraDevice('back') const objectOutput = VisionCamera.createObjectOutput({ enabledObjectTypes: ['qr'] }) objectOutput.setOnObjectsScannedCallback((objects) => { console.log(`Scanned ${objects.length} objects!`) }) await session.configure([ { input: device, outputs: [ { output: objectOutput, mirrorMode: 'auto' } ], constraints: [] } ], {}) await session.start() ``` -------------------------------- ### Imperative Lifecycle Control with Navigation Listeners Source: https://visioncamera.margelo.com/docs/lifecycle Manually start and stop the CameraSession using navigation focus and blur events. This provides fine-grained control over the camera's active state. ```javascript const session = ... const focusListener = navigation.addListener('focus', async () => { await session.start() }) const blurListener = navigation.addListener('blur', async () => { await session.stop() }) ``` -------------------------------- ### Set Camera Constraints with Resolution Bias (Hook) Source: https://visioncamera.margelo.com/docs/constraints Configure camera constraints using the `constraints` option within the `useCamera` hook. This example prioritizes resolution bias. ```javascript function App() { const videoOutput = useVideoOutput({ targetResolution: CommonResolutions.UHD_16_9 }) const device = useCameraDevice('back') const camera = useCamera({ isActive: true, device: device, outputs: [videoOutput], constraints: [ { resolutionBias: videoOutput }, { fps: 60 } ] }) return ... } ``` -------------------------------- ### Call a Native Plugin from JS Frame Processor Source: https://visioncamera.margelo.com/docs/native-frame-processor-plugins Example of how to call a native plugin from a JavaScript frame processor. Ensure the plugin is correctly initialized and disposed. ```javascript const myNativePlugin = ... const frameOutput = useFrameOutput({ onFrame(frame) { 'worklet' myNativePlugin.call(frame) frame.dispose() } }) ``` -------------------------------- ### Create Photo Output with useCamera Hook Source: https://visioncamera.margelo.com/docs/photo-output Configure photo output using the `useCamera` hook for imperative control. This setup is suitable when you need to manage camera sessions more directly. ```typescript function App() { const device = useCameraDevice('back') const photoOutput = usePhotoOutput({ /* options */ }) const camera = useCamera({ isActive: true, device: device, outputs: [photoOutput], }) } ``` -------------------------------- ### Create Camera and Video Output (useCamera Hook) Source: https://visioncamera.margelo.com/docs/video-output This snippet demonstrates setting up a camera and video output using the useCamera hook. It's an alternative to the component-based approach for managing camera state. ```javascript function App() { const device = useCameraDevice('back') const videoOutput = useVideoOutput({ /* options */ }) const camera = useCamera({ isActive: true, device: device, outputs: [videoOutput], }) } ``` -------------------------------- ### Getting Exposure Bias Source: https://visioncamera.margelo.com/docs/exposure-bias To get the current Exposure value on a `CameraController`, use the `exposureBias` property. ```APIDOC ## Getting Exposure Bias ### Description Retrieves the current exposure bias value of the camera. ### Method `exposureBias: number` (getter) ### Parameters None ### Request Example ```javascript const controller = ... console.log(controller.exposureBias) ``` ### Response #### Success Response (200) - **exposureBias** (number) - The current exposure bias value. ``` -------------------------------- ### Getting current Focus Lens Position Source: https://visioncamera.margelo.com/docs/locking-ae-af-awb Get the current focus lens position from the CameraController to determine the current focus distance. ```APIDOC ## Getting current Focus Lens Position ### Description Retrieve the current focus lens position. ### Properties - **lensPosition** (number) - The current focus lens position, between 0.0 and 1.0. ### Request Example ```javascript const controller = ... console.log(controller.lensPosition) // between 0...1 ``` ``` -------------------------------- ### Set FPS Imperatively Source: https://visioncamera.margelo.com/docs/fps Configure the target FPS for a camera session using the imperative API. This method involves creating a session, configuring it with device and FPS constraints, and then starting the session. ```javascript const session = await VisionCamera.createCameraSession(false) const device = await getDefaultCameraDevice('back') await session.configure([ { input: device, constraints: [ { fps: 60 } ] } ], {}) await session.start() ``` -------------------------------- ### Accessing Native Frame Buffer and Orientation in C++ Source: https://visioncamera.margelo.com/docs/native-frame-processor-plugins Demonstrates how to get the native buffer and orientation from a frame in C++. Includes platform-specific casting for AHardwareBuffer (Android) and CVPixelBufferRef (iOS). ```cpp void call(const std::shared_ptr& frame) { auto nativeBuffer = frame->getNativeBuffer(); auto orientation = frame->getOrientation(); #ifdef ANDROID AHardwareBuffer* hardwareBuffer = reinterpret_cast(nativeBuffer.pointer); #else // iOS CVPixelBufferRef pixelBuffer = reinterpret_cast(nativeBuffer.pointer); #endif } ``` -------------------------------- ### Handle Camera Output Readiness with Camera Component Source: https://visioncamera.margelo.com/docs/camera-outputs Use the onConfigured prop of the Camera component to execute a callback once the output is configured and ready. ```javascript function App() { const output = ... return ( { output.doSomething() }} /> ) } ``` -------------------------------- ### Get Default Camera Device Source: https://visioncamera.margelo.com/docs/devices Use this hook to get the default back camera device. This is often the most suitable for general use cases. ```javascript const device = useCameraDevice("back") ``` -------------------------------- ### Get CameraControllers from CameraSession Configure Source: https://visioncamera.margelo.com/docs/camera-controller The `configure` method of `CameraSession` returns an array of CameraControllers, one for each configured CameraSessionConnection. This is useful for managing multiple camera inputs or complex configurations. ```typescript const device = ... const session = ... const controllers = await session.configure([ { input: device, outputs: [], constraints: [] } ], {}) ``` -------------------------------- ### Create Recorder Instance Source: https://visioncamera.margelo.com/docs/video-output This snippet shows how to create a `Recorder` instance from a `VideoOutput` object. This recorder is then used to manage the video recording process. ```javascript const recorder = await videoOutput.createRecorder({ // ...options }) ``` -------------------------------- ### Connect Camera Outputs Imperatively with createCameraSession Source: https://visioncamera.margelo.com/docs/camera-outputs Configure CameraOutputs by passing them in the 'outputs' array within the session.configure method. ```javascript const session = await VisionCamera.createCameraSession(false) const device = await getDefaultCameraDevice('back') const photoOutput = ... const videoOutput = ... await session.configure([ { input: device, outputs: [ { output: photoOutput, mirrorMode: 'auto' }, { output: videoOutput, mirrorMode: 'auto' } ], constraints: [] } ], {}) await session.start() ``` -------------------------------- ### Get Default Camera Device (Imperative) Source: https://visioncamera.margelo.com/docs/devices Use this imperative function to get the default back camera device. This is often the most suitable for general use cases. ```javascript const device = await getDefaultCameraDevice("back") ``` -------------------------------- ### Select Specific Camera Device by Criteria Source: https://visioncamera.margelo.com/docs/devices Use this hook to select a specific camera device that matches your criteria, such as a triple-camera setup. This loads faster than a bigger virtual camera and supports most resolutions. ```javascript const device = useCameraDevice("back", { physicalDevices: ['ultra-wide-angle', 'wide-angle', 'telephoto'] }) ``` -------------------------------- ### Get Location with useLocation Hook Source: https://visioncamera.margelo.com/docs/location Use the `useLocation` hook to get the current location. Request permission if not already granted. Logs the location if permission is available. ```javascript const location = useLocation({ /* options */ }) useEffect(() => { if (!location.hasPermission) { location.requestPermission() } }, [location.hasPermission]) if (location.hasPermission) { console.log(location.location) } ``` -------------------------------- ### Creating a Photo Output Source: https://visioncamera.margelo.com/docs/photo-output Demonstrates how to initialize and use the `usePhotoOutput` hook or imperative `VisionCamera.createPhotoOutput` to set up photo capturing capabilities. ```APIDOC ## Creating a Photo Output This section shows how to create a `PhotoOutput` instance using either a hook or imperative API. ### React Hook Example ```javascript import { useCameraDevice, usePhotoOutput } from 'react-native-vision-camera' function App() { const device = useCameraDevice('back') const photoOutput = usePhotoOutput({ /* options */ }) return ( ) } ``` ### Imperative Example ```javascript import { VisionCamera, getDefaultCameraDevice } from 'react-native-vision-camera' async function setupCamera() { const session = await VisionCamera.createCameraSession(false) const device = await getDefaultCameraDevice('back') const photoOutput = VisionCamera.createPhotoOutput({ /* options */ }) await session.configure([ { input: device, outputs: [ { output: photoOutput, mirrorMode: 'auto' } ], constraints: [] } ], {}) await session.start() } ``` Refer to `PhotoOutputOptions` for available configuration options. ``` -------------------------------- ### Enable Audio Recording (Imperative) Source: https://visioncamera.margelo.com/docs/video-output This snippet demonstrates enabling audio recording using the imperative `createVideoOutput` function by setting `enableAudio` to `true`. Microphone permission is required. ```javascript const videoOutput = VisionCamera.createVideoOutput({ enableAudio: true }) ``` -------------------------------- ### Getting current White Balance Gains Source: https://visioncamera.margelo.com/docs/locking-ae-af-awb Access the current White-Balance Gains from the CameraController. ```APIDOC ## Getting current White Balance Gains ### Description Retrieve the current white balance gains. ### Properties - **whiteBalanceGains** (object) - The current white balance gains. ### Request Example ```javascript const controller = ... console.log(controller.whiteBalanceGains) ``` ### Warnings - If the device does not support manual control over AE/AF/AWB, invalid values (e.g. `0`) will be returned. ``` -------------------------------- ### Prefer Simpler Camera Devices for Faster Initialization Source: https://visioncamera.margelo.com/docs/performance Use a simpler CameraDevice with fewer physical devices to speed up initialization. Prefer 'wide-angle-camera' over 'triple-camera'. ```typescript const fasterDevice = useCameraDevice('back', { physicalDevices: ['wide-angle-camera'], }) const slowerDevice = useCameraDevice('back', { physicalDevices: ['ultra-wide-angle-camera', 'wide-angle-camera', 'telephoto-camera'], }) ``` ```typescript const devices = getAllCameraDevices() const fasterDevice = getCameraDevice(devices, 'back', { physicalDevices: ['wide-angle-camera'], }) const slowerDevice = getCameraDevice(devices, 'back', { physicalDevices: ['ultra-wide-angle-camera', 'wide-angle-camera', 'telephoto-camera'], }) ``` -------------------------------- ### Get Raw Barcode Bytes Source: https://visioncamera.margelo.com/docs/a-barcode Retrieve the barcode's value as an ArrayBuffer for low-level access. ```javascript const barcode = ... const buffer = barcode.rawBytes const data = new Uint8Array(buffer) console.log(data) // [104, 116, 116, 112, ...] ``` -------------------------------- ### Create Camera Session and Photo Output Imperatively Source: https://visioncamera.margelo.com/docs/photo-output Set up a camera session and photo output using VisionCamera's imperative API. This method allows for detailed configuration of inputs, outputs, and session parameters. ```typescript const session = await VisionCamera.createCameraSession(false) const device = await getDefaultCameraDevice('back') const photoOutput = VisionCamera.createPhotoOutput({ /* options */ }) await session.configure([ { input: device, outputs: [ { output: photoOutput, mirrorMode: 'auto' } ], constraints: [] } ], {}) await session.start() ``` -------------------------------- ### Access Barcode Format Source: https://visioncamera.margelo.com/docs/a-barcode Retrieve the format of a detected barcode. Example formats include 'code-128' or 'qr-code'. ```javascript const barcode = ... console.log(barcode.format) // "qr-code" ``` -------------------------------- ### Handle Camera Output Readiness with useCamera Hook Source: https://visioncamera.margelo.com/docs/camera-outputs Provide an onConfigured callback to the useCamera hook options to run code after the output is ready. ```javascript function App() { const output = ... const camera = useCamera({ isActive: true, device: 'back', outputs: [output], onConfigured() { output.doSomething() } }) } ``` -------------------------------- ### Access Raw Barcode Value Source: https://visioncamera.margelo.com/docs/a-barcode Get the raw string representation of a barcode's value if it can be decoded as text. ```javascript const barcode = ... console.log(barcode.rawValue) // "https://margelo.com" ``` -------------------------------- ### Get a Location (Imperative) Source: https://visioncamera.margelo.com/docs/location This section demonstrates the imperative approach to managing location updates and permissions using a LocationManager. ```APIDOC ## Get a Location (Imperative) ### Description Instantiate a `LocationManager` to imperatively control location updates and permission requests. ### Usage ```javascript const locationManager = createLocationManager({ /* options */ }) if (locationManager.locationPermissionStatus !== 'authorized') { const hasPermission = await locationManager.requestLocationPermission() if (!hasPermission) return } await locationManager.startUpdating() locationManager.addOnLocationChangedListener((location) => { console.log(location) }) ``` ``` -------------------------------- ### Get and Release NativeBuffer Source: https://visioncamera.margelo.com/docs/a-frames-nativebuffer Retrieve a Frame's NativeBuffer and ensure it is released after use to prevent memory leaks. ```javascript const frame = ... const nativeBuffer = frame.getNativeBuffer() // ...processing... nativeBuffer.release() frame.dispose() ``` -------------------------------- ### Enable Camera Extension with View Source: https://visioncamera.margelo.com/docs/camera-extensions Configure the `` component to use a specific camera extension by passing the selected extension to the `cameraExtension` prop. ```javascript function App() { const device = ... const extensions = useCameraDeviceExtensions(device) const extension = extensions.find((e) => e.type === 'night') return ( ) } ``` -------------------------------- ### Prepare the pipeline for RAW Source: https://visioncamera.margelo.com/docs/raw-photos Configure your CameraPhotoOutput to capture in RAW/DNG photos by setting the containerFormat to 'dng'. This can be done using the usePhotoOutput hook or the createPhotoOutput static method. ```APIDOC ## Prepare the pipeline for RAW ### Description Configure your `CameraPhotoOutput` to capture in RAW/DNG photos by setting the `containerFormat` to `'dng'`. ### Usage #### Using `usePhotoOutput` hook: ```javascript const photoOutput = usePhotoOutput({ containerFormat: 'dng' }) ``` #### Using `createPhotoOutput` static method: ```javascript const photoOutput = VisionCamera.createPhotoOutput({ containerFormat: 'dng' }) ``` ### Notes - The `CameraSession` negotiates constraints to support RAW capture. If RAW is not available, session configuration will fail. Check support upfront via `isSessionConfigSupported(...)`. - For instant user feedback due to RAW capture's higher bandwidth and slower speed, request a Preview Image. See "Photo Output Callbacks: Preview Image" for details. ``` -------------------------------- ### Render NativePreviewView Source: https://visioncamera.margelo.com/docs/nativepreview-view Renders the NativePreviewView component, passing the previewOutput prop. Ensure usePreviewOutput is called to get the preview output. ```javascript function App() { const previewOutput = usePreviewOutput() return ( ) } ``` -------------------------------- ### Get Current Focus Lens Position Source: https://visioncamera.margelo.com/docs/locking-ae-af-awb Retrieve the current focus lens position, indicating the current focus distance. ```javascript const controller = ... console.log(controller.lensPosition) // between 0...1 ``` -------------------------------- ### Get Current Exposure Duration and ISO Source: https://visioncamera.margelo.com/docs/locking-ae-af-awb Retrieve the current exposure duration and ISO values, which can be used for light metering. ```javascript const controller = ... console.log(controller.exposureDuration) console.log(controller.iso) ``` -------------------------------- ### Create AsyncRunner using useAsyncRunner() Source: https://visioncamera.margelo.com/docs/async-frame-processing Use this hook to create an AsyncRunner instance within a React component. Ensure react-native-vision-camera-worklets is installed. ```javascript const asyncRunner = useAsyncRunner() ``` -------------------------------- ### Create Multi-Camera Session Source: https://visioncamera.margelo.com/docs/multi-camera Check if the system supports multi-camera sessions and create a new session if it does. This is the entry point for using multiple cameras. ```javascript if (VisionCamera.supportsMultiCamSessions) { const session = await VisionCamera.createCameraSession(true) } ``` -------------------------------- ### Get a Location (Hook) Source: https://visioncamera.margelo.com/docs/location This hook provides a convenient way to access location data and manage permissions using a functional component. ```APIDOC ## Get a Location (Hook) ### Description Use the `useLocation` hook to get the current location and manage location permissions within your React Native component. ### Usage ```javascript const location = useLocation({ /* options */ }) useEffect(() => { if (!location.hasPermission) { location.requestPermission() } }, [location.hasPermission]) if (location.hasPermission) { console.log(location.location) } ``` ``` -------------------------------- ### Set Target Resolution for Photo and Video Outputs (Imperative) Source: https://visioncamera.margelo.com/docs/camera-outputs Create photo and video outputs with a specified target resolution using the targetResolution option. ```javascript const photoOutput = createPhotoOutput({ targetResolution: CommonResolutions.UHD_16_9 }) const videoOutput = createPhotoOutput({ targetResolution: CommonResolutions.UHD_16_9 }) ``` -------------------------------- ### Get CameraController from useCamera Hook Source: https://visioncamera.margelo.com/docs/camera-controller The `useCamera` hook simplifies obtaining the CameraController. Pass the desired configuration options directly to the hook. ```typescript function App() { const camera = useCamera({ isActive: true, device: 'back', }) } ``` -------------------------------- ### Enable Audio Recording (useVideoOutput Hook) Source: https://visioncamera.margelo.com/docs/video-output This snippet shows how to enable audio recording by setting `enableAudio` to `true` when using the `useVideoOutput` hook. Ensure microphone permission is granted. ```javascript const videoOutput = useVideoOutput({ enableAudio: true }) ``` -------------------------------- ### Handling CameraSession Interruptions (Imperative) Source: https://visioncamera.margelo.com/docs/camera-session Shows how to add listeners to handle interruptions and resumption of a CameraSession. ```APIDOC ## CameraSession Interruptions (Imperative) ### Description Listen for interruptions to the camera session (e.g., due to incoming calls) and for when the session resumes. ### Method `session.addOnInterruptionStartedListener(listener: (reason: InterruptionReason) => void): void` `session.addOnInterruptionEndedListener(listener: () => void): void` ### Parameters #### Path Parameters - **listener** (function) - Required - The callback function to execute when an interruption starts or ends. - **reason** (InterruptionReason) - The reason for the interruption (only for `addOnInterruptionStartedListener`). ### Endpoint `session.addOnInterruptionStartedListener` `session.addOnInterruptionEndedListener` ### Request Example ```javascript const session = ... // Obtained CameraSession instance session.addOnInterruptionStartedListener((reason) => { console.log(`Interrupted: ${reason}`) }) session.addOnInterruptionEndedListener(() => { console.log(`Interruption ended`) }) ``` ``` -------------------------------- ### Enable Camera Extension Imperatively Source: https://visioncamera.margelo.com/docs/camera-extensions Configure a `CameraSession` to use a specific camera extension by including it in the `constraints` object during session configuration. ```javascript const device = ... const session = ... const extensions = await getSupportedExtensions(device) const extension = extensions.find((e) => e.type === 'night') const controllers = await session.configure([ { input: device, outputs: [], constraints: { cameraExtension: extension } } ], {}) ``` -------------------------------- ### Create AsyncRunner using createAsyncRunner() Source: https://visioncamera.margelo.com/docs/async-frame-processing Use this function to create an AsyncRunner instance. This method is an alternative to the `useAsyncRunner` hook. Ensure react-native-vision-camera-worklets is installed. ```javascript const asyncRunner = createAsyncRunner() ``` -------------------------------- ### Get Barcode Value Type Source: https://visioncamera.margelo.com/docs/a-barcode Determine the type of value contained within a barcode. Common value types are 'url', 'text', and 'wifi'. ```javascript const barcode = ... console.log(barcode.valueType) // "url" ``` -------------------------------- ### Run iOS App (Non-Expo) Source: https://visioncamera.margelo.com/docs Build a new binary for your iOS app after applying native changes using npm. ```bash npm run ios ``` -------------------------------- ### Getting current Exposure Duration/ISO Source: https://visioncamera.margelo.com/docs/locking-ae-af-awb Retrieve the current exposure duration and ISO values from the CameraController, useful for using VisionCamera as a light meter. ```APIDOC ## Getting current Exposure Duration/ISO ### Description Retrieve the current exposure duration and ISO values. ### Properties - **exposureDuration** (number) - The current exposure duration. - **iso** (number) - The current ISO value. ### Request Example ```javascript const controller = ... console.log(controller.exposureDuration) console.log(controller.iso) ``` ``` -------------------------------- ### Get Current Exposure Bias Source: https://visioncamera.margelo.com/docs/exposure-bias Retrieve the current exposure bias value from a `CameraController` using the `exposureBias` property. The default value is 0. ```javascript const controller = ... console.log(controller.exposureBias) // 0 ``` -------------------------------- ### Implement CameraOutput Factory in Swift Source: https://visioncamera.margelo.com/docs/custom-native-camera-outputs Implement the `HybridMyCustomOutputFactorySpec` in Swift to return an instance of your custom `MyCustomOutput`. ```swift import AVFoundation import VisionCamera class MyCustomOutputFactory: HybridMyCustomOutputFactorySpec { func createMyCustomOutput() -> any HybridCameraOutputSpec { return MyCustomOutput() } } ``` -------------------------------- ### Getting Current Zoom Value Source: https://visioncamera.margelo.com/docs/zooming Access the current zoom level of the camera through the `zoom` property of the `CameraController`. This property updates in real-time as zoom changes. ```APIDOC ## Getting Current Zoom Value ### Description Access the current zoom level of the camera through the `zoom` property of the `CameraController`. This property updates in real-time as zoom changes. ### Example ```javascript const controller = ... console.log(controller.zoom) // Outputs the current zoom value, e.g., 1 await controller.setZoom(5) console.log(controller.zoom) // Outputs the updated zoom value, e.g., 5 ``` ``` -------------------------------- ### Create Camera Preview Output with Hooks Source: https://visioncamera.margelo.com/docs/preview-output Use the `usePreviewOutput` hook to create a `CameraPreviewOutput` when using the hook-based API. This output can then be passed to the `useCamera` hook. ```javascript function App() { const device = useCameraDevice('back') const previewOutput = usePreviewOutput() const camera = useCamera({ isActive: true, device: device, outputs: [previewOutput], }) } ``` -------------------------------- ### Get Current Zoom Value Source: https://visioncamera.margelo.com/docs/zooming Access the current zoom level of the camera via the `zoom` property on the `CameraController`. This reflects the active zoom factor. ```javascript const controller = ... console.log(controller.zoom) // 1 await controller.setZoom(5) console.log(controller.zoom) // 5 ``` -------------------------------- ### Get Available Camera Extensions (Hooks) Source: https://visioncamera.margelo.com/docs/camera-extensions Use the `useCameraDeviceExtensions` hook to retrieve all supported camera extensions for a given device in a React Native component. ```javascript const device = ... const extensions = useCameraDeviceExtensions(device) ``` -------------------------------- ### Get CameraController from Camera Ref Source: https://visioncamera.margelo.com/docs/camera-controller Access the CameraController via the `controller` property on the CameraRef after the camera is configured. Ensure the `Camera` component is active and has been configured. ```typescript function App() { const camera = useRef(null) return ( { // Now the `controller` is set on the ref const controller = camera.current.controller }} /> ) } ``` -------------------------------- ### Manually creating a Location Source: https://visioncamera.margelo.com/docs/location Provides instructions on how to manually create a `Location` object, useful for testing or simulating locations. ```APIDOC ## Manually creating a Location ### Description Create a custom `Location` object using the `createLocation` function. This is useful for testing or spoofing location data. ### Usage ```javascript const location = createLocation(48.2084, 16.3735) ``` ``` -------------------------------- ### Manually Resolve Constraints with resolveConstraints Source: https://visioncamera.margelo.com/docs/constraints Use the VisionCamera.resolveConstraints function to determine a compatible CameraSessionConfig without starting a session. This is useful for pre-validation or configuration checks. ```javascript const device = ... const videoOutput = ... const config = await VisionCamera.resolveConstraints( device, [ { output: videoOutput, mirrorMode: 'auto' } ], [ { resolutionBias: videoOutput }, { fps: 60 } ] ) console.log(`Config resolved: ${config.toString()}`) ``` -------------------------------- ### Run iOS App with Expo Source: https://visioncamera.margelo.com/docs Build a new binary for your iOS app after applying native changes using Expo. ```bash npx expo run:ios ``` -------------------------------- ### Focus Metering with Steady Responsiveness Source: https://visioncamera.margelo.com/docs/tap-to-focus Set the focus responsiveness to 'steady' for video recording to avoid disruptive focus changes. This may result in slower focusing. ```typescript const controller = ... const meteringPoint = ... await controller.focusTo(meteringPoint, { responsiveness: 'steady' }) ``` -------------------------------- ### Get Pixel Buffer from Frame Source: https://visioncamera.margelo.com/docs/a-frame Access the native, GPU-backed pixel buffer of a VisionCamera frame for zero-copy access. The buffer is only valid as long as the Frame is valid. ```javascript const frame = ... const buffer = frame.getPixelBuffer() ```