forked from Michaelvilleneuve/react-native-document-scanner
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
134 lines (118 loc) · 3.85 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
import React from "react";
import {
requireNativeComponent,
NativeModules,
View,
Platform,
PermissionsAndroid,
DeviceEventEmitter,
Text
} from "react-native";
import PropTypes from "prop-types";
const RNPdfScanner = requireNativeComponent("RNPdfScanner", PdfScanner);
const CameraManager = NativeModules.RNPdfScannerManager || {};
class PdfScanner extends React.Component {
constructor(props) {
super(props);
this.state = {
permissionsAuthorized: Platform.OS === "ios"
};
}
onPermissionsDenied = () => {
if (this.props.onPermissionsDenied) this.props.onPermissionsDenied();
};
componentDidMount() {
this.getAndroidPermissions();
}
async getAndroidPermissions() {
if (Platform.OS !== "android") return;
try {
const granted = await PermissionsAndroid.requestMultiple([
PermissionsAndroid.PERMISSIONS.READ_EXTERNAL_STORAGE,
PermissionsAndroid.PERMISSIONS.WRITE_EXTERNAL_STORAGE
]);
if (
granted["android.permission.READ_EXTERNAL_STORAGE"] ===
PermissionsAndroid.RESULTS.GRANTED &&
granted["android.permission.WRITE_EXTERNAL_STORAGE"] ===
PermissionsAndroid.RESULTS.GRANTED
)
this.setState({ permissionsAuthorized: true });
else this.onPermissionsDenied();
} catch (err) {
this.onPermissionsDenied();
}
}
static defaultProps = {
onPictureTaken: () => {},
onProcessing: () => {}
};
sendOnPictureTakenEvent(event) {
return this.props.onPictureTaken(event.nativeEvent);
}
sendOnRectanleDetectEvent(event) {
if (!this.props.onRectangleDetect) return null;
return this.props.onRectangleDetect(event.nativeEvent);
}
getImageQuality() {
if (!this.props.quality) return 0.8;
if (this.props.quality > 1) return 1;
if (this.props.quality < 0.1) return 0.1;
return this.props.quality;
}
componentWillMount() {
if (Platform.OS === "android") {
const { onPictureTaken, onProcessing } = this.props;
DeviceEventEmitter.addListener("onPictureTaken", onPictureTaken);
DeviceEventEmitter.addListener("onProcessingChange", onProcessing);
}
}
componentWillUnmount() {
if (Platform.OS === "android") {
const { onPictureTaken, onProcessing } = this.props;
DeviceEventEmitter.removeListener("onPictureTaken", onPictureTaken);
DeviceEventEmitter.removeListener("onProcessingChange", onProcessing);
}
}
capture() {
// NativeModules.RNPdfScannerManager.capture();
if (this.state.permissionsAuthorized) CameraManager.capture();
}
render() {
if (!this.state.permissionsAuthorized) return null;
return (
<RNPdfScanner
{...this.props}
onPictureTaken={this.sendOnPictureTakenEvent.bind(this)}
onRectangleDetect={this.sendOnRectanleDetectEvent.bind(this)}
useFrontCam={this.props.useFrontCam || false}
brightness={this.props.brightness || 0}
saturation={this.props.saturation || 1}
contrast={this.props.contrast || 1}
quality={this.getImageQuality()}
detectionCountBeforeCapture={
this.props.detectionCountBeforeCapture || 5
}
detectionRefreshRateInMS={this.props.detectionRefreshRateInMS || 50}
/>
);
}
}
PdfScanner.propTypes = {
onPictureTaken: PropTypes.func,
onRectangleDetect: PropTypes.func,
overlayColor: PropTypes.oneOfType([PropTypes.number, PropTypes.string]),
enableTorch: PropTypes.bool,
useFrontCam: PropTypes.bool,
saturation: PropTypes.number,
brightness: PropTypes.number,
contrast: PropTypes.number,
detectionCountBeforeCapture: PropTypes.number,
detectionRefreshRateInMS: PropTypes.number,
quality: PropTypes.number,
documentAnimation: PropTypes.bool,
noGrayScale: PropTypes.bool,
manualOnly: PropTypes.bool,
...View.propTypes // include the default view properties
};
export default PdfScanner;