1
1
2
2
// @author Adam G. Freeman, adamgf@gmail.com
3
- import { NativeModules , requireNativeComponent , View , UIManager , Platform } from 'react-native' ;
3
+ import { NativeModules , requireNativeComponent , View , UIManager , Platform , PermissionsAndroid } from 'react-native' ;
4
4
5
5
import React , { Component } from 'react' ;
6
6
import PropTypes from 'prop-types' ;
@@ -19,7 +19,32 @@ var RNFS = require('react-native-fs')
19
19
class CvCamera extends Component {
20
20
constructor ( props ) {
21
21
super ( props )
22
- this . cvCamera = React . createRef ( )
22
+ this . cvCamera = React . createRef ( )
23
+ if ( Platform . OS === 'android' && ! PermissionsAndroid . PERMISSIONS . CAMERA ) this . requestCameraPermission ( )
24
+ this . state = {
25
+ cameraPermissed : Platform . OS === 'ios' ? true : PermissionsAndroid . PERMISSIONS . CAMERA
26
+ }
27
+ }
28
+ async requestCameraPermission ( ) {
29
+ const defaultOptions = {
30
+ title : 'Permission to use camera' ,
31
+ message : 'We need your permission to use your camera' ,
32
+ buttonPositive : 'Ok' ,
33
+ buttonNegative : 'Cancel' ,
34
+ }
35
+ try {
36
+ const granted = await PermissionsAndroid . request (
37
+ PermissionsAndroid . PERMISSIONS . CAMERA ,
38
+ this . props . androidCameraPermissionOptions || defaultOptions ,
39
+ )
40
+ if ( granted === PermissionsAndroid . RESULTS . GRANTED ) {
41
+ this . setState ( { cameraPermissed :true } )
42
+ } else {
43
+ console . log ( 'Camera permission denied' )
44
+ }
45
+ } catch ( err ) {
46
+ console . warn ( err )
47
+ }
23
48
}
24
49
setOverlay ( overlayMat ) {
25
50
if ( Platform . OS === 'android' ) {
@@ -37,11 +62,11 @@ class CvCamera extends Component {
37
62
async takePicture ( filename ) {
38
63
const outputFilename = RNFS . DocumentDirectoryPath + '/' + filename
39
64
const pictureOptions = { 'filename' : outputFilename }
40
-
65
+
41
66
if ( Platform . OS === 'android' ) {
42
- return await NativeModules . CvCameraModule . takePicture ( pictureOptions , findNodeHandle ( this ) )
67
+ return await NativeModules . CvCameraModule . takePicture ( pictureOptions , findNodeHandle ( this ) )
43
68
}
44
- else {
69
+ else {
45
70
return await NativeModules . CvCameraView . takePicture ( pictureOptions , findNodeHandle ( this ) )
46
71
}
47
72
}
@@ -51,30 +76,32 @@ class CvCamera extends Component {
51
76
outputFilename = RNFS . ExternalStorageDirectoryPath + '/' + filename
52
77
}
53
78
const pictureOptions = { 'filename' : outputFilename }
54
-
79
+
55
80
if ( Platform . OS === 'android' ) {
56
- NativeModules . CvCameraModule . startRecording ( pictureOptions , findNodeHandle ( this ) )
81
+ NativeModules . CvCameraModule . startRecording ( pictureOptions , findNodeHandle ( this ) )
57
82
}
58
- else {
83
+ else {
59
84
NativeModules . CvCameraView . startRecording ( pictureOptions , findNodeHandle ( this ) )
60
85
}
61
86
}
62
- async stopRecording ( ) {
87
+ async stopRecording ( ) {
63
88
if ( Platform . OS === 'android' ) {
64
- return await NativeModules . CvCameraModule . stopRecording ( findNodeHandle ( this ) )
89
+ return await NativeModules . CvCameraModule . stopRecording ( findNodeHandle ( this ) )
65
90
}
66
- else {
91
+ else {
67
92
return await NativeModules . CvCameraView . stopRecording ( findNodeHandle ( this ) )
68
93
}
69
94
}
70
95
render ( ) {
96
+ if ( ! this . state . cameraPermissed ) return ( < View /> )
71
97
return ( < CvCameraView ref = { this . cvCamera } { ...this . props } /> ) ;
72
98
}
73
99
}
74
100
75
101
CvCamera . propTypes = {
76
102
...View . propTypes ,
77
103
facing : PropTypes . string ,
104
+ androidCameraPermissionOptions : PropTypes . object
78
105
} ;
79
106
80
107
class CvInvokeGroup extends Component {
0 commit comments