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
Initialize the Docutain SDK for Android 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 Document.writeImage
method. Pass the page you want to export as JPG and the target path where to save it. If you want to export all pages as JPG, you can get the number of pages via Document.pageCount()
and loop through all pages like in the follwing example:
- Kotlin
- Java
//...
//scan a document
//...
val pageCount = Document.pageCount()
for (i in 1..pageCount) {
val file = File(filesDir, "Image$i.jpg")
val fileReturn = Document.writeImage(i, file)
}
//...
//scan a document
//...
int pageCount = Document.pageCount();
for (int i = 1; i <= pageCount; i++) {
File file = new File(getFilesDir(), String.format("Image%d.jpg", i));
File fileReturn = Document.writeImage(i, file);
}
Get images as Bitmap
Getting the pages as Bitmaps is possible by using the Document.getImage
method.
- Kotlin
- Java
//...
//scan a document
//...
val pageCount = Document.pageCount()
for (i in 1..pageCount) {
var bitmap = Document.getImage(i)
}
//...
//scan a document
//...
int pageCount = Document.pageCount();
for (int i = 1; i <= pageCount; i++) {
Bitmap bitmap = Document.getImage(i);
}
You can pass your own Bitmap Options in order to get back a Bitmap that fits your needs.
- Kotlin
- Java
val options = BitmapFactory.Options()
options.inSampleSize = 2
val pageCount = Document.pageCount()
for (i in 1..pageCount) {
var bitmap = Document.getImage(i, options)
}
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 2;
int pageCount = Document.pageCount();
for (int i = 1; i <= pageCount; i++) {
Bitmap bitmap = Document.getImage(i, options);
}
Get images as ByteArray
You also have the possibility to get the images as ByteArray:
- Kotlin
- Java
//...
//scan a document
//...
val pageCount = Document.pageCount()
for (i in 1..pageCount) {
var imageBytes = Document.getImageBytes(i)
}
//...
//scan a document
//...
int pageCount = Document.pageCount();
for (int i = 1; i <= pageCount; i++) {
byte[] imageBytes = Document.getImageBytes(i);
}
PageSourceType
When getting the images as Bitmap or ByteArray, you can define a PageSourceType
.
- Kotlin
- Java
//...
//scan a document
//...
val pageCount = Document.pageCount()
for (i in 1..pageCount) {
var bitmap = Document.getImage(i, null, PageSourceType.CUT_FILTER);
}
//...
//scan a document
//...
int pageCount = Document.pageCount();
for (int i = 1; i <= pageCount; i++) {
Bitmap bitmap = Document.getImage(i, null, Document.PageSourceType.CUT_FILTER);
}
You have the following options:
PageSourceType.CUT_FILTER
This is the default value if you do not provide any. The image will be the cut and filtered image which is the one the user sees when finishing the scan process.PageSourceType.CUT_ONLY
This will give you the cut but unfiltered image. If you for example 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 toPageSourceType.CUT_FILTER
. But this is no general rule and highly depends on your pipeline.PageSourceType.ORIGINAL
This will give you the uncut, unfiltered image as it was provided by the camera.