Skip to content

Document scanner, features live border detection, perspective correction, image filters and more ! 📲📸

License

Notifications You must be signed in to change notification settings

Pompette/react-native-document-scanner

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

88 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Demo gif Demo cropper

React Native Document Scanner and Cropper (iOS only)

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
  • Native Cropper

Getting started

This is a forked version of https://github.com/Michaelvilleneuve/react-native-document-scanner, our versions diverged by the embed native cropper I added. I'm planning to add android scanner-cropper soon (https://github.com/Diastorm/rn-doc-scanner-android).

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

npm install git+https://github.com/Pompette/react-native-document-scanner.git

$ react-native link 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.

With Cocoapods

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

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

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}
          onCrop={(data) => {
            console.log('Cropped data : ', data);
          }}
          onCancelCrop={() => {
            console.log('Cancel crop');
          }}
          cropperOpts={{
            base64: true, // false by default
            // doneButtonTitle: "Valider",
            // cancelButtonTitle: "Annuler"
            // saveFile: false, // true by default
            // directory: '/my/super/path',
            // fileName: 'test'
          }}
          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

Prop Default Type Description
overlayColor  none  string  Color of the detected rectangle : rgba recommended
detectionCountBeforeCapture  5  integer  Number of correct rectangle to detect before capture
detectionRefreshRateInMS  50  integer  Time between two rectangle detection attempt
enableTorch  false bool  Allows to active or deactivate flash during document detection
useFrontCam  false bool  Allows you to switch between front and back camera
brightness  0  float Increase or decrease camera brightness. Normal as default.
saturation  1  float  Increase or decrease camera saturation. Set 0 for black & white
contrast  1  float  Increase or decrease camera contrast. Normal as default
quality  0.8  float  Image compression. Reduces both image size and quality
useBase64  false  bool  If base64 representation should be passed instead of image uri's
saveInAppDocument  false  bool  If should save in app document in case of not using base 64
captureMultiple  false  bool  Keeps the scanner on after a successful capture
cropperOpts  {}  object  Options for the cropper

Cropper

In order to use the cropper, you must specify onCrop function.

Props Params Type Description
onCrop (data) function Function called when you press the done button of the cropper
onCancelCrop () function Function called when you press the cancel button of the cropper

CropperOpts

Prop Default Type Description
base64  false  bool  If should return base 64 on crop
doneButtonTitle  done  string  Title of done button
cancelButtonTitle  cancel  string  Title of cancel button
saveFile  true bool  If should save file or not
directory  none string  Set a subdirectory where to store cropped file, if saveInAppDocument is enabled it will create it in document directory, otherwise it will create it in temp directory
fileName  none string  Custom fileName, by default it will be called to_cropped_img_TIMESTAMP

Manual capture

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

Handle play/pause on camera

  • First get component ref
<DocumentScanner ref={(ref) => this.scanner = ref} />
  • Then call :
this.scanner.startCamera(true); // If true will start, if false will stop

Each rectangle detection

Props Params Type Description
onRectangleDetect { stableCounter, lastDetectionType } object See 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 0. Correctly formated rectangle

  1. Wrong perspective, bad angle
  2. Too far

Returned image

Prop Params  Type Description
onPictureTaken  data object  Returns the captured image in an object { croppedImage: ('URI or BASE64 string'), initialImage: 'URI or BASE64 string', rectangleCoordinates: 'object of coordinates' }

Save in app document

Demo save document

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

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

If you prefer manual installation

  1. In XCode, in the project navigator, right click Libraries ➜ Add Files to [your project's name]
  2. Go to node_modules ➜ react-native-pdf-scanner and add RNPdfScanner.xcodeproj
  3. In XCode, in the project navigator, select your project. Add libRNPdfScanner.a to your project's Build Phases ➜ Link Binary With Libraries
  4. Run your project (Cmd+R)<

About

Document scanner, features live border detection, perspective correction, image filters and more ! 📲📸

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Objective-C 95.0%
  • JavaScript 2.9%
  • Java 1.1%
  • Other 1.0%