-
Notifications
You must be signed in to change notification settings - Fork 18
/
index.js
123 lines (103 loc) · 3.26 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
/**
* Created by qiepeipei on 18/8/25.
*/
'use strict';
import {
DeviceEventEmitter,
NativeModules,
PermissionsAndroid,
NativeEventEmitter,
Platform,
ImageEditor,
} from 'react-native'
let baiduEventEmitterIos = new NativeEventEmitter(NativeModules.BaiDuOcrModule);
class BaiDuOcr{
constructor () {
this.baiduCallBack = null;
this.baiduOcrEventIos = null;
this.baiduOcrEventAndroid = null;
}
requestAndroidPermissions() {
return new Promise((resolve, reject)=>{
if (Platform.OS === 'android') {
var permissions = [
PermissionsAndroid.PERMISSIONS.CAMERA,
PermissionsAndroid.PERMISSIONS.READ_EXTERNAL_STORAGE,
PermissionsAndroid.PERMISSIONS.WRITE_EXTERNAL_STORAGE
];
try {
PermissionsAndroid.requestMultiple(permissions).then(granted => {
resolve(true);
});
} catch (err) {
resolve(false);
}
} else if (Platform.OS === 'ios') {
resolve(true);
}
})
}
//初始化
init(ak,sk,callBack){
NativeModules.BaiDuOcrModule.initOcr(ak,sk,callBack);
}
//调用相机
callCamera(callBack){
this.baiduCallBack = callBack;
NativeModules.BaiDuOcrModule.callCamera();
}
//调用相册
callAlbum(callBack){
this.baiduCallBack = callBack;
NativeModules.BaiDuOcrModule.callAlbum();
}
//调用识别二维码
callQRCode(callBack){
this.baiduCallBack = callBack;
NativeModules.BaiDuOcrModule.callQRCode();
}
//生成二维码
createQRCode(jsonStr, callBack){
this.baiduCallBack = callBack;
NativeModules.BaiDuOcrModule.createQRCode(jsonStr);
}
//识别 type CAMERA 银行卡 CARD_FRONT 身份证正面 CARD_BACK 身份证反面
ocr(type,callBack){
this.baiduCallBack = callBack;
let myType = type || "";
NativeModules.BaiDuOcrModule.callOcr(myType);
}
//监听
addListener(){
if(Platform.OS === "ios"){
this.baiduOcrEventIos = baiduEventEmitterIos.addListener(
'BaiDuOcrEmitter',
(data) =>{
console.log("输出监听回调");
console.log(data);
if(this.baiduCallBack){
this.baiduCallBack(data);
this.baiduCallBack = null;
}
});
}else{
this.baiduOcrEventAndroid = DeviceEventEmitter.addListener("BaiDuOcrEmitter",(data)=>{
console.log("输出监听回调");
console.log(data);
if(this.baiduCallBack){
this.baiduCallBack(data);
this.baiduCallBack = null;
}
})
}
}
//移除监听
removeListener(){
if(Platform.OS === "ios"){
this.baiduOcrEventIos && this.baiduOcrEventIos.remove();
}else{
this.baiduOcrEventAndroid && this.baiduOcrEventAndroid.remove();
}
}
}
export default new BaiDuOcr();