Skip to content

Commit 97e6fea

Browse files
authored
add android camera permission request example (#38)
1 parent 525685c commit 97e6fea

File tree

1 file changed

+63
-1
lines changed

1 file changed

+63
-1
lines changed

README.md

Lines changed: 63 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ apps that let users scan notes, homework, business cards, receipts, or anything
1313

1414
```bash
1515
npm install react-native-document-scanner-plugin
16-
cd ios && pod install && cd ..
1716
```
1817

1918
After installing the plugin, you need to follow the steps below
@@ -26,10 +25,17 @@ After installing the plugin, you need to follow the steps below
2625

2726
- `NSCameraUsageDescription` (`Privacy - Camera Usage Description`)
2827

28+
3. Install pods by running
29+
```bash
30+
cd ios && pod install && cd ..
31+
```
32+
2933
### Android
3034

3135
1. Open `android/gradle.properties` and add `org.gradle.jvmargs=-Xmx2048m`
3236

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+
3339
## Examples
3440

3541
* [Basic Example](#basic-example)
@@ -284,6 +290,62 @@ or
284290
eas build
285291
```
286292

293+
## Common Mistakes
294+
295+
* [Android Camera Permissions](#android-camera-permissions)
296+
297+
### Android Camera Permissions
298+
299+
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`.
300+
301+
In that case if you don't request camera permissions you get this error
302+
`Error: error - error opening camera: Permission Denial: starting Intent { act=android.media.action.IMAGE_CAPTURE`
303+
304+
Here's an example of how to request camera permissions.
305+
306+
```javascript
307+
import React, { useState, useEffect } from 'react'
308+
import { Platform, PermissionsAndroid, Image, Alert } from 'react-native'
309+
import DocumentScanner from 'react-native-document-scanner-plugin'
310+
311+
export default () => {
312+
const [scannedImage, setScannedImage] = useState();
313+
314+
const scanDocument = async () => {
315+
// prompt user to accept camera permission request if they haven't already
316+
if (Platform.OS === 'android' && await PermissionsAndroid.request(
317+
PermissionsAndroid.PERMISSIONS.CAMERA
318+
) !== PermissionsAndroid.RESULTS.GRANTED) {
319+
Alert.alert('Error', 'User must grant camera permissions to use document scanner.')
320+
return
321+
}
322+
323+
// start the document scanner
324+
const { scannedImages } = await DocumentScanner.scanDocument()
325+
326+
// get back an array with scanned image file paths
327+
if (scannedImages.length > 0) {
328+
// set the img src, so we can view the first scanned image
329+
setScannedImage(scannedImages[0])
330+
}
331+
}
332+
333+
useEffect(() => {
334+
// call scanDocument on load
335+
scanDocument()
336+
}, []);
337+
338+
return (
339+
<Image
340+
resizeMode="contain"
341+
style={{ width: '100%', height: '100%' }}
342+
source={{ uri: scannedImage }}
343+
/>
344+
)
345+
}
346+
```
347+
348+
287349
## License
288350

289351
Copyright 2022 David Marcus

0 commit comments

Comments
 (0)