Merged master to feature/image-source
@ -0,0 +1,27 @@
|
||||
var Quagga = require('../lib/quagga').default;
|
||||
|
||||
var buffer = require('fs').readFileSync('../test/fixtures/code_128/image-001.jpg');
|
||||
|
||||
decode(buffer);
|
||||
function decode(buff){
|
||||
Quagga.decodeSingle({
|
||||
src: buff,
|
||||
numOfWorkers: 0,
|
||||
inputStream: {
|
||||
mime: "image/jpeg",
|
||||
size: 800,
|
||||
area: {
|
||||
top: "10%",
|
||||
right: "5%",
|
||||
left: "5%",
|
||||
bottom: "10%"
|
||||
}
|
||||
}
|
||||
}, function(result) {
|
||||
if (result.codeResult) {
|
||||
console.log("result", result.codeResult.code);
|
||||
} else {
|
||||
console.log("not detected");
|
||||
}
|
||||
});
|
||||
}
|
@ -0,0 +1,257 @@
|
||||
import BarcodeReader from './barcode_reader';
|
||||
|
||||
function TwoOfFiveReader(opts) {
|
||||
BarcodeReader.call(this, opts);
|
||||
this.barSpaceRatio = [1, 1];
|
||||
}
|
||||
|
||||
var N = 1,
|
||||
W = 3,
|
||||
properties = {
|
||||
START_PATTERN: {value: [W, N, W, N, N, N]},
|
||||
STOP_PATTERN: {value: [W, N, N, N, W]},
|
||||
CODE_PATTERN: {value: [
|
||||
[N, N, W, W, N],
|
||||
[W, N, N, N, W],
|
||||
[N, W, N, N, W],
|
||||
[W, W, N, N, N],
|
||||
[N, N, W, N, W],
|
||||
[W, N, W, N, N],
|
||||
[N, W, W, N, N],
|
||||
[N, N, N, W, W],
|
||||
[W, N, N, W, N],
|
||||
[N, W, N, W, N]
|
||||
]},
|
||||
SINGLE_CODE_ERROR: {value: 0.78, writable: true},
|
||||
AVG_CODE_ERROR: {value: 0.30, writable: true},
|
||||
FORMAT: {value: "2of5"}
|
||||
};
|
||||
|
||||
const startPatternLength = properties.START_PATTERN.value.reduce((sum, val) => sum + val, 0);
|
||||
|
||||
TwoOfFiveReader.prototype = Object.create(BarcodeReader.prototype, properties);
|
||||
TwoOfFiveReader.prototype.constructor = TwoOfFiveReader;
|
||||
|
||||
TwoOfFiveReader.prototype._findPattern = function(pattern, offset, isWhite, tryHarder) {
|
||||
var counter = [],
|
||||
self = this,
|
||||
i,
|
||||
counterPos = 0,
|
||||
bestMatch = {
|
||||
error: Number.MAX_VALUE,
|
||||
code: -1,
|
||||
start: 0,
|
||||
end: 0
|
||||
},
|
||||
error,
|
||||
j,
|
||||
sum,
|
||||
epsilon = self.AVG_CODE_ERROR;
|
||||
|
||||
isWhite = isWhite || false;
|
||||
tryHarder = tryHarder || false;
|
||||
|
||||
if (!offset) {
|
||||
offset = self._nextSet(self._row);
|
||||
}
|
||||
|
||||
for ( i = 0; i < pattern.length; i++) {
|
||||
counter[i] = 0;
|
||||
}
|
||||
|
||||
for ( i = offset; i < self._row.length; i++) {
|
||||
if (self._row[i] ^ isWhite) {
|
||||
counter[counterPos]++;
|
||||
} else {
|
||||
if (counterPos === counter.length - 1) {
|
||||
sum = 0;
|
||||
for ( j = 0; j < counter.length; j++) {
|
||||
sum += counter[j];
|
||||
}
|
||||
error = self._matchPattern(counter, pattern);
|
||||
if (error < epsilon) {
|
||||
bestMatch.error = error;
|
||||
bestMatch.start = i - sum;
|
||||
bestMatch.end = i;
|
||||
return bestMatch;
|
||||
}
|
||||
if (tryHarder) {
|
||||
for (j = 0; j < counter.length - 2; j++) {
|
||||
counter[j] = counter[j + 2];
|
||||
}
|
||||
counter[counter.length - 2] = 0;
|
||||
counter[counter.length - 1] = 0;
|
||||
counterPos--;
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
} else {
|
||||
counterPos++;
|
||||
}
|
||||
counter[counterPos] = 1;
|
||||
isWhite = !isWhite;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
};
|
||||
|
||||
TwoOfFiveReader.prototype._findStart = function() {
|
||||
var self = this,
|
||||
leadingWhitespaceStart,
|
||||
offset = self._nextSet(self._row),
|
||||
startInfo,
|
||||
narrowBarWidth = 1;
|
||||
|
||||
while (!startInfo) {
|
||||
startInfo = self._findPattern(self.START_PATTERN, offset, false, true);
|
||||
if (!startInfo) {
|
||||
return null;
|
||||
}
|
||||
narrowBarWidth = Math.floor((startInfo.end - startInfo.start) / startPatternLength);
|
||||
leadingWhitespaceStart = startInfo.start - narrowBarWidth * 5;
|
||||
if (leadingWhitespaceStart >= 0) {
|
||||
if (self._matchRange(leadingWhitespaceStart, startInfo.start, 0)) {
|
||||
return startInfo;
|
||||
}
|
||||
}
|
||||
offset = startInfo.end;
|
||||
startInfo = null;
|
||||
}
|
||||
};
|
||||
|
||||
TwoOfFiveReader.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 null;
|
||||
};
|
||||
|
||||
TwoOfFiveReader.prototype._findEnd = function() {
|
||||
var self = this,
|
||||
endInfo,
|
||||
tmp,
|
||||
offset;
|
||||
|
||||
self._row.reverse();
|
||||
offset = self._nextSet(self._row);
|
||||
endInfo = self._findPattern(self.STOP_PATTERN, offset, false, true);
|
||||
self._row.reverse();
|
||||
|
||||
if (endInfo === null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
// reverse numbers
|
||||
tmp = endInfo.start;
|
||||
endInfo.start = self._row.length - endInfo.end;
|
||||
endInfo.end = self._row.length - tmp;
|
||||
|
||||
return endInfo !== null ? self._verifyTrailingWhitespace(endInfo) : null;
|
||||
};
|
||||
|
||||
TwoOfFiveReader.prototype._decodeCode = function(counter) {
|
||||
var j,
|
||||
self = this,
|
||||
sum = 0,
|
||||
normalized,
|
||||
error,
|
||||
epsilon = self.AVG_CODE_ERROR,
|
||||
code,
|
||||
bestMatch = {
|
||||
error: Number.MAX_VALUE,
|
||||
code: -1,
|
||||
start: 0,
|
||||
end: 0
|
||||
};
|
||||
|
||||
for ( j = 0; j < counter.length; j++) {
|
||||
sum += counter[j];
|
||||
}
|
||||
for (code = 0; code < self.CODE_PATTERN.length; code++) {
|
||||
error = self._matchPattern(counter, self.CODE_PATTERN[code]);
|
||||
if (error < bestMatch.error) {
|
||||
bestMatch.code = code;
|
||||
bestMatch.error = error;
|
||||
}
|
||||
}
|
||||
if (bestMatch.error < epsilon) {
|
||||
return bestMatch;
|
||||
}
|
||||
};
|
||||
|
||||
TwoOfFiveReader.prototype._decodePayload = function(counters, result, decodedCodes) {
|
||||
var i,
|
||||
self = this,
|
||||
pos = 0,
|
||||
counterLength = counters.length,
|
||||
counter = [0, 0, 0, 0, 0],
|
||||
code;
|
||||
|
||||
while (pos < counterLength) {
|
||||
for (i = 0; i < 5; i++) {
|
||||
counter[i] = counters[pos] * this.barSpaceRatio[0];
|
||||
pos += 2;
|
||||
}
|
||||
code = self._decodeCode(counter);
|
||||
if (!code) {
|
||||
return null;
|
||||
}
|
||||
result.push(code.code + "");
|
||||
decodedCodes.push(code);
|
||||
}
|
||||
return code;
|
||||
};
|
||||
|
||||
TwoOfFiveReader.prototype._verifyCounterLength = function(counters) {
|
||||
return (counters.length % 10 === 0);
|
||||
};
|
||||
|
||||
TwoOfFiveReader.prototype._decode = function() {
|
||||
var startInfo,
|
||||
endInfo,
|
||||
self = this,
|
||||
code,
|
||||
result = [],
|
||||
decodedCodes = [],
|
||||
counters;
|
||||
|
||||
startInfo = self._findStart();
|
||||
if (!startInfo) {
|
||||
return null;
|
||||
}
|
||||
decodedCodes.push(startInfo);
|
||||
|
||||
endInfo = self._findEnd();
|
||||
if (!endInfo) {
|
||||
return null;
|
||||
}
|
||||
|
||||
counters = self._fillCounters(startInfo.end, endInfo.start, false);
|
||||
if (!self._verifyCounterLength(counters)) {
|
||||
return null;
|
||||
}
|
||||
code = self._decodePayload(counters, result, decodedCodes);
|
||||
if (!code) {
|
||||
return null;
|
||||
}
|
||||
if (result.length < 5) {
|
||||
return null;
|
||||
}
|
||||
|
||||
decodedCodes.push(endInfo);
|
||||
return {
|
||||
code: result.join(""),
|
||||
start: startInfo.start,
|
||||
end: endInfo.end,
|
||||
startInfo: startInfo,
|
||||
decodedCodes: decodedCodes
|
||||
};
|
||||
};
|
||||
|
||||
export default TwoOfFiveReader;
|
@ -0,0 +1,251 @@
|
||||
import BarcodeReader from './barcode_reader';
|
||||
import ArrayHelper from '../common/array_helper';
|
||||
|
||||
function Code93Reader() {
|
||||
BarcodeReader.call(this);
|
||||
}
|
||||
|
||||
const ALPHABETH_STRING = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ-. $/+%abcd*";
|
||||
|
||||
var properties = {
|
||||
ALPHABETH_STRING: {value: ALPHABETH_STRING},
|
||||
ALPHABET: {value: ALPHABETH_STRING.split('').map(char => char.charCodeAt(0))},
|
||||
CHARACTER_ENCODINGS: {value: [
|
||||
0x114, 0x148, 0x144, 0x142, 0x128, 0x124, 0x122, 0x150, 0x112, 0x10A,
|
||||
0x1A8, 0x1A4, 0x1A2, 0x194, 0x192, 0x18A, 0x168, 0x164, 0x162, 0x134,
|
||||
0x11A, 0x158, 0x14C, 0x146, 0x12C, 0x116, 0x1B4, 0x1B2, 0x1AC, 0x1A6,
|
||||
0x196, 0x19A, 0x16C, 0x166, 0x136, 0x13A, 0x12E, 0x1D4, 0x1D2, 0x1CA,
|
||||
0x16E, 0x176, 0x1AE, 0x126, 0x1DA, 0x1D6, 0x132, 0x15E
|
||||
]},
|
||||
ASTERISK: {value: 0x15E},
|
||||
FORMAT: {value: "code_93", writeable: false}
|
||||
};
|
||||
|
||||
Code93Reader.prototype = Object.create(BarcodeReader.prototype, properties);
|
||||
Code93Reader.prototype.constructor = Code93Reader;
|
||||
|
||||
Code93Reader.prototype._decode = function() {
|
||||
var self = this,
|
||||
counters = [0, 0, 0, 0, 0, 0],
|
||||
result = [],
|
||||
start = self._findStart(),
|
||||
decodedChar,
|
||||
lastStart,
|
||||
pattern,
|
||||
nextStart;
|
||||
|
||||
if (!start) {
|
||||
return null;
|
||||
}
|
||||
nextStart = self._nextSet(self._row, start.end);
|
||||
|
||||
do {
|
||||
counters = self._toCounters(nextStart, counters);
|
||||
pattern = self._toPattern(counters);
|
||||
if (pattern < 0) {
|
||||
return null;
|
||||
}
|
||||
decodedChar = self._patternToChar(pattern);
|
||||
if (decodedChar < 0){
|
||||
return null;
|
||||
}
|
||||
result.push(decodedChar);
|
||||
lastStart = nextStart;
|
||||
nextStart += ArrayHelper.sum(counters);
|
||||
nextStart = self._nextSet(self._row, nextStart);
|
||||
} while (decodedChar !== '*');
|
||||
result.pop();
|
||||
|
||||
if (!result.length) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (!self._verifyEnd(lastStart, nextStart, counters)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (!self._verifyChecksums(result)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
result = result.slice(0, result.length - 2);
|
||||
if ((result = self._decodeExtended(result)) === null) {
|
||||
return null;
|
||||
};
|
||||
|
||||
return {
|
||||
code: result.join(""),
|
||||
start: start.start,
|
||||
end: nextStart,
|
||||
startInfo: start,
|
||||
decodedCodes: result
|
||||
};
|
||||
};
|
||||
|
||||
Code93Reader.prototype._verifyEnd = function(lastStart, nextStart) {
|
||||
if (lastStart === nextStart || !this._row[nextStart]) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
};
|
||||
|
||||
Code93Reader.prototype._patternToChar = function(pattern) {
|
||||
var i,
|
||||
self = this;
|
||||
|
||||
for (i = 0; i < self.CHARACTER_ENCODINGS.length; i++) {
|
||||
if (self.CHARACTER_ENCODINGS[i] === pattern) {
|
||||
return String.fromCharCode(self.ALPHABET[i]);
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
};
|
||||
|
||||
Code93Reader.prototype._toPattern = function(counters) {
|
||||
const numCounters = counters.length;
|
||||
let pattern = 0;
|
||||
let sum = 0;
|
||||
for (let i = 0; i < numCounters; i++) {
|
||||
sum += counters[i];
|
||||
}
|
||||
|
||||
for (let i = 0; i < numCounters; i++) {
|
||||
let normalized = Math.round(counters[i] * 9 / sum);
|
||||
if (normalized < 1 || normalized > 4) {
|
||||
return -1;
|
||||
}
|
||||
if ((i & 1) === 0) {
|
||||
for (let j = 0; j < normalized; j++) {
|
||||
pattern = (pattern << 1) | 1;
|
||||
}
|
||||
} else {
|
||||
pattern <<= normalized;
|
||||
}
|
||||
}
|
||||
|
||||
return pattern;
|
||||
};
|
||||
|
||||
Code93Reader.prototype._findStart = function() {
|
||||
var self = this,
|
||||
offset = self._nextSet(self._row),
|
||||
patternStart = offset,
|
||||
counter = [0, 0, 0, 0, 0, 0],
|
||||
counterPos = 0,
|
||||
isWhite = false,
|
||||
i,
|
||||
j,
|
||||
whiteSpaceMustStart;
|
||||
|
||||
for ( i = offset; i < self._row.length; i++) {
|
||||
if (self._row[i] ^ isWhite) {
|
||||
counter[counterPos]++;
|
||||
} else {
|
||||
if (counterPos === counter.length - 1) {
|
||||
// find start pattern
|
||||
if (self._toPattern(counter) === self.ASTERISK) {
|
||||
whiteSpaceMustStart = Math.floor(Math.max(0, patternStart - ((i - patternStart) / 4)));
|
||||
if (self._matchRange(whiteSpaceMustStart, patternStart, 0)) {
|
||||
return {
|
||||
start: patternStart,
|
||||
end: i
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
patternStart += counter[0] + counter[1];
|
||||
for ( j = 0; j < 4; j++) {
|
||||
counter[j] = counter[j + 2];
|
||||
}
|
||||
counter[4] = 0;
|
||||
counter[5] = 0;
|
||||
counterPos--;
|
||||
} else {
|
||||
counterPos++;
|
||||
}
|
||||
counter[counterPos] = 1;
|
||||
isWhite = !isWhite;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
};
|
||||
|
||||
Code93Reader.prototype._decodeExtended = function(charArray) {
|
||||
const length = charArray.length;
|
||||
const result = [];
|
||||
for (let i = 0; i < length; i++) {
|
||||
const char = charArray[i];
|
||||
if (char >= 'a' && char <= 'd') {
|
||||
if (i > (length - 2)) {
|
||||
return null;
|
||||
}
|
||||
const nextChar = charArray[++i];
|
||||
const nextCharCode = nextChar.charCodeAt(0);
|
||||
let decodedChar;
|
||||
switch (char) {
|
||||
case 'a':
|
||||
if (nextChar >= 'A' && nextChar <= 'Z') {
|
||||
decodedChar = String.fromCharCode(nextCharCode - 64);
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
break;
|
||||
case 'b':
|
||||
if (nextChar >= 'A' && nextChar <= 'E') {
|
||||
decodedChar = String.fromCharCode(nextCharCode - 38);
|
||||
} else if (nextChar >= 'F' && nextChar <= 'J') {
|
||||
decodedChar = String.fromCharCode(nextCharCode - 11);
|
||||
} else if (nextChar >= 'K' && nextChar <= 'O') {
|
||||
decodedChar = String.fromCharCode(nextCharCode + 16);
|
||||
} else if (nextChar >= 'P' && nextChar <= 'S') {
|
||||
decodedChar = String.fromCharCode(nextCharCode + 43);
|
||||
} else if (nextChar >= 'T' && nextChar <= 'Z') {
|
||||
decodedChar = String.fromCharCode(127);
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
break;
|
||||
case 'c':
|
||||
if (nextChar >= 'A' && nextChar <= 'O') {
|
||||
decodedChar = String.fromCharCode(nextCharCode - 32);
|
||||
} else if (nextChar === 'Z') {
|
||||
decodedChar = ':';
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
break;
|
||||
case 'd':
|
||||
if (nextChar >= 'A' && nextChar <= 'Z') {
|
||||
decodedChar = String.fromCharCode(nextCharCode + 32);
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
break;
|
||||
}
|
||||
result.push(decodedChar);
|
||||
} else {
|
||||
result.push(char);
|
||||
}
|
||||
}
|
||||
return result;
|
||||
};
|
||||
|
||||
Code93Reader.prototype._verifyChecksums = function(charArray) {
|
||||
return this._matchCheckChar(charArray, charArray.length - 2, 20)
|
||||
&& this._matchCheckChar(charArray, charArray.length - 1, 15);
|
||||
};
|
||||
|
||||
Code93Reader.prototype._matchCheckChar = function(charArray, index, maxWeight) {
|
||||
const arrayToCheck = charArray.slice(0, index);
|
||||
const length = arrayToCheck.length;
|
||||
const weightedSums = arrayToCheck.reduce((sum, char, i) => {
|
||||
const weight = (((i * -1) + (length - 1)) % maxWeight) + 1;
|
||||
const value = this.ALPHABET.indexOf(char.charCodeAt(0));
|
||||
return sum + (weight * value);
|
||||
}, 0);
|
||||
|
||||
const checkChar = this.ALPHABET[(weightedSums % 47)];
|
||||
return checkChar === charArray[index].charCodeAt(0);
|
||||
};
|
||||
|
||||
export default Code93Reader;
|
After Width: | Height: | Size: 174 KiB |
After Width: | Height: | Size: 178 KiB |
After Width: | Height: | Size: 171 KiB |
After Width: | Height: | Size: 162 KiB |
After Width: | Height: | Size: 164 KiB |
After Width: | Height: | Size: 145 KiB |
After Width: | Height: | Size: 122 KiB |
After Width: | Height: | Size: 168 KiB |
After Width: | Height: | Size: 156 KiB |
After Width: | Height: | Size: 183 KiB |
After Width: | Height: | Size: 173 KiB |
After Width: | Height: | Size: 169 KiB |
After Width: | Height: | Size: 147 KiB |
After Width: | Height: | Size: 187 KiB |
After Width: | Height: | Size: 143 KiB |
After Width: | Height: | Size: 160 KiB |
After Width: | Height: | Size: 151 KiB |
After Width: | Height: | Size: 128 KiB |
After Width: | Height: | Size: 154 KiB |
After Width: | Height: | Size: 111 KiB |
After Width: | Height: | Size: 117 KiB |
After Width: | Height: | Size: 121 KiB |
After Width: | Height: | Size: 144 KiB |
After Width: | Height: | Size: 140 KiB |