Merge pull request #29 from serratus/upc-barcode
UPC-A, UPC-E and EAN-8 support and further improving overall robustness. Closes #24pull/35/head
@ -0,0 +1,83 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="utf-8" />
|
||||
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
|
||||
|
||||
<title>Camera</title>
|
||||
<script type="text/javascript">
|
||||
navigator.getUserMedia = navigator.getUserMedia || navigator.webkitGetUserMedia || navigator.mozGetUserMedia || navigator.msGetUserMedia;
|
||||
window.URL = window.URL || window.webkitURL || window.mozURL || window.msURL;
|
||||
|
||||
function getUserMedia(constraints, success, failure) {
|
||||
navigator.getUserMedia(constraints, function(stream) {
|
||||
var videoSrc = (window.URL && window.URL.createObjectURL(stream)) || stream;
|
||||
success.apply(null, [videoSrc]);
|
||||
}, failure);
|
||||
}
|
||||
|
||||
|
||||
function initCamera(constraints, video, callback) {
|
||||
getUserMedia(constraints, function (src) {
|
||||
video.src = src;
|
||||
video.addEventListener('loadeddata', function() {
|
||||
var attempts = 10;
|
||||
|
||||
function checkVideo() {
|
||||
if (attempts > 0) {
|
||||
if (video.videoWidth > 0 && video.videoHeight > 0) {
|
||||
console.log(video.videoWidth + "px x " + video.videoHeight + "px");
|
||||
video.play();
|
||||
callback();
|
||||
} else {
|
||||
window.setTimeout(checkVideo, 100);
|
||||
}
|
||||
} else {
|
||||
callback('Unable to play video stream.');
|
||||
}
|
||||
attempts--;
|
||||
}
|
||||
|
||||
checkVideo();
|
||||
}, false);
|
||||
}, function(e) {
|
||||
console.log(e);
|
||||
});
|
||||
}
|
||||
|
||||
function copyToCanvas(video, ctx) {
|
||||
( function frame() {
|
||||
ctx.drawImage(video, 0, 0);
|
||||
window.requestAnimationFrame(frame);
|
||||
}());
|
||||
}
|
||||
|
||||
window.addEventListener('load', function() {
|
||||
var constraints = {
|
||||
video: {
|
||||
mandatory: {
|
||||
minWidth: 1280,
|
||||
minHeight: 720
|
||||
}
|
||||
}
|
||||
},
|
||||
video = document.createElement('video'),
|
||||
canvas = document.createElement('canvas');
|
||||
|
||||
document.body.appendChild(video);
|
||||
document.body.appendChild(canvas);
|
||||
|
||||
initCamera(constraints, video, function() {
|
||||
canvas.setAttribute('width', video.videoWidth);
|
||||
canvas.setAttribute('height', video.videoHeight);
|
||||
copyToCanvas(video, canvas.getContext('2d'));
|
||||
});
|
||||
}, false);
|
||||
|
||||
|
||||
</script>
|
||||
</head>
|
||||
<body>
|
||||
|
||||
</body>
|
||||
</html>
|
@ -0,0 +1,45 @@
|
||||
/* jshint undef: true, unused: true, browser:true, devel: true */
|
||||
/* global define */
|
||||
|
||||
define(
|
||||
[
|
||||
"./ean_reader"
|
||||
],
|
||||
function(EANReader) {
|
||||
"use strict";
|
||||
|
||||
function EAN8Reader() {
|
||||
EANReader.call(this);
|
||||
}
|
||||
|
||||
EAN8Reader.prototype = Object.create(EANReader.prototype);
|
||||
EAN8Reader.prototype.constructor = EAN8Reader;
|
||||
|
||||
EAN8Reader.prototype._decodePayload = function(code, result, decodedCodes) {
|
||||
var i,
|
||||
self = this;
|
||||
|
||||
for ( i = 0; i < 4; i++) {
|
||||
code = self._decodeCode(code.end);
|
||||
result.push(code.code);
|
||||
decodedCodes.push(code);
|
||||
}
|
||||
|
||||
code = self._findPattern(self.MIDDLE_PATTERN, code.end, true);
|
||||
if (code === null) {
|
||||
return null;
|
||||
}
|
||||
decodedCodes.push(code);
|
||||
|
||||
for ( i = 0; i < 4; i++) {
|
||||
code = self._decodeCode(code.end, self.CODE_G_START);
|
||||
decodedCodes.push(code);
|
||||
result.push(code.code);
|
||||
}
|
||||
|
||||
return code;
|
||||
};
|
||||
|
||||
return (EAN8Reader);
|
||||
}
|
||||
);
|
@ -0,0 +1,109 @@
|
||||
/* jshint undef: true, unused: true, browser:true, devel: true */
|
||||
/* global define */
|
||||
|
||||
define(
|
||||
[
|
||||
"./ean_reader"
|
||||
],
|
||||
function(EANReader) {
|
||||
"use strict";
|
||||
|
||||
function UPCEReader() {
|
||||
EANReader.call(this);
|
||||
}
|
||||
|
||||
var properties = {
|
||||
CODE_FREQUENCY : {value: [
|
||||
[ 56, 52, 50, 49, 44, 38, 35, 42, 41, 37 ],
|
||||
[7, 11, 13, 14, 19, 25, 28, 21, 22, 26]]},
|
||||
STOP_PATTERN: { value: [1 / 6 * 7, 1 / 6 * 7, 1 / 6 * 7, 1 / 6 * 7, 1 / 6 * 7, 1 / 6 * 7]}
|
||||
};
|
||||
|
||||
UPCEReader.prototype = Object.create(EANReader.prototype, properties);
|
||||
UPCEReader.prototype.constructor = UPCEReader;
|
||||
|
||||
UPCEReader.prototype._decodePayload = function(code, result, decodedCodes) {
|
||||
var i,
|
||||
self = this,
|
||||
codeFrequency = 0x0;
|
||||
|
||||
for ( i = 0; i < 6; i++) {
|
||||
code = self._decodeCode(code.end);
|
||||
if (code.code >= self.CODE_G_START) {
|
||||
code.code = code.code - self.CODE_G_START;
|
||||
codeFrequency |= 1 << (5 - i);
|
||||
} else {
|
||||
codeFrequency |= 0 << (5 - i);
|
||||
}
|
||||
result.push(code.code);
|
||||
decodedCodes.push(code);
|
||||
}
|
||||
self._determineParity(codeFrequency, result);
|
||||
|
||||
return code;
|
||||
};
|
||||
|
||||
UPCEReader.prototype._determineParity = function(codeFrequency, result) {
|
||||
var self =this,
|
||||
i,
|
||||
nrSystem;
|
||||
|
||||
for (nrSystem = 0; nrSystem < self.CODE_FREQUENCY.length; nrSystem++){
|
||||
for ( i = 0; i < self.CODE_FREQUENCY[nrSystem].length; i++) {
|
||||
if (codeFrequency === self.CODE_FREQUENCY[nrSystem][i]) {
|
||||
result.unshift(nrSystem);
|
||||
result.push(i);
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
UPCEReader.prototype._convertToUPCA = function(result) {
|
||||
var upca = [result[0]],
|
||||
lastDigit = result[result.length - 2];
|
||||
|
||||
if (lastDigit <= 2) {
|
||||
upca = upca.concat(result.slice(1, 3))
|
||||
.concat([lastDigit, 0, 0, 0, 0])
|
||||
.concat(result.slice(3, 6));
|
||||
} else if (lastDigit === 3) {
|
||||
upca = upca.concat(result.slice(1, 4))
|
||||
.concat([0 ,0, 0, 0, 0])
|
||||
.concat(result.slice(4,6));
|
||||
} else if (lastDigit === 4) {
|
||||
upca = upca.concat(result.slice(1, 5))
|
||||
.concat([0, 0, 0, 0, 0, result[5]]);
|
||||
} else {
|
||||
upca = upca.concat(result.slice(1, 6))
|
||||
.concat([0, 0, 0, 0, lastDigit]);
|
||||
}
|
||||
|
||||
upca.push(result[result.length - 1]);
|
||||
return upca;
|
||||
};
|
||||
|
||||
UPCEReader.prototype._checksum = function(result) {
|
||||
return EANReader.prototype._checksum.call(this, this._convertToUPCA(result));
|
||||
};
|
||||
|
||||
UPCEReader.prototype._findEnd = function(offset, isWhite) {
|
||||
isWhite = true;
|
||||
return EANReader.prototype._findEnd.call(this, offset, isWhite);
|
||||
};
|
||||
|
||||
UPCEReader.prototype._verifyTrailingWhitespace = function(endInfo) {
|
||||
var self = this,
|
||||
trailingWhitespaceEnd;
|
||||
|
||||
trailingWhitespaceEnd = endInfo.end + ((endInfo.end - endInfo.start)/2);
|
||||
if (trailingWhitespaceEnd < self._row.length) {
|
||||
if (self._matchRange(endInfo.end, trailingWhitespaceEnd, 0)) {
|
||||
return endInfo;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
return (UPCEReader);
|
||||
}
|
||||
);
|
@ -0,0 +1,31 @@
|
||||
/* jshint undef: true, unused: true, browser:true, devel: true */
|
||||
/* global define */
|
||||
|
||||
define(
|
||||
[
|
||||
"./ean_reader"
|
||||
],
|
||||
function(EANReader) {
|
||||
"use strict";
|
||||
|
||||
function UPCReader() {
|
||||
EANReader.call(this);
|
||||
}
|
||||
|
||||
UPCReader.prototype = Object.create(EANReader.prototype);
|
||||
UPCReader.prototype.constructor = UPCReader;
|
||||
|
||||
UPCReader.prototype._decode = function() {
|
||||
var result = EANReader.prototype._decode.call(this);
|
||||
|
||||
if (result && result.code && result.code.length === 13 && result.code.charAt(0) === "0") {
|
||||
|
||||
result.code = result.code.substring(1);
|
||||
return result;
|
||||
}
|
||||
return null;
|
||||
};
|
||||
|
||||
return (UPCReader);
|
||||
}
|
||||
);
|
Before Width: | Height: | Size: 570 KiB After Width: | Height: | Size: 48 KiB |
Before Width: | Height: | Size: 710 KiB After Width: | Height: | Size: 84 KiB |
Before Width: | Height: | Size: 674 KiB After Width: | Height: | Size: 69 KiB |
Before Width: | Height: | Size: 797 KiB After Width: | Height: | Size: 93 KiB |
Before Width: | Height: | Size: 736 KiB After Width: | Height: | Size: 60 KiB |
Before Width: | Height: | Size: 774 KiB After Width: | Height: | Size: 88 KiB |
Before Width: | Height: | Size: 748 KiB After Width: | Height: | Size: 72 KiB |
Before Width: | Height: | Size: 741 KiB After Width: | Height: | Size: 136 KiB |
Before Width: | Height: | Size: 570 KiB After Width: | Height: | Size: 60 KiB |
Before Width: | Height: | Size: 562 KiB After Width: | Height: | Size: 115 KiB |
Before Width: | Height: | Size: 678 KiB After Width: | Height: | Size: 68 KiB |
Before Width: | Height: | Size: 595 KiB After Width: | Height: | Size: 84 KiB |
Before Width: | Height: | Size: 678 KiB After Width: | Height: | Size: 63 KiB |
Before Width: | Height: | Size: 570 KiB After Width: | Height: | Size: 118 KiB |
Before Width: | Height: | Size: 774 KiB After Width: | Height: | Size: 111 KiB |
Before Width: | Height: | Size: 687 KiB After Width: | Height: | Size: 89 KiB |
Before Width: | Height: | Size: 738 KiB After Width: | Height: | Size: 125 KiB |
Before Width: | Height: | Size: 662 KiB After Width: | Height: | Size: 60 KiB |
Before Width: | Height: | Size: 664 KiB After Width: | Height: | Size: 85 KiB |
Before Width: | Height: | Size: 757 KiB After Width: | Height: | Size: 98 KiB |
After Width: | Height: | Size: 48 KiB |
After Width: | Height: | Size: 50 KiB |
After Width: | Height: | Size: 64 KiB |
After Width: | Height: | Size: 50 KiB |
After Width: | Height: | Size: 76 KiB |
After Width: | Height: | Size: 88 KiB |
After Width: | Height: | Size: 47 KiB |
After Width: | Height: | Size: 36 KiB |
After Width: | Height: | Size: 50 KiB |
After Width: | Height: | Size: 52 KiB |
After Width: | Height: | Size: 48 KiB |
After Width: | Height: | Size: 53 KiB |
After Width: | Height: | Size: 84 KiB |
After Width: | Height: | Size: 66 KiB |
After Width: | Height: | Size: 78 KiB |
After Width: | Height: | Size: 46 KiB |
After Width: | Height: | Size: 80 KiB |
After Width: | Height: | Size: 62 KiB |
After Width: | Height: | Size: 82 KiB |
After Width: | Height: | Size: 60 KiB |
After Width: | Height: | Size: 73 KiB |
After Width: | Height: | Size: 60 KiB |
After Width: | Height: | Size: 69 KiB |
After Width: | Height: | Size: 63 KiB |
After Width: | Height: | Size: 66 KiB |
After Width: | Height: | Size: 78 KiB |
After Width: | Height: | Size: 57 KiB |
After Width: | Height: | Size: 71 KiB |
After Width: | Height: | Size: 65 KiB |
After Width: | Height: | Size: 78 KiB |