-
Notifications
You must be signed in to change notification settings - Fork 68
/
tip-01-try-default-then-select.htm
143 lines (117 loc) · 5.48 KB
/
tip-01-try-default-then-select.htm
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
<!DOCTYPE html>
<html lang="en">
<head>
<title>Scanner.js Tip: Scan Using "default" Source With "select" as Fallback</title>
<meta charset='utf-8'>
<script src="https://cdn.asprise.com/scannerjs/scanner.js" type="text/javascript"></script>
<script>
// Please read scanner.js developer's guide at: http://asprise.com/document-scan-upload-image-browser/ie-chrome-firefox-scanner-docs.html
var scanRequest = {
"source_name" : "select", // Device selection: "select" (prompts device selection dialog), "default" (uses the current default device) or the exact device name.
"use_asprise_dialog": false, // Whether to use Asprise Scanning Dialog
"show_scanner_ui": false, // Whether scanner UI should be shown
"twain_cap_setting" : { // Optional scanning settings
"ICAP_PIXELTYPE" : "TWPT_RGB", // Color
"ICAP_SUPPORTEDSIZES" : "TWSS_USLETTER" // Paper size: TWSS_USLETTER, TWSS_A4, ...
},
"output_settings": [
{
"type": "return-base64",
"format": "jpg"
}
]
};
/** Sets the source_name in request and triggers the scan */
function scan(sourceName) {
sourceName = typeof sourceName !== 'undefined' ? sourceName : 'default'; // Use 'default' if sourceName is not specified.
scanRequest.source_name = sourceName;
log("Attempts to scan with source = " + scanRequest.source_name + " ...");
scanner.scan(handleScanResult, scanRequest);
}
/** Checks response before parsing and performs fallback scanning if possible. */
function handleScanResult(successful, mesg, response) {
var errorMesg = successful ? undefined : mesg;
if(successful && response != null) {
var responseAsJson = JSON.parse(response);
if(responseAsJson != null && responseAsJson.image_count == 0 && responseAsJson.last_transfer_rc == "TWRC_FAILURE") {
errorMesg = "Device failure";
} else {
try {
displayImagesOnPage(successful, mesg, response);
} catch (exp) {
errorMesg = exp;
}
}
}
// feel free to check response and add other failure criteria ...
if(errorMesg) {
log("Error occurred when scanning with source = " + scanRequest.source_name + ": " + errorMesg, true);
if (scanRequest.source_name == 'default') { // fall back to select
log("Failed to scan with source = " + scanRequest.source_name + "; attempt source = 'select' ...");
scan('select');
} else { // report final error here ...
log("Fatal error: Failed to scan (tried both default and select)", true);
}
} else {
log("Scan succeeds with source = " + scanRequest.source_name);
}
}
function log(mesg, isError) {
var line = (new Date().toLocaleTimeString()) + " " + (isError ? "ERROR " : " INFO ") + mesg;
var textArea = document.getElementById("textarea_logging");
if(textArea) {
textArea.value = textArea.value + '\r' + line;
} else {
alert(line);
}
}
// --------------- below functions are identical with many other demo scripts ---------------
/** Processes the scan result */
function displayImagesOnPage(successful, mesg, response) {
if(!successful) { // On error
console.error('Failed: ' + mesg);
return;
}
if(successful && mesg != null && mesg.toLowerCase().indexOf('user cancel') >= 0) { // User cancelled.
console.info('User cancelled');
return;
}
var scannedImages = scanner.getScannedImages(response, true, false); // returns an array of ScannedImage
for(var i = 0; (scannedImages instanceof Array) && i < scannedImages.length; i++) {
var scannedImage = scannedImages[i];
processScannedImage(scannedImage);
}
}
/** Images scanned so far. */
var imagesScanned = [];
/** Processes a ScannedImage */
function processScannedImage(scannedImage) {
imagesScanned.push(scannedImage);
var elementImg = scanner.createDomElementFromModel( {
'name': 'img',
'attributes': {
'class': 'scanned',
'src': scannedImage.src
}
});
document.getElementById('images').appendChild(elementImg);
}
</script>
<style>
img.scanned {
height: 200px; /** Sets the display size */
margin-right: 12px;
}
div#images {
margin-top: 20px;
}
</style>
</head>
<body>
<h2>Scanner.js: Scan Using "default" Source With "select" as Fallback</h2>
<button type="button" onclick="scan('default');">Scan using 'default' with 'select' as fallback</button>
<button type="button" onclick="scan('select');">Scan using 'select' only</button>
<div id="images"></div>
<textarea id="textarea_logging" style="width: 100%; height: 400px;">--- Logging ---</textarea>
</body>
</html>