# Document Capture

## Available Document Frames

In this camera mode, there is a capture frame to assist the user in positioning the document correctly. Once the document is properly positioned, the user must click the button to take a photo of the document.

{% hint style="warning" %}
The SDK does not perform any validation of what is being captured.
{% endhint %}

In this camera mode, it is possible to capture the following documents:

* **CPF:** Capture the front of the CPF;
* **CNH:** Capture the open CNH;
* **CNH Front:** Capture the front of the CNH;
* **CNH Back:** Capture the back of the CNH;
* **RG Front:** Capture the front of the RG;
* **RG Back:** Capture the back of the RG;
* **Others:** Capture any other document.

<figure><img src="/files/HLj641ijN8MXnYcrPz4D" alt="" width="375"><figcaption></figcaption></figure>

<br>

## Initializing the SDK

To get started, create an instance of the builder (generated through the UnicoCheckBuilder interface), providing the context and the implementation of the UnicoListener class as parameters.

The implementation of this class is quite simple and can be done with just a few lines of code. All you need to do is override the callback methods with your application's business logic.

{% tabs %}
{% tab title="Dart" %}

```dart
class _MyHomePageState extends State<MyHomePage> implements UnicoListener {

    late UnicoCheckBuilder _unicoCheck;

    /// Unico callbacks
      @override
      void onErrorUnico(UnicoError error) {}

      @override
      void onUserClosedCameraManually() {}

      @override
      void onSystemChangedTypeCameraTimeoutFaceInference() {}

      @override
      void onSystemClosedCameraTimeoutSession() {}

      /// Document callbacks
      @override
      void onSuccessDocument(ResultCamera resultCamera) { }

      @override
      void onErrorDocument(UnicoError error) { }

}
```

{% endtab %}
{% endtabs %}

## Implementing the callback functions

Note that, as in the previous example, the implementation work of the `UnicoListener` class is largely about configuring the callback methods. Each method is called in a specific situation based on the SDK's response.

Override the methods exemplified above with your application’s business logic:

{% stepper %}
{% step %}
**`onErrorUnico(UnicoError error)`**

When invoked, the method receives a parameter of type `UnicoError`, which contains details about the error. Learn more about the `UnicoError` type in the SDK's [error handling](https://devcenter.unico.io/idcloud-v2/by-client-integration/sdk/available-sdks/flutter-sdk/error-handling-guide) section.

{% endstep %}

{% step %}
**`onUserClosedCameraManually()`**

This method is invoked whenever the user manually closes the camera, such as by clicking the "Back" button.

{% endstep %}

{% step %}
**`onSystemClosedCameraTimeoutSession()`**

This method is invoked as soon as the maximum session time is reached (without capturing any image).

{% hint style="warning" %}
It can be configured in the builder through the **setTimeoutSession** method. This method should receive the maximum session time in seconds. You can change the maximum session time for your user by using the face detection feature **(Selfie camera with smart capture)**. If the user exceeds the time set in your process to capture the photo, you can display a customizable message or instruction to the user. The default value is **40 seconds**, and **the minimum value is also 40 seconds.**
{% endhint %}

{% endstep %}

{% step %}
**`onSystemChangedTypeCameraTimeoutFaceInference()`**

This method is invoked as soon as the maximum time for detecting a user's face is reached (without detecting anything). In this case, the camera mode is automatically switched to manual capture mode (without the smart capture silhouette).

{% hint style="warning" %}
The maximum capture time when using face detection (Selfie camera with smart capture) is **13 seconds**. If the user encounters difficulty capturing the photo via face detection and exceeds the time set in your process, the capture mode is automatically switched to manual, aiming to make the action easier for the user (**TimeoutToFaceInference**)..
{% endhint %}
{% endstep %}
{% endstepper %}

{% hint style="danger" %}
All the methods above must be created as indicated in your project (even if without any logic). Otherwise, the project will not compile successfully.
{% endhint %}

## Implementing Listeners for Camera Events

{% hint style="info" %}
The implementation of these listener methods must be done through an instance of the `iAcessoBioSelfie` class.
{% endhint %}

The camera opening method needs to know what to do when it successfully captures an image or encounters an error during the process. "What to do" is communicated to the camera opening method by implementing *listeners* that are called in cases of success or error.

Through the implementation of the listeners, you can specify what happens in your app in case of error (`onErrorDocument` method) or success (`onSuccessDocument` method) when capturing images.

### `onSucessDocument` Method

When successfully capturing an image, this method is invoked and returns an object of type `ResultCamera`, which is later used in the REST API  requests:

```dart
public void onSuccessDocument(ResultCamera resultCamera) { }
```

The `ResultCamera` object returns 2 attributes: `base64` and `encrypted`:

* The `base64` attribute can be used if you want to display a preview of the image in your app;
* Both the `encrypted` and `base64` attributes can be sent in the REST API calls to by Client.

{% hint style="success" %}
If it is necessary to convert the base64 string to a bitmap, the standard method does not work for Android. You need to perform a split at the comma (,) for it to function correctly. If you want to learn more, read the article: [How to convert a Base64 string into a Bitmap image to show it in a ImageView?](https://stackoverflow.com/questions/4837110/how-to-convert-a-base64-string-into-a-bitmap-image-to-show-it-in-a-imageview).
{% endhint %}

### `onErrorDocument` Method

If an error occurs during the image capture, this method is invoked and returns an object of type `UnicoError`:

```dart
public void onErrorDocument(UnicoError error) { }
```

{% hint style="info" %}
Learn more about the types of `ErrorBio` in the SDK's [error handling](https://devcenter.unico.io/idcloud-v2/by-client-integration/sdk/available-sdks/flutter-sdk/error-handling-guide) section.
{% endhint %}

## Preparing and Opening the Camera

To open the camera, the `openCameraDocument()` method is used. This method is provided through the object generated with an instance of the `UnicoCheck` class.

This method takes the following parameters:

* A JSON file with the credentials, generated in the credential setup step;
* The listeners configured above;
* The type of document to be captured, which can be:

| Parameter                                     | Description                   |
| --------------------------------------------- | ----------------------------- |
| **`DocumentCameraTypes.CPF`**                 | Capture the front of the CPF  |
| **`DocumentCameraTypes.CNH`**                 | Capture the open CNH          |
| **`DocumentCameraTypes.CNH_FRENTE`**          | Capture the front of the CNH  |
| **`DocumentCameraTypes.CNH_VERSO`**           | Capture the back of the CNH   |
| **`DocumentCameraTypes.RG_FRENTE`**           | Capture the front of the RG   |
| **`DocumentCameraTypes.RG_VERSO`**            | Capture the back of the RG RG |
| **`DocumentCameraTypes.OUTROS("descrição")`** | Capture any other document    |

{% hint style="success" %}
If you need to capture a document that doesn't have a specific frame (e.g., RNE, among others), use the frame `DocumentCameraTypes.OUTROS("description")`, which will provide a generic, rectangular frame that can be used to guide any capture.
{% endhint %}

Example using the open CNH:

```dart
 _unicoCheck.build().openCameraDocument(
        jsonFileName: androidJsonFileName,
        documentType: DocumentType.CNH,
        listener: this);
```

## Making a POST Request to the Client API

Capturing the images is only the first part of the journey. After capturing the image, it is necessary to send the `base64` generated by the SDK to the REST APIs of by Client.&#x20;


---

# Agent Instructions: 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://devcenter.unico.io/unico-idcloud/by-client-integration/sdk/available-sdks/flutter-sdk/usage-and-integration-guide/document-capture.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.
