-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathvideo.js
190 lines (168 loc) · 4.94 KB
/
video.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
/**
* HTML5 Webcam Barcode Reader with Dynamsoft Barcode Reader SDK.
* Support PC & Mobile.
*/
var videoElement = document.querySelector('video');
var canvas = document.getElementById('pcCanvas');
var mobileCanvas = document.getElementById('mobileCanvas');
var ctx = canvas.getContext('2d');
var mobileCtx = mobileCanvas.getContext('2d');
var videoSelect = document.querySelector('select#videoSource');
var videoOption = document.getElementById('videoOption');
var buttonGo = document.getElementById('go');
var barcode_result = document.getElementById('dbr');
var isPaused = false;
var videoWidth = 640,
videoHeight = 480;
var mobileVideoWidth = 240,
mobileVideoHeight = 320;
var isPC = true;
// check devices
function browserRedirect() {
var deviceType;
var sUserAgent = navigator.userAgent.toLowerCase();
var bIsIpad = sUserAgent.match(/ipad/i) == "ipad";
var bIsIphoneOs = sUserAgent.match(/iphone os/i) == "iphone os";
var bIsMidp = sUserAgent.match(/midp/i) == "midp";
var bIsUc7 = sUserAgent.match(/rv:1.2.3.4/i) == "rv:1.2.3.4";
var bIsUc = sUserAgent.match(/ucweb/i) == "ucweb";
var bIsAndroid = sUserAgent.match(/android/i) == "android";
var bIsCE = sUserAgent.match(/windows ce/i) == "windows ce";
var bIsWM = sUserAgent.match(/windows mobile/i) == "windows mobile";
if (bIsIpad || bIsIphoneOs || bIsMidp || bIsUc7 || bIsUc || bIsAndroid || bIsCE || bIsWM) {
deviceType = 'phone';
} else {
deviceType = 'pc';
}
return deviceType;
}
if (browserRedirect() == 'pc') {
isPC = true;
} else {
isPC = false;
}
// stackoverflow: http://stackoverflow.com/questions/4998908/convert-data-uri-to-file-then-append-to-formdata/5100158
function dataURItoBlob(dataURI) {
// convert base64/URLEncoded data component to raw binary data held in a string
var byteString;
if (dataURI.split(',')[0].indexOf('base64') >= 0)
byteString = atob(dataURI.split(',')[1]);
else
byteString = unescape(dataURI.split(',')[1]);
// separate out the mime component
var mimeString = dataURI.split(',')[0].split(':')[1].split(';')[0];
// write the bytes of the string to a typed array
var ia = new Uint8Array(byteString.length);
for (var i = 0; i < byteString.length; i++) {
ia[i] = byteString.charCodeAt(i);
}
return new Blob([ia], {
type: mimeString
});
}
// add button event
buttonGo.onclick = function () {
if (isPC) {
canvas.style.display = 'none';
} else {
mobileCanvas.style.display = 'none';
}
isPaused = false;
scanBarcode();
buttonGo.disabled = true;
};
// scan barcode
function scanBarcode() {
if (isPaused) {
return;
}
var data = null,
context = null,
width = 0,
height = 0,
dbrCanvas = null;
if (isPC) {
context = ctx;
width = videoWidth;
height = videoHeight;
dbrCanvas = canvas;
} else {
context = mobileCtx;
width = mobileVideoWidth;
height = mobileVideoHeight;
dbrCanvas = mobileCanvas;
}
context.drawImage(videoElement, 0, 0, width, height);
// convert canvas to base64
var base64 = dbrCanvas.toDataURL('image/png', 1.0);
var imgData = base64.replace(/^data:image\/(png|jpeg|jpg);base64,/, "");
// var imgData = JSON.stringify(data);
$.ajax({
url: '/dbr',
dataType: 'json',
data: imgData,
type: 'POST',
success: function (result) {
console.log(result);
if (isPaused) {
return;
}
var barcode_result = document.getElementById('dbr');
barcode_result.textContent = result;
// display barcode result
if (result.indexOf("No barcode") == -1) {
isPaused = true;
buttonGo.disabled = false;
if (isPC) {
canvas.style.display = 'block';
} else {
mobileCanvas.style.display = 'block';
}
}
else {
setTimeout(scanBarcode, 200);
}
}
});
}
// https://github.com/samdutton/simpl/tree/gh-pages/getusermedia/sources
navigator.mediaDevices.enumerateDevices()
.then(gotDevices).then(getStream).catch(handleError);
videoSelect.onchange = getStream;
function gotDevices(deviceInfos) {
for (var i = deviceInfos.length - 1; i >= 0; --i) {
var deviceInfo = deviceInfos[i];
var option = document.createElement('option');
option.value = deviceInfo.deviceId;
if (deviceInfo.kind === 'videoinput') {
option.text = deviceInfo.label || 'camera ' +
(videoSelect.length + 1);
videoSelect.appendChild(option);
} else {
console.log('Found one other kind of source/device: ', deviceInfo);
}
}
}
function getStream() {
if (window.stream) {
window.stream.getTracks().forEach(function (track) {
track.stop();
});
}
var constraints = {
video: {
deviceId: {
exact: videoSelect.value
}
}
};
navigator.mediaDevices.getUserMedia(constraints).
then(gotStream).catch(handleError);
}
function gotStream(stream) {
window.stream = stream;
videoElement.srcObject = stream;
}
function handleError(error) {
console.log('Error: ', error);
}