Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork95
A React Native document scanner plugin for Android and iOS
License
WebsiteBeaver/react-native-document-scanner-plugin
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
This is a React Native plugin that lets you scan documents using Android and iOS. You can use it to createapps that let users scan notes, homework, business cards, receipts, or anything with a rectangular shape.
| iOS | Android |
|---|---|
![]() | ![]() |
npm install react-native-document-scanner-plugin
After installing the plugin, you need to follow the steps below
- iOS requires the following usage description be added and filled out for your app in
Info.plist:
NSCameraUsageDescription(Privacy - Camera Usage Description)
- Install pods by running
cd ios&& pod install&&cd ..
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. SeeAndroid Camera Permissions.
importReact,{useState,useEffect}from'react'import{Image}from'react-native'importDocumentScannerfrom'react-native-document-scanner-plugin'exportdefault()=>{const[scannedImage,setScannedImage]=useState();constscanDocument=async()=>{// start the document scannerconst{ scannedImages}=awaitDocumentScanner.scanDocument()// get back an array with scanned image file pathsif(scannedImages.length>0){// set the img src, so we can view the first scanned imagesetScannedImage(scannedImages[0])}}useEffect(()=>{// call scanDocument on loadscanDocument()},[]);return(<ImageresizeMode="contain"style={{width:'100%',height:'100%'}}source={{uri:scannedImage}}/>)}
Here's what this example looks like with several items
Dollar.iOS.mp4
Dollar.Android.mp4
Business.Card.iOS.mp4
Business.Card.Android.mp4
Sign.iOS.mp4
Sign.Android.mp4
Notes.iOS.mp4
Notes.Android.mp4
Laptop.iOS.mp4
Laptop.Android.mp4
You can limit the number of scans. For example if your app lets a user scan a businesscard you might want them to only capture the front and back. In this case you can setmaxNumDocuments to 2. This only works on Android.
importReact,{useState,useEffect}from'react'import{Image}from'react-native'importDocumentScannerfrom'react-native-document-scanner-plugin'exportdefault()=>{const[scannedImage,setScannedImage]=useState();constscanDocument=async()=>{// start the document scannerconst{ scannedImages}=awaitDocumentScanner.scanDocument({maxNumDocuments:2})// get back an array with scanned image file pathsif(scannedImages.length>0){// set the img src, so we can view the first scanned imagesetScannedImage(scannedImages[0])}}useEffect(()=>{// call scanDocument on loadscanDocument()},[]);return(<ImageresizeMode="contain"style={{width:'100%',height:'100%'}}source={{uri:scannedImage}}/>)}
Limit.Num.Scans.Android.mp4
scanDocument(options?:ScanDocumentOptions|undefined)=>Promise<ScanDocumentResponse>
Opens the camera, and starts the document scan
| Param | Type |
|---|---|
options | ScanDocumentOptions |
Returns:Promise<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. |
| Prop | Type | Description | Default |
|---|---|---|---|
croppedImageQuality | number | The quality of the cropped image from 0 - 100. 100 is the best quality. | : 100 |
maxNumDocuments | number | Android only: The maximum number of photos an user can take (not counting photo retakes) | : undefined |
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 |
| 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. |
| 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. |
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 react-native-document-scanner-plugin
Addreact-native-document-scanner-plugin to plugins inapp.json orapp.config.json.
{"name":"my expo app","plugins":[["react-native-document-scanner-plugin",{"cameraPermission":"We need camera access, so you can scan documents"}]]}
npx expo prebuild
or
eas build
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'sAndroidManifest.xml.
In that case if you don't request camera permissions you get this errorError: error - error opening camera: Permission Denial: starting Intent { act=android.media.action.IMAGE_CAPTURE
Here's an example of how to request camera permissions.
importReact,{useState,useEffect}from'react'import{Platform,PermissionsAndroid,Image,Alert}from'react-native'importDocumentScannerfrom'react-native-document-scanner-plugin'exportdefault()=>{const[scannedImage,setScannedImage]=useState();constscanDocument=async()=>{// prompt user to accept camera permission request if they haven't alreadyif(Platform.OS==='android'&&awaitPermissionsAndroid.request(PermissionsAndroid.PERMISSIONS.CAMERA)!==PermissionsAndroid.RESULTS.GRANTED){Alert.alert('Error','User must grant camera permissions to use document scanner.')return}// start the document scannerconst{ scannedImages}=awaitDocumentScanner.scanDocument()// get back an array with scanned image file pathsif(scannedImages.length>0){// set the img src, so we can view the first scanned imagesetScannedImage(scannedImages[0])}}useEffect(()=>{// call scanDocument on loadscanDocument()},[]);return(<ImageresizeMode="contain"style={{width:'100%',height:'100%'}}source={{uri:scannedImage}}/>)}
About
A React Native document scanner plugin for Android and iOS
Topics
Resources
License
Code of conduct
Contributing
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Sponsor this project
Uh oh!
There was an error while loading.Please reload this page.
Packages0
Uh oh!
There was an error while loading.Please reload this page.
Contributors7
Uh oh!
There was an error while loading.Please reload this page.

