# How to Read Barcodes from an Image

> [!Important]
> Features on this page are only available for the **Foundational APIs**.

> [!Note]
> Refer to the [DecodeFromAnImage sample](https://github.com/Dynamsoft/barcode-reader-mobile-samples/tree/main/ios/FoundationalAPISamples/DecodeFromAnImage){:target="_blank"} for an example of decoding from the photo gallery.

The `capture` methods of the `CaptureVisionRouter` class are designed to process a single image.

```objc
- (DSCapturedResult *)captureFromFile:(NSString *)filePath templateName:(NSString *)templateName error:(NSError **)error;
- (DSCapturedResult *)captureFromFileBytes:(NSData *)fileBytes templateName:(NSString *)templateName error:(NSError **)error;
- (DSCapturedResult *)captureFromImage:(UIImage *)image templateName:(NSString *)templateName error:(NSError **)error;
- (DSCapturedResult *)captureFromBuffer:(NSData *)buffer width:(NSInteger)width height:(NSInteger)height stride:(NSInteger)stride format:(DSImagePixelFormat)format orientation:(NSInteger)orientation templateName:(NSString *)templateName error:(NSError **)error;
```

## Supported Image Types

The following image input types are supported:

1. An image specified by file path.
2. An image in memory (file bytes).
3. `UIImage`
4. [com.dynamsoft.core.basic_structures.ImageData](https://www.dynamsoft.com/capture-vision/docs/mobile/programming/ios/api-reference/core/basic-structures/image-data.md)

> [!Note]
> When decoding with a file path, provide the full file path, including the extension. Supported extensions are ".bmp", ".jpg", ".png", ".gif", and single-page ".tiff".

## How to Specify Template Name

When using a `capture` method, `templateName` is required. You can specify either a preset template or a custom template name.

### Use a Preset Template

`EnumPresetTemplate.PT_READ_BARCODES_READ_RATE_FIRST` is recommended when processing an image.

<div class="sample-code-prefix"></div>
>- Objective-C
>- Swift
>
>1. 
```objc
@property (nonatomic, strong) DSCaptureVisionRouter *cvr;
self.cvr = [[DSCaptureVisionRouter alloc] init];
DSCapturedResult *capturedResult = [self.cvr captureFromFile:@"Your-file-path" templateName:DSPresetTemplateReadBarcodes];
```
2. 
```swift
let capturedResult = try cvr.captureFromFile("Your-file-path", templateName: PresetTemplate.readBarcodes.rawValue)
```

### Use a Customized Template

For details, see [Parameters and Settings - Use a Customized Template](https://www.dynamsoft.com/barcode-reader/docs/mobile/programming/objectivec-swift/user-guide/capabilities/init-customized-template.md).

## How to Extract Barcode Info

<div class="sample-code-prefix"></div>
>- Objective-C
>- Swift
>
>1. 
```objc
NSError *error = nil;
DSCapturedResult *capturedResult = [self.cvr captureFromFile:@"Your file path" templateName:DSPresetTemplateReadBarcodes];
DSDecodedBarcodesResult *decodedBarcodesResult = capturedResult.decodedBarcodesResult;
for (DSBarcodeResultItem *barcodeResultItem in decodedBarcodesResult.items) {
   NSString *barcodeText = barcodeResultItem.text;
   NSString *barcodeFormatString = barcodeResultItem.formatString;
}
```
2. 
```swift
let capturedResult = try cvr.captureFromFile("Your file path", templateName: PresetTemplate.readBarcodes.rawValue)
if let decodedBarcodesResult = capturedResult.decodedBarcodesResult {
   guard let barcodeResultItems = decodedBarcodesResult.items, barcodeResultItems.count>0 else
   {
          // result count = 0
          return
   }
   for barcodeResultItem in barcodeResultItems
   {
          // deal with the results
   }
}
```

- `CapturedResult`: The largest set of image processing results. `DecodedBarcodesResult` is one subset of `CapturedResult`.
- `DecodedBarcodesResult`: The set of barcode results. It contains an array of `BarcodeResultItem` objects and additional information.
- `BarcodeResultItem`: An object that represents a single decoded barcode.
