This is an Android library that lets you scan documents. You can use it to create apps that let users scan notes, homework, business cards, receipts, or anything with a rectangular shape.
Open build.gradle
and add this to dependencies
implementation 'com.websitebeaver:documentscanner:1.3.5'
package com.your.project
import com.websitebeaver.documentscanner.utils.ImageUtil
import android.os.Bundle
import android.util.Log
import android.widget.ImageView
import androidx.appcompat.app.AppCompatActivity
import com.websitebeaver.documentscanner.DocumentScanner
class MainActivity : AppCompatActivity() {
private lateinit var croppedImageView: ImageView
private val documentScanner = DocumentScanner(
this,
{ croppedImageResults ->
// display the first cropped image
croppedImageView.setImageBitmap(
ImageUtil().readBitmapFromFileUriString(
croppedImageResults.first(),
contentResolver
)
)
},
{
// an error happened
errorMessage -> Log.v("documentscannerlogs", errorMessage)
},
{
// user canceled document scan
Log.v("documentscannerlogs", "User canceled document scan")
}
)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// cropped image
croppedImageView = findViewById(R.id.cropped_image_view)
// start document scan
documentScanner.startScan()
}
}
Here's what this example looks like with several items
You can limit the number of scans. For example if your app lets a user scan a business card you might want them to only capture the front and back. In this case you can set maxNumDocuments to 2.
package com.your.project
import com.websitebeaver.documentscanner.utils.ImageUtil
import android.os.Bundle
import android.util.Log
import android.widget.ImageView
import androidx.appcompat.app.AppCompatActivity
import com.websitebeaver.documentscanner.DocumentScanner
import com.websitebeaver.documentscanner.constants.ResponseType
class MainActivity : AppCompatActivity() {
private lateinit var croppedImageView: ImageView
private val documentScanner = DocumentScanner(
this,
{ croppedImageResults ->
// display the first cropped image
croppedImageView.setImageBitmap(
ImageUtil().readBitmapFromFileUriString(
croppedImageResults.first(),
contentResolver
)
)
},
{
// an error happened
errorMessage -> Log.v("documentscannerlogs", errorMessage)
},
{
// user canceled document scan
Log.v("documentscannerlogs", "User canceled document scan")
},
ResponseType.IMAGE_FILE_PATH,
true,
2
)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// cropped image
croppedImageView = findViewById(R.id.cropped_image_view)
// start document scan
documentScanner.startScan()
}
}
You can automatically accept the detected document corners, and prevent the user from making adjustments. Set letUserAdjustCrop to false to skip the crop screen. This limits the max number of scans to 1.
package com.your.project
import com.websitebeaver.documentscanner.utils.ImageUtil
import android.os.Bundle
import android.util.Log
import android.widget.ImageView
import androidx.appcompat.app.AppCompatActivity
import com.websitebeaver.documentscanner.DocumentScanner
import com.websitebeaver.documentscanner.constants.ResponseType
class MainActivity : AppCompatActivity() {
private lateinit var croppedImageView: ImageView
private val documentScanner = DocumentScanner(
this,
{ croppedImageResults ->
// display the first cropped image
croppedImageView.setImageBitmap(
ImageUtil().readBitmapFromFileUriString(
croppedImageResults.first(),
contentResolver
)
)
},
{
// an error happened
errorMessage -> Log.v("documentscannerlogs", errorMessage)
},
{
// user canceled document scan
Log.v("documentscannerlogs", "User canceled document scan")
},
ResponseType.IMAGE_FILE_PATH,
false
)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// cropped image
croppedImageView = findViewById(R.id.cropped_image_view)
// start document scan
documentScanner.startScan()
}
}
Even though all of the examples so far have been in Kotlin, you can use this library with Java.
package com.your.project;
import com.websitebeaver.documentscanner.utils.ImageUtil;
import android.os.Bundle;
import android.util.Log;
import android.widget.ImageView;
import androidx.appcompat.app.AppCompatActivity;
import com.websitebeaver.documentscanner.DocumentScanner;
public class MainActivity extends AppCompatActivity {
private ImageView croppedImageView;
DocumentScanner documentScanner = new DocumentScanner(
this,
(croppedImageResults) -> {
// display the first cropped image
croppedImageView.setImageBitmap(
ImageUtil().readBitmapFromFileUriString(
croppedImageResults.first(),
getContentResolver()
)
);
return null;
},
(errorMessage) -> {
// an error happened
Log.v("documentscannerlogs", errorMessage);
return null;
},
() -> {
// user canceled document scan
Log.v("documentscannerlogs", "User canceled document scan");
return null;
},
null,
null,
null
);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// cropped image
croppedImageView = findViewById(R.id.cropped_image_view);
// start document scan
documentScanner.startScan();
}
}
Copyright 2022 David Marcus
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.