# Read from Camera

> [!Important]
> This page is for **Foundational APIs** only. Refer to [Quick Start](https://www.dynamsoft.com/barcode-reader/docs/mobile/programming/objectivec-swift/user-guide.md) 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`.

   <div class="sample-code-prefix"></div>
   >- Objective-C
   >- Swift
   >
   >1. 
   ```objc
   @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. 
   ```swift
   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.

   <div class="sample-code-prefix"></div>
   >- Objective-C
   >- Swift
   >
   >1. 
   ```objc
   @property (nonatomic, strong) DSCaptureVisionRouter *cvr;
   self.cvr = [[DSCaptureVisionRouter alloc] init];
   NSError *error = nil;
   [self.cvr setInput:self.dce error:&error];
   ```
   2. 
   ```swift
   let cvr = CaptureVisionRouter()
   try? cvr.setInput(cameraEnhancer)
   ```

### Other Camera Input

> Note:
> If you are using a AVFoundation library, refer to the [`DecodeWithAVCaptureSession`](https://github.com/Dynamsoft/barcode-reader-mobile-samples/tree/main/ios/FoundationalAPISamples/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.

<div class="sample-code-prefix"></div>
>- Objective-C
>- Swift
>
>1. 
```objc
@interface ViewController () <DSCapturedResultReceiver>
- (void)setUpDCV {
   [self.cvr addResultReceiver:self];
}
- (void)onDecodedBarcodesReceived:(DSDecodedBarcodesResult *)result {
}
```
2. 
```swift
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.

<div class="sample-code-prefix"></div>
>- Objective-C
>- Swift
>
>1. 
```objc
[self.cvr startCapturing:DSPresetTemplateReadBarcodes completionHandler:nil];
```
2. 
```swift
cvr.startCapturing(PresetTemplate.readBarcodes.rawValue) { isSuccess, error in
   if (!isSuccess) {
          if let error = error {
             self.showResult("Error", error.localizedDescription)
          }
   }
}
```
