Permalink
Browse files

moved debug code to code.QRCodeDecoder.decode() and implemented qrcod…

…e.QRCodeDecoder.findPattern()
  • Loading branch information...
1 parent 4b9bda6 commit 9ea0479b90e683182176b05eb9d2962a7a5e032c @yanbe committed Nov 11, 2008
Showing with 70 additions and 39 deletions.
  1. +2 −0 .gitignore
  2. +1 −0 Makefile
  3. +4 −11 QRCodeReader.as
  4. +1 −1 qrcode/DecodeResult.as
  5. +31 −14 qrcode/FinderPattern.as
  6. +31 −13 qrcode/QRCodeDecoder.as
View
@@ -0,0 +1,2 @@
+*.swf
+*.swp
View
@@ -1,2 +1,3 @@
all:
mxmlc QRCodeReader.as
+ open -a /Applications/Firefox.app QRCodeReader.swf
View
@@ -21,14 +21,14 @@ package {
if (camera!=null) {
camera.setMode(640, 480, 30);
- video = new Video(camera.width,camera.height);
+ video = new Video(camera.width/2,camera.height/2);
video.attachCamera(camera);
pixels = new BitmapData(video.width, video.height, false, 0x00000000);
createBitmap(pixels, 0, 0);
debug = new BitmapData(video.width, video.height, false, 0x00000000);
- createBitmap(debug, 640, 0);
+ createBitmap(debug, 320, 0);
addEventListener(Event.ENTER_FRAME, onEnterFrame);
- text = createTextField(0, 480, 640, 480);
+ text = createTextField(0, 240, 320, 240);
}
}
@@ -55,15 +55,8 @@ package {
private function onEnterFrame(event:Event):void {
pixels.draw(video);
- var result:DecodeResult = QRCodeDecoder.decode(pixels);
+ var result:DecodeResult = QRCodeDecoder.decode(pixels, debug);
text.text = result.text;
- debug.fillRect(new Rectangle(0,0,debug.width,debug.height), 0x00000000);
- debug.draw(result.debug);
- for each(var line:Object in result.across) {
- for (var i:int=0; i<line.offset; i++) {
- debug.setPixel(line.end.x-i, line.end.y, 0x00ff00);
- }
- }
}
}
}
@@ -7,7 +7,7 @@ package qrcode {
public var errorCorrectionLevel:uint;
public var text:String;
public var debug:BitmapData;
- public var across:Array;
+ public var acrossLines:Object;
public var pos:Object = {leftTop:Point, rightTop:Point,
leftBottom:Point, rightBottom:Point};
}
@@ -3,25 +3,42 @@ package qrcode {
import flash.geom.*;
public class FinderPattern {
+ private static const HORIZONTAL:int = 0;
+ private static const VERTICAL:int = 1;
+
public static function findPattern(pixels:BitmapData):Object {
- var linesAcross:Array = findLinesAcross(pixels);
- var centors:Array = findCenters(linesAcross);
- return {leftTop:new Point(10,10),
- rightTop:new Point(200,10),
- leftBottom:new Point(10,200),
- across:linesAcross};
+ var linesAcrossHorizontally:Array = findLinesAcross(pixels, HORIZONTAL);
+ var linesAcrossVertically:Array = findLinesAcross(pixels, VERTICAL);
+ return {leftTop: new Point(10,10),
+ rightTop: new Point(200,10),
+ leftBottom: new Point(10,200),
+ acrossLines: {vertical: linesAcrossVertically,
+ horizontal: linesAcrossHorizontally}
+ };
}
- private static function findLinesAcross(pixels:BitmapData):Array {
+ public static function findLinesAcross(pixels:BitmapData,
+ direction:int):Array {
+ var MAX_SIDE_PRIMARY:int;
+ var MAX_SIDE_SECONDARY:int;
+ if (direction==HORIZONTAL) {
+ MAX_SIDE_PRIMARY=pixels.width; MAX_SIDE_SECONDARY=pixels.height;
+ } else if (direction==VERTICAL) {
+ MAX_SIDE_PRIMARY=pixels.height; MAX_SIDE_SECONDARY=pixels.width;
+ }
+
var reference:Array = new Array(1, 1, 3, 1, 1);
var referenceSum:int = 7; //1+1+3+1+1
var recent:Array = new Array();
var linesAcross:Array = new Array();
- for (var y:int=0; y<pixels.height; y++) {
+
+ for (var b:int=0; b<MAX_SIDE_SECONDARY; b++) {
var last:int = 0;
var current:int = 0;
var length:int = 0;
- for (var x:int=0; x<pixels.width; x++) {
- current = pixels.getPixel(x, y);
+ for (var a:int=0; a<MAX_SIDE_PRIMARY; a++) {
+ current = direction==HORIZONTAL ? pixels.getPixel(a,b) :
+ pixels.getPixel(b,a);
+
if (current==last) {
length++;
} else {
@@ -45,16 +62,16 @@ package qrcode {
}
}
if (i==recent.length) {
- linesAcross.push({
- end:new Point(x-1,y),
- offset:recentSum});
+ var endPoint:Point = direction==HORIZONTAL ? new Point(a-1,b) :
+ new Point(b,a-1);
+ linesAcross.push({endPoint:endPoint, offset:recentSum});
}
}
}
length = 1;
last = current;
}
- :
+ }
recent = new Array();
length = 0;
}
@@ -4,25 +4,43 @@ package qrcode {
import flash.utils.*;
public class QRCodeDecoder {
- public static function decode(pixels:BitmapData):DecodeResult {
- var b:BitmapData = createBinaryImage(pixels);
- var patterns:Object = FinderPattern.findPattern(b);
+ public static function decode(pixels:BitmapData, debug:BitmapData=null):DecodeResult {
+ var binaryPixels:BitmapData = createBinaryPixels(pixels);
+ var patterns:Object = FinderPattern.findPattern(binaryPixels);
var result:DecodeResult = new DecodeResult();
- result.across = patterns.across;
+ result.acrossLines = patterns.acrossLines;
result.pos.leftTop = patterns.leftTop;
result.pos.rightTop = patterns.rightTop;
result.pos.leftBottom = patterns.leftBottom;
- result.text = "("+result.pos.leftTop.x+", "+result.pos.leftTop.y+") "+result.across.length;
- result.debug = b;
+ result.text = "horizontal:"+result.acrossLines.horizontal.length+
+ " vertical:"+result.acrossLines.vertical.length;
+
+ if (debug!=null) {
+ debug.fillRect(new Rectangle(0,0,debug.width,debug.height), 0x00000000);
+ debug.draw(binaryPixels);
+ var line:Object;
+ var i:int;
+ for each(line in result.acrossLines.horizontal) {
+ for (i=0; i<line.offset; i++) {
+ debug.setPixel(line.endPoint.x-i, line.endPoint.y, 0x00ff00);
+ }
+ }
+ for each(line in result.acrossLines.vertical) {
+ for (i=0; i<line.offset; i++) {
+ debug.setPixel(line.endPoint.x, line.endPoint.y-i, 0x00ff00);
+ }
+ }
+ }
+
return result;
}
- private static function createBinaryImage(pixels:BitmapData):BitmapData {
+ private static function createBinaryPixels(pixels:BitmapData):BitmapData {
var nDivision:int = 4;
- var b:BitmapData = new BitmapData(pixels.width, pixels.height);
+ var binaryPixels:BitmapData = new BitmapData(pixels.width, pixels.height);
- var areaWidth:int=b.width/nDivision;
- var areaHeight:int=b.height/nDivision;
+ var areaWidth:int=binaryPixels.width/nDivision;
+ var areaHeight:int=binaryPixels.height/nDivision;
for (var ay:int=0; ay<nDivision; ay++) {
for (var ax:int=0; ax<nDivision; ax++) {
@@ -41,11 +59,11 @@ package qrcode {
var color:uint = 0x00000000;
var maskColor:uint = 0x000000ff;
- b.threshold(pixels, rectangle, new Point(ax*areaWidth, ay*areaHeight), "<=", threshold, color,
- maskColor, false);
+ binaryPixels.threshold(pixels, rectangle, new Point(ax*areaWidth,
+ ay*areaHeight), "<=", threshold, color, maskColor, false);
}
}
- return b;
+ return binaryPixels;
}
}
}

0 comments on commit 9ea0479

Please sign in to comment.