This repository has been archived by the owner on Feb 21, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 19
/
zbar-main.js
141 lines (123 loc) · 4.56 KB
/
zbar-main.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
// Copyright (c) 2013 Yury Delendik
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
var takePicture = document.getElementById("take-picture");
var canvas = document.createElement("canvas");
var codeType = document.getElementById("code-type");
var codeContent = document.getElementById("code-content");
var openUrlButton = document.getElementById("open-url");
var searchCodeButton = document.getElementById("search-code");
function scan() {
if (typeof MozActivity !== 'undefined') {
// in Firefox OS v1.0.1, input[file] does not work, using Web Activities
takePictureUsingWebActivity();
return;
}
takePicture.click();
}
function openUrl() {
window.open(codeContent.value);
}
function searchCode() {
window.open('https://www.google.com/search?q=' + codeContent.value);
}
// https://hacks.mozilla.org/2013/01/introducing-web-activities/
function takePictureUsingWebActivity() {
var pick = new MozActivity({
name: "pick",
data: { type: ["image/png", "image/jpg", "image/jpeg"] }
});
pick.onsuccess = function () {
loadImage(URL.createObjectURL(this.result.blob));
};
}
takePicture.onchange = function (event) {
var files = event.target.files;
if (!files || files.length === 0) {
return;
}
var file = files[0];
var imgURL = (window.URL || window.webkitURL).createObjectURL(file);
loadImage(imgURL);
}
function loadImage(imgURL) {
// clean
codeType.textContent = '';
codeContent.value = '';
openUrlButton.setAttribute('hidden', 'hidden');
searchCodeButton.setAttribute('hidden', 'hidden');
var img = new Image();
img.onload = function () {
var canvas = document.createElement('canvas');
// resizing image to 320x240 for slow devices
var k = (320 + 240) / (img.width + img.height);
canvas.width = Math.ceil(img.width * k);
canvas.height = Math.ceil(img.height * k);
var ctx = canvas.getContext('2d');
ctx.drawImage(img, 0, 0, img.width, img.height,
0, 0, canvas.width, canvas.height);
var data = ctx.getImageData(0, 0, canvas.width, canvas.height);
var t0 = Date.now();
var codes = zbarProcessImageData(data);
var t = Date.now() - t0;
document.body.classList.remove('processing');
if (codes.length === 0) {
codeType.textContent = 'N/A';
document.body.classList.add('not-detected');
return;
}
var type = codes[0][0];
var data = codes[0][2];
// publishing data
codeType.textContent = type;
codeContent.value = data;
var isUrl = /^(http|https|ftp|mailto):/.test(data);
if (isUrl) {
openUrlButton.removeAttribute('hidden');
} else {
searchCodeButton.removeAttribute('hidden');
}
};
img.src = imgURL;
document.body.classList.add('processing');
document.body.classList.remove('not-detected');
document.getElementById('info').removeAttribute('hidden');
};
codeContent.value = '';
setTimeout(scan, 500); // trying to start "scanning"
var canInstall = !!(navigator.mozApps && navigator.mozApps.install);
if (canInstall) {
var installButton = document.getElementById('install');
var manifestURL = installButton.href;
var request = window.navigator.mozApps.checkInstalled(manifestURL);
request.onsuccess = function(e) {
if (request.result) {
return;
}
// App is not installed
installButton.removeAttribute('hidden');
installButton.addEventListener('click', function (e) {
var request = navigator.mozApps.install(manifestURL);
request.onsuccess = function (e) {
alert('Application installed');
};
e.preventDefault();
}, false);
};
}