|
| 1 | +# React Native Document Scanner |
| 2 | + |
| 3 | +[](https://www.npmjs.com/package/react-native-document-scanner-plugin) [](https://github.com/WebsiteBeaver/react-native-document-scanner-plugin/blob/master/package.json) |
| 4 | + |
| 5 | +This is a React Native plugin that lets you scan documents using Android and iOS. You can use it to create |
| 6 | +apps that let users scan notes, homework, business cards, receipts, or anything with a rectangular shape. |
| 7 | + |
| 8 | +| iOS | Android | |
| 9 | +| -------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------ | |
| 10 | +|  |  | |
| 11 | + |
| 12 | +## Install |
| 13 | + |
| 14 | +```bash |
| 15 | +npm install react-native-document-scanner-plugin |
| 16 | +``` |
| 17 | + |
| 18 | +After installing the plugin, you need to follow the steps below |
| 19 | + |
| 20 | +### iOS |
| 21 | + |
| 22 | +1. Open `ios/Podfile` and set `platform :ios` to `13` or higher |
| 23 | + |
| 24 | +2. iOS requires the following usage description be added and filled out for your app in `Info.plist`: |
| 25 | + |
| 26 | +- `NSCameraUsageDescription` (`Privacy - Camera Usage Description`) |
| 27 | + |
| 28 | +3. Install pods by running |
| 29 | +```bash |
| 30 | +cd ios && pod install && cd .. |
| 31 | +``` |
| 32 | + |
| 33 | +### Android |
| 34 | + |
| 35 | +1. Open `android/gradle.properties` and add `org.gradle.jvmargs=-Xmx2048m` |
| 36 | + |
| 37 | +**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](#android-camera-permissions). |
| 38 | + |
| 39 | +## Examples |
| 40 | + |
| 41 | +* [Basic Example](#basic-example) |
| 42 | +* [Limit Number of Scans](#limit-number-of-scans) |
| 43 | + |
| 44 | +### Basic Example |
| 45 | + |
| 46 | +```javascript |
| 47 | +import React, { useState, useEffect } from 'react' |
| 48 | +import { Image } from 'react-native' |
| 49 | +import DocumentScanner from 'react-native-document-scanner-plugin' |
| 50 | + |
| 51 | +export default () => { |
| 52 | + const [scannedImage, setScannedImage] = useState(); |
| 53 | + |
| 54 | + const scanDocument = async () => { |
| 55 | + // start the document scanner |
| 56 | + const { scannedImages } = await DocumentScanner.scanDocument() |
| 57 | + |
| 58 | + // get back an array with scanned image file paths |
| 59 | + if (scannedImages.length > 0) { |
| 60 | + // set the img src, so we can view the first scanned image |
| 61 | + setScannedImage(scannedImages[0]) |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + useEffect(() => { |
| 66 | + // call scanDocument on load |
| 67 | + scanDocument() |
| 68 | + }, []); |
| 69 | + |
| 70 | + return ( |
| 71 | + <Image |
| 72 | + resizeMode="contain" |
| 73 | + style={{ width: '100%', height: '100%' }} |
| 74 | + source={{ uri: scannedImage }} |
| 75 | + /> |
| 76 | + ) |
| 77 | +} |
| 78 | +``` |
| 79 | + |
| 80 | +Here's what this example looks like with several items |
| 81 | + |
| 82 | +<video src="https://user-images.githubusercontent.com/26162804/160264220-0a77a55c-33b1-492a-9617-6d2c083b0583.mp4" data-canonical-src="https://user-images.githubusercontent.com/26162804/160264220-0a77a55c-33b1-492a-9617-6d2c083b0583.mp4" controls="controls" muted="muted" class="d-block rounded-bottom-2 border-top width-fit" style="max-height:640px;"></video> |
| 83 | + |
| 84 | +<video src="https://user-images.githubusercontent.com/26162804/160264222-bef1ba3d-d6c1-43c8-ba2e-77ff5baef836.mp4" data-canonical-src="https://user-images.githubusercontent.com/26162804/160264222-bef1ba3d-d6c1-43c8-ba2e-77ff5baef836.mp4" controls="controls" muted="muted" class="d-block rounded-bottom-2 border-top width-fit" style="max-height:640px;"></video> |
| 85 | + |
| 86 | +<video src="https://user-images.githubusercontent.com/26162804/161643046-57536193-0c6c-4edf-8f29-6f3ef9854dc5.mp4" data-canonical-src="https://user-images.githubusercontent.com/26162804/161643046-57536193-0c6c-4edf-8f29-6f3ef9854dc5.mp4" controls="controls" muted="muted" class="d-block rounded-bottom-2 border-top width-fit" style="max-height:640px;"></video> |
| 87 | + |
| 88 | +<video src="https://user-images.githubusercontent.com/26162804/161643075-365b5008-4bc8-4507-969d-b2c188f372ec.mp4" data-canonical-src="https://user-images.githubusercontent.com/26162804/161643075-365b5008-4bc8-4507-969d-b2c188f372ec.mp4" controls="controls" muted="muted" class="d-block rounded-bottom-2 border-top width-fit" style="max-height:640px;"></video> |
| 89 | + |
| 90 | +<video src="https://user-images.githubusercontent.com/26162804/161643102-35283536-73a3-4b05-bd76-c06514ca3928.mp4" data-canonical-src="https://user-images.githubusercontent.com/26162804/161643102-35283536-73a3-4b05-bd76-c06514ca3928.mp4" controls="controls" muted="muted" class="d-block rounded-bottom-2 border-top width-fit" style="max-height:640px;"></video> |
| 91 | + |
| 92 | +<video src="https://user-images.githubusercontent.com/26162804/161643126-f5c2461d-768d-481c-8dee-4d74a0cae778.mp4" data-canonical-src="https://user-images.githubusercontent.com/26162804/161643126-f5c2461d-768d-481c-8dee-4d74a0cae778.mp4" controls="controls" muted="muted" class="d-block rounded-bottom-2 border-top width-fit" style="max-height:640px;"></video> |
| 93 | + |
| 94 | +<video src="https://user-images.githubusercontent.com/26162804/161643156-4ce1abac-d78b-4211-a99a-f0bebd40e2a6.mp4" data-canonical-src="https://user-images.githubusercontent.com/26162804/161643156-4ce1abac-d78b-4211-a99a-f0bebd40e2a6.mp4" controls="controls" muted="muted" class="d-block rounded-bottom-2 border-top width-fit" style="max-height:640px;"></video> |
| 95 | + |
| 96 | +<video src="https://user-images.githubusercontent.com/26162804/161643167-fc751455-1a1a-4b1c-b06f-a3a2cef0d0b0.mp4" data-canonical-src="https://user-images.githubusercontent.com/26162804/161643167-fc751455-1a1a-4b1c-b06f-a3a2cef0d0b0.mp4" controls="controls" muted="muted" class="d-block rounded-bottom-2 border-top width-fit" style="max-height:640px;"></video> |
| 97 | + |
| 98 | +<video src="https://user-images.githubusercontent.com/26162804/161643192-71db71af-392d-4b6a-b94d-851a3369dbf3.mp4" data-canonical-src="https://user-images.githubusercontent.com/26162804/161643192-71db71af-392d-4b6a-b94d-851a3369dbf3.mp4" controls="controls" muted="muted" class="d-block rounded-bottom-2 border-top width-fit" style="max-height:640px;"></video> |
| 99 | + |
| 100 | +<video src="https://user-images.githubusercontent.com/26162804/161643203-2a265cc1-5cf1-4474-b43c-7b1b2dcba704.mp4" data-canonical-src="https://user-images.githubusercontent.com/26162804/161643203-2a265cc1-5cf1-4474-b43c-7b1b2dcba704.mp4" controls="controls" muted="muted" class="d-block rounded-bottom-2 border-top width-fit" style="max-height:640px;"></video> |
| 101 | + |
| 102 | +### Limit Number of Scans |
| 103 | + |
| 104 | +You can limit the number of scans. For example if your app lets a user scan a business |
| 105 | +card you might want them to only capture the front and back. In this case you can set |
| 106 | +maxNumDocuments to 2. This only works on Android. |
| 107 | + |
| 108 | +```javascript |
| 109 | +import React, { useState, useEffect } from 'react' |
| 110 | +import { Image } from 'react-native' |
| 111 | +import DocumentScanner from 'react-native-document-scanner-plugin' |
| 112 | + |
| 113 | +export default () => { |
| 114 | + const [scannedImage, setScannedImage] = useState(); |
| 115 | + |
| 116 | + const scanDocument = async () => { |
| 117 | + // start the document scanner |
| 118 | + const { scannedImages } = await DocumentScanner.scanDocument({ |
| 119 | + maxNumDocuments: 2 |
| 120 | + }) |
| 121 | + |
| 122 | + // get back an array with scanned image file paths |
| 123 | + if (scannedImages.length > 0) { |
| 124 | + // set the img src, so we can view the first scanned image |
| 125 | + setScannedImage(scannedImages[0]) |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + useEffect(() => { |
| 130 | + // call scanDocument on load |
| 131 | + scanDocument() |
| 132 | + }, []); |
| 133 | + |
| 134 | + return ( |
| 135 | + <Image |
| 136 | + resizeMode="contain" |
| 137 | + style={{ width: '100%', height: '100%' }} |
| 138 | + source={{ uri: scannedImage }} |
| 139 | + /> |
| 140 | + ) |
| 141 | +} |
| 142 | +``` |
| 143 | + |
| 144 | +<video src="https://user-images.githubusercontent.com/26162804/161643345-6fe15f33-9414-46f5-b5d5-24d88948e801.mp4" data-canonical-src="https://user-images.githubusercontent.com/26162804/161643345-6fe15f33-9414-46f5-b5d5-24d88948e801.mp4" controls="controls" muted="muted" class="d-block rounded-bottom-2 border-top width-fit" style="max-height:640px;"></video> |
| 145 | + |
| 146 | +## Documentation |
| 147 | + |
| 148 | +* [`scanDocument(...)`](#scandocument) |
| 149 | +* [Interfaces](#interfaces) |
| 150 | +* [Enums](#enums) |
| 151 | + |
| 152 | +### scanDocument(...) |
| 153 | + |
| 154 | +```typescript |
| 155 | +scanDocument(options?: ScanDocumentOptions | undefined) => Promise<ScanDocumentResponse> |
| 156 | +``` |
| 157 | + |
| 158 | +Opens the camera, and starts the document scan |
| 159 | + |
| 160 | +| Param | Type | |
| 161 | +| ------------- | ------------------------------------------------------------------- | |
| 162 | +| **`options`** | <code><a href="#scandocumentoptions">ScanDocumentOptions</a></code> | |
| 163 | + |
| 164 | +**Returns:** <code>Promise<<a href="#scandocumentresponse">ScanDocumentResponse</a>></code> |
| 165 | + |
| 166 | +-------------------- |
| 167 | + |
| 168 | + |
| 169 | +### Interfaces |
| 170 | + |
| 171 | + |
| 172 | +#### ScanDocumentResponse |
| 173 | + |
| 174 | +| Prop | Type | Description | |
| 175 | +| ------------------- | --------------------------------------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------- | |
| 176 | +| **`scannedImages`** | <code>string[]</code> | This is an array with either file paths or base64 images for the document scan. | |
| 177 | +| **`status`** | <code><a href="#scandocumentresponsestatus">ScanDocumentResponseStatus</a></code> | The status lets you know if the document scan completes successfully, or if the user cancels before completing the document scan. | |
| 178 | + |
| 179 | + |
| 180 | +#### ScanDocumentOptions |
| 181 | + |
| 182 | +| Prop | Type | Description | Default | |
| 183 | +| ----------------------- | ----------------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ----------------------------------------- | |
| 184 | +| **`croppedImageQuality`** | <code>number</code> | The quality of the cropped image from 0 - 100. 100 is the best quality. | <code>: 100</code> | |
| 185 | +| **`maxNumDocuments`** | <code>number</code> | Android only: The maximum number of photos an user can take (not counting photo retakes) | <code>: undefined</code> | |
| 186 | +| **`responseType`** | <code><a href="#responsetype">ResponseType</a></code> | The response comes back in this format on success. It can be the document scan image file paths or base64 images. | <code>: ResponseType.ImageFilePath</code> | |
| 187 | + |
| 188 | + |
| 189 | +### Enums |
| 190 | + |
| 191 | + |
| 192 | +#### ScanDocumentResponseStatus |
| 193 | + |
| 194 | +| Members | Value | Description | |
| 195 | +| ------------- | ---------------------- | --------------------------------------------------------------------------------------------------------- | |
| 196 | +| **`Success`** | <code>'success'</code> | The status comes back as success if the document scan completes successfully. | |
| 197 | +| **`Cancel`** | <code>'cancel'</code> | The status comes back as cancel if the user closes out of the camera before completing the document scan. | |
| 198 | + |
| 199 | + |
| 200 | +#### ResponseType |
| 201 | + |
| 202 | +| Members | Value | Description | |
| 203 | +| ------------------- | ---------------------------- | ------------------------------------------------------------------------------- | |
| 204 | +| **`Base64`** | <code>'base64'</code> | Use this response type if you want document scan returned as base64 images. | |
| 205 | +| **`ImageFilePath`** | <code>'imageFilePath'</code> | Use this response type if you want document scan returned as inmage file paths. | |
| 206 | + |
| 207 | +## Expo |
| 208 | + |
| 209 | +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. |
| 210 | + |
| 211 | +```bash |
| 212 | +npx expo install react-native-document-scanner-plugin |
| 213 | +``` |
| 214 | + |
| 215 | +Add `react-native-document-scanner-plugin` to plugins in `app.json` or `app.config.json`. |
| 216 | + |
| 217 | +```javascript |
| 218 | +{ |
| 219 | + "name": "my expo app", |
| 220 | + "plugins": [ |
| 221 | + [ |
| 222 | + "react-native-document-scanner-plugin", |
| 223 | + { |
| 224 | + "cameraPermission": "We need camera access, so you can scan documents" |
| 225 | + } |
| 226 | + ] |
| 227 | + ] |
| 228 | +} |
| 229 | +``` |
| 230 | + |
| 231 | +```bash |
| 232 | +npx expo prebuild |
| 233 | +``` |
| 234 | +or |
| 235 | +```bash |
| 236 | +eas build |
| 237 | +``` |
| 238 | + |
| 239 | +## Common Mistakes |
| 240 | + |
| 241 | +* [Android Camera Permissions](#android-camera-permissions) |
| 242 | + |
| 243 | +### Android Camera Permissions |
| 244 | + |
| 245 | +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`. |
| 246 | + |
| 247 | +In that case if you don't request camera permissions you get this error |
| 248 | +`Error: error - error opening camera: Permission Denial: starting Intent { act=android.media.action.IMAGE_CAPTURE` |
| 249 | + |
| 250 | +Here's an example of how to request camera permissions. |
| 251 | + |
| 252 | +```javascript |
| 253 | +import React, { useState, useEffect } from 'react' |
| 254 | +import { Platform, PermissionsAndroid, Image, Alert } from 'react-native' |
| 255 | +import DocumentScanner from 'react-native-document-scanner-plugin' |
| 256 | + |
| 257 | +export default () => { |
| 258 | + const [scannedImage, setScannedImage] = useState(); |
| 259 | + |
| 260 | + const scanDocument = async () => { |
| 261 | + // prompt user to accept camera permission request if they haven't already |
| 262 | + if (Platform.OS === 'android' && await PermissionsAndroid.request( |
| 263 | + PermissionsAndroid.PERMISSIONS.CAMERA |
| 264 | + ) !== PermissionsAndroid.RESULTS.GRANTED) { |
| 265 | + Alert.alert('Error', 'User must grant camera permissions to use document scanner.') |
| 266 | + return |
| 267 | + } |
| 268 | + |
| 269 | + // start the document scanner |
| 270 | + const { scannedImages } = await DocumentScanner.scanDocument() |
| 271 | + |
| 272 | + // get back an array with scanned image file paths |
| 273 | + if (scannedImages.length > 0) { |
| 274 | + // set the img src, so we can view the first scanned image |
| 275 | + setScannedImage(scannedImages[0]) |
| 276 | + } |
| 277 | + } |
| 278 | + |
| 279 | + useEffect(() => { |
| 280 | + // call scanDocument on load |
| 281 | + scanDocument() |
| 282 | + }, []); |
| 283 | + |
| 284 | + return ( |
| 285 | + <Image |
| 286 | + resizeMode="contain" |
| 287 | + style={{ width: '100%', height: '100%' }} |
| 288 | + source={{ uri: scannedImage }} |
| 289 | + /> |
| 290 | + ) |
| 291 | +} |
| 292 | +``` |
| 293 | + |
| 294 | +## License |
| 295 | + |
| 296 | +Copyright 2021 David Marcus |
| 297 | + |
| 298 | +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: |
| 299 | + |
| 300 | +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. |
| 301 | + |
| 302 | +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. |
0 commit comments