Adding Code39VINReader

This commit is contained in:
Lindsey Simon
2015-05-11 10:46:55 -07:00
parent fc04683054
commit d362791f61
26 changed files with 205 additions and 49 deletions
+18 -15
View File
@@ -7,6 +7,7 @@ define([
'code_128_reader',
'ean_reader',
'code_39_reader',
'code_39_vin_reader',
'codabar_reader',
'upc_reader',
'ean_8_reader',
@@ -17,17 +18,19 @@ define([
Code128Reader,
EANReader,
Code39Reader,
Code39VINReader,
CodabarReader,
UPCReader,
EAN8Reader,
UPCEReader) {
"use strict";
var readers = {
code_128_reader: Code128Reader,
ean_reader: EANReader,
ean_8_reader: EAN8Reader,
code_39_reader: Code39Reader,
code_39_vin_reader: Code39VINReader,
codabar_reader: CodabarReader,
upc_reader: UPCReader,
upc_e_reader: UPCEReader
@@ -48,7 +51,7 @@ define([
},
_barcodeReaders = [],
_barcodeReader = null;
initCanvas();
initReaders();
initConfig();
@@ -113,9 +116,9 @@ define([
}
/**
* extend the line on both ends
* extend the line on both ends
* @param {Array} line
* @param {Number} angle
* @param {Number} angle
*/
function getExtendedLine(line, angle, ext) {
function extendLine(amount) {
@@ -141,7 +144,7 @@ define([
}
return line;
}
function getLine(box) {
return [{
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]
}];
}
function tryDecode(line) {
var result = null,
i,
barcodeLine = Bresenham.getBarcodeLine(inputImageWrapper, line[0], line[1]);
if (config.showFrequency) {
ImageDebug.drawPath(line, {x: 'x', y: 'y'}, _canvas.ctx.overlay, {color: 'red', lineWidth: 3});
Bresenham.debug.printFrequency(barcodeLine.line, _canvas.dom.frequency);
@@ -165,7 +168,7 @@ define([
if (config.showPattern) {
Bresenham.debug.printPattern(barcodeLine.line, _canvas.dom.pattern);
}
for ( i = 0; i < _barcodeReaders.length && result === null; i++) {
result = _barcodeReaders[i].decodePattern(barcodeLine.line);
if (result !== null) {
@@ -179,15 +182,15 @@ define([
codeResult: result,
barcodeLine: barcodeLine
};
}
/**
* 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
* @param {Array} box
* @param {Array} line
* @param {Number} lineAngle
* @param {Number} 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)),
@@ -198,7 +201,7 @@ define([
extension,
xdir = Math.sin(lineAngle),
ydir = Math.cos(lineAngle);
for ( i = 1; i < slices && result === null; i++) {
// move line perpendicular to angle
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.
* @param {Object} box The area to search in
* @returns {Object} the result {codeResult, line, angle, pattern, threshold}
@@ -251,7 +254,7 @@ define([
if(result === null) {
result = tryDecodeBruteForce(box, line, lineAngle);
}
if(result === null) {
return null;
}
@@ -293,4 +296,4 @@ define([
};
return (BarcodeDecoder);
});
});
+7 -3
View File
@@ -5,7 +5,7 @@ define(["html_utils"], function(HtmlUtils) {
"use strict";
var streamRef,
loadedDataHandler;
/**
* Wraps browser-specific getUserMedia
* @param {Object} constraints
@@ -74,6 +74,8 @@ define(["html_utils"], function(HtmlUtils) {
videoConstraints = HtmlUtils.mergeObjects({
width: 640,
height: 480,
minAspectRatio: 1,
maxAspectRatio: 1,
facing: "environment"
}, config);
@@ -89,7 +91,9 @@ define(["html_utils"], function(HtmlUtils) {
constraints.video = {
mandatory: {
minWidth: videoConstraints.width,
minHeight: videoConstraints.height
minHeight: videoConstraints.height,
minAspectRatio: videoConstraints.minAspectRatio,
maxAspectRatio: videoConstraints.maxAspectRatio
},
optional: [{
sourceId: videoSourceId
@@ -132,4 +136,4 @@ define(["html_utils"], function(HtmlUtils) {
streamRef = null;
}
};
});
});
+59
View File
@@ -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);
}
);
+5 -3
View File
@@ -1,5 +1,5 @@
/**
* The basic configuration
* The basic configuration
*/
define(function(){
@@ -9,6 +9,8 @@ define(function(){
constraints: {
width: 640,
height: 480,
minAspectRatio: 1,
maxAspectRatio: 1,
facing: "environment" // or user
}
},
@@ -46,6 +48,6 @@ define(function(){
}
}
};
return config;
});
});