Recognize image

Document Reader SDK provides the opportunity to recognize data from an image.

First of all, you need to check image browsing permissions.

Start Intent for getting image:

// creates and starts image browsing intent
// results will be handled in onActivityResult method
private void createImageBrowsingRequest() {
    Intent intent = new Intent();
    intent.setType("image/*");
    intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
    intent.setAction(Intent.ACTION_GET_CONTENT);
    startActivityForResult(Intent.createChooser(intent, "Select Picture"), REQUEST_BROWSE_PICTURE);
}

Handle image browsing results:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (resultCode == RESULT_OK) {
       //Image browsing intent processed successfully
        if (requestCode == REQUEST_BROWSE_PICTURE){
            if (data.getData() != null) {
                Uri selectedImage = data.getData();
                Bitmap bmp = getBitmap(selectedImage, 1920, 1080);

                DocumentReader.Instance().recognizeImage(bmp, completion);
             }
        }
    }
}

Handle scan results:

private IDocumentReaderCompletion completion = new IDocumentReaderCompletion() {
    @Override
    public void onCompleted(int action, DocumentReaderResults results, String 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();
        }
    }
}

Last updated