Skip to content

Commit

Permalink
Added UPC-E Reader
Browse files Browse the repository at this point in the history
  • Loading branch information
serratus committed Apr 27, 2015
1 parent 5bf9add commit dbc1fa5
Show file tree
Hide file tree
Showing 7 changed files with 259 additions and 24 deletions.
135 changes: 126 additions & 9 deletions dist/quagga.js
Original file line number Diff line number Diff line change
Expand Up @@ -1259,18 +1259,23 @@ define(
}
};

EANReader.prototype._findEnd = function(offset) {
EANReader.prototype._verifyTrailingWhitespace = function(endInfo) {
var self = this,
trailingWhitespaceEnd,
endInfo = self._findPattern(self.STOP_PATTERN, offset);
trailingWhitespaceEnd;

trailingWhitespaceEnd = endInfo.end + (endInfo.end - endInfo.start);
if (trailingWhitespaceEnd < self._row.length) {
if (self._matchRange(endInfo.end, trailingWhitespaceEnd, 0)) {
return endInfo;
}
}
return null;
};

EANReader.prototype._findEnd = function(offset, isWhite) {
var self = this,
endInfo = self._findPattern(self.STOP_PATTERN, offset, isWhite);

return self._verifyTrailingWhitespace(endInfo);
};

EANReader.prototype._decodePayload = function(code, result, decodedCodes) {
Expand Down Expand Up @@ -1328,7 +1333,7 @@ define(
};
decodedCodes.push(code);
code = self._decodePayload(code, result, decodedCodes);
code = self._findEnd(code.end);
code = self._findEnd(code.end, false);
if (code === null){
return null;
}
Expand Down Expand Up @@ -7115,6 +7120,115 @@ define(
/* jshint undef: true, unused: true, browser:true, devel: true */
/* global define */

define(
'upc_e_reader',[
"./ean_reader"
],
function(EANReader) {


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);
}
);
/* jshint undef: true, unused: true, browser:true, devel: true */
/* global define */

define('barcode_decoder',[
"bresenham",
"image_debug",
Expand All @@ -7123,7 +7237,8 @@ define('barcode_decoder',[
'code_39_reader',
'codabar_reader',
'upc_reader',
'ean_8_reader'
'ean_8_reader',
'upc_e_reader'
], function(
Bresenham,
ImageDebug,
Expand All @@ -7132,7 +7247,8 @@ define('barcode_decoder',[
Code39Reader,
CodabarReader,
UPCReader,
EAN8Reader) {
EAN8Reader,
UPCEReader) {


var readers = {
Expand All @@ -7141,7 +7257,8 @@ define('barcode_decoder',[
ean_8_reader: EAN8Reader,
code_39_reader: Code39Reader,
codabar_reader: CodabarReader,
upc_reader: UPCReader
upc_reader: UPCReader,
upc_e_reader: UPCEReader
};
var BarcodeDecoder = {
create : function(config, inputImageWrapper) {
Expand Down Expand Up @@ -7353,7 +7470,7 @@ define('barcode_decoder',[
line = getLine(box);
lineLength = getLineLength(line);
lineAngle = Math.atan2(line[1].y - line[0].y, line[1].x - line[0].x);
line = getExtendedLine(line, lineAngle, Math.floor(lineLength*0.07));
line = getExtendedLine(line, lineAngle, Math.floor(lineLength*0.1));
if(line === null){
return null;
}
Expand Down
8 changes: 4 additions & 4 deletions dist/quagga.min.js

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion example/file_input.html
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,9 @@ <h3>Working with file-input</h3>
<option value="code_128">Code 128</option>
<option value="code_39">Code 39</option>
<option value="ean">EAN</option>
<option value="ean_8" selected="selected">EAN-8</option>
<option value="ean_8">EAN-8</option>
<option value="upc">UPC</option>
<option value="upc_e" selected="selected">UPC-E</option>
<option value="codabar">Codabar</option>
</select>
</label>
Expand Down
2 changes: 1 addition & 1 deletion example/file_input.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ $(function() {
},
numOfWorkers: 0,
decoder: {
readers: ["ean_8_reader"],
readers: ["upc_e_reader"],
showFrequency: true,
showPattern: true
},
Expand Down
11 changes: 7 additions & 4 deletions src/barcode_decoder.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ define([
'code_39_reader',
'codabar_reader',
'upc_reader',
'ean_8_reader'
'ean_8_reader',
'upc_e_reader'
], function(
Bresenham,
ImageDebug,
Expand All @@ -18,7 +19,8 @@ define([
Code39Reader,
CodabarReader,
UPCReader,
EAN8Reader) {
EAN8Reader,
UPCEReader) {
"use strict";

var readers = {
Expand All @@ -27,7 +29,8 @@ define([
ean_8_reader: EAN8Reader,
code_39_reader: Code39Reader,
codabar_reader: CodabarReader,
upc_reader: UPCReader
upc_reader: UPCReader,
upc_e_reader: UPCEReader
};
var BarcodeDecoder = {
create : function(config, inputImageWrapper) {
Expand Down Expand Up @@ -239,7 +242,7 @@ define([
line = getLine(box);
lineLength = getLineLength(line);
lineAngle = Math.atan2(line[1].y - line[0].y, line[1].x - line[0].x);
line = getExtendedLine(line, lineAngle, Math.floor(lineLength*0.07));
line = getExtendedLine(line, lineAngle, Math.floor(lineLength*0.1));
if(line === null){
return null;
}
Expand Down
15 changes: 10 additions & 5 deletions src/ean_reader.js
Original file line number Diff line number Diff line change
Expand Up @@ -186,18 +186,23 @@ define(
}
};

EANReader.prototype._findEnd = function(offset) {
EANReader.prototype._verifyTrailingWhitespace = function(endInfo) {
var self = this,
trailingWhitespaceEnd,
endInfo = self._findPattern(self.STOP_PATTERN, offset);
trailingWhitespaceEnd;

trailingWhitespaceEnd = endInfo.end + (endInfo.end - endInfo.start);
if (trailingWhitespaceEnd < self._row.length) {
if (self._matchRange(endInfo.end, trailingWhitespaceEnd, 0)) {
return endInfo;
}
}
return null;
};

EANReader.prototype._findEnd = function(offset, isWhite) {
var self = this,
endInfo = self._findPattern(self.STOP_PATTERN, offset, isWhite);

return self._verifyTrailingWhitespace(endInfo);
};

EANReader.prototype._decodePayload = function(code, result, decodedCodes) {
Expand Down Expand Up @@ -255,7 +260,7 @@ define(
};
decodedCodes.push(code);
code = self._decodePayload(code, result, decodedCodes);
code = self._findEnd(code.end);
code = self._findEnd(code.end, false);
if (code === null){
return null;
}
Expand Down
109 changes: 109 additions & 0 deletions src/upc_e_reader.js
Original file line number Diff line number Diff line change
@@ -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 comments on commit dbc1fa5

Please sign in to comment.