Read from Camera
This page is for Foundational APIs only. Refer to Quick Start for how to scan from camera with
BarcodeScannercomponent.
Follow these three steps to read barcodes from the camera:
- Set input
- Register result receiver
- Start capturing
Set input
CameraEnhancer - Dynamsoft Standard Camera Input
-
Create a
CameraEnhancerobject and bind it toCameraView.- Objective-C
- Swift
-
@property (nonatomic, strong) DSCameraEnhancer *dce; @property (nonatomic, strong) DSCameraView *cameraView; self.dce = [[DSCameraEnhancer alloc] init]; self.cameraView = [[DSCameraView alloc] initWithFrame:self.view.bounds]; self.dce.cameraView = self.cameraView; -
let cameraEnhancer = CameraEnhancer() let cameraView = CameraView(frame: self.view.bounds) cameraEnhancer.cameraView = cameraView
-
Use
CaptureVisionRouter.setInputto set theCameraEnhancerobject as the input source.- Objective-C
- Swift
-
@property (nonatomic, strong) DSCaptureVisionRouter *cvr; self.cvr = [[DSCaptureVisionRouter alloc] init]; NSError *error = nil; [self.cvr setInput:self.dce error:&error]; -
let cvr = CaptureVisionRouter() try? cvr.setInput(cameraEnhancer)
Other Camera Input
Note: If you are using a AVFoundation library, refer to the
DecodeWithAVCaptureSessionsample for camera input integration.
To use another camera source, complete the following steps so the library can process the input:
- Receive the camera input.
- Convert the raw camera input to a
com.dynamsoft.core.basic_structures.ImageDataobject. - Use
addImageToBufferto add theImageDataobject to the buffer.
Result Receiver
Use CapturedResultReceiver to receive capture results. The callback is triggered each time an image is processed, regardless of whether a barcode is decoded.
- Objective-C
- Swift
@interface ViewController () <DSCapturedResultReceiver> - (void)setUpDCV { [self.cvr addResultReceiver:self]; } - (void)onDecodedBarcodesReceived:(DSDecodedBarcodesResult *)result { }class ViewController: UIViewController, CapturedResultReceiver { func setUpDCV() { cvr.addResultReceiver(self) } func onDecodedBarcodesReceived(_ result: DecodedBarcodesResult) { } }
Start Capturing
Use startCapturing and stopCapturing to control when barcode decoding starts and stops.
- Objective-C
- Swift
[self.cvr startCapturing:DSPresetTemplateReadBarcodes completionHandler:nil];cvr.startCapturing(PresetTemplate.readBarcodes.rawValue) { isSuccess, error in if (!isSuccess) { if let error = error { self.showResult("Error", error.localizedDescription) } } }