Socket
Book a DemoInstallSign in
Socket

@woonivers/react-native-document-scanner

Package Overview
Dependencies
Maintainers
2
Versions
12
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@woonivers/react-native-document-scanner

Scan documents, automatic border detection, automatic crop

Source
npmnpm
Version
1.6.2
Version published
Weekly downloads
107
-56.5%
Maintainers
2
Weekly downloads
 
Created
Source

Demo gif

Set up dev environment

Medium article

React Native Document Scanner

Live document detection library. Returns either a URI or a base64 encoded string of the captured image, allowing you to easily store it or use it as you wish !

Features :

  • Live detection
  • Perspective correction and crop of the image
  • Live camera filters (brightness, saturation, contrast)
  • Flash
  • Easy to use base64 image

Can be easily plugged with react-native-perspective-image-cropper

Demo crop gif

Both Platform

Use version >=1.4.1 if you are using react-native 0.48+

$ npm install @woonivers/react-native-document-scanner --save

$ react-native link @woonivers/react-native-document-scanner

Edit the info.plist file in XCode and add the following permission : NSCameraUsageDescription

Remember, this library uses your device camera, you can't run it on a simulator.

iOS if you want to use Cocoapods instead of default linking

If you want to use Cocoapods insteads of react-native link, add the following to your Podfile

  pod 'RNPdfScanner', :path => '../node_modules/@woonivers/react-native-document-scanner/ios'

Android Only

If you do not have it already in your project, you must link openCV in your settings.gradle file

include ':openCVLibrary310'
project(':openCVLibrary310').projectDir = new File(rootProject.projectDir,'../node_modules/@woonivers/react-native-document-scanner/android/openCVLibrary310')

In android/app/src/main/AndroidManifest.xml

Change manifest header to avoid "Manifest merger error". After you add xmlns:tools="http://schemas.android.com/tools" should look like this:

<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.<yourAppName>" xmlns:tools="http://schemas.android.com/tools">

Add tools:replace="android:allowBackup" in <application tag. It should look like this:

<application tools:replace="android:allowBackup" android:name=".MainApplication" android:label="@string/app_name" android:icon="@mipmap/ic_launcher" android:allowBackup="false" android:theme="@style/AppTheme">

Add Camera permissions request:

<uses-permission android:name="android.permission.CAMERA" />

Usage

import React, { Component } from "react"
import { View, Image } from "react-native"

import DocumentScanner from "react-native-document-scanner"

class YourComponent extends Component {
  render() {
    return (
      <View>
        <DocumentScanner
          useBase64
          saveInAppDocument={false}
          onPictureTaken={data =>
            this.setState({
              image: data.croppedImage,
              initialImage: data.initialImage,
              rectangleCoordinates: data.rectangleCoordinates,
            })
          }
          overlayColor="rgba(255,130,0, 0.7)"
          enableTorch={false}
          brightness={0.3}
          saturation={1}
          contrast={1.1}
          quality={0.5}
          onRectangleDetect={({ stableCounter, lastDetectionType }) =>
            this.setState({ stableCounter, lastDetectionType })
          }
          detectionCountBeforeCapture={5}
          detectionRefreshRateInMS={50}
        />
        <Image
          source={{ uri: `data:image/jpeg;base64,${this.state.image}` }}
          resizeMode="contain"
        />
      </View>
    )
  }
}

Properties

PropPlatformDefaultTypeDescription
overlayColorBothnonestringColor of the detected rectangle : rgba recommended
detectionCountBeforeCaptureBoth5integerNumber of correct rectangle to detect before capture
detectionRefreshRateInMSiOS50integerTime between two rectangle detection attempt
enableTorchBothfalseboolAllows to active or deactivate flash during document detection
useFrontCamiOSfalseboolAllows you to switch between front and back camera
brightnessiOS0floatIncrease or decrease camera brightness. Normal as default.
saturationiOS1floatIncrease or decrease camera saturation. Set 0 for black & white
contrastiOS1floatIncrease or decrease camera contrast. Normal as default
qualityiOS0.8floatImage compression. Reduces both image size and quality
useBase64iOSfalseboolIf base64 representation should be passed instead of image uri's
saveInAppDocumentiOSfalseboolIf should save in app document in case of not using base 64
captureMultipleiOSfalseboolKeeps the scanner on after a successful capture
saveOnDeviceAndroidfalseboolSave the image in the device storage (Need permissions)

Manual capture

  • First get component ref
<DocumentScanner ref={ref => (this.scanner = ref)} />
  • Then call :
this.scanner.capture()

Each rectangle detection (iOS only)

PropsParamsTypeDescription
onRectangleDetect{ stableCounter, lastDetectionType }objectSee below

The returned object includes the following keys :

  • stableCounter

Number of correctly formated rectangle found (this number triggers capture once it goes above detectionCountBeforeCapture)

  • lastDetectionType

Enum (0, 1 or 2) corresponding to the type of rectangle found

  • Correctly formated rectangle
  • Wrong perspective, bad angle
  • Too far

Returned image

PropParamsTypeDescription
onPictureTakendataobjectReturns the captured image in an object { croppedImage: ('URI or BASE64 string'), initialImage: 'URI or BASE64 string', rectangleCoordinates: 'object of coordinates' }

Save in app document

If you want to use saveInAppDocument options, then don't forget to add those raws in .plist :

<key>LSSupportsOpeningDocumentsInPlace</key>
<true/>

Keywords

react-native

FAQs

Package last updated on 27 Sep 2019

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts