From 5491b5b8084a4850f94cbd24bc049a45bcead2d4 Mon Sep 17 00:00:00 2001 From: Christoph Oberhofer Date: Thu, 14 Apr 2016 15:09:05 +0200 Subject: [PATCH] Experimenting with detecting the whole area --- src/common/geometric.js | 10 ++++++ src/detector/pdf_147_detector.js | 56 +++++++++++++++++++++++--------- src/reader/common.js | 4 +-- 3 files changed, 52 insertions(+), 18 deletions(-) diff --git a/src/common/geometric.js b/src/common/geometric.js index 336acd2..8e4a4aa 100644 --- a/src/common/geometric.js +++ b/src/common/geometric.js @@ -1,3 +1,6 @@ +const vec2 = { + clone: require('gl-vec2/clone') +}; export function getCenterLineFromBox(box) { return [{ @@ -41,3 +44,10 @@ export function getExtendedLine(inputImageWrapper, line, angle, ext) { } return line; } + +export function getPointOnLine(line, distance, angle) { + const x = distance * Math.cos(angle) + line[0].x, + y = distance * Math.sin(angle) + line[0].y; + + return vec2.clone([x, y]); +} diff --git a/src/detector/pdf_147_detector.js b/src/detector/pdf_147_detector.js index 3bf043a..f25869e 100644 --- a/src/detector/pdf_147_detector.js +++ b/src/detector/pdf_147_detector.js @@ -2,7 +2,8 @@ import { getCenterLineFromBox as getLine, getLineLength, getLineAngle, - getExtendedLine + getExtendedLine, + getPointOnLine } from '../common/geometric'; import ImageDebug from '../common/image_debug'; import Bresenham from '../decoder/bresenham'; @@ -11,29 +12,52 @@ import {findPattern} from '../reader/common'; const vec2 = { clone: require('gl-vec2/clone'), dot: require('gl-vec2/dot') -} +}; const START_PATTERN = [8, 1, 1, 1, 1, 1, 1, 3], STOP_PATTERN = [7, 1, 1, 3, 1, 1, 1, 2, 1], - MODULO = START_PATTERN.reduce((sum, i) => (sum + i),0); + MODULO = START_PATTERN.reduce((sum, i) => (sum + i), 0); + +function findTopLeft(line, angle, inputImageWrapper, ctx) { + let barcodeLine, + match, + pos = [ + vec2.clone([0, 0]), + vec2.clone([0, 0]) + ], + xdir = Math.sin(angle), + ydir = Math.cos(angle); + + do { + barcodeLine = Bresenham.getBarcodeLine(inputImageWrapper, line[0], line[1]); + if (ENV.development && ctx) { + ImageDebug.drawPath(line, {x: 'x', y: 'y'}, ctx, {color: 'red', lineWidth: 1}); + } + + Bresenham.toBinaryLine(barcodeLine); + match = findPattern(barcodeLine.line, START_PATTERN, {modulo: MODULO}); + if (match) { + pos[0] = getPointOnLine(line, match.start, angle); + pos[1] = getPointOnLine(line, match.end, angle); + line[0].x += xdir*10; + line[0].y -= ydir*10; + line[1].x += xdir*10; + line[1].y -= ydir*10; + } + ImageDebug.drawVertices(pos, {x: 0, y: 1}, ctx, {color: 'blue', lineWidth: 1}); + } while (match); + + return pos; +} export default function detect(inputImageWrapper, box, ctx) { let line = getLine(box), lineLength = getLineLength(line), lineAngle = getLineAngle(line), - extendedLine = getExtendedLine(inputImageWrapper, line, lineAngle, Math.floor(lineLength * 0.15)), - barcodeLine = Bresenham.getBarcodeLine(inputImageWrapper, extendedLine[0], extendedLine[1]); - - if (ENV.development) { - if (ctx) { - ImageDebug.drawPath(extendedLine, {x: 'x', y: 'y'}, ctx, {color: 'red', lineWidth: 1}); - } - } - - Bresenham.toBinaryLine(barcodeLine); + extendedLine = getExtendedLine(inputImageWrapper, line, lineAngle, Math.floor(lineLength * 0.15)); - const match = findPattern(barcodeLine.line, START_PATTERN, {modulo: MODULO}); - console.log(match); + var topLeft = findTopLeft(extendedLine, lineAngle, inputImageWrapper, ctx); + console.log(topLeft); return [ vec2.clone([199, 84]), @@ -44,5 +68,5 @@ export default function detect(inputImageWrapper, box, ctx) { vec2.clone([310, 295]), vec2.clone([554, 83]), vec2.clone([580, 277]) - ] + ]; }; diff --git a/src/reader/common.js b/src/reader/common.js index 5f21e42..e0d6df7 100644 --- a/src/reader/common.js +++ b/src/reader/common.js @@ -60,8 +60,8 @@ export function findPattern(row, pattern, { offset, isWhite=false, tryHarder=true, - epsilon=0.5, - maxSingleError=0.5, + epsilon=0.8, + maxSingleError=0.8, modulo}) { var counter = [], i,