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 Xamarin 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:
- Xamarin.Forms
- Xamarin.Android
- Xamarin.iOS
//...
//scan a document
//...
var pageCount = Document.PageCount;
for(int i = 1; i <= pageCount; i++)
{
var fileName = Path.Combine(FileSystem.AppDataDirectory, string.Format("Image{0}.jpg", i));
var fileReturn = Document.WriteImage(i, fileName);
}
//...
//scan a document
//...
var pageCount = Document.PageCount();
for (int i = 1; i <= pageCount; i++)
{
var file = new Java.IO.File(ExternalCacheDir, string.Format("Image{0}.jpg", i));
var fileReturn = Document.WriteImage(i, file);
}
//...
//scan a document
//...
var urls = NSFileManager.DefaultManager.GetUrls(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomain.User);
var url = urls[0];
var pageCount = Document.PageCount;
for(int i = 1; i <= pageCount; i++)
{
url = url.Append(string.Format("Image{0}.jpg", i), false);
var returnUrl = Document.WriteImage(i, url);
}
Get images as ByteArray
You also have the possibility to get the images as ByteArray:
- Xamarin.Forms
- Xamarin.Android
- Xamarin.iOS
//...
//scan a document
//...
var pageCount = Document.PageCount;
for (int i = 1; i <= pageCount; i++)
{
var imageBytes = Document.GetImageBytes(i);
}
//...
//scan a document
//...
var pageCount = Document.PageCount();
for (int i = 1; i <= pageCount; i++)
{
var imageBytes = Document.GetImageBytes(i);
}
//...
//scan a document
//...
var pageCount = Document.PageCount;
for (int i = 1; i <= pageCount; i++)
{
var imageData = Document.GetImage(i);
var imageBytes = imageData?.ToArray();
}
PageSourceType
When getting the images as ByteArray, you can define a PageSourceType
.
You have the following options:
PageSourceType.CutFilter
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.CutOnly
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.CutFilter
. 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.