Step By Step Guide To Build Document Scanner Android App.

Hrishita Mavani
4 min readAug 13, 2020

I have been working on a document scanner app in android and as I have successfully released my app today, I decided to publish this blog today. If you are looking for the step-by-step guide to create a scanner app, this story might be useful to you.

Here are some snapshots from my app:

You can find it on play store using the following link

Let’s Begin

Image Scanning for mobile app can be implemented using the following ways:

1. Implementing from scratch using OpenCV

2. Implementing using Readily available open-source libraries.

In this tutorial, we are going to use a readily available scan library. There are a bunch of them but we are going to use the following library

To use this library you need to integrate it with your android studio project. here are steps for integration.

  1. Download the library from here https://github.com/jhansireddy/AndroidScannerDemo.

2. Place it in your project directory as shown in the screenshot.

3. Open <build.gradle> and adjust SDK version with your current project, include dependencies, change it to ‘implementation’.

4. Go to File->New->Import Module :scanlibrary

5. 1. open setting.gradle and add library manually:

include ‘:app’, ‘:scanlibrary’

6. Include scan library in your project using implementation project(‘:scanlibrary’)

7. Then, add some permission <AndroidManifest.xml>

<uses-permission android:name=”android.permission.CAMERA” /><uses-permission android:name=”android.permission.READ_EXTERNAL_STORAGE” /><uses-permission android:name=”android.permission.WRITE_EXTERNAL_STORAGE” />

That’s it for integration now you can start building your document scanner. Along with this tutorial you should also check out the official documentation from the repository.

Building a document scanner primarily consists of 3 steps:

Step 1: Start the Camera and capture image.

Step 2: Detect edges.

Step 3: Use the edges in the image to find the contour (outline) representing the piece of paper being scanned.

Step 4: Apply a perspective transform to obtain the top-down view of the document.

  1. Capture Image using camera intent
int REQUEST_CODE = 99;int preference = ScanConstants.OPEN_CAMERA;Intent intent = new Intent(this, ScanActivity.class);intent.putExtra(ScanConstants.OPEN_INTENT_PREFERENCE, preference);startActivityForResult(intent, REQUEST_CODE);

2. The result can be retrieved using onActivtyResult:

  @Override  protected void onActivityResult(int requestCode, int resultCode, Intent data) {    super.onActivityResult(requestCode, resultCode, data);          if (requestCode == REQUEST_CODE && resultCode == Activity.RESULT_OK) {              Uri uri = data.getExtras().getParcelable(ScanConstants.SCANNED_RESULT);              Bitmap bitmap = null;              try {                  bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), uri);                  getContentResolver().delete(uri, null, null);                 scannedImageView.setImageBitmap(bitmap);             } catch (IOException e) {               e.printStackTrace();             }         }     }

Here we get the bitmap object which can be used to display in an imageview as shown in the above example. If you want to save the image you can do it as follows:

FileOutputStream fos = new FileOutputStream(pictureFile);bitmap.compress(Bitmap.CompressFormat.PNG, 90, fos);fos.close();

That’s it. Very Easy Isn’t it? If you face issues feel free to ask in comments.

Don’t forget to check out my app on the play store and leave your valuable opinion.

--

--