Adding Code39VINReader

pull/35/head
Lindsey Simon 10 years ago
parent fc04683054
commit d362791f61

124
dist/quagga.js vendored

@ -6589,7 +6589,9 @@ define(
return counter; return counter;
}; };
Code39Reader.prototype._decode = function() {
Code39Reader.prototype._decodeToResult = function() {
var self = this, var self = this,
counters = [0,0,0,0,0,0,0,0,0], counters = [0,0,0,0,0,0,0,0,0],
result = [], result = [],
@ -6599,6 +6601,7 @@ define(
pattern, pattern,
nextStart; nextStart;
if (!start) { if (!start) {
return null; return null;
} }
@ -6621,8 +6624,6 @@ define(
} while(decodedChar !== '*'); } while(decodedChar !== '*');
result.pop(); result.pop();
return { return {
code : result.join(""), code : result.join(""),
start : start.start, start : start.start,
@ -6632,6 +6633,10 @@ define(
}; };
}; };
Code39Reader.prototype._decode = function() {
return this._decodeToResult();
};
Code39Reader.prototype._patternToChar = function(pattern) { Code39Reader.prototype._patternToChar = function(pattern) {
var i, var i,
self = this; self = this;
@ -6740,6 +6745,67 @@ define(
return (Code39Reader); return (Code39Reader);
} }
); );
/* jshint undef: true, unused: true, browser:true, devel: true */
/* global define */
define(
'code_39_vin_reader',[
"./code_39_reader"
],
function(Code39Reader) {
"use strict";
function Code39VINReader() {
Code39Reader.call(this);
}
var patterns = {
IOQ: /[IOQ]/g,
AZ09: /[A-Z0-9]{17}/
};
Code39VINReader.prototype = Object.create(Code39Reader.prototype);
Code39VINReader.prototype.constructor = Code39VINReader;
// Cribbed from:
// https://github.com/zxing/zxing/blob/master/core/src/main/java/com/google/zxing/client/result/VINResultParser.java
Code39VINReader.prototype._decode = function() {
var result = this._decodeToResult();
if (!result) {
return null;
}
var code = result.code;
if (!code) {
return;
}
code = code.replace(patterns.IOQ, '');
if (!code.match(patterns.AZ09)) {
console.log('Failed AZ09 pattern code:', code);
return null;
}
if (!this._checkChecksum(code)) {
return null;
}
result.code = code;
return result;
};
Code39VINReader.prototype._checkChecksum = function(code) {
// TODO
return !!code;
};
return (Code39VINReader);
}
);
/* jshint undef: true, unused: true, browser:true, devel: true */ /* jshint undef: true, unused: true, browser:true, devel: true */
/* global define */ /* global define */
@ -7247,6 +7313,7 @@ define('barcode_decoder',[
'code_128_reader', 'code_128_reader',
'ean_reader', 'ean_reader',
'code_39_reader', 'code_39_reader',
'code_39_vin_reader',
'codabar_reader', 'codabar_reader',
'upc_reader', 'upc_reader',
'ean_8_reader', 'ean_8_reader',
@ -7257,17 +7324,19 @@ define('barcode_decoder',[
Code128Reader, Code128Reader,
EANReader, EANReader,
Code39Reader, Code39Reader,
Code39VINReader,
CodabarReader, CodabarReader,
UPCReader, UPCReader,
EAN8Reader, EAN8Reader,
UPCEReader) { UPCEReader) {
"use strict"; "use strict";
var readers = { var readers = {
code_128_reader: Code128Reader, code_128_reader: Code128Reader,
ean_reader: EANReader, ean_reader: EANReader,
ean_8_reader: EAN8Reader, ean_8_reader: EAN8Reader,
code_39_reader: Code39Reader, code_39_reader: Code39Reader,
code_39_vin_reader: Code39VINReader,
codabar_reader: CodabarReader, codabar_reader: CodabarReader,
upc_reader: UPCReader, upc_reader: UPCReader,
upc_e_reader: UPCEReader upc_e_reader: UPCEReader
@ -7288,7 +7357,7 @@ define('barcode_decoder',[
}, },
_barcodeReaders = [], _barcodeReaders = [],
_barcodeReader = null; _barcodeReader = null;
initCanvas(); initCanvas();
initReaders(); initReaders();
initConfig(); initConfig();
@ -7353,9 +7422,9 @@ define('barcode_decoder',[
} }
/** /**
* extend the line on both ends * extend the line on both ends
* @param {Array} line * @param {Array} line
* @param {Number} angle * @param {Number} angle
*/ */
function getExtendedLine(line, angle, ext) { function getExtendedLine(line, angle, ext) {
function extendLine(amount) { function extendLine(amount) {
@ -7381,7 +7450,7 @@ define('barcode_decoder',[
} }
return line; return line;
} }
function getLine(box) { function getLine(box) {
return [{ return [{
x : (box[1][0] - box[0][0]) / 2 + box[0][0], x : (box[1][0] - box[0][0]) / 2 + box[0][0],
@ -7391,12 +7460,12 @@ define('barcode_decoder',[
y : (box[3][1] - box[2][1]) / 2 + box[2][1] y : (box[3][1] - box[2][1]) / 2 + box[2][1]
}]; }];
} }
function tryDecode(line) { function tryDecode(line) {
var result = null, var result = null,
i, i,
barcodeLine = Bresenham.getBarcodeLine(inputImageWrapper, line[0], line[1]); barcodeLine = Bresenham.getBarcodeLine(inputImageWrapper, line[0], line[1]);
if (config.showFrequency) { if (config.showFrequency) {
ImageDebug.drawPath(line, {x: 'x', y: 'y'}, _canvas.ctx.overlay, {color: 'red', lineWidth: 3}); ImageDebug.drawPath(line, {x: 'x', y: 'y'}, _canvas.ctx.overlay, {color: 'red', lineWidth: 3});
Bresenham.debug.printFrequency(barcodeLine.line, _canvas.dom.frequency); Bresenham.debug.printFrequency(barcodeLine.line, _canvas.dom.frequency);
@ -7405,7 +7474,7 @@ define('barcode_decoder',[
if (config.showPattern) { if (config.showPattern) {
Bresenham.debug.printPattern(barcodeLine.line, _canvas.dom.pattern); Bresenham.debug.printPattern(barcodeLine.line, _canvas.dom.pattern);
} }
for ( i = 0; i < _barcodeReaders.length && result === null; i++) { for ( i = 0; i < _barcodeReaders.length && result === null; i++) {
result = _barcodeReaders[i].decodePattern(barcodeLine.line); result = _barcodeReaders[i].decodePattern(barcodeLine.line);
if (result !== null) { if (result !== null) {
@ -7419,15 +7488,15 @@ define('barcode_decoder',[
codeResult: result, codeResult: result,
barcodeLine: barcodeLine barcodeLine: barcodeLine
}; };
} }
/** /**
* This method slices the given area apart and tries to detect a barcode-pattern * This method slices the given area apart and tries to detect a barcode-pattern
* for each slice. It returns the decoded barcode, or null if nothing was found * for each slice. It returns the decoded barcode, or null if nothing was found
* @param {Array} box * @param {Array} box
* @param {Array} line * @param {Array} line
* @param {Number} lineAngle * @param {Number} lineAngle
*/ */
function tryDecodeBruteForce(box, line, lineAngle) { function tryDecodeBruteForce(box, line, lineAngle) {
var sideLength = Math.sqrt(Math.pow(box[1][0] - box[0][0], 2) + Math.pow((box[1][1] - box[0][1]), 2)), var sideLength = Math.sqrt(Math.pow(box[1][0] - box[0][0], 2) + Math.pow((box[1][1] - box[0][1]), 2)),
@ -7438,7 +7507,7 @@ define('barcode_decoder',[
extension, extension,
xdir = Math.sin(lineAngle), xdir = Math.sin(lineAngle),
ydir = Math.cos(lineAngle); ydir = Math.cos(lineAngle);
for ( i = 1; i < slices && result === null; i++) { for ( i = 1; i < slices && result === null; i++) {
// move line perpendicular to angle // move line perpendicular to angle
dir = sideLength / slices * i * (i % 2 === 0 ? -1 : 1); dir = sideLength / slices * i * (i % 2 === 0 ? -1 : 1);
@ -7463,7 +7532,7 @@ define('barcode_decoder',[
} }
/** /**
* With the help of the configured readers (Code128 or EAN) this function tries to detect a * With the help of the configured readers (Code128 or EAN) this function tries to detect a
* valid barcode pattern within the given area. * valid barcode pattern within the given area.
* @param {Object} box The area to search in * @param {Object} box The area to search in
* @returns {Object} the result {codeResult, line, angle, pattern, threshold} * @returns {Object} the result {codeResult, line, angle, pattern, threshold}
@ -7491,7 +7560,7 @@ define('barcode_decoder',[
if(result === null) { if(result === null) {
result = tryDecodeBruteForce(box, line, lineAngle); result = tryDecodeBruteForce(box, line, lineAngle);
} }
if(result === null) { if(result === null) {
return null; return null;
} }
@ -7533,7 +7602,8 @@ define('barcode_decoder',[
}; };
return (BarcodeDecoder); return (BarcodeDecoder);
}); });
/* jshint undef: true, unused: true, browser:true, devel: true */ /* jshint undef: true, unused: true, browser:true, devel: true */
/* global define */ /* global define */
@ -7668,7 +7738,7 @@ define('html_utils',[], function() {
}; };
}); });
/** /**
* The basic configuration * The basic configuration
*/ */
define('config',[],function(){ define('config',[],function(){
@ -7678,6 +7748,8 @@ define('config',[],function(){
constraints: { constraints: {
width: 640, width: 640,
height: 480, height: 480,
minAspectRatio: 1,
maxAspectRatio: 1,
facing: "environment" // or user facing: "environment" // or user
} }
}, },
@ -7715,9 +7787,10 @@ define('config',[],function(){
} }
} }
}; };
return config; return config;
}); });
/* jshint undef: true, unused: true, browser:true, devel: true */ /* jshint undef: true, unused: true, browser:true, devel: true */
/* global define */ /* global define */
@ -7816,7 +7889,7 @@ define('camera_access',["html_utils"], function(HtmlUtils) {
"use strict"; "use strict";
var streamRef, var streamRef,
loadedDataHandler; loadedDataHandler;
/** /**
* Wraps browser-specific getUserMedia * Wraps browser-specific getUserMedia
* @param {Object} constraints * @param {Object} constraints
@ -7885,6 +7958,8 @@ define('camera_access',["html_utils"], function(HtmlUtils) {
videoConstraints = HtmlUtils.mergeObjects({ videoConstraints = HtmlUtils.mergeObjects({
width: 640, width: 640,
height: 480, height: 480,
minAspectRatio: 1,
maxAspectRatio: 1,
facing: "environment" facing: "environment"
}, config); }, config);
@ -7900,7 +7975,9 @@ define('camera_access',["html_utils"], function(HtmlUtils) {
constraints.video = { constraints.video = {
mandatory: { mandatory: {
minWidth: videoConstraints.width, minWidth: videoConstraints.width,
minHeight: videoConstraints.height minHeight: videoConstraints.height,
minAspectRatio: videoConstraints.minAspectRatio,
maxAspectRatio: videoConstraints.maxAspectRatio
}, },
optional: [{ optional: [{
sourceId: videoSourceId sourceId: videoSourceId
@ -7943,7 +8020,8 @@ define('camera_access',["html_utils"], function(HtmlUtils) {
streamRef = null; streamRef = null;
} }
}; };
}); });
/* jshint undef: true, unused: true, browser:true, devel: true, evil: true */ /* jshint undef: true, unused: true, browser:true, devel: true, evil: true */
/* global define, vec2 */ /* global define, vec2 */

@ -37,6 +37,7 @@
<select name="decoder_readers;input-stream_src"> <select name="decoder_readers;input-stream_src">
<option value="code_128" selected="selected">Code 128</option> <option value="code_128" selected="selected">Code 128</option>
<option value="code_39">Code 39</option> <option value="code_39">Code 39</option>
<option value="code_39_vin">Code 39 VIN</option>
<option value="ean">EAN</option> <option value="ean">EAN</option>
<option value="ean_8">EAN-8</option> <option value="ean_8">EAN-8</option>
<option value="upc">UPC</option> <option value="upc">UPC</option>

@ -84,18 +84,27 @@ $(function() {
state: { state: {
inputStream: { name: "Test", inputStream: { name: "Test",
type: "ImageStream", type: "ImageStream",
src: "../test/fixtures/code_128/", src: "../test/fixtures/code_39_vin/",
length: 10 length: 10
}, },
decoder : { decoder : {
readers : ["code_128_reader"] readers : ["code_39_vin_reader"],
drawScanline: true,
},
numOfWorkers: 4,
locator: {
halfSample: false,
showSkeleton: true,
showFoundPatches: true
} }
} }
}; };
App.init(); App.init();
window.App = App;
Quagga.onProcessed(function(result) { Quagga.onProcessed(function(result) {
console.log('Quagga.onProcessed', result);
var drawingCtx = Quagga.canvas.ctx.overlay, var drawingCtx = Quagga.canvas.ctx.overlay,
drawingCanvas = Quagga.canvas.dom.overlay; drawingCanvas = Quagga.canvas.dom.overlay;
@ -129,4 +138,4 @@ $(function() {
$node.find("h4.code").html(detectedCode); $node.find("h4.code").html(detectedCode);
$("#result_strip ul.thumbnails").prepend($node); $("#result_strip ul.thumbnails").prepend($node);
}); });
}); });

@ -4,7 +4,7 @@
"description": "An advanced barcode-scanner written in JavaScript", "description": "An advanced barcode-scanner written in JavaScript",
"main": "dist/quagga.js", "main": "dist/quagga.js",
"devDependencies": { "devDependencies": {
"grunt": "~0.4.6", "grunt": "~0.4.5",
"grunt-contrib-jshint": "~0.10.0", "grunt-contrib-jshint": "~0.10.0",
"grunt-contrib-nodeunit": "~0.4.1", "grunt-contrib-nodeunit": "~0.4.1",
"grunt-contrib-uglify": "~0.5.0", "grunt-contrib-uglify": "~0.5.0",

@ -7,6 +7,7 @@ define([
'code_128_reader', 'code_128_reader',
'ean_reader', 'ean_reader',
'code_39_reader', 'code_39_reader',
'code_39_vin_reader',
'codabar_reader', 'codabar_reader',
'upc_reader', 'upc_reader',
'ean_8_reader', 'ean_8_reader',
@ -17,17 +18,19 @@ define([
Code128Reader, Code128Reader,
EANReader, EANReader,
Code39Reader, Code39Reader,
Code39VINReader,
CodabarReader, CodabarReader,
UPCReader, UPCReader,
EAN8Reader, EAN8Reader,
UPCEReader) { UPCEReader) {
"use strict"; "use strict";
var readers = { var readers = {
code_128_reader: Code128Reader, code_128_reader: Code128Reader,
ean_reader: EANReader, ean_reader: EANReader,
ean_8_reader: EAN8Reader, ean_8_reader: EAN8Reader,
code_39_reader: Code39Reader, code_39_reader: Code39Reader,
code_39_vin_reader: Code39VINReader,
codabar_reader: CodabarReader, codabar_reader: CodabarReader,
upc_reader: UPCReader, upc_reader: UPCReader,
upc_e_reader: UPCEReader upc_e_reader: UPCEReader
@ -48,7 +51,7 @@ define([
}, },
_barcodeReaders = [], _barcodeReaders = [],
_barcodeReader = null; _barcodeReader = null;
initCanvas(); initCanvas();
initReaders(); initReaders();
initConfig(); initConfig();
@ -113,9 +116,9 @@ define([
} }
/** /**
* extend the line on both ends * extend the line on both ends
* @param {Array} line * @param {Array} line
* @param {Number} angle * @param {Number} angle
*/ */
function getExtendedLine(line, angle, ext) { function getExtendedLine(line, angle, ext) {
function extendLine(amount) { function extendLine(amount) {
@ -141,7 +144,7 @@ define([
} }
return line; return line;
} }
function getLine(box) { function getLine(box) {
return [{ return [{
x : (box[1][0] - box[0][0]) / 2 + box[0][0], x : (box[1][0] - box[0][0]) / 2 + box[0][0],
@ -151,12 +154,12 @@ define([
y : (box[3][1] - box[2][1]) / 2 + box[2][1] y : (box[3][1] - box[2][1]) / 2 + box[2][1]
}]; }];
} }
function tryDecode(line) { function tryDecode(line) {
var result = null, var result = null,
i, i,
barcodeLine = Bresenham.getBarcodeLine(inputImageWrapper, line[0], line[1]); barcodeLine = Bresenham.getBarcodeLine(inputImageWrapper, line[0], line[1]);
if (config.showFrequency) { if (config.showFrequency) {
ImageDebug.drawPath(line, {x: 'x', y: 'y'}, _canvas.ctx.overlay, {color: 'red', lineWidth: 3}); ImageDebug.drawPath(line, {x: 'x', y: 'y'}, _canvas.ctx.overlay, {color: 'red', lineWidth: 3});
Bresenham.debug.printFrequency(barcodeLine.line, _canvas.dom.frequency); Bresenham.debug.printFrequency(barcodeLine.line, _canvas.dom.frequency);
@ -165,7 +168,7 @@ define([
if (config.showPattern) { if (config.showPattern) {
Bresenham.debug.printPattern(barcodeLine.line, _canvas.dom.pattern); Bresenham.debug.printPattern(barcodeLine.line, _canvas.dom.pattern);
} }
for ( i = 0; i < _barcodeReaders.length && result === null; i++) { for ( i = 0; i < _barcodeReaders.length && result === null; i++) {
result = _barcodeReaders[i].decodePattern(barcodeLine.line); result = _barcodeReaders[i].decodePattern(barcodeLine.line);
if (result !== null) { if (result !== null) {
@ -179,15 +182,15 @@ define([
codeResult: result, codeResult: result,
barcodeLine: barcodeLine barcodeLine: barcodeLine
}; };
} }
/** /**
* This method slices the given area apart and tries to detect a barcode-pattern * This method slices the given area apart and tries to detect a barcode-pattern
* for each slice. It returns the decoded barcode, or null if nothing was found * for each slice. It returns the decoded barcode, or null if nothing was found
* @param {Array} box * @param {Array} box
* @param {Array} line * @param {Array} line
* @param {Number} lineAngle * @param {Number} lineAngle
*/ */
function tryDecodeBruteForce(box, line, lineAngle) { function tryDecodeBruteForce(box, line, lineAngle) {
var sideLength = Math.sqrt(Math.pow(box[1][0] - box[0][0], 2) + Math.pow((box[1][1] - box[0][1]), 2)), var sideLength = Math.sqrt(Math.pow(box[1][0] - box[0][0], 2) + Math.pow((box[1][1] - box[0][1]), 2)),
@ -198,7 +201,7 @@ define([
extension, extension,
xdir = Math.sin(lineAngle), xdir = Math.sin(lineAngle),
ydir = Math.cos(lineAngle); ydir = Math.cos(lineAngle);
for ( i = 1; i < slices && result === null; i++) { for ( i = 1; i < slices && result === null; i++) {
// move line perpendicular to angle // move line perpendicular to angle
dir = sideLength / slices * i * (i % 2 === 0 ? -1 : 1); dir = sideLength / slices * i * (i % 2 === 0 ? -1 : 1);
@ -223,7 +226,7 @@ define([
} }
/** /**
* With the help of the configured readers (Code128 or EAN) this function tries to detect a * With the help of the configured readers (Code128 or EAN) this function tries to detect a
* valid barcode pattern within the given area. * valid barcode pattern within the given area.
* @param {Object} box The area to search in * @param {Object} box The area to search in
* @returns {Object} the result {codeResult, line, angle, pattern, threshold} * @returns {Object} the result {codeResult, line, angle, pattern, threshold}
@ -251,7 +254,7 @@ define([
if(result === null) { if(result === null) {
result = tryDecodeBruteForce(box, line, lineAngle); result = tryDecodeBruteForce(box, line, lineAngle);
} }
if(result === null) { if(result === null) {
return null; return null;
} }
@ -293,4 +296,4 @@ define([
}; };
return (BarcodeDecoder); return (BarcodeDecoder);
}); });

@ -5,7 +5,7 @@ define(["html_utils"], function(HtmlUtils) {
"use strict"; "use strict";
var streamRef, var streamRef,
loadedDataHandler; loadedDataHandler;
/** /**
* Wraps browser-specific getUserMedia * Wraps browser-specific getUserMedia
* @param {Object} constraints * @param {Object} constraints
@ -74,6 +74,8 @@ define(["html_utils"], function(HtmlUtils) {
videoConstraints = HtmlUtils.mergeObjects({ videoConstraints = HtmlUtils.mergeObjects({
width: 640, width: 640,
height: 480, height: 480,
minAspectRatio: 1,
maxAspectRatio: 1,
facing: "environment" facing: "environment"
}, config); }, config);
@ -89,7 +91,9 @@ define(["html_utils"], function(HtmlUtils) {
constraints.video = { constraints.video = {
mandatory: { mandatory: {
minWidth: videoConstraints.width, minWidth: videoConstraints.width,
minHeight: videoConstraints.height minHeight: videoConstraints.height,
minAspectRatio: videoConstraints.minAspectRatio,
maxAspectRatio: videoConstraints.maxAspectRatio
}, },
optional: [{ optional: [{
sourceId: videoSourceId sourceId: videoSourceId
@ -132,4 +136,4 @@ define(["html_utils"], function(HtmlUtils) {
streamRef = null; streamRef = null;
} }
}; };
}); });

@ -0,0 +1,59 @@
/* jshint undef: true, unused: true, browser:true, devel: true */
/* global define */
define(
[
"./code_39_reader"
],
function(Code39Reader) {
"use strict";
function Code39VINReader() {
Code39Reader.call(this);
}
var patterns = {
IOQ: /[IOQ]/g,
AZ09: /[A-Z0-9]{17}/
};
Code39VINReader.prototype = Object.create(Code39Reader.prototype);
Code39VINReader.prototype.constructor = Code39VINReader;
// Cribbed from:
// https://github.com/zxing/zxing/blob/master/core/src/main/java/com/google/zxing/client/result/VINResultParser.java
Code39VINReader.prototype._decode = function() {
var result = Code39Reader.prototype._decode.apply(this);
if (!result) {
return null;
}
var code = result.code;
if (!code) {
return;
}
code = code.replace(patterns.IOQ, '');
if (!code.match(patterns.AZ09)) {
console.log('Failed AZ09 pattern code:', code);
return null;
}
if (!this._checkChecksum(code)) {
return null;
}
result.code = code;
return result;
};
Code39VINReader.prototype._checkChecksum = function(code) {
// TODO
return !!code;
};
return (Code39VINReader);
}
);

@ -1,5 +1,5 @@
/** /**
* The basic configuration * The basic configuration
*/ */
define(function(){ define(function(){
@ -9,6 +9,8 @@ define(function(){
constraints: { constraints: {
width: 640, width: 640,
height: 480, height: 480,
minAspectRatio: 1,
maxAspectRatio: 1,
facing: "environment" // or user facing: "environment" // or user
} }
}, },
@ -46,6 +48,6 @@ define(function(){
} }
} }
}; };
return config; return config;
}); });

Binary file not shown.

Before

Width:  |  Height:  |  Size: 7.0 MiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 5.1 MiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 4.5 MiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 4.4 MiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 5.8 MiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 5.5 MiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 2.7 MiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 3.2 MiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 381 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 615 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 500 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 483 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 548 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 513 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 312 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 356 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 387 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 375 KiB

Loading…
Cancel
Save