1
- //
1
+ //@flow
2
2
// NodeCameraModule.js
3
3
//
4
4
// Created by Mingliang Chen on 2017/11/29.
5
+ // Upgraded by Anh Tuan Nguyen on 2018/08/06.
5
6
// Copyright © 2017年 NodeMedia. All rights reserved.
6
7
//
7
8
8
- import React , { Component } from 'react' ;
9
- import { PropTypes } from 'prop-types' ;
10
- import { requireNativeComponent , View , UIManager , findNodeHandle } from 'react-native' ;
11
-
12
-
9
+ import React , { PureComponent } from "react"
10
+ import {
11
+ requireNativeComponent ,
12
+ UIManager ,
13
+ findNodeHandle ,
14
+ ViewPropTypes
15
+ } from "react-native"
13
16
14
- var RCT_VIDEO_REF = 'NodeCameraView' ;
17
+ type Camera = {
18
+ cameraId : 0 | 1 ,
19
+ cameraFrontMirror : boolean
20
+ }
21
+ type Audio = {
22
+ bitrate : number ,
23
+ profile : 0 | 1 | 2 ,
24
+ samplerate : 8000 | 16000 | 32000 | 44100 | 48000
25
+ }
26
+ type Video = {
27
+ preset : number ,
28
+ bitrate : number ,
29
+ profile : 0 | 1 | 2 ,
30
+ fps : 15 | 20 | 24 | 30 ,
31
+ videoFrontMirror : boolean
32
+ }
33
+ type Props = {
34
+ outputUrl : string ,
35
+ camera : Camera ,
36
+ audio : Audio ,
37
+ video : Video ,
38
+ autopreview : boolean ,
39
+ denoise : boolean ,
40
+ smoothSkinLevel : 0 | 1 | 2 | 3 | 4 | 5 ,
41
+ onStatus : Function ,
42
+ ...ViewPropTypes
43
+ }
44
+ class NodeCameraView extends PureComponent < Props > {
45
+ name : string = "NodeCameraView"
46
+ videoRef : any = React . createRef ( )
15
47
16
- class NodeCameraView extends Component {
17
- constructor ( props ) {
18
- super ( props ) ;
19
- }
20
- _onChange ( event ) {
48
+ _onChange ( event : Object ) {
21
49
if ( ! this . props . onStatus ) {
22
- return ;
50
+ return
23
51
}
24
- this . props . onStatus ( event . nativeEvent . code , event . nativeEvent . message ) ;
52
+ this . props . onStatus ( event . nativeEvent . code , event . nativeEvent . message )
25
53
}
26
54
27
55
switchCamera ( ) {
28
56
UIManager . dispatchViewManagerCommand (
29
- findNodeHandle ( this . refs [ RCT_VIDEO_REF ] ) ,
57
+ findNodeHandle ( this . videoRef . current ) ,
30
58
UIManager . RCTNodeCamera . Commands . switchCamera ,
31
59
null
32
- ) ;
60
+ )
33
61
}
34
62
35
63
flashEnable ( enable ) {
36
64
UIManager . dispatchViewManagerCommand (
37
- findNodeHandle ( this . refs [ RCT_VIDEO_REF ] ) ,
65
+ findNodeHandle ( this . videoRef . current ) ,
38
66
UIManager . RCTNodeCamera . Commands . flashEnable ,
39
67
[ enable ]
40
- ) ;
68
+ )
41
69
}
42
70
43
71
startPreview ( ) {
44
72
UIManager . dispatchViewManagerCommand (
45
- findNodeHandle ( this . refs [ RCT_VIDEO_REF ] ) ,
73
+ findNodeHandle ( this . videoRef . current ) ,
46
74
UIManager . RCTNodeCamera . Commands . startprev ,
47
75
null
48
- ) ;
76
+ )
49
77
}
50
78
51
79
stopPreview ( ) {
52
80
UIManager . dispatchViewManagerCommand (
53
- findNodeHandle ( this . refs [ RCT_VIDEO_REF ] ) ,
81
+ findNodeHandle ( this . videoRef . current ) ,
54
82
UIManager . RCTNodeCamera . Commands . stopprev ,
55
83
null
56
- ) ;
84
+ )
57
85
}
58
86
59
87
start ( ) {
60
88
UIManager . dispatchViewManagerCommand (
61
- findNodeHandle ( this . refs [ RCT_VIDEO_REF ] ) ,
89
+ findNodeHandle ( this . videoRef . current ) ,
62
90
UIManager . RCTNodeCamera . Commands . start ,
63
91
null
64
- ) ;
92
+ )
65
93
}
66
94
67
95
stop ( ) {
68
96
UIManager . dispatchViewManagerCommand (
69
- findNodeHandle ( this . refs [ RCT_VIDEO_REF ] ) ,
97
+ findNodeHandle ( this . videoRef . current ) ,
70
98
UIManager . RCTNodeCamera . Commands . stop ,
71
99
null
72
- ) ;
100
+ )
73
101
}
74
102
75
103
render ( ) {
76
- return < RCTNodeCamera
77
- { ...this . props }
78
- ref = { RCT_VIDEO_REF }
79
- onChange = { this . _onChange . bind ( this ) }
80
- /> ;
81
- } ;
104
+ return (
105
+ < RCTNodeCamera
106
+ { ...this . props }
107
+ ref = { this . videoRef }
108
+ onChange = { this . _onChange . bind ( this ) }
109
+ />
110
+ )
111
+ }
82
112
}
83
113
84
- NodeCameraView . name = RCT_VIDEO_REF ;
85
- NodeCameraView . propTypes = {
86
- outputUrl : PropTypes . string ,
87
- camera : PropTypes . shape ( {
88
- cameraId : PropTypes . oneOf ( [ 0 , 1 ] ) ,
89
- cameraFrontMirror : PropTypes . bool
90
- } ) ,
91
- audio : PropTypes . shape ( {
92
- bitrate : PropTypes . number ,
93
- profile : PropTypes . oneOf ( [ 0 , 1 , 2 ] ) ,
94
- samplerate : PropTypes . oneOf ( [ 8000 , 16000 , 32000 , 44100 , 48000 ] ) ,
95
- } ) ,
96
- video : PropTypes . shape ( {
97
- preset : PropTypes . number ,
98
- bitrate : PropTypes . number ,
99
- profile : PropTypes . oneOf ( [ 0 , 1 , 2 ] ) ,
100
- fps : PropTypes . oneOf ( [ 15 , 20 , 24 , 30 ] ) ,
101
- videoFrontMirror : PropTypes . bool
102
- } ) ,
103
- autopreview :PropTypes . bool ,
104
- denoise : PropTypes . bool ,
105
- smoothSkinLevel : PropTypes . oneOf ( [ 0 , 1 , 2 , 3 , 4 , 5 ] ) ,
106
- onStatus : PropTypes . func ,
107
- ...View . propTypes // 包含默认的View的属性
108
- } ;
109
-
110
- const RCTNodeCamera = requireNativeComponent ( 'RCTNodeCamera' , NodeCameraView , {
114
+ const RCTNodeCamera = requireNativeComponent ( "RCTNodeCamera" , NodeCameraView , {
111
115
nativeOnly : { onChange : true }
112
- } ) ;
116
+ } )
113
117
114
- module . exports = NodeCameraView ;
118
+ export default NodeCameraView
0 commit comments