Skip to content

hansemannn/titanium-scanner

Repository files navigation

Titanium iOS 13+ Document Scanner

Use the iOS 13+ VisionKit document scanner API in Appcelerator Titanium. Pro tip: Combine with Ti.Vision to apply machine learning to the detected document.

Requirements

  • iOS 13+
  • Titanium SDK 8.2.0+
  • Granted camera permissions

APIs

Methods

  • showScanner
  • imageOfPageAtIndex(index) (after the success event)
  • pdfOfPageAtIndex(index) (after the success event)
  • pdfOfAllPages(params) (after the success event - the params include resizeImages and padding to be used to generate resized A4 PDF's)

Events

  • success
  • error
  • cancel

Example

import Scanner from 'ti.scanner';

const win = Ti.UI.createWindow({
    backgroundColor: '#fff'
});

const btn = Ti.UI.createButton({
    title: 'Scan Document'
});

btn.addEventListener('click', () => {
    Ti.Media.requestCameraPermissions(event => {
        if (!event.success) {
            alert('No camera permissions');
            return;
        }
        Scanner.showScanner();
    });
});

Scanner.addEventListener('cancel', () => {
    Ti.API.warn('Cancelled …');
});

Scanner.addEventListener('error', event => {
    Ti.API.error('Errored …');
    Ti.API.error(event.error);
});

Scanner.addEventListener('success', event => {
    Ti.API.warn('Succeeded …');
    Ti.API.warn(event);

    const win2 = Ti.UI.createWindow({
        backgroundColor: '#333'
    });

    const image = Ti.UI.createImageView({
        height: '70%',
        image: Scanner.imageOfPageAtIndex(0) /* Or pdfOfPageAtIndex(0) if you need the PDF of it, or many images via "event.count" */
    });

    win2.add(image);
    win2.open();
});

win.add(btn);
win.open();

License

MIT

Author

Hans Knöchel