Skip to content

Show scanner

Open Camera Activity

After the initialization is completed and the scenario is set, you can start the document scanning process. There are several options on how to open the camera activity.

#1

Use this method to open the camera preview activity using the default back camera, which will pass frames for recognition and return results to the completion block when they are ready. The activity will be opened from the context which has been passed to the method:

DocumentReader.Instance().showScanner(MainActivity.this, completion);
DocumentReader.Instance().showScanner(this@MainActivity, completion)

#2

Use this method to open the camera preview activity using the specified camera, which will pass frames for recognition and return results to the completion block when they are ready. The activity will be opened from the context which has been passed to the method:

DocumentReader.Instance().showScanner(MainActivity.this, 0, completion);
DocumentReader.Instance().showScanner(this@MainActivity, 0, completion)

Completion Callback

The current results are returned in the completion block from each camera shot with different actions (DocReaderAction enum) during the scanning process:

  • COMPLETE—the scanning process was finished
  • PROCESS—the scanning process is not finished, you can get an intermediate result from the completion block
  • CANCEL—the scanning process was canceled by the user
private IDocumentReaderCompletion completion = new IDocumentReaderCompletion() {
    @Override
    public void onCompleted(int action, DocumentReaderResults results, Throwable error) {
        // processing is finished, all results are ready
        if (action == DocReaderAction.COMPLETE) { 
            // scanning process was finished              
        } else {
            // something happened before all results were ready
            if (action == DocReaderAction.CANCEL){
                Toast.makeText(MainActivity.this, "Scanning was cancelled",Toast.LENGTH_LONG).show();
            } else if(action == DocReaderAction.ERROR){
                Toast.makeText(MainActivity.this, "Error:" + error, Toast.LENGTH_LONG).show();
            }
        }
    }
};
private val completion = IDocumentReaderCompletion { action, results, error ->
    // processing is finished, all results are ready
    if (action == DocReaderAction.COMPLETE) {
        // scanning process was finished
    } else {
        // something happened before all results were ready
        if (action == DocReaderAction.CANCEL) {
            Toast.makeText(this@MainActivity, "Scanning was cancelled", Toast.LENGTH_LONG).show()
        } else if (action == DocReaderAction.ERROR) {
            Toast.makeText(this@MainActivity, "Error:$error", Toast.LENGTH_LONG).show()
        }
    }
}

Close Scanner

To close the scanner, that is the camera activity, invoke:

DocumentReader.Instance().stopScanner(MainActivity.this);
DocumentReader.Instance().stopScanner(this@MainActivity)