Sign Up for Free

RunKit +

Try any Node.js package right in your browser

This is a playground to test code. It runs a full Node.js environment and already has all of npm’s 400,000 packages pre-installed, including react-native-document-scanner with all npm packages installed. Try it out:

require("react-native/package.json"); // react-native is a peer dependency. var reactNativeDocumentScanner = require("react-native-document-scanner")

This service is provided by RunKit and is not affiliated with npm, Inc or the package authors.

react-native-document-scanner v1.4.2

Scan documents, automatic border detection, automatic crop

Demo gif

React Native Document Scanner (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

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

Demo crop gif

Getting started

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

$ npm install react-native-document-scanner --save

$ 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
          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

PropDefaultTypeDescription
overlayColornonestringColor of the detected rectangle : rgba recommended
detectionCountBeforeCapture5integerNumber of correct rectangle to detect before capture
detectionRefreshRateInMS50integerTime between two rectangle detection attempt
enableTorchfalseboolAllows to active or deactivate flash during document detection
useFrontCamfalseboolAllows you to switch between front and back camera
brightness0floatIncrease or decrease camera brightness. Normal as default.
saturation1floatIncrease or decrease camera saturation. Set 0 for black & white
contrast1floatIncrease or decrease camera contrast. Normal as default
quality0.8floatImage compression. Reduces both image size and quality
useBase64falseboolIf base64 representation should be passed instead of image uri's
captureMultiplefalseboolKeeps the scanner on after a successful capture

Manual capture

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

Each rectangle detection

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

  1. Correctly formated rectangle
  2. Wrong perspective, bad angle
  3. 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' }

If you prefer manual installation

  1. In XCode, in the project navigator, right click LibrariesAdd Files to [your project's name]
  2. Go to node_modulesreact-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 PhasesLink Binary With Libraries
  4. Run your project (Cmd+R)<

Credits

This repo is a React Native implementation of the following native library : https://github.com/mmackh/IPDFCameraViewController Special thank to Mark Peysale :)

RunKit is a free, in-browser JavaScript dev environment for prototyping Node.js code, with every npm package installed. Sign up to share your code.
Sign Up for Free