Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(397)

Side by Side Diff: chrome/common/extensions/docs/examples/api/document_scan/scan.js

Issue 286933006: Implement a JavaScript API for document scanning (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: IDL indicates multiple images can be returned Created 6 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 var requestButton = document.getElementById("requestButton");
2 var listScanners = document.getElementById('listScanners');
3 var scanButton = document.getElementById('scanButton');
4 var scanOption = document.getElementById('scanOption');
5 var scanModes = [
6 document.getElementById('scanModeColor'),
7 document.getElementById('scanModeGrey'),
8 document.getElementById('scanModeBW')
9 ];
10 var scannerName = document.getElementById('scannerName');
11 var scannedImages = document.getElementById('scannedImages');
12 var waitAnimation = document.getElementById('waitAnimation');
13 var scanner;
14 var imageMimeType;
15
16 function setOnlyChild(parent, child) {
17 while (parent.firstChild) {
18 parent.removeChild(parent.firstChild);
19 }
20 parent.appendChild(child);
21 }
22
23 var gotScannerList = function(scanners) {
24 waitAnimation.style.display = 'none';
25 console.log('Got ' + scanners.length + ' scanners');
26 if (scanners.length > 0) {
27 scanOption.style.display = 'block';
28 scanner = scanners[0].name;
29 imageMimeType = scanners[0].imageMimeType;
30 console.log('Using scanner ' + scanner);
31 setOnlyChild(scannerName, document.createTextNode(scanner));
32 } else {
33 scanOption.style.display = 'none';
34 }
35 };
36
37 var gotPermission = function(result) {
38 waitAnimation.style.display = 'block';
39 requestButton.style.display = 'none';
40 listScanners.style.display = 'block';
41 console.log('App was granted the "documentScan" permission.');
42 chrome.documentScan.listScanners(gotScannerList);
43 };
44
45 var permissionObj = {permissions: ['documentScan']};
46
47 requestButton.addEventListener('click', function() {
48 waitAnimation.style.display = 'block';
49 chrome.permissions.request( permissionObj, function(result) {
50 if (result) {
51 gotPermission();
52 } else {
53 console.log('App was not granted the "documentScan" permission.');
54 console.log(chrome.runtime.lastError);
55 }
56 });
57 });
58
59 listScanners.addEventListener('click', function() {
60 waitAnimation.style.display = 'block';
61 chrome.documentScan.listScanners(gotScannerList);
62 });
63
64 function getPngDataUrl(buffer) {
65 // Much discussion on the relative performance of various methods for doing
66 // this conversion at http://jsperf.com/encoding-xhr-image-data/16 and
67 // https://gist.github.com/jonleighton/958841. Picking the most concise
68 // version that works.
69 console.log('Making bytes from buffer');
70 var bytes = new Uint8Array(buffer);
71 var len = bytes.byteLength;
72 console.log('Making binary from bytes');
73 var binary = '';
74 for (var i = 0; i < len; i++) {
75 binary += String.fromCharCode(bytes[i]);
76 }
77 console.log('Returning btoa');
78 return 'data:' + imageMimeType + ';base64,' + btoa(binary);
79 }
80
81 var onScanCompleted = function(buffer) {
82 waitAnimation.style.display = 'none';
83 if (chrome.runtime.lastError) {
84 console.log('Scan failed: ' + chrome.runtime.lastError.message);
85 return;
86 }
87 console.log('Scan completed with data length ' +
88 buffer.byteLength + '.');
89 console.log(buffer);
90 urlData = getPngDataUrl(buffer);
91 console.log('URL is ' + urlData);
92 var scannedImage = document.createElement('img');
93 scannedImage.src = urlData;
94 scannedImages.insertBefore(scannedImage, scannedImages.firstChild);
95 };
96
97 scanButton.addEventListener('click', function() {
98 var scanProperties = {};
99 for (var idx = 0; idx < scanModes.length; ++idx) {
100 if (scanModes[idx].checked) {
101 var scanMode = scanModes[idx].value;
102 console.log('Scanning type is ' + scanMode);
103 scanProperties['mode'] = scanMode;
104 break;
105 }
106 }
107 waitAnimation.style.display = 'block';
108 chrome.documentScan.scan(scanner, scanProperties, onScanCompleted);
109 });
110
111 chrome.permissions.contains(permissionObj, function(result) {
112 if (result) {
113 gotPermission();
114 }
115 });
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698