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 ) ;
614const CameraManager = NativeModules . RNPdfScannerManager || { } ;
715
816class 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
89134export default PdfScanner ;
0 commit comments