-
Notifications
You must be signed in to change notification settings - Fork 66
/
index.js
262 lines (243 loc) · 7.06 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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
// @author Adam G. Freeman, [email protected]
import { NativeModules, requireNativeComponent, View, UIManager, Platform, PermissionsAndroid } from 'react-native';
import React, { Component } from 'react';
import PropTypes from 'prop-types';
const { RNOpencv3 } = NativeModules;
import { ColorConv, CvType, Imgproc, Core } from './constants';
import { CvScalar, CvPoint, CvSize, CvRect } from './coretypes';
import { Mat, MatOfInt, MatOfFloat, setTo, get } from './mats';
import { CvImage } from './cvimage';
import { findNodeHandle } from 'react-native';
const CvCameraView = requireNativeComponent('CvCameraView', CvCamera);
var RNFS = require('react-native-fs')
class CvCamera extends Component {
constructor(props) {
super(props)
this.state = {
cameraPermissed: Platform.OS === 'ios' ? true : PermissionsAndroid.PERMISSIONS.CAMERA
}
}
componentDidMount = () => {
if (Platform.OS === 'android') this.requestCameraPermissions()
}
async requestCameraPermissions() {
try {
let granted = false
if (this.props.useStorage) {
granted = await PermissionsAndroid.requestMultiple([
PermissionsAndroid.PERMISSIONS.CAMERA,
PermissionsAndroid.PERMISSIONS.READ_EXTERNAL_STORAGE,
PermissionsAndroid.PERMISSIONS.WRITE_EXTERNAL_STORAGE
])
}
else {
granted = await PermissionsAndroid.request(PermissionsAndroid.PERMISSIONS.CAMERA)
}
if (granted === PermissionsAndroid.RESULTS.GRANTED) {
const retJson = await NativeModules.CvCameraModule.initCamera(findNodeHandle(this))
if (retJson.cameraInitialized) {
this.setState({cameraPermissed:true})
}
} else {
console.log('Camera permission denied')
}
} catch (err) {
console.warn(err)
}
}
setOverlay(overlayMat) {
if (Platform.OS === 'android') {
UIManager.dispatchViewManagerCommand(
findNodeHandle(this),
UIManager.getViewManagerConfig('CvCameraView').Commands.setOverlay,
[overlayMat],
)
}
else {
const options = { 'overlayMat' : overlayMat }
NativeModules.CvCameraView.setOverlay(options, findNodeHandle(this))
}
}
async takePicture(filename) {
const outputFilename = RNFS.DocumentDirectoryPath + '/' + filename
const pictureOptions = { 'filename' : outputFilename }
if (Platform.OS === 'android') {
return await NativeModules.CvCameraModule.takePicture(pictureOptions, findNodeHandle(this))
}
else {
return await NativeModules.CvCameraView.takePicture(pictureOptions, findNodeHandle(this))
}
}
startRecording(filename) {
var outputFilename = RNFS.DocumentDirectoryPath + '/' + filename
if (Platform.OS === 'android') {
outputFilename = RNFS.ExternalStorageDirectoryPath + '/' + filename
}
const pictureOptions = { 'filename' : outputFilename }
if (Platform.OS === 'android') {
NativeModules.CvCameraModule.startRecording(pictureOptions, findNodeHandle(this))
}
else {
NativeModules.CvCameraView.startRecording(pictureOptions, findNodeHandle(this))
}
}
async stopRecording() {
if (Platform.OS === 'android') {
return await NativeModules.CvCameraModule.stopRecording(findNodeHandle(this))
}
else {
return await NativeModules.CvCameraView.stopRecording(findNodeHandle(this))
}
}
render() {
if (!this.state.cameraPermissed) return (<View/>)
return (<CvCameraView {...this.props} />);
}
}
CvCamera.propTypes = {
...View.propTypes,
facing: PropTypes.string,
useStorage: PropTypes.bool
};
class CvInvokeGroup extends Component {
static propTypes = {
children: PropTypes.any.isRequired,
groupid: PropTypes.string.isRequired
}
constructor(props) {
super(props)
}
renderChildren() {
const { children, groupid, cvinvoke } = this.props
let ins = []
let functions = []
let paramsArr = []
let outs = []
let callbacks = []
let groupids = []
if (cvinvoke && cvinvoke.ins) {
ins = cvinvoke.ins
}
if (cvinvoke && cvinvoke.functions) {
functions = cvinvoke.functions
}
if (cvinvoke && cvinvoke.paramsArr) {
paramsArr = cvinvoke.paramsArr
}
if (cvinvoke && cvinvoke.outs) {
outs = cvinvoke.outs
}
if (cvinvoke && cvinvoke.callbacks) {
callbacks = cvinvoke.callbacks
}
if (cvinvoke && cvinvoke.groupids) {
groupids = cvinvoke.groupids
}
const offspring = React.Children.map(children,
(child,i) => {
if (child.type.displayName === 'CvInvoke') {
const {inobj, func, params, outobj, callback} = child.props
ins.push(inobj) // can be nil
functions.push(func)
paramsArr.push(params)
outs.push(outobj) // can be nil
callbacks.push(callback) // can be nil
groupids.push(groupid)
}
else {
return React.cloneElement(child, {
// pass info down to the next CvInvokeGroup or to the CvCamera
...child.props, "cvinvoke" : { "ins" : ins, "functions" : functions, "paramsArr": paramsArr, "outs" : outs, "callbacks": callbacks, "groupids": groupids }
})
}
})
return offspring
}
render() {
return (
<React.Fragment>
{this.renderChildren()}
</React.Fragment>
)
}
}
class CvInvoke extends Component {
static propTypes = {
inobj: PropTypes.string,
func: PropTypes.string.isRequired,
params: PropTypes.any,
outobj: PropTypes.string,
callback: PropTypes.string
}
constructor(props) {
super(props)
}
renderChildren() {
const { cvinvoke, inobj, func, params, outobj, callback, children } = this.props
if (children) {
let newins = []
let newfunctions = []
let newparamsarr = []
let newouts = []
let newcallbacks = []
if (cvinvoke && cvinvoke.ins) {
newins = cvinvoke.ins
}
if (cvinvoke && cvinvoke.functions) {
newfunctions = cvinvoke.functions
}
if (cvinvoke && cvinvoke.paramsArr) {
newparamsarr = cvinvoke.paramsArr
}
if (cvinvoke && cvinvoke.outs) {
newouts = cvinvoke.outs
}
if (cvinvoke && cvinvoke.callbacks) {
newcallbacks = cvinvoke.callbacks
}
newins.push(inobj)
newfunctions.push(func)
newparamsarr.push(params)
newouts.push(outobj)
newcallbacks.push(callback)
const newKidsOnTheBlock = React.Children.map(children,
(child,i) => React.cloneElement(child, {
// pass info down to the CvCamera
...child.props, "cvinvoke" : { "ins" : newins, "functions" : newfunctions, "paramsArr": newparamsarr, "outs" : newouts, "callbacks": newcallbacks }
})
)
return newKidsOnTheBlock
}
else {
return (
<CvInvoke inobj={inobj} func={func} params={params} outobj={outobj} callback={callback}/>
)
}
}
render() {
return(
<React.Fragment>
{this.renderChildren()}
</React.Fragment>
)
}
}
const RNCv = RNOpencv3
export {
RNCv,
CvImage,
CvCamera,
CvInvoke,
CvInvokeGroup,
ColorConv,
CvType,
Imgproc,
Core,
Mat,
MatOfInt,
MatOfFloat,
CvScalar,
CvPoint,
CvSize,
CvRect
};