Skip to content

Instantly share code, notes, and snippets.

@lgoldstein
Created October 2, 2013 05:09
Show Gist options
  • Save lgoldstein/6789366 to your computer and use it in GitHub Desktop.
Save lgoldstein/6789366 to your computer and use it in GitHub Desktop.
Incomplete porting of selectBestPatterns function in findpat.js for LazarSoft/jsqrcode code
diff --git a/demo/src/main/webapp/static/js/jsqrcode/findpat.js b/demo/src/main/webapp/static/js/jsqrcode/findpat.js
index 4d7c7dd..91495c3 100644
--- a/demo/src/main/webapp/static/js/jsqrcode/findpat.js
+++ b/demo/src/main/webapp/static/js/jsqrcode/findpat.js
@@ -384,7 +384,6 @@ function FinderPatternFinder()
this.selectBestPatterns=function()
{
-
var startSize = this.possibleCenters.length;
if (startSize < 3)
{
@@ -397,15 +396,33 @@ function FinderPatternFinder()
{
// But we can only afford to do so if we have at least 4 possibilities to choose from
var totalModuleSize = 0.0;
+ var square = 0.0;
for (var i = 0; i < startSize; i++)
{
- totalModuleSize += this.possibleCenters[i].EstimatedModuleSize;
+ var centerValue=this.possibleCenters[i].EstimatedModuleSize;
+ totalModuleSize += centerValue;
+ square += (centerValue * centerValue);
}
+
var average = totalModuleSize / startSize;
+ this.possibleCenters.sort(function(center1,center2) {
+ var dA=Math.abs(center2.EstimatedModuleSize - average);
+ var dB=Math.abs(center1.EstimatedModuleSize - average);
+ if (dA < dB) {
+ return (-1);
+ } else if (dA == dB) {
+ return 0;
+ } else {
+ return 1;
+ }
+ });
+
+ var stdDev = Math.sqrt(square / startSize - average * average);
+ var limit = Math.max(0.2 * average, stdDev);
for (var i = 0; i < this.possibleCenters.length && this.possibleCenters.length > 3; i++)
{
var pattern = this.possibleCenters[i];
- if (Math.abs(pattern.EstimatedModuleSize - average) > 0.2 * average)
+ if (Math.abs(pattern.EstimatedModuleSize - average) > limit)
{
this.possibleCenters.remove(i);
i--;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment