forked from react-native-camera/react-native-camera
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
276 lines (238 loc) · 7.54 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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import {
DeviceEventEmitter, // android
NativeAppEventEmitter, // ios
NativeModules,
Platform,
StyleSheet,
requireNativeComponent,
View,
ViewPropTypes
} from 'react-native';
const CameraManager = NativeModules.CameraManager || NativeModules.CameraModule;
const CAMERA_REF = 'camera';
function convertNativeProps(props) {
const newProps = { ...props };
if (typeof props.aspect === 'string') {
newProps.aspect = Camera.constants.Aspect[props.aspect];
}
if (typeof props.flashMode === 'string') {
newProps.flashMode = Camera.constants.FlashMode[props.flashMode];
}
if (typeof props.orientation === 'string') {
newProps.orientation = Camera.constants.Orientation[props.orientation];
}
if (typeof props.torchMode === 'string') {
newProps.torchMode = Camera.constants.TorchMode[props.torchMode];
}
if (typeof props.type === 'string') {
newProps.type = Camera.constants.Type[props.type];
}
if (typeof props.captureQuality === 'string') {
newProps.captureQuality = Camera.constants.CaptureQuality[props.captureQuality];
}
if (typeof props.captureMode === 'string') {
newProps.captureMode = Camera.constants.CaptureMode[props.captureMode];
}
if (typeof props.captureTarget === 'string') {
newProps.captureTarget = Camera.constants.CaptureTarget[props.captureTarget];
}
// do not register barCodeTypes if no barcode listener
if (typeof props.onBarCodeRead !== 'function') {
newProps.barCodeTypes = [];
}
newProps.barcodeScannerEnabled = typeof props.onBarCodeRead === 'function'
return newProps;
}
export default class Camera extends Component {
static constants = {
Aspect: CameraManager.Aspect,
BarCodeType: CameraManager.BarCodeType,
Type: CameraManager.Type,
CaptureMode: CameraManager.CaptureMode,
CaptureTarget: CameraManager.CaptureTarget,
CaptureQuality: CameraManager.CaptureQuality,
Orientation: CameraManager.Orientation,
FlashMode: CameraManager.FlashMode,
TorchMode: CameraManager.TorchMode
};
static propTypes = {
...ViewPropTypes,
aspect: PropTypes.oneOfType([
PropTypes.string,
PropTypes.number
]),
captureAudio: PropTypes.bool,
captureMode: PropTypes.oneOfType([
PropTypes.string,
PropTypes.number
]),
captureQuality: PropTypes.oneOfType([
PropTypes.string,
PropTypes.number
]),
captureTarget: PropTypes.oneOfType([
PropTypes.string,
PropTypes.number
]),
defaultOnFocusComponent: PropTypes.bool,
flashMode: PropTypes.oneOfType([
PropTypes.string,
PropTypes.number
]),
keepAwake: PropTypes.bool,
onBarCodeRead: PropTypes.func,
barcodeScannerEnabled: PropTypes.bool,
onFocusChanged: PropTypes.func,
onZoomChanged: PropTypes.func,
mirrorImage: PropTypes.bool,
fixOrientation: PropTypes.bool,
barCodeTypes: PropTypes.array,
orientation: PropTypes.oneOfType([
PropTypes.string,
PropTypes.number
]),
playSoundOnCapture: PropTypes.bool,
torchMode: PropTypes.oneOfType([
PropTypes.string,
PropTypes.number
]),
type: PropTypes.oneOfType([
PropTypes.string,
PropTypes.number
])
};
static defaultProps = {
aspect: CameraManager.Aspect.fill,
type: CameraManager.Type.back,
orientation: CameraManager.Orientation.auto,
fixOrientation: false,
captureAudio: false,
captureMode: CameraManager.CaptureMode.still,
captureTarget: CameraManager.CaptureTarget.cameraRoll,
captureQuality: CameraManager.CaptureQuality.high,
defaultOnFocusComponent: true,
flashMode: CameraManager.FlashMode.off,
playSoundOnCapture: true,
torchMode: CameraManager.TorchMode.off,
mirrorImage: false,
barCodeTypes: Object.values(CameraManager.BarCodeType),
};
static checkDeviceAuthorizationStatus = CameraManager.checkDeviceAuthorizationStatus;
static checkVideoAuthorizationStatus = CameraManager.checkVideoAuthorizationStatus;
static checkAudioAuthorizationStatus = CameraManager.checkAudioAuthorizationStatus;
setNativeProps(props) {
this.refs[CAMERA_REF].setNativeProps(props);
}
constructor() {
super();
this.state = {
isAuthorized: false,
isRecording: false
};
}
async componentWillMount() {
this._addOnBarCodeReadListener()
let { captureMode } = convertNativeProps({ captureMode: this.props.captureMode })
let hasVideoAndAudio = this.props.captureAudio && captureMode === Camera.constants.CaptureMode.video
let check = hasVideoAndAudio ? Camera.checkDeviceAuthorizationStatus : Camera.checkVideoAuthorizationStatus;
if (check) {
const isAuthorized = await check();
this.setState({ isAuthorized });
}
}
componentWillUnmount() {
this._removeOnBarCodeReadListener()
if (this.state.isRecording) {
this.stopCapture();
}
}
componentWillReceiveProps(newProps) {
const { onBarCodeRead } = this.props
if (onBarCodeRead !== newProps.onBarCodeRead) {
this._addOnBarCodeReadListener(newProps)
}
}
_addOnBarCodeReadListener(props) {
const { onBarCodeRead } = props || this.props
this._removeOnBarCodeReadListener()
if (onBarCodeRead) {
this.cameraBarCodeReadListener = Platform.select({
ios: NativeAppEventEmitter.addListener('CameraBarCodeRead', this._onBarCodeRead),
android: DeviceEventEmitter.addListener('CameraBarCodeReadAndroid', this._onBarCodeRead)
})
}
}
_removeOnBarCodeReadListener() {
const listener = this.cameraBarCodeReadListener
if (listener) {
listener.remove()
}
}
render() {
const style = [styles.base, this.props.style];
const nativeProps = convertNativeProps(this.props);
return <RCTCamera ref={CAMERA_REF} {...nativeProps} />;
}
_onBarCodeRead = (data) => {
if (this.props.onBarCodeRead) {
this.props.onBarCodeRead(data)
}
};
capture(options) {
const props = convertNativeProps(this.props);
options = {
audio: props.captureAudio,
barCodeTypes: props.barCodeTypes,
mode: props.captureMode,
playSoundOnCapture: props.playSoundOnCapture,
target: props.captureTarget,
quality: props.captureQuality,
type: props.type,
title: '',
description: '',
mirrorImage: props.mirrorImage,
fixOrientation: props.fixOrientation,
...options
};
if (options.mode === Camera.constants.CaptureMode.video) {
options.totalSeconds = (options.totalSeconds > -1 ? options.totalSeconds : -1);
options.preferredTimeScale = options.preferredTimeScale || 30;
this.setState({ isRecording: true });
}
return CameraManager.capture(options);
}
stopCapture() {
if (this.state.isRecording) {
this.setState({ isRecording: false });
return CameraManager.stopCapture();
}
return Promise.resolve("Not Recording.");
}
getFOV() {
return CameraManager.getFOV();
}
hasFlash() {
if (Platform.OS === 'android') {
const props = convertNativeProps(this.props);
return CameraManager.hasFlash({
type: props.type
});
}
return CameraManager.hasFlash();
}
}
export const constants = Camera.constants;
const RCTCamera = requireNativeComponent('RCTCamera', Camera, {nativeOnly: {
testID: true,
renderToHardwareTextureAndroid: true,
accessibilityLabel: true,
importantForAccessibility: true,
accessibilityLiveRegion: true,
accessibilityComponentType: true,
onLayout: true
}});
const styles = StyleSheet.create({
base: {},
});