Skip to main content

Camera Setup

To perform OCR, you need to pass camera frames to the SDK. This page explains how to set up the camera.

1. Camera Permission Settings​

Add a camera usage description to Info.plist.

Open Project > Info > Custom iOS Target Properties in Xcode and add Privacy - Camera Usage Description with an appropriate usage description.

Info.plist settings

2. Camera Session Configuration​

Set up camera input and output using AVCaptureSession.

import AVFoundation

private let captureSession = AVCaptureSession()
private lazy var videoOutput = AVCaptureVideoDataOutput()

func setupCaptureSession() {
guard let videoDevice = AVCaptureDevice.default(
.builtInWideAngleCamera, for: .video, position: .back
) else { fatalError("Camera device not found") }

let videoDeviceInput = try! AVCaptureDeviceInput(device: videoDevice)
captureSession.addInput(videoDeviceInput)
captureSession.addOutput(videoOutput)
captureSession.sessionPreset = .high

let sampleBufferQueue = DispatchQueue(label: "sampleBufferQueue")
videoOutput.setSampleBufferDelegate(self, queue: sampleBufferQueue)
videoOutput.alwaysDiscardsLateVideoFrames = true

// The pixel format must be set to kCMPixelFormat_32BGRA
videoOutput.videoSettings = [
String(kCVPixelBufferPixelFormatTypeKey): kCMPixelFormat_32BGRA
]
videoOutput.connection(with: .video)?.videoRotationAngle = 90.0
}
Pixel Format

The SDK requires kCMPixelFormat_32BGRA. Specifying a different format will cause incorrect behavior.

3. Per-Frame OCR Processing​

Pass each frame captured from the camera to the SDK to perform OCR.

Call scan inside the AVCaptureVideoDataOutputSampleBufferDelegate's captureOutput method.

func captureOutput(
_ output: AVCaptureOutput,
didOutput sampleBuffer: CMSampleBuffer,
from connection: AVCaptureConnection
) {
guard let scanResult = try? api.scan(
sampleBuffer,
viewBounds: viewBounds,
cropRect: cropRect
) else { return }

let detections = scanResult.getTextDetections()
for detection in detections {
let text = detection.getText()
if !text.isEmpty {
print("detected: \(text)")
}
}
}