Coder Social home page Coder Social logo

android-document-scanner's Introduction

Android Document Scanner

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.

Dollar Android

Install

Open build.gradle and add this to dependencies

implementation 'com.websitebeaver:documentscanner:1.1.0'

Examples

Basic Example

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

Dollar.Android.mp4

Business.Card.Android.mp4

Sign.Android.mp4

Notes.Android.mp4

Laptop.Android.mp4

Limit Number of Scans

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()
    }
}

Limit.Num.Scans.Android.mp4

Remove Cropper

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()
    }
}

Remove.Cropper.Android.mp4

Java Example

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();
    }
}

License

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.

android-document-scanner's People

Contributors

dmarcs avatar husseinhj avatar johnygqd avatar pierrenicol avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.