-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.android.js
101 lines (82 loc) · 2.76 KB
/
index.android.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
import React, { Component } from 'react';
import { requireNativeComponent, NativeModules, View, TouchableHighlight } from 'react-native';
import PropTypes from 'prop-types'
const ocrReaderModule = NativeModules['OcrReaderModule'];
const TEXT_READ = "text_read";
const LOW_STORAGE_EXCEPTION = "low_storage";
const NOT_YET_OPERATIONAL_EXCEPTION = "not_yet_operational";
const NO_PLAY_SERVICES_EXCEPTION = 'no_play_services';
class OcrReader extends Component {
static propTypes = {
onTextRead: PropTypes.func, // Callback that fires whenever a new ocr is read
onException: PropTypes.func, // function(reason)
focusMode: PropTypes.number, // int
cameraFillMode: PropTypes.number, // int
...View.propTypes
};
constructor(props) {
super(props)
this._onChange = this._onChange.bind(this);
}
componentWillMount() {
resumeReader()
.then(() => {
console.log("OcrReader was resumed on component mount.");
})
.catch(e => {
console.log(e);
});
}
componentWillUnmount() {
pauseReader()
.then(() => {
console.log("OcrReader was paused on component mount.");
})
.catch(e => {
console.log(e);
});
}
_onChange(event: Event) {
// Kirim data dari Native ke Javascript
switch (event.nativeEvent.key) {
case TEXT_READ:
const onTextRead = this.props.onTextRead;
if (onTextRead) {
onTextRead({
data: event.nativeEvent.data,
});
}
break;
case NOT_YET_OPERATIONAL_EXCEPTION:
case LOW_STORAGE_EXCEPTION:
case NO_PLAY_SERVICES_EXCEPTION:
if (this.props.onException) this.props.onException(event.nativeEvent.key);
break;
}
}
render() {
return (
<NativeOcrReader
{...this.props}
onChange={this._onChange}
/>
);
}
}
// Ambil Native View Manager dan Connect ke OcrReader (Public class Javascript)
const NativeOcrReader = requireNativeComponent('RCTOcrReaderManager', OcrReader, {
nativeOnly: {onChange: true}
});
/* --------------------------------------
* ------------- Exports ----------------
* --------------------------------------
*/
// Alternatives: AUTO, TAP, FIXED. Note: focusMode TAP won't work if you place a view on top of OcrReader, that catches all touch events.
export const FocusMode = ocrReaderModule.FocusMode;
// Alternatives: COVER, FIT
export const CameraFillMode = ocrReaderModule.CameraFillMode;
export const Exception = { LOW_STORAGE: LOW_STORAGE_EXCEPTION, NOT_OPERATIONAL: NOT_YET_OPERATIONAL_EXCEPTION, NO_PLAY_SERVICES: NO_PLAY_SERVICES_EXCEPTION };
// Mapping fungsi Native ke Javascript
export const pauseReader = ocrReaderModule.pause;
export const resumeReader = ocrReaderModule.resume;
export default OcrReader;