> For the complete documentation index, see [llms.txt](https://assetstore.easymlkit.voxelbusters.com/tutorials/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://assetstore.easymlkit.voxelbusters.com/tutorials/features/barcode-scanner.md).

# Barcode Scanner

With Easy ML Kit's barcode scanning API, you can read data encoded using most standard barcode formats. Barcode scanning happens on the device, and doesn't require a network connection.

Barcodes are a convenient way to pass information from the real world to your app. In particular, when using 2D formats such as QR code, you can encode structured data such as contact information or WiFi network credentials. Because Easy ML Kit can automatically recognize and parse this data, your app can respond intelligently when a user scans a barcode.

> Import VoxelBusters.EasyMLKit and VoxelBusters.CoreLibrary namespaces

```csharp
using VoxelBusters.EasyMLKit;
using VoxelBusters.CoreLibrary;
```

#### Create Instance

Create an instance of the **BarcodeScanner** instance.

```csharp
private BarcodeScanner CreateBarcodeScanner()
{
    BarcodeScanner scanner = new BarcodeScanner();   
    return scanner;
}
```

#### Prepare

For preparing, you need to pass on above created Input source along with **BarcodeScannerOptions** and a callback to know when prepare is complete.

```csharp
private IImageInputSource CreateImageInputSource(Texture2D texture)
{
    return new ImageInputSource(texture);
}

private IImageInputSource CreateLiveCameraInputSource()
{
    IImageInputSource inputSource = new LiveCameraInputSource()
    {
        EnableFlash = false,
        IsFrontFacing = false
    };

    return inputSource;
}

private BarcodeScannerOptions CreateBarcodeScannerOptions()
{
    BarcodeScannerOptions.Builder builder = new BarcodeScannerOptions.Builder();
    builder.SetScannableFormats(BarcodeFormat.QR_CODE);//BarcodeFormat.QR_ALL;
    return builder.Build();
}

private void Prepare()
{
    IImageInputSource inputSource = CreateImageInputSource(TEXTURE);// Pass "readable" texture here.
    BarcodeScannerOptions options = CreateBarcodeScannerOptions();
    Debug.Log("Starting prepare...");
    scanner.Prepare(inputSource, options, OnPrepareComplete);
}

private void OnPrepareComplete(BarcodeScanner scanner, Error error)
{
        Debug.Log("Prepare complete..." + error);
        if (error == null)
        {
            Debug.Log("Prepare completed successfully!");
        }
        else
        {
            Debug.Log("Failed preparing Barcode scanner : " + error.Description);
        }
}
```

#### Process

Once prepare is complete, you can start processing which gives the result in a callback.

```csharp
private void OnPrepareComplete(BarcodeScanner scanner, Error error)
{
        Debug.Log("Prepare complete..." + error);
        if (error == null)
        {
            Debug.Log("Prepare completed successfully!");
            scanner.Process(OnProcessUpdate);
        }
        else
        {
            Debug.Log("Failed preparing Barcode scanner : " + error.Description);
        }
}

private void OnProcessUpdate(BarcodeScanner scanner, BarcodeScannerResult result)
{
    if (!result.HasError())
    {
        Debug.Log(string.Format("Received {0} Barcodes", result.Barcodes.Count));

        foreach (Barcode each in result.Barcodes)
        {
            Debug.Log(string.Format("Format : {0}, Value Type : {1}, Raw Value : {2}, Bounding Box : {3}", each.Format, each.ValueType, each.RawValue, each.BoundingBox));
        }
        if(result.Barcodes.Count > 0)
        {
            scanner.Close(null);
        }
    }
    else
    {
        Debug.Log("Barcode scanner failed processing : " + result.Error.Description, false);
    }
});
```

#### Close

Close the scanner once you are done processing.

```csharp
scanner.Close(null);//Or pass a callback to know once its complete
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://assetstore.easymlkit.voxelbusters.com/tutorials/features/barcode-scanner.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
