Skip to main content

Image Export

In addition to exporting scanned documents as PDF, you can also retrieve the pages of the currently scanned document as image files (JPG).

Initialization​

  • Follow the Getting started guide
  • Initialize the Docutain SDK for Kotlin Multiplatform as described here

Export scanned pages as image files​

Write images to local files​

In order to write the currently scanned pages to local JPG files, you can use the DocutainSdk.writeImage method. Pass the page you want to export as JPG and optionally the target URI where to save it. If you want to export all pages as JPG, you can get the number of pages via DocutainSdk.pageCount() and loop through all pages like in the following example:

val pages = DocutainSdk.pageCount()

val scope = rememberCoroutineScope()
scope.launch {
for (page in 1..pages) {
val imageFile = DocutainSdk.writeImage(pageNumber = page)
// do something with the generated image file
}
}

writeImage returns a file:// URL on both platforms.

info

Pages start at 1.

Get images as byte[]​

Getting the pages as a JPG byte array improves performance as it does not include any disk I/O. To do so, call DocutainSdk.getImageBytes, pass it the page number of the page to be exported as JPG and an optional PageSourceType.

val pages = DocutainSdk.pageCount()

val scope = rememberCoroutineScope()
scope.launch {
for (page in 1..pages) {
val image: ByteArray = DocutainSdk.getImageBytes(page)
}
}

Both methods are suspend functions. See Coroutines and suspend functions for more details.

info

If an image cannot be generated, the method throws DocutainException. See Error Handling for more details.

PageSourceType​

When getting the images as ByteArray, you can define a PageSourceType.

import de.docutain.sdk.kmp.PageSourceType

val scope = rememberCoroutineScope()
scope.launch {
val image = DocutainSdk.getImageBytes(
pageNumber = 1,
pageSourceType = PageSourceType.CUT_FILTER
)
}

You have the following options:

ValueDescription
CUT_FILTERThe cut and filtered image, which is the one the user sees when finishing the scan process. This is the default value.
CUT_ONLYThe cut but unfiltered image. If you use the image for further processing in your own OCR pipeline which uses custom filter operations, this option might improve your OCR results as opposed to CUT_FILTER. But this is no general rule and highly depends on your pipeline.
ORIGINALThe uncut, unfiltered image as it was provided by the camera.