Skip to content

Commit 2fd3deb

Browse files
committed
Ask for Android permissions before firing capture function
Add a callback action for denied permissions
1 parent b3ba43e commit 2fd3deb

File tree

2 files changed

+73
-28
lines changed

2 files changed

+73
-28
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,8 @@
22

33
# React Native Document Scanner
44

5-
65
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 !
76

8-
97
Features :
108

119
- Live detection
@@ -100,6 +98,7 @@ class YourComponent extends Component {
10098
}
10199
detectionCountBeforeCapture={5}
102100
detectionRefreshRateInMS={50}
101+
onPermissionsDenied={() => console.log("Permissions Denied")}
103102
/>
104103
<Image
105104
source={{ uri: `data:image/jpeg;base64,${this.state.image}` }}
@@ -127,6 +126,7 @@ class YourComponent extends Component {
127126
| useBase64 | iOS | `false` | `bool` | If base64 representation should be passed instead of image uri's |
128127
| saveInAppDocument | iOS | `false` | `bool` | If should save in app document in case of not using base 64 |
129128
| captureMultiple | iOS | `false` | `bool` | Keeps the scanner on after a successful capture |
129+
| onPermissionsDenied | android | `null` | `func` | Function to call when the Android permissions are denied |
130130

131131
## Manual capture
132132

index.js

Lines changed: 71 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,60 @@
1-
import React from 'react';
2-
import { requireNativeComponent, NativeModules, View, Platform, DeviceEventEmitter } from 'react-native';
3-
import PropTypes from 'prop-types';
1+
import React from "react";
2+
import {
3+
requireNativeComponent,
4+
NativeModules,
5+
View,
6+
Platform,
7+
PermissionsAndroid,
8+
DeviceEventEmitter,
9+
Text
10+
} from "react-native";
11+
import PropTypes from "prop-types";
412

5-
const RNPdfScanner = requireNativeComponent('RNPdfScanner', PdfScanner);
13+
const RNPdfScanner = requireNativeComponent("RNPdfScanner", PdfScanner);
614
const CameraManager = NativeModules.RNPdfScannerManager || {};
715

816
class PdfScanner extends React.Component {
17+
constructor(props) {
18+
super(props);
19+
this.state = {
20+
permissionsAuthorized: Platform.OS === "ios"
21+
};
22+
}
923

10-
static defaultProps = {
11-
onPictureTaken: ()=>{},
12-
onProcessing: ()=>{},
24+
onPermissionsDenied = () => {
25+
if (this.props.onPermissionsDenied) this.props.onPermissionsDenied();
26+
};
27+
28+
componentDidMount() {
29+
this.getAndroidPermissions();
1330
}
1431

32+
async getAndroidPermissions() {
33+
if (Platform.OS !== "android") return;
34+
try {
35+
const granted = await PermissionsAndroid.requestMultiple([
36+
PermissionsAndroid.PERMISSIONS.READ_EXTERNAL_STORAGE,
37+
PermissionsAndroid.PERMISSIONS.WRITE_EXTERNAL_STORAGE
38+
]);
39+
40+
if (
41+
granted["android.permission.READ_EXTERNAL_STORAGE"] ===
42+
PermissionsAndroid.RESULTS.GRANTED &&
43+
granted["android.permission.WRITE_EXTERNAL_STORAGE"] ===
44+
PermissionsAndroid.RESULTS.GRANTED
45+
)
46+
this.setState({ permissionsAuthorized: true });
47+
else this.onPermissionsDenied();
48+
} catch (err) {
49+
this.onPermissionsDenied();
50+
}
51+
}
52+
53+
static defaultProps = {
54+
onPictureTaken: () => {},
55+
onProcessing: () => {}
56+
};
57+
1558
sendOnPictureTakenEvent(event) {
1659
return this.props.onPictureTaken(event.nativeEvent);
1760
}
@@ -28,40 +71,43 @@ class PdfScanner extends React.Component {
2871
return this.props.quality;
2972
}
3073

31-
componentWillMount(){
32-
if (Platform.OS === 'android') {
74+
componentWillMount() {
75+
if (Platform.OS === "android") {
3376
const { onPictureTaken, onProcessing } = this.props;
34-
DeviceEventEmitter.addListener('onPictureTaken', onPictureTaken);
35-
DeviceEventEmitter.addListener('onProcessingChange', onProcessing);
77+
DeviceEventEmitter.addListener("onPictureTaken", onPictureTaken);
78+
DeviceEventEmitter.addListener("onProcessingChange", onProcessing);
3679
}
3780
}
38-
39-
componentWillUnmount(){
40-
if (Platform.OS === 'android') {
81+
82+
componentWillUnmount() {
83+
if (Platform.OS === "android") {
4184
const { onPictureTaken, onProcessing } = this.props;
42-
DeviceEventEmitter.removeListener('onPictureTaken', onPictureTaken);
43-
DeviceEventEmitter.removeListener('onProcessingChange', onProcessing);
85+
DeviceEventEmitter.removeListener("onPictureTaken", onPictureTaken);
86+
DeviceEventEmitter.removeListener("onProcessingChange", onProcessing);
4487
}
4588
}
4689

4790
capture() {
4891
// NativeModules.RNPdfScannerManager.capture();
49-
CameraManager.capture();
92+
if (this.state.permissionsAuthorized) CameraManager.capture();
5093
}
5194

5295
render() {
96+
if (!this.state.permissionsAuthorized) return null;
5397
return (
5498
<RNPdfScanner
5599
{...this.props}
56100
onPictureTaken={this.sendOnPictureTakenEvent.bind(this)}
57101
onRectangleDetect={this.sendOnRectanleDetectEvent.bind(this)}
58-
useFrontCam={this.props.useFrontCam||false}
59-
brightness={this.props.brightness||0}
60-
saturation={this.props.saturation||1}
61-
contrast={this.props.contrast||1}
102+
useFrontCam={this.props.useFrontCam || false}
103+
brightness={this.props.brightness || 0}
104+
saturation={this.props.saturation || 1}
105+
contrast={this.props.contrast || 1}
62106
quality={this.getImageQuality()}
63-
detectionCountBeforeCapture={this.props.detectionCountBeforeCapture||5}
64-
detectionRefreshRateInMS={this.props.detectionRefreshRateInMS||50}
107+
detectionCountBeforeCapture={
108+
this.props.detectionCountBeforeCapture || 5
109+
}
110+
detectionRefreshRateInMS={this.props.detectionRefreshRateInMS || 50}
65111
/>
66112
);
67113
}
@@ -79,11 +125,10 @@ PdfScanner.propTypes = {
79125
detectionCountBeforeCapture: PropTypes.number,
80126
detectionRefreshRateInMS: PropTypes.number,
81127
quality: PropTypes.number,
82-
83-
documentAnimation : PropTypes.bool,
128+
documentAnimation: PropTypes.bool,
84129
noGrayScale: PropTypes.bool,
85130
manualOnly: PropTypes.bool,
86-
...View.propTypes // include the default view properties
131+
...View.propTypes // include the default view properties
87132
};
88133

89134
export default PdfScanner;

0 commit comments

Comments
 (0)