BarCodeUtil.js
3.74 KB
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
class BarCodeUtil {
static barcode = null;
/**
* @private
*/
static hasLight = false;
/**
* 停止扫码
*/
static stopScan() {
if (this.barcode !== null) {
this.barcode.close();
}
}
/**
* 切换闪光灯
* @return {boolean} 切换后闪光灯是否开启
*/
static switchLight() {
if (this.barcode !== null) {
this.hasLight = !this.hasLight;
this.barcode.setFlash(this.hasLight);
}
return this.hasLight;
}
static typeToString(type) {
const { plus } = window;
// eslint-disable-next-line guard-for-in
for (const key in plus.barcode) {
const value = plus.barcode[key];
if (type === value) {
return key;
}
}
return '未知';
}
/**
* 开始扫码
* @param { (type, code)=>void } successHandler 扫码成功的处理方法
*/
static startScan(successHandler) {
this.stopScan();
let view = null;
const { plus } = window;
const height = plus.screen.resolutionHeight - plus.navigator.getStatusbarHeight();
this.barcode = plus.barcode.create(
'barcode',
[
plus.barcode.QR,
plus.barcode.EAN13,
plus.barcode.EAN8,
plus.barcode.AZTEC,
plus.barcode.DATAMATRIX,
plus.barcode.UPCA,
plus.barcode.UPCE,
plus.barcode.CODABAR,
plus.barcode.CODE39,
plus.barcode.CODE93,
plus.barcode.CODE128,
plus.barcode.ITF,
plus.barcode.MAXICODE,
plus.barcode.PDF417,
plus.barcode.RSS14,
plus.barcode.RSSEXPANDED,
],
{
top: '0',
left: '0px',
width: '100%',
buttom: '0',
position: 'absolute',
zIndex: '1',
},
);
this.barcode.onmarked = (type, result) => {
this.stopScan();
newBaseView.close();
successHandler(type, result);
};
view = new plus.nativeObj.View(
'test',
{
top: '20px', left: '20px', height: '30px', width: '80px',
},
[
{
tag: 'font',
id: 'font',
text: '关闭',
textStyles: { size: '18px', color: '#fff' },
position: {
top: '0px',
left: '0px',
width: '100%',
height: '30px',
},
},
],
);
// view.interceptTouchEvent(true);
// plus.webview.currentWebview().append(view);
const id = Date.now().toString();
const newBaseView = plus.webview.create('', id, { top: '0px', bottom: '0px', height });
newBaseView.append(this.barcode);
newBaseView.append(view);
newBaseView.show();
this.barcode.start();
view.addEventListener('click', () => {
newBaseView.close();
this.barcode.close();
}, false);
}
/* 调用手机摄像头并拍照 */
static getImage() {
const { plus } = window;
const cmr = plus.camera.getCamera();
cmr.captureImage((p) => {
plus.io.resolveLocalFileSystemURL(p, (entry) => {
this.uploadImage(entry.toLocalURL(), entry.name); // 上传拍照的图片
}, (e) => {
plus.nativeUI.toast(`读取拍照文件错误:${e.message}`);
});
}, (e) => {
plus.nativeUI.toast(`调用摄像头错误:${e.message}`);
}, {
filter: 'image',
});
}
/* 相册选择图片 */
static getPhoto() {
const { plus } = window;
plus.gallery.pick((path) => {
const name = path.substring(path.lastIndexOf('/') + 1);
this.uploadImage(path, name);// 上传选择的图片
}, (e) => {
plus.nativeUI.toast(`选取图片错误:${e.message}`);
}, { filter: 'image' });
}
/* 图片上传入库 */
static uploadImage(url, filename) {
console.log('uploadImage:', url, filename);
}
}
export default BarCodeUtil;