Table of contents
Swift
Objective-C

Read from Camera

This page is for Foundational APIs only. Refer to Quick Start for how to scan from camera with BarcodeScanner component.

Follow these three steps to read barcodes from the camera:

  1. Set input
  2. Register result receiver
  3. Start capturing

Set input

CameraEnhancer - Dynamsoft Standard Camera Input

  1. Create a CameraEnhancer object and bind it to CameraView.

    • Objective-C
    • Swift
    1. @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;
      
    2. let cameraEnhancer = CameraEnhancer()
      let cameraView = CameraView(frame: self.view.bounds)
      cameraEnhancer.cameraView = cameraView
      
  2. Use CaptureVisionRouter.setInput to set the CameraEnhancer object as the input source.

    • Objective-C
    • Swift
    1. @property (nonatomic, strong) DSCaptureVisionRouter *cvr;
      self.cvr = [[DSCaptureVisionRouter alloc] init];
      NSError *error = nil;
      [self.cvr setInput:self.dce error:&error];
      
    2. let cvr = CaptureVisionRouter()
      try? cvr.setInput(cameraEnhancer)
      

Other Camera Input

Note: If you are using a AVFoundation library, refer to the DecodeWithAVCaptureSession sample for camera input integration.

To use another camera source, complete the following steps so the library can process the input:

  1. Receive the camera input.
  2. Convert the raw camera input to a com.dynamsoft.core.basic_structures.ImageData object.
  3. Use addImageToBuffer to add the ImageData object 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
  1. @interface ViewController () <DSCapturedResultReceiver>
    - (void)setUpDCV {
       [self.cvr addResultReceiver:self];
    }
    - (void)onDecodedBarcodesReceived:(DSDecodedBarcodesResult *)result {
    }
    
  2. 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
  1. [self.cvr startCapturing:DSPresetTemplateReadBarcodes completionHandler:nil];
    
  2. cvr.startCapturing(PresetTemplate.readBarcodes.rawValue) { isSuccess, error in
       if (!isSuccess) {
          if let error = error {
             self.showResult("Error", error.localizedDescription)
          }
       }
    }
    

This page is compatible for: