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:
Java
Kotlin
1
// creates and starts image browsing intent
2
// results will be handled in onActivityResult method
3
private void createImageBrowsingRequest() {
4
Intent intent = new Intent();
5
intent.setType("image/*");
6
intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
7
intent.setAction(Intent.ACTION_GET_CONTENT);
8
startActivityForResult(Intent.createChooser(intent, "Select Picture"), REQUEST_BROWSE_PICTURE);
9
}
Copied!
1
// creates and starts image browsing intent
2
// results will be handled in onActivityResult method
3
private fun createImageBrowsingRequest() {
4
val intent = Intent()
5
intent.type = "image/*"
6
intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true)
7
intent.action = Intent.ACTION_GET_CONTENT
8
startActivityForResult(Intent.createChooser(intent, "Select Picture"), REQUEST_BROWSE_PICTURE)
9
}
Copied!
Handle image browsing results:
Java
Kotlin
1
@Override
2
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
3
super.onActivityResult(requestCode, resultCode, data);
4
5
if (resultCode == RESULT_OK) {
6
//Image browsing intent processed successfully
7
if (requestCode == REQUEST_BROWSE_PICTURE){
8
if (data.getData() != null) {
9
Uri selectedImage = data.getData();
10
Bitmap bmp = getBitmap(selectedImage, 1920, 1080);
11
12
DocumentReader.Instance().recognizeImage(bmp, completion);
13
}
14
}
15
}
16
}
Copied!
1
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
2
super.onActivityResult(requestCode, resultCode, data)
3
4
if (resultCode == Activity.RESULT_OK) {
5
//Image browsing intent processed successfully
6
if (requestCode == REQUEST_BROWSE_PICTURE) {
7
if (data!!.data != null) {
8
val selectedImage = data.data
9
val bmp = getBitmap(selectedImage, 1920, 1080)
10
11
loadingDialog = showDialog("Processing image")
12
13
DocumentReader.Instance().recognizeImage(bmp, completion)
14
}
15
}
16
}
17
}
Copied!
Handle scan results:
Java
Kotlin
1
private IDocumentReaderCompletion completion = new IDocumentReaderCompletion() {
2
@Override
3
public void onCompleted(int action, DocumentReaderResults results, Throwable error) {
4
// processing is finished, all results are ready
5
if (action == DocReaderAction.COMPLETE) {
6
// scanning process was finished
7
}
8
} else {
9
// something happened before all results were ready
10
if (action == DocReaderAction.CANCEL){
11
Toast.makeText(MainActivity.this, "Scanning was cancelled",Toast.LENGTH_LONG).show();
12
} else if(action == DocReaderAction.ERROR){
13
Toast.makeText(MainActivity.this, "Error:" + error, Toast.LENGTH_LONG).show();
14
}
15
}
16
}
Copied!
1
private val completion = IDocumentReaderCompletion { action, results, error ->
2
// processing is finished, all results are ready
3
if (action == DocReaderAction.COMPLETE) {
4
// scanning process was finished
5
} else {
6
// something happened before all results were ready
7
if (action == DocReaderAction.CANCEL) {
8
Toast.makeText(this@MainActivity, "Scanning was cancelled", Toast.LENGTH_LONG).show()
9
} else if (action == DocReaderAction.ERROR) {
10
Toast.makeText(this@MainActivity, "Error:$error", Toast.LENGTH_LONG).show()
11
}
12
}
Copied!
Last modified 1yr ago
Copy link