-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest-cameraloadtime.html
121 lines (107 loc) · 3.47 KB
/
test-cameraloadtime.html
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
<!DOCTYPE html>
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
<title>test</title>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/dbr.js"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/eruda.min.js"></script>
<style>
.native-video {
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
}
</style>
</head>
<body>
<button>dbr</button>
<button>native</button>
<button>clear localStorage</button>
<video class="native-video" id="video" style="display:none;"></video>
<script>
eruda.init();
let btns = document.querySelectorAll('button')
let video = document.getElementById('video');
btns[0].addEventListener('click', async()=>{
let scanner = await Dynamsoft.DBR.BarcodeScanner.createInstance();
/* scanner.dce.updateVideoSettings({
video:{
width:{ideal:1280},
height:{ideal:720},
// facingMode: 'environment',
"facingMode": {
"exact": "environment"
},
}
}); */
scanner.onFrameRead = results => {
if (results.length > 0) console.log(results);
};
scanner.onUnduplicatedRead = (txt, result) => {
alert(txt);
};
let bFirst = true;
let bestDevice = localStorage.getItem('dynamsoft-bestDevice');
let _old_getAllCameras;
if(bestDevice){
// 2rd time will enter here
console.log(`it's 2rd time, best DeviceId is ${bestDevice}`)
_old_getAllCameras = scanner.dce.getAllCameras
scanner.dce.getAllCameras = ()=>{
const devices = [{deviceId: bestDevice ,label:'camera2 0, facing back',_checked:true}];
scanner.dce._allCameras = devices;
return devices;
};
scanner.dce.funcGetCurrentCamera = () =>{
}
scanner.dce.updateVideoSettings({
video:{
width:{ideal:1280},
height:{ideal:720},
_passCameraChoose: true
}
});
bFirst = false;
} else{
console.log(`it's 1st time`)
}
let beginTime = Date.now()
await scanner.show();
console.log((Date.now() - beginTime) + 'ms');
// save best device into localStorage
bestDevice = scanner.dce._lastDeviceId;
if(bestDevice){
localStorage.setItem('dynamsoft-bestDevice',bestDevice);
console.log(`bestDevice ${bestDevice} had saved.`)
}
if(!bFirst){
scanner.dce.getAllCameras = _old_getAllCameras;
await scanner.dce.getAllCameras();
scanner.dce._renderSelCameraInfo();
}
})
btns[1].addEventListener('click', async () => {
const nativeTime = Date.now();
const stream = await navigator.mediaDevices.getUserMedia({
video: {
width:{ideal:1280},
height:{ideal:720}
}
});
video.onloadedmetadata = ()=>{
video.onloadedmetadata = null;
console.log((Date.now() - nativeTime) + 'ms');
video.style.display = ''
};
video.srcObject = stream;
video.play();
})
btns[2].addEventListener('click', () => {
localStorage.removeItem('dynamsoft-bestDevice')
})
</script>
</body>
</html>