Skip to main content

Storage and Encryption| Flutter Document Scanner

Storage

By default, the native Scanbot SDKs as well as the Plugin itself use the internal and secure storage locations for all produced files (JPG, PNG, PDF, TIFF, etc).

  • On Android all files will be stored in the internal files directory of your application. No permissions are required for your app to read or write files in this directory.

  • On iOS all files will be stored in the Application Support folder of your application.

Customize Storage Location

It is strongly recommended to use the default storage location. However, you can override the storage directory on initialization of the Plugin. The initializeSdk method can take an optional parameter storageBaseDirectory to set a custom storage location.

Directory storageDirectory;
if (Platform.isAndroid) {
storageDirectory = await getExternalStorageDirectory();
}
if (Platform.isIOS) {
storageDirectory = await getApplicationDocumentsDirectory();
}

// Please note: getExternalStorageDirectory() and getApplicationDocumentsDirectory()
// are provided via 3rd-party plugins like "path_provider".

var config = ScanbotSdkConfig(
storageBaseDirectory: "${directory.path}/my_custom_storage",
...
);
await ScanbotSdk.initScanbotSdk(config);

The value of the storageBaseDirectory must be a file URL ('file:///...) pointing to a valid platform-specific file system path. If this directory does not exist yet, the Plugin will try to create it. To work with the file system we recommend the Flutter Plugin path_provider.

For the full demo code please checkout our example app on GitHub.

Warning

When overriding the default storage location, make sure:

  • You have implemented proper handling for storage permission requests;
  • You fully understand the security implications and accessibility of the stored document files.

👉 For more details about the storage locations on Android and iOS please also see:

Storage Cleanup

There is no automatic file clean mechanism in this Plugin. Your app should decide when the perfect time is to remove the files produced by this Plugin (images, PDFs, etc).

To avoid storage space issues caused by too many produced image files, it is strongly recommended implementing a suitable cleanup functionality based on the requirements of your app. This Plugin provides the following helper methods to keep the storage clean:

  • deletePage method to delete a certain Page object with all its files.
  • cleanupStorage method to remove all generated files by this Plugin (scanned and imported images, exported files like PDF, TIFF, etc).

ScanbotSdk.cleanupStorage()

By calling this function all output files generated by the native Scanbot SDKs or by this Plugin will be deleted, including the image files of all scanned or imported Page objects, all produced PDF files, TIFF files, etc.

await ScanbotSdk.cleanupStorage();

ScanbotSdk.deletePage(page)

Removes a page with all its files (original image, document image, thumbnails, filtered images, etc) from the internal file storage. This method does not remove any export files (PDF, TIFF) which were generated based on this page object.

await ScanbotSdk.deletePage(page);

Refreshes image file URIs of provided pages

ScanbotSdk.refreshImageUris(pages)

API: Future<List<Page>> refreshImageUris(List<Page> pages)

See the section Persistence of Page Objects.

File & Directory Handles

The Scanbot SDK plugin works with file URIs. This means all input and output files (images, PDFs, etc) as well as directories are identified by file URIs. Please note that a file URI is not just a file path : "file:///some/file/path/.." vs. "/some/file/path/.."

Examples of valid file URIs:

  • On Android: file:///storage/emulated/0/Android/data/my.awesome.app/some-sub-folder/ce8de3c4-3c96-4ce1-b285-483d01e1dc9a.jpg

  • On iOS: file:///var/mobile/Containers/Data/Application/D2BF9FB2-1024-4418-99B2-3709AB2C171E/Documents/some-sub-folder/05719BF8-63DB-4C8A-9A57-25B233AED33C.jpg

Storage Encryption

The Scanbot SDK provides the ability to store the generated image files (JPG, PNG), PDF, and TIFF files encrypted. This feature provides an additional level of security to the default secure storage locations of the native SDKs.

By default the file encryption is disabled. To enable it you have to pass the following config parameters on SDK initialization:

  • password: A secure password or passphrase to derive the AES key for encryption/decryption.
  • mode: Encryption mode, AES128 or AES256 (default and recommended).
var config = ScanbotSdkConfig(
encryptionParameters: EncryptionParameters(
password: "password",
mode: FileEncryptionMode.AES256));
await ScanbotSdk.initScanbotSdk(config);

By activating the storage encryption the native Scanbot SDKs will use the built-in AES 128 or AES 256 encryption. All generated image files (JPG, PNG) including the preview image files, as well as the exported PDF files will be encrypted in memory and stored as encrypted data files on the device storage.

The Scanbot SDK derives the AES key from the given password, an internal salt value, and the internal number of iterations using the PBKDF2 function.

When applying image operations like cropping, rotation or image filters, the Scanbot SDK will decrypt the image file in memory, apply the changes, encrypt and store them again.

Also see Handle Encrypted Images.

Handle Encrypted Images

Display Encrypted Images

If the file encryption is enabled you will not be able to display preview images via file URIs (e.g. page.documentPreviewImageFileUri). Instead, you have to load the decrypted data of a preview image and use it for displaying an image. In order to do that there is the API function getDecryptedDataFromFile(fileUri: string):

Page page = ... // scanned page object
// use the low-res image "documentPreviewImageFileUri" for the preview:
var decryptedImageData = await ScanbotEncryptionHandler.getDecryptedDataFromFile(page.documentPreviewImageFileUri);
//use image widget to show preview
Image image = Image.memory(decryptedImageData);

👉 For a full implementation see our example app.

Upload Encrypted Images

To upload an image, you have the following options:

  1. Upload the encrypted image file to your server and decrypt it on the backend. For assistance with generating the corresponding AES key and decrypting images, please contact our team.
  2. Alternatively, retrieve the decrypted image data as a Base64 string on the mobile device using the getDecryptedDataFromFile(fileUri: Uri) function, and then use this data for the upload process.

Examples:

// For a Page image file:
Page page = ... // scanned page object
// use the final hi-res image "documentImageFileUri" for the upload process:
var decryptedImageData = await ScanbotEncryptionHandler.getDecryptedDataFromFile(page.documentImageFileUri);
yourCustomUploadFunction(decryptedImageData);

// For a PDF or TIFF file generated by the Scanbot SDK:
Uri fileUri = ... // Uri of the generated PDF or TIFF file
var decryptedFileData = await ScanbotEncryptionHandler.getDecryptedDataFromFile(fileUri);
yourCustomUploadFunction(decryptedFileData);

Want to scan longer than one minute?

Generate a free trial license to test the Scanbot SDK thoroughly.

Get your free Trial License

What do you think of this documentation?