Skip to main content

Storage for Pages | Android Document Scanner

Many components in Android Scanbot SDK operate with io.scanbot.sdk.persistence.Page objects as a concept for storing and processing image resources. To make this more flexible for SDK customers Scanbot SDK provides an API io.scanbot.sdk.persistence.PageFileStorage to get access to the page content.

val pageFileStorage = ScanbotSDK(context).createPageFileStorage()

Extracting the Page content#

PageFileStorage provides methods to extract original snapped, processed (filtered and cropped) and preview images for the specified Page. As an input these methods need the pageId string value (which can be received from the Page object), PageFileStorage.PageFileType and optionally BitmapFactory.Options, and as a result they return the image android.net.Uri or Bitmap.

Example:

val pageFileStorage = ScanbotSDK(context).createPageFileStorage()
val image: Bitmap? = pageFileStorage.getImage(yourPageId, PageFileStorage.PageFileType.DOCUMENT, BitmapFactory.Options())// orval imageUri: Uri = pageFileStorage.getImageURI(yourPageId, PageFileStorage.PageFileType.DOCUMENT)

and the same for the preview images:

val pageFileStorage = ScanbotSDK(context).createPageFileStorage()
val previewImage: Bitmap? = pageFileStorage.getPreviewImage(yourPageId, PageFileStorage.PageFileType.DOCUMENT, BitmapFactory.Options())// orval previewImageUri: Uri = pageFileStorage.getPreviewImageURI(yourPageId, PageFileStorage.PageFileType.DOCUMENT)

PageFileStorage.PageFileType enum represents a page image type which corresponds to the some specific image resource:

  • PageFileStorage.PageFileType.ORIGINAL - an originally snapped image from the camera
  • PageFileStorage.PageFileType.DOCUMENT - a processed (filtered and cropped) page image
  • PageFileStorage.PageFileType.UNFILTERED_DOCUMENT - a cropped (but not filtered) page image

Also, it's possible to get all stored pages - pageFileStorage.getStoredPages(). It returns the list of the ids of all the stored pages.

New Page creation#

User has the ability to create a new Page with an external image (imported from the Gallery or any other sources). PageFileStorage provides methods fun add(image: ByteArray): String and fun add(image: Bitmap): String for that. These methods return a pageId String as a result. Under the hood they create a copy of an input image in the internal SDK files storage structure.

Example:

val pageFileStorage = ScanbotSDK(context).createPageFileStorage()
val newPageId = pageFileStorage.add()val newPage = Page(newPageId)

There are 2 more methods fun add(image: ByteArray, pageImageSource: PageImageSource): String and fun add(image: Bitmap, pageImageSource: PageImageSource): String which require an additional parameter PageImageSource.

PageImageSource enum represents the source of the image of Page

  • PageImageSource.UNDEFINED - Used by default. If the source is not defined. For example, an image was imported.
  • PageImageSource.MANUAL_SNAP - If the source image was captured manually. For example, when the user pressed a Snap button
  • PageImageSource.AUTOSNAPPING - If the source image was captured automatically. For example, by auto snapping functions
  • PageImageSource.CAMERA_FRAME - If the source image was taken from the camera frame
  • PageImageSource.IMPORT - If the source image was imported from an external source

Page contact modification#

Also, PageFileStorage provides a functionality to replace internal Page images. There are a few methods for this in PageFileStorage:

  • fun setImageForId(image: ByteArray, existingPageId: String, type: PageFileType)
  • fun setImageForId(image: Bitmap, existingPageId: String, type: PageFileType)
  • fun setFilteredPreviewForId(filteredPreview: ByteArray, existingPageId: String, filter: ImageFilterType)
  • fun setFilteredPreviewForId(filteredPreview: Bitmap, existingPageId: String, filter: ImageFilterType)
  • fun generateAndSetFilteredPreviewForId(filteredImage: ByteArray, existingPageId: String, filter: ImageFilterType): Bitmap - returns generated filtered preview image
  • fun generateAndSetFilteredPreviewForId(filteredImage: Bitmap, existingPageId: String, filter: ImageFilterType): Bitmap - returns generated filtered preview image
caution

All existing affected image resources will be replaced.

Page content removing#

Page content could be completely removed from the file system:

val pageFileStorage = ScanbotSDK(context).createPageFileStorage()
pageFileStorage.remove(yourPageId)// or remove all pagespageFileStorage.removeAll()