# Barcode scanning

## Real-time barcode scanning

The SDK can detect barcodes located on the packaging of packaged foods. To implement real-time barcode scanning, the [camera setup for continuous image recognition](../food-recognition#local-neural-network-model) is needed.&#x20;

Specifically for barcode scanning, these steps are required:

1. When defining a `FoodDetectionConfiguration` object, enable the **`detectBarcode`** flag.
2. In the `recognitionResults` callback, check the property `candidate.barcodeCandidates`. If this list is empty, no barcodes have been recognised.
3. If there is a single or multiple barcode candidates, use their value property and invoke this function to fetch the [full nutrition data](nutrition-data).

{% tabs %}
{% tab title="iOS" %}
```swift
public func fetchFoodItemFor(productCode: String, completion: @escaping ((PassioNutritionAISDK.PassioFoodItem?) -> Void))
```
{% endtab %}

{% tab title="Android" %}
```kotlin
fun fetchFoodItemForProductCode(
    productCode: String,
    onResult: (foodItem: PassioFoodItem?) -> Unit
)
```
{% endtab %}

{% tab title="React Native" %}
```typescript
fetchFoodItemForProductCode(code: Barcode | PackagedFoodCode): Promise<PassioFoodItem | null>
```
{% endtab %}

{% tab title="Flutter" %}
```dart
Future<PassioFoodItem?> fetchFoodItemForProductCode(String productCode)
```
{% endtab %}
{% endtabs %}

{% hint style="info" %}
On smaller packages, barcodes can be very small. When trying to scan these barcodes, the user usually brings to package to close to the camera creating focus issues. To avoid this, but the camera zoom level to 2.
{% endhint %}

## Barcode scanning from an image

To recognise a barcode in an image, refer to the [recognizeImageRemote](https://passio.gitbook.io/nutrition-ai/guides/nutrition-ai-sdk/use-cases/food-recognition#remote-image-recognition) function. The result of the barcode scanning process will come in the `packagedFoodItem` attribute, and the resultType will be _Barcode_.

## UI Example

1. Set up the camera preview and detection session to recognise barcodes
2. Create a result view with two states: search and result
3. If the candidate.barcodeCandidates is empty, show the search state. Else, fetch the PassioFoodItem, and use it's name and icon to show the result

<div><figure><img src="https://872966647-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FES2kGechp4srgCTrBTxs%2Fuploads%2FGfnloImo8hCkm43LgflG%2FScreenshot%202024-08-14%20at%2019.43.47.png?alt=media&#x26;token=9fd77d57-3c4d-46aa-b27e-3a405f8a6d9f" alt="" width="220"><figcaption></figcaption></figure> <figure><img src="https://872966647-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FES2kGechp4srgCTrBTxs%2Fuploads%2F1owxeQfEduj1uSMbSisK%2FScreenshot%202024-08-14%20at%2019.43.57.png?alt=media&#x26;token=89157b07-c47d-40de-9f6f-1ebe90eb79d8" alt="" width="219"><figcaption></figcaption></figure></div>

Example of an image that produces multiple BarcodeCandidates:

<figure><img src="https://872966647-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FES2kGechp4srgCTrBTxs%2Fuploads%2FdVPk2ICt1OI1sFwoO1PO%2Fbarcode.png?alt=media&#x26;token=b38541cb-f427-4c02-8fcd-46b839d50e02" alt=""><figcaption><p>Barcode example image</p></figcaption></figure>

##
