forked from slims/slims9_bulian
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscanner.js
executable file
·295 lines (273 loc) · 7.59 KB
/
scanner.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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
/**
*
* Webcam feature
*
* Require : jQuery library
*
* by Indra Sutriadi Pipii 2013
*
*/
var scan_available = false;
var socket = null;
var part = 0;
var dataUrl2;
var imgArea;
var scan_host;
var scan_port;
var scan_res_x;
var scan_res_y;
var scan_capture_w;
var scan_capture_h;
var scan_max_w;
var scan_max_h;
function preview(img, _sel) {
if (!_sel.width || !_sel.height)
return;
scanvas.width = _sel.width;
scanvas.height = _sel.height;
scontext.drawImage(sdata, _sel.x1, _sel.y1, _sel.width, _sel.height, 0, 0, _sel.width, _sel.height);
scan_type();
}
function checkws() {
if (window.WebSocket) {
console.log('WebSocket is supported by your browser.');
scan_available = true;
}
else {
console.log('Websocket is not supported by your browser.');
scan_available = false;
$('#scan_dialog').empty();
$('#scan_dialog').text('This feature not supported by your browser.');
}
}
function openws(msg, opts) {
if (scan_available) {
//~ var serviceUrl = 'ws://localhost:8100/';
//~ var serviceUrl = $('#url').val();
var serviceUrl = 'ws://' + $('#scan_host').val() + ':' + $('#scan_port').val();
socket = new WebSocket(serviceUrl);
socket.binaryType = "blob";
socket.onopen = function () {
console.log('Connection established!');
switch (msg) {
case 'init':
console.log('Get available machines...');
break;
case 'scan':
console.log('Scan with machine: '+ $('#scan_machine').text());
break;
case 'recall':
console.log('Recall scanned image...');
break;
default:
console.log('Unknown command!');
}
sendmsg(msg + '#' + opts);
//~ closews();
};
socket.onclose = function () {
console.log('Connection closed!');
};
socket.onerror = function (error) {
console.log('Error occured: ' + error);
};
socket.onmessage = function (e) {
if (typeof e.data === "string") {
var rec = e.data;
switch (rec) {
case "880":
case "881":
case "882":
case "883":
case "884":
case "885":
case "886":
case "887":
case "888":
case "889":
console.log('Scanner is being used by another process or client.');
return;
default:
var n = rec.split('#');
}
}
else if (e.data instanceof ArrayBuffer) {
console.log('ArrayBuffer received: ' + e.data);
}
else if (e.data instanceof Blob) {
console.log('Blob received: ' + e.data.bytes + ' bytes');
}
switch (msg) {
case 'init':
$('#scan_machine').empty();
if (n.length > 0) {
for (var i = 0; i < n.length; i++) {
$('#scan_machine').append('<option value=' + i + '>' + n[i] + '</option>');
}
console.log('Init finished!');
}
else {
console.log('Machine not found!');
}
break;
case 'recall':
if (!rec) {
console.log('Recall failed!');
}
else {
console.log('Recall finished!');
sdata.src = rec;
}
case 'scan':
if (!rec) {
console.log('Scan failed!');
}
else {
console.log('Scan finished!');
sdata.src = rec;
}
break;
default:
console.log('Unknown result!');
}
closews();
};
}
}
function sendmsg(msg) {
if (socket != null) {
socket.send(msg);
}
};
function closews() {
socket.close();
}
function less_select() {
imgArea = $('img#my_imgdata').imgAreaSelect({ instance: true });
imgArea.cancelSelection();
}
function scan_reset() {
less_select();
sdata.src = "";
scanvas.width = 0;
scanvas.height = 0;
dataUrl2 = '';
$('textarea#base64picstring').empty();
}
function scan_init() {
scan_reset();
openws('init');
}
function scan() {
scan_reset();
var options = new Array(
$('#scan_machine').val(),
$('#scan_res_x').val() + ',' + $('#scan_res_y').val(),
$('#scan_capture_w').val() + ',' + $('#scan_capture_h').val(),
$('#scan_max_w').val() + ',' + $('#scan_max_h').val(),
$('#scan_type').val()
);
openws('scan', options.join('#'));
}
function scan_recall() {
scan_reset();
index = $('#scan_history').val();
openws('recall', (Math.floor(index) == index && $.isNumeric(index)) ? index : 1);
}
function scan_type() {
if (scanvas.width > 0 && scanvas.height > 0) {
switch ($('#scan_type').val()) {
case "png":
dataUrl2 = scanvas.toDataURL();
dataUrl2 = dataUrl2.split('data:image/png;base64,')[1];
dataUrl2 = dataUrl2 + '#image/type#' + 'png';
break;
case "jpg":
dataUrl2 = scanvas.toDataURL("image/jpeg");
dataUrl2 = dataUrl2.split('data:image/jpeg;base64,')[1];
dataUrl2 = dataUrl2 + '#image/type#' + 'jpg';
break;
}
$('textarea#base64picstring').text(dataUrl2);
}
}
function scan_rotate(type) {
var srotate = scanvas.getContext('2d');
var cw = scanvas.width;
var ch = scanvas.height;
var tmpcanvas = document.createElement("canvas");
var tmpctx = tmpcanvas.getContext("2d");
if (cw > ch) {
ch = cw;
}
else {
cw = ch;
}
tmpcanvas.width = cw;
tmpcanvas.height = ch;
tmpctx.drawImage(scanvas, 0, 0);
if (type == "right") {
tmpctx.translate(scanvas.height, 0);
tmpctx.rotate(90 * Math.PI / 180);
}
else {
tmpctx.translate(0, scanvas.width);
tmpctx.rotate(-90 * Math.PI / 180);
}
tmpctx.drawImage(tmpcanvas, 0, 0);
cw = scanvas.width;
ch = scanvas.height;
scanvas.width = ch;
scanvas.height = cw;
srotate.drawImage(tmpcanvas, 0, 0);
scan_type();
delete tmpctx;
delete tmpcanvas;
}
function toggle_options() {
$('#scan_options').toggleClass('makeHidden');
$('#scan_container').toggleClass('makeHidden');
less_select();
}
function toggle_dialog() {
var $scanDialog = $('#scan_dialog');
top.$.colorbox({
inline: true,
href: $scanDialog,
title: $scanDialog.attr('title'),
onOpen: function() {
$('#scan_options').addClass('makeHidden');
$('#scan_container').removeClass('makeHidden');
},
onComplete: function() {
checkws();
sdata = document.querySelector('#my_imgdata');
scanvas = document.querySelector('#my_selected');
scontext = scanvas.getContext('2d');
scanvas.width = 0;
scanvas.height = 0;
$('img#my_imgdata').imgAreaSelect({
handles: true,
onSelectEnd: preview,
});
},
onCleanup: function() {
less_select();
},
});
}
function toggle_search(query) {
let win = window;
let h = 640;
let w = 800;
const y = win.top.outerHeight / 2 + win.top.screenY - ( h / 2);
const x = win.top.outerWidth / 2 + win.top.screenX - ( w / 2);
let url = 'https://duckduckgo.com/?q='+query+'+book&t=h_&ia=images&iax=images';
let title = 'DuckduckGo Search Result';
if(query !== '') {
win.open(url, title, 'toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=no, resizable=no, copyhistory=no, width='+w+', height='+h+', top='+y+', left='+x);
win.focus();
} else {
parent.toastr.error("No title available", "Bibliography", {"closeButton":true,"debug":false,"newestOnTop":false,"progressBar":false,"positionClass":"toast-top-right","preventDuplicates":false,"onclick":null,"showDuration":300,"hideDuration":1000,"timeOut":5000,"extendedTimeOut":1000,"showEasing":"swing","hideEasing":"linear","showMethod":"fadeIn","hideMethod":"fadeOut"})
}
}