npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@gennadysx/react-native-document-scanner-plugin

v1.1.1

Published

A React Native plugin that lets you scan documents using Android and iOS

Downloads

13

Readme

React Native Document Scanner

Npm package version npm dev dependency version

This is a React Native plugin that lets you scan documents using Android and iOS. You can use it to create apps that let users scan notes, homework, business cards, receipts, or anything with a rectangular shape.

| iOS | Android | | -------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------ | | Dollar-iOS | Dollar Android |

Install

npm install @gennadysx/react-native-document-scanner-plugin

After installing the plugin, you need to follow the steps below

iOS

  1. Open ios/Podfile and set platform :ios to 13 or higher

  2. iOS requires the following usage description be added and filled out for your app in Info.plist:

  • NSCameraUsageDescription (Privacy - Camera Usage Description)
  1. Install pods by running
cd ios && pod install && cd ..

Android

  1. Open android/gradle.properties and add org.gradle.jvmargs=-Xmx2048m

Note: You don't need to prompt the user to accept camera permissions for this plugin to work unless you're using another plugin that requires the user to accept camera permissions. See Android Camera Permissions.

Examples

Basic Example

import React, { useState, useEffect } from 'react'
import { Image } from 'react-native'
import DocumentScanner from '@gennadysx/react-native-document-scanner-plugin'

export default () => {
  const [scannedImage, setScannedImage] = useState();

  const scanDocument = async () => {
    // start the document scanner
    const { scannedImages } = await DocumentScanner.scanDocument()

    // get back an array with scanned image file paths
    if (scannedImages.length > 0) {
      // set the img src, so we can view the first scanned image
      setScannedImage(scannedImages[0])
    }
  }

  useEffect(() => {
    // call scanDocument on load
    scanDocument()
  }, []);

  return (
    <Image
      resizeMode="contain"
      style={{ width: '100%', height: '100%' }}
      source={{ uri: scannedImage }}
    />
  )
}

Here's what this example looks like with several items

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. This only works on Android.

import React, { useState, useEffect } from 'react'
import { Image } from 'react-native'
import DocumentScanner from '@gennadysx/react-native-document-scanner-plugin'

export default () => {
  const [scannedImage, setScannedImage] = useState();

  const scanDocument = async () => {
    // start the document scanner
    const { scannedImages } = await DocumentScanner.scanDocument({
      maxNumDocuments: 2
    })

    // get back an array with scanned image file paths
    if (scannedImages.length > 0) {
      // set the img src, so we can view the first scanned image
      setScannedImage(scannedImages[0])
    }
  }

  useEffect(() => {
    // call scanDocument on load
    scanDocument()
  }, []);

  return (
    <Image
      resizeMode="contain"
      style={{ width: '100%', height: '100%' }}
      source={{ uri: scannedImage }}
    />
  )
}

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. This only works on Android.

import React, { useState, useEffect } from 'react'
import { Image } from 'react-native'
import DocumentScanner from '@gennadysx/react-native-document-scanner-plugin'

export default () => {
  const [scannedImage, setScannedImage] = useState();

  const scanDocument = async () => {
    // start the document scanner
    const { scannedImages } = await DocumentScanner.scanDocument({
      letUserAdjustCrop: false
    })

    // get back an array with scanned image file paths
    if (scannedImages.length > 0) {
      // set the img src, so we can view the first scanned image
      setScannedImage(scannedImages[0])
    }
  }

  useEffect(() => {
    // call scanDocument on load
    scanDocument()
  }, []);

  return (
    <Image
      resizeMode="contain"
      style={{ width: '100%', height: '100%' }}
      source={{ uri: scannedImage }}
    />
  )
}

Documentation

scanDocument(...)

scanDocument(options?: ScanDocumentOptions | undefined) => Promise<ScanDocumentResponse>

Opens the camera, and starts the document scan

| Param | Type | | ------------- | ------------------------------------------------------------------- | | options | ScanDocumentOptions |

Returns: Promise<ScanDocumentResponse>


Interfaces

ScanDocumentResponse

| Prop | Type | Description | | ------------------- | --------------------------------------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------- | | scannedImages | string[] | This is an array with either file paths or base64 images for the document scan. | | status | ScanDocumentResponseStatus | The status lets you know if the document scan completes successfully, or if the user cancels before completing the document scan. |

ScanDocumentOptions

| Prop | Type | Description | Default | | ----------------------- | ----------------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ----------------------------------------- | | croppedImageQuality | number | The quality of the cropped image from 0 - 100. 100 is the best quality. | : 100 | | letUserAdjustCrop | boolean | Android only: If true then once the user takes a photo, they get to preview the automatically detected document corners. They can then move the corners in case there needs to be an adjustment. If false then the user can't adjust the corners, and the user can only take 1 photo (maxNumDocuments can't be more than 1 in this case). | : true | | maxNumDocuments | number | Android only: The maximum number of photos an user can take (not counting photo retakes) | : 24 | | responseType | ResponseType | The response comes back in this format on success. It can be the document scan image file paths or base64 images. | : ResponseType.ImageFilePath |

Enums

ScanDocumentResponseStatus

| Members | Value | Description | | ------------- | ---------------------- | --------------------------------------------------------------------------------------------------------- | | Success | 'success' | The status comes back as success if the document scan completes successfully. | | Cancel | 'cancel' | The status comes back as cancel if the user closes out of the camera before completing the document scan. |

ResponseType

| Members | Value | Description | | ------------------- | ---------------------------- | ------------------------------------------------------------------------------- | | Base64 | 'base64' | Use this response type if you want document scan returned as base64 images. | | ImageFilePath | 'imageFilePath' | Use this response type if you want document scan returned as inmage file paths. |

Expo

This plugin doesn't run in Expo Go. It works with Expo, and you can avoid manually changing iOS and Android files by following these steps.

npx expo install @gennadysx/react-native-document-scanner-plugin

Add @gennadysx/react-native-document-scanner-plugin to plugins in app.json or app.config.json.

{
  "name": "my expo app",
  "plugins": [
    [
      "@gennadysx/react-native-document-scanner-plugin",
      {
        "cameraPermission": "We need camera access, so you can scan documents"
      }
    ]
  ]
}
npx expo prebuild

or

eas build

Common Mistakes

Android Camera Permissions

You don't need to request camera permissions unless you're using another camera plugin that adds <uses-permission android:name="android.permission.CAMERA" /> to the application's AndroidManifest.xml.

In that case if you don't request camera permissions you get this error Error: error - error opening camera: Permission Denial: starting Intent { act=android.media.action.IMAGE_CAPTURE

Here's an example of how to request camera permissions.

import React, { useState, useEffect } from 'react'
import { Platform, PermissionsAndroid, Image, Alert } from 'react-native'
import DocumentScanner from '@gennadysx/react-native-document-scanner-plugin'

export default () => {
  const [scannedImage, setScannedImage] = useState();

  const scanDocument = async () => {
    // prompt user to accept camera permission request if they haven't already
    if (Platform.OS === 'android' && await PermissionsAndroid.request(
      PermissionsAndroid.PERMISSIONS.CAMERA
    ) !== PermissionsAndroid.RESULTS.GRANTED) {
      Alert.alert('Error', 'User must grant camera permissions to use document scanner.')
      return
    }

    // start the document scanner
    const { scannedImages } = await DocumentScanner.scanDocument()

    // get back an array with scanned image file paths
    if (scannedImages.length > 0) {
      // set the img src, so we can view the first scanned image
      setScannedImage(scannedImages[0])
    }
  }

  useEffect(() => {
    // call scanDocument on load
    scanDocument()
  }, []);

  return (
    <Image
      resizeMode="contain"
      style={{ width: '100%', height: '100%' }}
      source={{ uri: scannedImage }}
    />
  )
}

License

Copyright 2022 David Marcus
Modified by GennadySX

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.