-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
18 changed files
with
3,967 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,279 @@ | ||
/* | ||
Ported to JavaScript by Lazar Laszlo 2011 | ||
lazarsoft@gmail.com, www.lazarsoft.info | ||
*/ | ||
|
||
/* | ||
* | ||
* Copyright 2007 ZXing authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
|
||
function AlignmentPattern(posX, posY, estimatedModuleSize) | ||
{ | ||
this.x=posX; | ||
this.y=posY; | ||
this.count = 1; | ||
this.estimatedModuleSize = estimatedModuleSize; | ||
|
||
this.__defineGetter__("EstimatedModuleSize", function() | ||
{ | ||
return this.estimatedModuleSize; | ||
}); | ||
this.__defineGetter__("Count", function() | ||
{ | ||
return this.count; | ||
}); | ||
this.__defineGetter__("X", function() | ||
{ | ||
return Math.floor(this.x); | ||
}); | ||
this.__defineGetter__("Y", function() | ||
{ | ||
return Math.floor(this.y); | ||
}); | ||
this.incrementCount = function() | ||
{ | ||
this.count++; | ||
} | ||
this.aboutEquals=function( moduleSize, i, j) | ||
{ | ||
if (Math.abs(i - this.y) <= moduleSize && Math.abs(j - this.x) <= moduleSize) | ||
{ | ||
var moduleSizeDiff = Math.abs(moduleSize - this.estimatedModuleSize); | ||
return moduleSizeDiff <= 1.0 || moduleSizeDiff / this.estimatedModuleSize <= 1.0; | ||
} | ||
return false; | ||
} | ||
|
||
} | ||
|
||
function AlignmentPatternFinder( image, startX, startY, width, height, moduleSize, resultPointCallback) | ||
{ | ||
this.image = image; | ||
this.possibleCenters = new Array(); | ||
this.startX = startX; | ||
this.startY = startY; | ||
this.width = width; | ||
this.height = height; | ||
this.moduleSize = moduleSize; | ||
this.crossCheckStateCount = new Array(0,0,0); | ||
this.resultPointCallback = resultPointCallback; | ||
|
||
this.centerFromEnd=function(stateCount, end) | ||
{ | ||
return (end - stateCount[2]) - stateCount[1] / 2.0; | ||
} | ||
this.foundPatternCross = function(stateCount) | ||
{ | ||
var moduleSize = this.moduleSize; | ||
var maxVariance = moduleSize / 2.0; | ||
for (var i = 0; i < 3; i++) | ||
{ | ||
if (Math.abs(moduleSize - stateCount[i]) >= maxVariance) | ||
{ | ||
return false; | ||
} | ||
} | ||
return true; | ||
} | ||
|
||
this.crossCheckVertical=function( startI, centerJ, maxCount, originalStateCountTotal) | ||
{ | ||
var image = this.image; | ||
|
||
var maxI = qrcode.height; | ||
var stateCount = this.crossCheckStateCount; | ||
stateCount[0] = 0; | ||
stateCount[1] = 0; | ||
stateCount[2] = 0; | ||
|
||
// Start counting up from center | ||
var i = startI; | ||
while (i >= 0 && image[centerJ + i*qrcode.width] && stateCount[1] <= maxCount) | ||
{ | ||
stateCount[1]++; | ||
i--; | ||
} | ||
// If already too many modules in this state or ran off the edge: | ||
if (i < 0 || stateCount[1] > maxCount) | ||
{ | ||
return NaN; | ||
} | ||
while (i >= 0 && !image[centerJ + i*qrcode.width] && stateCount[0] <= maxCount) | ||
{ | ||
stateCount[0]++; | ||
i--; | ||
} | ||
if (stateCount[0] > maxCount) | ||
{ | ||
return NaN; | ||
} | ||
|
||
// Now also count down from center | ||
i = startI + 1; | ||
while (i < maxI && image[centerJ + i*qrcode.width] && stateCount[1] <= maxCount) | ||
{ | ||
stateCount[1]++; | ||
i++; | ||
} | ||
if (i == maxI || stateCount[1] > maxCount) | ||
{ | ||
return NaN; | ||
} | ||
while (i < maxI && !image[centerJ + i*qrcode.width] && stateCount[2] <= maxCount) | ||
{ | ||
stateCount[2]++; | ||
i++; | ||
} | ||
if (stateCount[2] > maxCount) | ||
{ | ||
return NaN; | ||
} | ||
|
||
var stateCountTotal = stateCount[0] + stateCount[1] + stateCount[2]; | ||
if (5 * Math.abs(stateCountTotal - originalStateCountTotal) >= 2 * originalStateCountTotal) | ||
{ | ||
return NaN; | ||
} | ||
|
||
return this.foundPatternCross(stateCount)?this.centerFromEnd(stateCount, i):NaN; | ||
} | ||
|
||
this.handlePossibleCenter=function( stateCount, i, j) | ||
{ | ||
var stateCountTotal = stateCount[0] + stateCount[1] + stateCount[2]; | ||
var centerJ = this.centerFromEnd(stateCount, j); | ||
var centerI = this.crossCheckVertical(i, Math.floor (centerJ), 2 * stateCount[1], stateCountTotal); | ||
if (!isNaN(centerI)) | ||
{ | ||
var estimatedModuleSize = (stateCount[0] + stateCount[1] + stateCount[2]) / 3.0; | ||
var max = this.possibleCenters.length; | ||
for (var index = 0; index < max; index++) | ||
{ | ||
var center = this.possibleCenters[index]; | ||
// Look for about the same center and module size: | ||
if (center.aboutEquals(estimatedModuleSize, centerI, centerJ)) | ||
{ | ||
return new AlignmentPattern(centerJ, centerI, estimatedModuleSize); | ||
} | ||
} | ||
// Hadn't found this before; save it | ||
var point = new AlignmentPattern(centerJ, centerI, estimatedModuleSize); | ||
this.possibleCenters.push(point); | ||
if (this.resultPointCallback != null) | ||
{ | ||
this.resultPointCallback.foundPossibleResultPoint(point); | ||
} | ||
} | ||
return null; | ||
} | ||
|
||
this.find = function() | ||
{ | ||
var startX = this.startX; | ||
var height = this.height; | ||
var maxJ = startX + width; | ||
var middleI = startY + (height >> 1); | ||
// We are looking for black/white/black modules in 1:1:1 ratio; | ||
// this tracks the number of black/white/black modules seen so far | ||
var stateCount = new Array(0,0,0); | ||
for (var iGen = 0; iGen < height; iGen++) | ||
{ | ||
// Search from middle outwards | ||
var i = middleI + ((iGen & 0x01) == 0?((iGen + 1) >> 1):- ((iGen + 1) >> 1)); | ||
stateCount[0] = 0; | ||
stateCount[1] = 0; | ||
stateCount[2] = 0; | ||
var j = startX; | ||
// Burn off leading white pixels before anything else; if we start in the middle of | ||
// a white run, it doesn't make sense to count its length, since we don't know if the | ||
// white run continued to the left of the start point | ||
while (j < maxJ && !image[j + qrcode.width* i]) | ||
{ | ||
j++; | ||
} | ||
var currentState = 0; | ||
while (j < maxJ) | ||
{ | ||
if (image[j + i*qrcode.width]) | ||
{ | ||
// Black pixel | ||
if (currentState == 1) | ||
{ | ||
// Counting black pixels | ||
stateCount[currentState]++; | ||
} | ||
else | ||
{ | ||
// Counting white pixels | ||
if (currentState == 2) | ||
{ | ||
// A winner? | ||
if (this.foundPatternCross(stateCount)) | ||
{ | ||
// Yes | ||
var confirmed = this.handlePossibleCenter(stateCount, i, j); | ||
if (confirmed != null) | ||
{ | ||
return confirmed; | ||
} | ||
} | ||
stateCount[0] = stateCount[2]; | ||
stateCount[1] = 1; | ||
stateCount[2] = 0; | ||
currentState = 1; | ||
} | ||
else | ||
{ | ||
stateCount[++currentState]++; | ||
} | ||
} | ||
} | ||
else | ||
{ | ||
// White pixel | ||
if (currentState == 1) | ||
{ | ||
// Counting black pixels | ||
currentState++; | ||
} | ||
stateCount[currentState]++; | ||
} | ||
j++; | ||
} | ||
if (this.foundPatternCross(stateCount)) | ||
{ | ||
var confirmed = this.handlePossibleCenter(stateCount, i, maxJ); | ||
if (confirmed != null) | ||
{ | ||
return confirmed; | ||
} | ||
} | ||
} | ||
|
||
// Hmm, nothing we saw was observed and confirmed twice. If we had | ||
// any guess at all, return it. | ||
if (!(this.possibleCenters.length == 0)) | ||
{ | ||
return this.possibleCenters[0]; | ||
} | ||
|
||
throw "Couldn't find enough alignment patterns"; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
/* | ||
Ported to JavaScript by Lazar Laszlo 2011 | ||
lazarsoft@gmail.com, www.lazarsoft.info | ||
*/ | ||
|
||
/* | ||
* | ||
* Copyright 2007 ZXing authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
|
||
function BitMatrix( width, height) | ||
{ | ||
if(!height) | ||
height=width; | ||
if (width < 1 || height < 1) | ||
{ | ||
throw "Both dimensions must be greater than 0"; | ||
} | ||
this.width = width; | ||
this.height = height; | ||
var rowSize = width >> 5; | ||
if ((width & 0x1f) != 0) | ||
{ | ||
rowSize++; | ||
} | ||
this.rowSize = rowSize; | ||
this.bits = new Array(rowSize * height); | ||
for(var i=0;i<this.bits.length;i++) | ||
this.bits[i]=0; | ||
|
||
this.__defineGetter__("Width", function() | ||
{ | ||
return this.width; | ||
}); | ||
this.__defineGetter__("Height", function() | ||
{ | ||
return this.height; | ||
}); | ||
this.__defineGetter__("Dimension", function() | ||
{ | ||
if (this.width != this.height) | ||
{ | ||
throw "Can't call getDimension() on a non-square matrix"; | ||
} | ||
return this.width; | ||
}); | ||
|
||
this.get_Renamed=function( x, y) | ||
{ | ||
var offset = y * this.rowSize + (x >> 5); | ||
return ((URShift(this.bits[offset], (x & 0x1f))) & 1) != 0; | ||
} | ||
this.set_Renamed=function( x, y) | ||
{ | ||
var offset = y * this.rowSize + (x >> 5); | ||
this.bits[offset] |= 1 << (x & 0x1f); | ||
} | ||
this.flip=function( x, y) | ||
{ | ||
var offset = y * this.rowSize + (x >> 5); | ||
this.bits[offset] ^= 1 << (x & 0x1f); | ||
} | ||
this.clear=function() | ||
{ | ||
var max = this.bits.length; | ||
for (var i = 0; i < max; i++) | ||
{ | ||
this.bits[i] = 0; | ||
} | ||
} | ||
this.setRegion=function( left, top, width, height) | ||
{ | ||
if (top < 0 || left < 0) | ||
{ | ||
throw "Left and top must be nonnegative"; | ||
} | ||
if (height < 1 || width < 1) | ||
{ | ||
throw "Height and width must be at least 1"; | ||
} | ||
var right = left + width; | ||
var bottom = top + height; | ||
if (bottom > this.height || right > this.width) | ||
{ | ||
throw "The region must fit inside the matrix"; | ||
} | ||
for (var y = top; y < bottom; y++) | ||
{ | ||
var offset = y * this.rowSize; | ||
for (var x = left; x < right; x++) | ||
{ | ||
this.bits[offset + (x >> 5)] |= 1 << (x & 0x1f); | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.