initial commit
This commit is contained in:
467
reader/QRCodeDataBlockReader.cs
Normal file
467
reader/QRCodeDataBlockReader.cs
Normal file
@@ -0,0 +1,467 @@
|
||||
using System;
|
||||
using QRCodeDecoder = ThoughtWorks.QRCode.Codec.QRCodeDecoder;
|
||||
using InvalidDataBlockException = ThoughtWorks.QRCode.ExceptionHandler.InvalidDataBlockException;
|
||||
using DebugCanvas = ThoughtWorks.QRCode.Codec.Util.DebugCanvas;
|
||||
using SystemUtils = ThoughtWorks.QRCode.Codec.Util.SystemUtils;
|
||||
namespace ThoughtWorks.QRCode.Codec.Reader
|
||||
{
|
||||
|
||||
public class QRCodeDataBlockReader
|
||||
{
|
||||
virtual internal int NextMode
|
||||
{
|
||||
get
|
||||
{
|
||||
//canvas.println("data blocks:"+ (blocks.length - numErrorCorrectionCode));
|
||||
if ((blockPointer > blocks.Length - numErrorCorrectionCode - 2))
|
||||
return 0;
|
||||
else
|
||||
return getNextBits(4);
|
||||
}
|
||||
|
||||
}
|
||||
virtual public sbyte[] DataByte
|
||||
{
|
||||
get
|
||||
{
|
||||
canvas.println("Reading data blocks.");
|
||||
System.IO.MemoryStream output = new System.IO.MemoryStream();
|
||||
|
||||
try
|
||||
{
|
||||
do
|
||||
{
|
||||
int mode = NextMode;
|
||||
//canvas.println("mode: " + mode);
|
||||
if (mode == 0)
|
||||
{
|
||||
if (output.Length > 0)
|
||||
break;
|
||||
else
|
||||
throw new InvalidDataBlockException("Empty data block");
|
||||
}
|
||||
//if (mode != 1 && mode != 2 && mode != 4 && mode != 8)
|
||||
// break;
|
||||
//}
|
||||
if (mode != MODE_NUMBER && mode != MODE_ROMAN_AND_NUMBER && mode != MODE_8BIT_BYTE && mode != MODE_KANJI)
|
||||
{
|
||||
/* canvas.println("Invalid mode: " + mode);
|
||||
mode = guessMode(mode);
|
||||
canvas.println("Guessed mode: " + mode); */
|
||||
throw new InvalidDataBlockException("Invalid mode: " + mode + " in (block:" + blockPointer + " bit:" + bitPointer + ")");
|
||||
}
|
||||
dataLength = getDataLength(mode);
|
||||
if (dataLength < 1)
|
||||
throw new InvalidDataBlockException("Invalid data length: " + dataLength);
|
||||
//canvas.println("length: " + dataLength);
|
||||
switch (mode)
|
||||
{
|
||||
|
||||
case MODE_NUMBER:
|
||||
//canvas.println("Mode: Figure");
|
||||
sbyte[] temp_sbyteArray;
|
||||
temp_sbyteArray = SystemUtils.ToSByteArray(SystemUtils.ToByteArray(getFigureString(dataLength)));
|
||||
output.Write(SystemUtils.ToByteArray(temp_sbyteArray), 0, temp_sbyteArray.Length);
|
||||
break;
|
||||
|
||||
case MODE_ROMAN_AND_NUMBER:
|
||||
//canvas.println("Mode: Roman&Figure");
|
||||
sbyte[] temp_sbyteArray2;
|
||||
temp_sbyteArray2 = SystemUtils.ToSByteArray(SystemUtils.ToByteArray(getRomanAndFigureString(dataLength)));
|
||||
output.Write(SystemUtils.ToByteArray(temp_sbyteArray2), 0, temp_sbyteArray2.Length);
|
||||
break;
|
||||
|
||||
case MODE_8BIT_BYTE:
|
||||
//canvas.println("Mode: 8bit Byte");
|
||||
sbyte[] temp_sbyteArray3;
|
||||
temp_sbyteArray3 = get8bitByteArray(dataLength);
|
||||
output.Write(SystemUtils.ToByteArray(temp_sbyteArray3), 0, temp_sbyteArray3.Length);
|
||||
break;
|
||||
|
||||
case MODE_KANJI:
|
||||
//canvas.println("Mode: Kanji");
|
||||
sbyte[] temp_sbyteArray4;
|
||||
temp_sbyteArray4 = SystemUtils.ToSByteArray(SystemUtils.ToByteArray(getKanjiString(dataLength)));
|
||||
output.Write(SystemUtils.ToByteArray(temp_sbyteArray4), 0, temp_sbyteArray4.Length);
|
||||
break;
|
||||
}
|
||||
//
|
||||
//canvas.println("DataLength: " + dataLength);
|
||||
//Console.out.println(dataString);
|
||||
}
|
||||
while (true);
|
||||
}
|
||||
catch (System.IndexOutOfRangeException e)
|
||||
{
|
||||
SystemUtils.WriteStackTrace(e, Console.Error);
|
||||
throw new InvalidDataBlockException("Data Block Error in (block:" + blockPointer + " bit:" + bitPointer + ")");
|
||||
}
|
||||
catch (System.IO.IOException e)
|
||||
{
|
||||
throw new InvalidDataBlockException(e.Message);
|
||||
}
|
||||
return SystemUtils.ToSByteArray(output.ToArray());
|
||||
}
|
||||
|
||||
}
|
||||
virtual public String DataString
|
||||
{
|
||||
get
|
||||
{
|
||||
canvas.println("Reading data blocks...");
|
||||
String dataString = "";
|
||||
do
|
||||
{
|
||||
int mode = NextMode;
|
||||
canvas.println("mode: " + mode);
|
||||
if (mode == 0)
|
||||
break;
|
||||
//if (mode != 1 && mode != 2 && mode != 4 && mode != 8)
|
||||
// break;
|
||||
//}
|
||||
if (mode != MODE_NUMBER && mode != MODE_ROMAN_AND_NUMBER && mode != MODE_8BIT_BYTE && mode != MODE_KANJI)
|
||||
{
|
||||
// mode = guessMode(mode);
|
||||
// do not guesswork
|
||||
//Console.out.println("guessed mode: " + mode);
|
||||
}
|
||||
|
||||
dataLength = getDataLength(mode);
|
||||
canvas.println(System.Convert.ToString(blocks[blockPointer]));
|
||||
System.Console.Out.WriteLine("length: " + dataLength);
|
||||
switch (mode)
|
||||
{
|
||||
|
||||
case MODE_NUMBER:
|
||||
//canvas.println("Mode: Figure");
|
||||
dataString += getFigureString(dataLength);
|
||||
break;
|
||||
|
||||
case MODE_ROMAN_AND_NUMBER:
|
||||
//canvas.println("Mode: Roman&Figure");
|
||||
dataString += getRomanAndFigureString(dataLength);
|
||||
break;
|
||||
|
||||
case MODE_8BIT_BYTE:
|
||||
//canvas.println("Mode: 8bit Byte");
|
||||
dataString += get8bitByteString(dataLength);
|
||||
break;
|
||||
|
||||
case MODE_KANJI:
|
||||
//canvas.println("Mode: Kanji");
|
||||
dataString += getKanjiString(dataLength);
|
||||
break;
|
||||
}
|
||||
//canvas.println("DataLength: " + dataLength);
|
||||
//Console.out.println(dataString);
|
||||
}
|
||||
while (true);
|
||||
System.Console.Out.WriteLine("");
|
||||
return dataString;
|
||||
}
|
||||
|
||||
}
|
||||
internal int[] blocks;
|
||||
internal int dataLengthMode;
|
||||
internal int blockPointer;
|
||||
internal int bitPointer;
|
||||
internal int dataLength;
|
||||
internal int numErrorCorrectionCode;
|
||||
internal DebugCanvas canvas;
|
||||
|
||||
const int MODE_NUMBER = 1;
|
||||
const int MODE_ROMAN_AND_NUMBER = 2;
|
||||
const int MODE_8BIT_BYTE = 4;
|
||||
const int MODE_KANJI = 8;
|
||||
int[][] sizeOfDataLengthInfo = new int[][] { new int[] { 10, 9, 8, 8 }, new int[] { 12, 11, 16, 10 }, new int[] { 14, 13, 16, 12 } };
|
||||
|
||||
public QRCodeDataBlockReader(int[] blocks, int version, int numErrorCorrectionCode)
|
||||
{
|
||||
blockPointer = 0;
|
||||
bitPointer = 7;
|
||||
dataLength = 0;
|
||||
this.blocks = blocks;
|
||||
this.numErrorCorrectionCode = numErrorCorrectionCode;
|
||||
if (version <= 9)
|
||||
dataLengthMode = 0;
|
||||
else if (version >= 10 && version <= 26)
|
||||
dataLengthMode = 1;
|
||||
else if (version >= 27 && version <= 40)
|
||||
dataLengthMode = 2;
|
||||
canvas = QRCodeDecoder.Canvas;
|
||||
}
|
||||
|
||||
internal virtual int getNextBits(int numBits)
|
||||
{
|
||||
int bits = 0;
|
||||
if (numBits < bitPointer + 1)
|
||||
{
|
||||
// next word fits into current data block
|
||||
int mask = 0;
|
||||
for (int i = 0; i < numBits; i++)
|
||||
{
|
||||
mask += (1 << i);
|
||||
}
|
||||
mask <<= (bitPointer - numBits + 1);
|
||||
|
||||
bits = (blocks[blockPointer] & mask) >> (bitPointer - numBits + 1);
|
||||
bitPointer -= numBits;
|
||||
return bits;
|
||||
}
|
||||
else if (numBits < bitPointer + 1 + 8)
|
||||
{
|
||||
// next word crosses 2 data blocks
|
||||
int mask1 = 0;
|
||||
for (int i = 0; i < bitPointer + 1; i++)
|
||||
{
|
||||
mask1 += (1 << i);
|
||||
}
|
||||
bits = (blocks[blockPointer] & mask1) << (numBits - (bitPointer + 1));
|
||||
blockPointer++;
|
||||
bits += ((blocks[blockPointer]) >> (8 - (numBits - (bitPointer + 1))));
|
||||
|
||||
bitPointer = bitPointer - numBits % 8;
|
||||
if (bitPointer < 0)
|
||||
{
|
||||
bitPointer = 8 + bitPointer;
|
||||
}
|
||||
return bits;
|
||||
}
|
||||
else if (numBits < bitPointer + 1 + 16)
|
||||
{
|
||||
// next word crosses 3 data blocks
|
||||
int mask1 = 0; // mask of first block
|
||||
int mask3 = 0; // mask of 3rd block
|
||||
//bitPointer + 1 : number of bits of the 1st block
|
||||
//8 : number of the 2nd block (note that use already 8bits because next word uses 3 data blocks)
|
||||
//numBits - (bitPointer + 1 + 8) : number of bits of the 3rd block
|
||||
for (int i = 0; i < bitPointer + 1; i++)
|
||||
{
|
||||
mask1 += (1 << i);
|
||||
}
|
||||
int bitsFirstBlock = (blocks[blockPointer] & mask1) << (numBits - (bitPointer + 1));
|
||||
blockPointer++;
|
||||
|
||||
int bitsSecondBlock = blocks[blockPointer] << (numBits - (bitPointer + 1 + 8));
|
||||
blockPointer++;
|
||||
|
||||
for (int i = 0; i < numBits - (bitPointer + 1 + 8); i++)
|
||||
{
|
||||
mask3 += (1 << i);
|
||||
}
|
||||
mask3 <<= 8 - (numBits - (bitPointer + 1 + 8));
|
||||
int bitsThirdBlock = (blocks[blockPointer] & mask3) >> (8 - (numBits - (bitPointer + 1 + 8)));
|
||||
|
||||
bits = bitsFirstBlock + bitsSecondBlock + bitsThirdBlock;
|
||||
bitPointer = bitPointer - (numBits - 8) % 8;
|
||||
if (bitPointer < 0)
|
||||
{
|
||||
bitPointer = 8 + bitPointer;
|
||||
}
|
||||
return bits;
|
||||
}
|
||||
else
|
||||
{
|
||||
System.Console.Out.WriteLine("ERROR!");
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
internal virtual int guessMode(int mode)
|
||||
{
|
||||
//correct modes: 0001 0010 0100 1000
|
||||
//possible data: 0000 0011 0101 1001 0110 1010 1100
|
||||
// 0111 1101 1011 1110 1111
|
||||
// MODE_NUMBER = 1;
|
||||
// MODE_ROMAN_AND_NUMBER = 2;
|
||||
// MODE_8BIT_BYTE = 4;
|
||||
// MODE_KANJI = 8;
|
||||
switch (mode)
|
||||
{
|
||||
|
||||
case 3:
|
||||
return MODE_NUMBER;
|
||||
|
||||
case 5:
|
||||
return MODE_8BIT_BYTE;
|
||||
|
||||
case 6:
|
||||
return MODE_8BIT_BYTE;
|
||||
|
||||
case 7:
|
||||
return MODE_8BIT_BYTE;
|
||||
|
||||
case 9:
|
||||
return MODE_KANJI;
|
||||
|
||||
case 10:
|
||||
return MODE_KANJI;
|
||||
|
||||
case 11:
|
||||
return MODE_KANJI;
|
||||
|
||||
case 12:
|
||||
return MODE_8BIT_BYTE;
|
||||
|
||||
case 13:
|
||||
return MODE_8BIT_BYTE;
|
||||
|
||||
case 14:
|
||||
return MODE_8BIT_BYTE;
|
||||
|
||||
case 15:
|
||||
return MODE_8BIT_BYTE;
|
||||
|
||||
default:
|
||||
return MODE_KANJI;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
internal virtual int getDataLength(int modeIndicator)
|
||||
{
|
||||
int index = 0;
|
||||
while (true)
|
||||
{
|
||||
if ((modeIndicator >> index) == 1)
|
||||
break;
|
||||
index++;
|
||||
}
|
||||
|
||||
return getNextBits(sizeOfDataLengthInfo[dataLengthMode][index]);
|
||||
}
|
||||
|
||||
|
||||
internal virtual String getFigureString(int dataLength)
|
||||
{
|
||||
int length = dataLength;
|
||||
int intData = 0;
|
||||
String strData = "";
|
||||
do
|
||||
{
|
||||
if (length >= 3)
|
||||
{
|
||||
intData = getNextBits(10);
|
||||
if (intData < 100)
|
||||
strData += "0";
|
||||
if (intData < 10)
|
||||
strData += "0";
|
||||
length -= 3;
|
||||
}
|
||||
else if (length == 2)
|
||||
{
|
||||
intData = getNextBits(7);
|
||||
if (intData < 10)
|
||||
strData += "0";
|
||||
length -= 2;
|
||||
}
|
||||
else if (length == 1)
|
||||
{
|
||||
intData = getNextBits(4);
|
||||
length -= 1;
|
||||
}
|
||||
strData += System.Convert.ToString(intData);
|
||||
}
|
||||
while (length > 0);
|
||||
|
||||
return strData;
|
||||
}
|
||||
|
||||
internal virtual String getRomanAndFigureString(int dataLength)
|
||||
{
|
||||
int length = dataLength;
|
||||
int intData = 0;
|
||||
String strData = "";
|
||||
char[] tableRomanAndFigure = new char[]{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', ' ', '$', '%', '*', '+', '-', '.', '/', ':'};
|
||||
do
|
||||
{
|
||||
if (length > 1)
|
||||
{
|
||||
intData = getNextBits(11);
|
||||
int firstLetter = intData / 45;
|
||||
int secondLetter = intData % 45;
|
||||
strData += System.Convert.ToString(tableRomanAndFigure[firstLetter]);
|
||||
strData += System.Convert.ToString(tableRomanAndFigure[secondLetter]);
|
||||
length -= 2;
|
||||
}
|
||||
else if (length == 1)
|
||||
{
|
||||
intData = getNextBits(6);
|
||||
strData += System.Convert.ToString(tableRomanAndFigure[intData]);
|
||||
length -= 1;
|
||||
}
|
||||
}
|
||||
while (length > 0);
|
||||
|
||||
return strData;
|
||||
}
|
||||
|
||||
public virtual sbyte[] get8bitByteArray(int dataLength)
|
||||
{
|
||||
int length = dataLength;
|
||||
int intData = 0;
|
||||
System.IO.MemoryStream output = new System.IO.MemoryStream();
|
||||
|
||||
do
|
||||
{
|
||||
canvas.println("Length: " + length);
|
||||
intData = getNextBits(8);
|
||||
output.WriteByte((byte) intData);
|
||||
length--;
|
||||
}
|
||||
while (length > 0);
|
||||
return SystemUtils.ToSByteArray(output.ToArray());
|
||||
}
|
||||
|
||||
internal virtual String get8bitByteString(int dataLength)
|
||||
{
|
||||
int length = dataLength;
|
||||
int intData = 0;
|
||||
String strData = "";
|
||||
do
|
||||
{
|
||||
intData = getNextBits(8);
|
||||
strData += (char) intData;
|
||||
length--;
|
||||
}
|
||||
while (length > 0);
|
||||
return strData;
|
||||
}
|
||||
|
||||
internal virtual String getKanjiString(int dataLength)
|
||||
{
|
||||
int length = dataLength;
|
||||
int intData = 0;
|
||||
String unicodeString = "";
|
||||
do
|
||||
{
|
||||
intData = getNextBits(13);
|
||||
int lowerByte = intData % 0xC0;
|
||||
int higherByte = intData / 0xC0;
|
||||
|
||||
int tempWord = (higherByte << 8) + lowerByte;
|
||||
int shiftjisWord = 0;
|
||||
if (tempWord + 0x8140 <= 0x9FFC)
|
||||
{
|
||||
// between 8140 - 9FFC on Shift_JIS character set
|
||||
shiftjisWord = tempWord + 0x8140;
|
||||
}
|
||||
else
|
||||
{
|
||||
// between E040 - EBBF on Shift_JIS character set
|
||||
shiftjisWord = tempWord + 0xC140;
|
||||
}
|
||||
|
||||
sbyte[] tempByte = new sbyte[2];
|
||||
tempByte[0] = (sbyte) (shiftjisWord >> 8);
|
||||
tempByte[1] = (sbyte) (shiftjisWord & 0xFF);
|
||||
unicodeString += new String(SystemUtils.ToCharArray(SystemUtils.ToByteArray(tempByte)));
|
||||
length--;
|
||||
}
|
||||
while (length > 0);
|
||||
|
||||
|
||||
return unicodeString;
|
||||
}
|
||||
}
|
||||
}
|
||||
970
reader/QRCodeImageReader.cs
Normal file
970
reader/QRCodeImageReader.cs
Normal file
@@ -0,0 +1,970 @@
|
||||
using System;
|
||||
using QRCodeDecoder = ThoughtWorks.QRCode.Codec.QRCodeDecoder;
|
||||
using ThoughtWorks.QRCode.Codec.Data;
|
||||
using AlignmentPatternNotFoundException = ThoughtWorks.QRCode.ExceptionHandler.AlignmentPatternNotFoundException;
|
||||
using FinderPatternNotFoundException = ThoughtWorks.QRCode.ExceptionHandler.FinderPatternNotFoundException;
|
||||
using SymbolNotFoundException = ThoughtWorks.QRCode.ExceptionHandler.SymbolNotFoundException;
|
||||
using InvalidVersionException = ThoughtWorks.QRCode.ExceptionHandler.InvalidVersionException;
|
||||
using VersionInformationException = ThoughtWorks.QRCode.ExceptionHandler.VersionInformationException;
|
||||
using ThoughtWorks.QRCode.Geom;
|
||||
using ThoughtWorks.QRCode.Codec.Reader.Pattern;
|
||||
using ThoughtWorks.QRCode.Codec.Util;
|
||||
using SystemUtils = ThoughtWorks.QRCode.Codec.Util.SystemUtils;
|
||||
|
||||
namespace ThoughtWorks.QRCode.Codec.Reader
|
||||
{
|
||||
|
||||
public class QRCodeImageReader
|
||||
{
|
||||
internal DebugCanvas canvas;
|
||||
//boolean[][] image;
|
||||
//DP =
|
||||
//23 ...side pixels of image will be limited maximum 255 (8 bits)
|
||||
//22 .. side pixels of image will be limited maximum 511 (9 bits)
|
||||
//21 .. side pixels of image will be limited maximum 1023 (10 bits)
|
||||
|
||||
//I think it's good idea to use DECIMAL_POINT with type "long" too.
|
||||
|
||||
public static int DECIMAL_POINT = 21;
|
||||
public const bool POINT_DARK = true;
|
||||
public const bool POINT_LIGHT = false;
|
||||
internal SamplingGrid samplingGrid;
|
||||
internal bool[][] bitmap;
|
||||
|
||||
|
||||
public QRCodeImageReader()
|
||||
{
|
||||
this.canvas = QRCodeDecoder.Canvas;
|
||||
}
|
||||
|
||||
|
||||
private class ModulePitch
|
||||
{
|
||||
public int top;
|
||||
public int left;
|
||||
public int bottom;
|
||||
public int right;
|
||||
|
||||
public ModulePitch(QRCodeImageReader enclosingInstance)
|
||||
{
|
||||
InitBlock(enclosingInstance);
|
||||
}
|
||||
private void InitBlock(QRCodeImageReader enclosingInstance)
|
||||
{
|
||||
this.enclosingInstance = enclosingInstance;
|
||||
}
|
||||
private QRCodeImageReader enclosingInstance;
|
||||
public QRCodeImageReader Enclosing_Instance
|
||||
{
|
||||
get
|
||||
{
|
||||
return enclosingInstance;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
internal virtual bool[][] applyMedianFilter(bool[][] image, int threshold)
|
||||
{
|
||||
bool[][] filteredMatrix = new bool[image.Length][];
|
||||
for (int i = 0; i < image.Length; i++)
|
||||
{
|
||||
filteredMatrix[i] = new bool[image[0].Length];
|
||||
}
|
||||
//filtering noise in image with median filter
|
||||
int numPointDark;
|
||||
for (int y = 1; y < image[0].Length - 1; y++)
|
||||
{
|
||||
for (int x = 1; x < image.Length - 1; x++)
|
||||
{
|
||||
//if (image[x][y] == true) {
|
||||
numPointDark = 0;
|
||||
for (int fy = - 1; fy < 2; fy++)
|
||||
{
|
||||
for (int fx = - 1; fx < 2; fx++)
|
||||
{
|
||||
if (image[x + fx][y + fy] == true)
|
||||
{
|
||||
numPointDark++;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (numPointDark > threshold)
|
||||
filteredMatrix[x][y] = POINT_DARK;
|
||||
}
|
||||
}
|
||||
|
||||
return filteredMatrix;
|
||||
}
|
||||
internal virtual bool[][] applyCrossMaskingMedianFilter(bool[][] image, int threshold)
|
||||
{
|
||||
bool[][] filteredMatrix = new bool[image.Length][];
|
||||
for (int i = 0; i < image.Length; i++)
|
||||
{
|
||||
filteredMatrix[i] = new bool[image[0].Length];
|
||||
}
|
||||
//filtering noise in image with median filter
|
||||
int numPointDark;
|
||||
for (int y = 2; y < image[0].Length - 2; y++)
|
||||
{
|
||||
for (int x = 2; x < image.Length - 2; x++)
|
||||
{
|
||||
//if (image[x][y] == true) {
|
||||
numPointDark = 0;
|
||||
for (int f = - 2; f < 3; f++)
|
||||
{
|
||||
if (image[x + f][y] == true)
|
||||
numPointDark++;
|
||||
|
||||
if (image[x][y + f] == true)
|
||||
numPointDark++;
|
||||
}
|
||||
|
||||
if (numPointDark > threshold)
|
||||
filteredMatrix[x][y] = POINT_DARK;
|
||||
}
|
||||
}
|
||||
|
||||
return filteredMatrix;
|
||||
}
|
||||
internal virtual bool[][] filterImage(int[][] image)
|
||||
{
|
||||
imageToGrayScale(image);
|
||||
bool[][] bitmap = grayScaleToBitmap(image);
|
||||
return bitmap;
|
||||
}
|
||||
|
||||
internal virtual void imageToGrayScale(int[][] image)
|
||||
{
|
||||
for (int y = 0; y < image[0].Length; y++)
|
||||
{
|
||||
for (int x = 0; x < image.Length; x++)
|
||||
{
|
||||
int r = image[x][y] >> 16 & 0xFF;
|
||||
int g = image[x][y] >> 8 & 0xFF;
|
||||
int b = image[x][y] & 0xFF;
|
||||
int m = (r * 30 + g * 59 + b * 11) / 100;
|
||||
image[x][y] = m;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
internal virtual bool[][] grayScaleToBitmap(int[][] grayScale)
|
||||
{
|
||||
int[][] middle = getMiddleBrightnessPerArea(grayScale);
|
||||
int sqrtNumArea = middle.Length;
|
||||
int areaWidth = grayScale.Length / sqrtNumArea;
|
||||
int areaHeight = grayScale[0].Length / sqrtNumArea;
|
||||
bool[][] bitmap = new bool[grayScale.Length][];
|
||||
for (int i = 0; i < grayScale.Length; i++)
|
||||
{
|
||||
bitmap[i] = new bool[grayScale[0].Length];
|
||||
}
|
||||
|
||||
for (int ay = 0; ay < sqrtNumArea; ay++)
|
||||
{
|
||||
for (int ax = 0; ax < sqrtNumArea; ax++)
|
||||
{
|
||||
for (int dy = 0; dy < areaHeight; dy++)
|
||||
{
|
||||
for (int dx = 0; dx < areaWidth; dx++)
|
||||
{
|
||||
bitmap[areaWidth * ax + dx][areaHeight * ay + dy] = (grayScale[areaWidth * ax + dx][areaHeight * ay + dy] < middle[ax][ay])?true:false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return bitmap;
|
||||
}
|
||||
|
||||
internal virtual int[][] getMiddleBrightnessPerArea(int[][] image)
|
||||
{
|
||||
int numSqrtArea = 4;
|
||||
//obtain middle brightness((min + max) / 2) per area
|
||||
int areaWidth = image.Length / numSqrtArea;
|
||||
int areaHeight = image[0].Length / numSqrtArea;
|
||||
int[][][] minmax = new int[numSqrtArea][][];
|
||||
for (int i = 0; i < numSqrtArea; i++)
|
||||
{
|
||||
minmax[i] = new int[numSqrtArea][];
|
||||
for (int i2 = 0; i2 < numSqrtArea; i2++)
|
||||
{
|
||||
minmax[i][i2] = new int[2];
|
||||
}
|
||||
}
|
||||
for (int ay = 0; ay < numSqrtArea; ay++)
|
||||
{
|
||||
for (int ax = 0; ax < numSqrtArea; ax++)
|
||||
{
|
||||
minmax[ax][ay][0] = 0xFF;
|
||||
for (int dy = 0; dy < areaHeight; dy++)
|
||||
{
|
||||
for (int dx = 0; dx < areaWidth; dx++)
|
||||
{
|
||||
int target = image[areaWidth * ax + dx][areaHeight * ay + dy];
|
||||
if (target < minmax[ax][ay][0])
|
||||
minmax[ax][ay][0] = target;
|
||||
if (target > minmax[ax][ay][1])
|
||||
minmax[ax][ay][1] = target;
|
||||
}
|
||||
}
|
||||
//minmax[ax][ay][0] = (minmax[ax][ay][0] + minmax[ax][ay][1]) / 2;
|
||||
}
|
||||
}
|
||||
int[][] middle = new int[numSqrtArea][];
|
||||
for (int i3 = 0; i3 < numSqrtArea; i3++)
|
||||
{
|
||||
middle[i3] = new int[numSqrtArea];
|
||||
}
|
||||
for (int ay = 0; ay < numSqrtArea; ay++)
|
||||
{
|
||||
for (int ax = 0; ax < numSqrtArea; ax++)
|
||||
{
|
||||
middle[ax][ay] = (minmax[ax][ay][0] + minmax[ax][ay][1]) / 2;
|
||||
//Console.out.print(middle[ax][ay] + ",");
|
||||
}
|
||||
//Console.out.println("");
|
||||
}
|
||||
//Console.out.println("");
|
||||
|
||||
return middle;
|
||||
}
|
||||
|
||||
public virtual QRCodeSymbol getQRCodeSymbol(int[][] image)
|
||||
{
|
||||
int longSide = (image.Length < image[0].Length)?image[0].Length:image.Length;
|
||||
QRCodeImageReader.DECIMAL_POINT = 23 - QRCodeUtility.sqrt(longSide / 256);
|
||||
bitmap = filterImage(image);
|
||||
canvas.println("Drawing matrix.");
|
||||
canvas.drawMatrix(bitmap);
|
||||
|
||||
canvas.println("Scanning Finder Pattern.");
|
||||
FinderPattern finderPattern = null;
|
||||
try
|
||||
{
|
||||
finderPattern = FinderPattern.findFinderPattern(bitmap);
|
||||
}
|
||||
catch (FinderPatternNotFoundException e)
|
||||
{
|
||||
canvas.println("Not found, now retrying...");
|
||||
bitmap = applyCrossMaskingMedianFilter(bitmap, 5);
|
||||
canvas.drawMatrix(bitmap);
|
||||
for (int i = 0; i < 1000000000; i++)
|
||||
;
|
||||
try
|
||||
{
|
||||
finderPattern = FinderPattern.findFinderPattern(bitmap);
|
||||
}
|
||||
catch (FinderPatternNotFoundException e2)
|
||||
{
|
||||
throw new SymbolNotFoundException(e2.Message);
|
||||
}
|
||||
catch (VersionInformationException e2)
|
||||
{
|
||||
throw new SymbolNotFoundException(e2.Message);
|
||||
}
|
||||
}
|
||||
catch (VersionInformationException e)
|
||||
{
|
||||
throw new SymbolNotFoundException(e.Message);
|
||||
}
|
||||
|
||||
|
||||
canvas.println("FinderPattern at");
|
||||
String finderPatternCoordinates = finderPattern.getCenter(FinderPattern.UL).ToString() + finderPattern.getCenter(FinderPattern.UR).ToString() + finderPattern.getCenter(FinderPattern.DL).ToString();
|
||||
canvas.println(finderPatternCoordinates);
|
||||
int[] sincos = finderPattern.getAngle();
|
||||
canvas.println("Angle*4098: Sin " + System.Convert.ToString(sincos[0]) + " " + "Cos " + System.Convert.ToString(sincos[1]));
|
||||
|
||||
int version = finderPattern.Version;
|
||||
canvas.println("Version: " + System.Convert.ToString(version));
|
||||
if (version < 1 || version > 40)
|
||||
throw new InvalidVersionException("Invalid version: " + version);
|
||||
|
||||
AlignmentPattern alignmentPattern = null;
|
||||
try
|
||||
{
|
||||
alignmentPattern = AlignmentPattern.findAlignmentPattern(bitmap, finderPattern);
|
||||
}
|
||||
catch (AlignmentPatternNotFoundException e)
|
||||
{
|
||||
throw new SymbolNotFoundException(e.Message);
|
||||
}
|
||||
|
||||
int matrixLength = alignmentPattern.getCenter().Length;
|
||||
canvas.println("AlignmentPatterns at");
|
||||
for (int y = 0; y < matrixLength; y++)
|
||||
{
|
||||
String alignmentPatternCoordinates = "";
|
||||
for (int x = 0; x < matrixLength; x++)
|
||||
{
|
||||
alignmentPatternCoordinates += alignmentPattern.getCenter()[x][y].ToString();
|
||||
}
|
||||
canvas.println(alignmentPatternCoordinates);
|
||||
}
|
||||
//for(int i = 0; i < 500000; i++) Console.out.println("");
|
||||
|
||||
canvas.println("Creating sampling grid.");
|
||||
//[TODO] need all-purpose method
|
||||
//samplingGrid = getSamplingGrid2_6(finderPattern, alignmentPattern);
|
||||
samplingGrid = getSamplingGrid(finderPattern, alignmentPattern);
|
||||
canvas.println("Reading grid.");
|
||||
bool[][] qRCodeMatrix = null;
|
||||
try
|
||||
{
|
||||
qRCodeMatrix = getQRCodeMatrix(bitmap, samplingGrid);
|
||||
}
|
||||
catch (System.IndexOutOfRangeException e)
|
||||
{
|
||||
throw new SymbolNotFoundException("Sampling grid exceeded image boundary");
|
||||
}
|
||||
//canvas.drawMatrix(qRCodeMatrix);
|
||||
return new QRCodeSymbol(qRCodeMatrix);
|
||||
}
|
||||
|
||||
public virtual QRCodeSymbol getQRCodeSymbolWithAdjustedGrid(Point adjust)
|
||||
{
|
||||
if (bitmap == null || samplingGrid == null)
|
||||
{
|
||||
throw new System.SystemException("This method must be called after QRCodeImageReader.getQRCodeSymbol() called");
|
||||
}
|
||||
samplingGrid.adjust(adjust);
|
||||
canvas.println("Sampling grid adjusted d(" + adjust.X + "," + adjust.Y + ")");
|
||||
|
||||
bool[][] qRCodeMatrix = null;
|
||||
try
|
||||
{
|
||||
qRCodeMatrix = getQRCodeMatrix(bitmap, samplingGrid);
|
||||
}
|
||||
catch (System.IndexOutOfRangeException e)
|
||||
{
|
||||
throw new SymbolNotFoundException("Sampling grid exceeded image boundary");
|
||||
}
|
||||
return new QRCodeSymbol(qRCodeMatrix);
|
||||
}
|
||||
|
||||
// For only version 1 which has no Alignement Patterns
|
||||
/* SamplingGrid getSamplingGrid1(FinderPattern finderPattern) {
|
||||
int sqrtNumArea = 1;
|
||||
int sqrtNumModules = finderPattern.getSqrtNumModules(); //get nummber of modules at side
|
||||
int sqrtNumAreaModules = sqrtNumModules / sqrtNumArea;
|
||||
Point[] centers = finderPattern.getCenter();
|
||||
int logicalDistance = 14;
|
||||
SamplingGrid samplingGrid = new SamplingGrid(sqrtNumArea);
|
||||
Line baseLineX, baseLineY, gridLineX, gridLineY;
|
||||
|
||||
|
||||
ModulePitch modulePitch = new ModulePitch(); //store (up,left) order
|
||||
modulePitch.top = getAreaModulePitch(centers[0], centers[1], logicalDistance);
|
||||
modulePitch.left = getAreaModulePitch(centers[0], centers[2], logicalDistance);
|
||||
|
||||
//X軸に垂直の基線(一般に縦)
|
||||
baseLineX = new Line(
|
||||
finderPattern.getCenter(FinderPattern.UL),
|
||||
finderPattern.getCenter(FinderPattern.DL));
|
||||
|
||||
Axis axis = new Axis(finderPattern.getAngle(), modulePitch.top);
|
||||
axis.setOrigin(baseLineX.getP1());
|
||||
baseLineX.setP1(axis.translate(-3, -3));
|
||||
|
||||
axis.setModulePitch(modulePitch.left);
|
||||
axis.setOrigin(baseLineX.getP2());
|
||||
baseLineX.setP2(axis.translate(-3, 3));
|
||||
|
||||
//Y軸に垂直の基線(一般に横)
|
||||
baseLineY =
|
||||
new Line(finderPattern.getCenter(FinderPattern.UL),
|
||||
finderPattern.getCenter(FinderPattern.UR));
|
||||
|
||||
axis.setModulePitch(modulePitch.left);
|
||||
axis.setOrigin(baseLineY.getP1());
|
||||
baseLineY.setP1(axis.translate(-3, -3));
|
||||
|
||||
|
||||
axis.setModulePitch(modulePitch.left);
|
||||
axis.setOrigin(baseLineY.getP2());
|
||||
baseLineY.setP2(axis.translate(3, -3));
|
||||
|
||||
//baseLineX.translate(1,1);
|
||||
//baseLineY.translate(1,1);
|
||||
|
||||
samplingGrid.initGrid(0, 0, sqrtNumAreaModules, sqrtNumAreaModules);
|
||||
|
||||
for (int i = 0; i < sqrtNumAreaModules; i++) {
|
||||
|
||||
gridLineX = new Line(baseLineX.getP1(), baseLineX.getP2());
|
||||
|
||||
axis.setOrigin(gridLineX.getP1());
|
||||
axis.setModulePitch(modulePitch.top);
|
||||
gridLineX.setP1(axis.translate(i,0));
|
||||
|
||||
axis.setOrigin(gridLineX.getP2());
|
||||
axis.setModulePitch(modulePitch.top);
|
||||
gridLineX.setP2(axis.translate(i,0));
|
||||
|
||||
|
||||
gridLineY = new Line(baseLineY.getP1(), baseLineY.getP2());
|
||||
axis.setOrigin(gridLineY.getP1());
|
||||
axis.setModulePitch(modulePitch.left);
|
||||
gridLineY.setP1(axis.translate(0,i));
|
||||
|
||||
axis.setOrigin(gridLineY.getP2());
|
||||
axis.setModulePitch(modulePitch.left);
|
||||
gridLineY.setP2(axis.translate(0,i));
|
||||
|
||||
|
||||
samplingGrid.setXLine(0,0,i,gridLineX);
|
||||
samplingGrid.setYLine(0,0,i,gridLineY);
|
||||
}
|
||||
for (int ay = 0; ay < samplingGrid.getHeight(); ay++) {
|
||||
for (int ax = 0; ax < samplingGrid.getWidth();ax++) {
|
||||
canvas.drawLines(samplingGrid.getXLines(ax,ay), Color.BLUE);
|
||||
canvas.drawLines(samplingGrid.getYLines(ax,ay), Color.BLUE);
|
||||
}
|
||||
}
|
||||
return samplingGrid;
|
||||
}*/
|
||||
|
||||
//sampllingGrid[areaX][areaY][direction(x=0,y=1)][EachLines]
|
||||
/* SamplingGrid getSamplingGrid2_6(FinderPattern finderPattern, AlignmentPattern alignmentPattern) {
|
||||
|
||||
Point centers[][] = alignmentPattern.getCenter();
|
||||
centers[0][0] = finderPattern.getCenter(FinderPattern.UL);
|
||||
centers[1][0] = finderPattern.getCenter(FinderPattern.UR);
|
||||
centers[0][1] = finderPattern.getCenter(FinderPattern.DL);
|
||||
int sqrtNumModules = finderPattern.getSqrtNumModules(); //一辺当たりのモジュール数を得る
|
||||
|
||||
SamplingGrid samplingGrid = new SamplingGrid(1);
|
||||
Line baseLineX, baseLineY, gridLineX, gridLineY;
|
||||
|
||||
int logicalDistance = alignmentPattern.getLogicalDistance();
|
||||
Axis axis = new Axis(finderPattern.getAngle(), finderPattern.getModuleSize());
|
||||
|
||||
ModulePitch modulePitch = new ModulePitch(); //top left bottom rightの順に格納
|
||||
|
||||
modulePitch.top = getAreaModulePitch(centers[0][0], centers[1][0], logicalDistance + 6);
|
||||
modulePitch.left = getAreaModulePitch(centers[0][0], centers[0][1], logicalDistance + 6);
|
||||
axis.setModulePitch(modulePitch.top);
|
||||
axis.setOrigin(centers[0][1]);
|
||||
modulePitch.bottom = getAreaModulePitch(axis.translate(0, -3), centers[1][1], logicalDistance + 3);
|
||||
axis.setModulePitch(modulePitch.left);
|
||||
axis.setOrigin(centers[1][0]);
|
||||
modulePitch.right = getAreaModulePitch(axis.translate(-3, 0), centers[1][1], logicalDistance + 3);
|
||||
|
||||
//X軸に垂直の基線(一般に縦)
|
||||
baseLineX = new Line();
|
||||
baseLineY = new Line();
|
||||
|
||||
axis.setOrigin(centers[0][0]);
|
||||
modulePitch.top = getAreaModulePitch(centers[0][0], centers[1][0], logicalDistance + 6);
|
||||
modulePitch.left = getAreaModulePitch(centers[0][0], centers[0][1], logicalDistance + 6);
|
||||
axis.setModulePitch(modulePitch.top);
|
||||
axis.setOrigin(centers[0][1]);
|
||||
modulePitch.bottom = getAreaModulePitch(axis.translate(0,-3), centers[1][1], logicalDistance + 3);
|
||||
axis.setModulePitch(modulePitch.left);
|
||||
axis.setOrigin(centers[1][0]);
|
||||
modulePitch.right = getAreaModulePitch(axis.translate(-3,0), centers[1][1], logicalDistance + 3);
|
||||
|
||||
|
||||
axis.setOrigin(centers[0][0]);
|
||||
axis.setModulePitch(modulePitch.top);
|
||||
baseLineX.setP1(axis.translate(-3,-3));
|
||||
|
||||
axis.setModulePitch(modulePitch.left);
|
||||
baseLineY.setP1(axis.translate(-3,-3));
|
||||
|
||||
axis.setOrigin(centers[0][1]);
|
||||
axis.setModulePitch(modulePitch.bottom);
|
||||
baseLineX.setP2(axis.translate(-3,3));
|
||||
|
||||
axis.setOrigin(centers[1][0]);
|
||||
axis.setModulePitch(modulePitch.right);
|
||||
baseLineY.setP2(axis.translate(3,-3));
|
||||
|
||||
|
||||
baseLineX.translate(1,1);
|
||||
baseLineY.translate(1,1);
|
||||
|
||||
samplingGrid.initGrid(0, 0, sqrtNumModules, sqrtNumModules);
|
||||
|
||||
for (int i = 0; i < sqrtNumModules; i++) {
|
||||
gridLineX = new Line(baseLineX.getP1(), baseLineX.getP2());
|
||||
|
||||
axis.setOrigin(gridLineX.getP1());
|
||||
axis.setModulePitch(modulePitch.top);
|
||||
gridLineX.setP1(axis.translate(i,0));
|
||||
|
||||
axis.setOrigin(gridLineX.getP2());
|
||||
axis.setModulePitch(modulePitch.bottom);
|
||||
gridLineX.setP2(axis.translate(i,0));
|
||||
|
||||
|
||||
gridLineY = new Line(baseLineY.getP1(), baseLineY.getP2());
|
||||
|
||||
axis.setOrigin(gridLineY.getP1());
|
||||
axis.setModulePitch(modulePitch.left);
|
||||
gridLineY.setP1(axis.translate(0,i));
|
||||
|
||||
axis.setOrigin(gridLineY.getP2());
|
||||
axis.setModulePitch(modulePitch.right);
|
||||
gridLineY.setP2(axis.translate(0,i));
|
||||
|
||||
|
||||
samplingGrid.setXLine(0,0,i,gridLineX);
|
||||
samplingGrid.setYLine(0,0,i,gridLineY);
|
||||
|
||||
}
|
||||
|
||||
for (int ay = 0; ay < samplingGrid.getHeight(); ay++) {
|
||||
for (int ax = 0; ax < samplingGrid.getWidth();ax++) {
|
||||
canvas.drawLines(samplingGrid.getXLines(ax,ay), java.awt.Color.BLUE);
|
||||
canvas.drawLines(samplingGrid.getYLines(ax,ay), java.awt.Color.BLUE);
|
||||
}
|
||||
}
|
||||
return samplingGrid;
|
||||
}
|
||||
|
||||
|
||||
|
||||
//for version 7-13
|
||||
SamplingGrid getSamplingGrid7_13(FinderPattern finderPattern, AlignmentPattern alignmentPattern) {
|
||||
|
||||
Point centers[][] = alignmentPattern.getCenter();
|
||||
centers[0][0] = finderPattern.getCenter(FinderPattern.UL);
|
||||
centers[2][0] = finderPattern.getCenter(FinderPattern.UR);
|
||||
centers[0][2] = finderPattern.getCenter(FinderPattern.DL);
|
||||
int sqrtNumModules = finderPattern.getSqrtNumModules(); //一辺当たりのモジュール数を得る
|
||||
int sqrtNumArea = 2;
|
||||
int sqrtNumAreaModules = sqrtNumModules / sqrtNumArea;
|
||||
sqrtNumAreaModules++;
|
||||
SamplingGrid samplingGrid = new SamplingGrid(sqrtNumArea);
|
||||
Line baseLineX, baseLineY, gridLineX, gridLineY;
|
||||
|
||||
int logicalDistance = alignmentPattern.getLogicalDistance();
|
||||
Axis axis = new Axis(finderPattern.getAngle(), finderPattern.getModuleSize());
|
||||
ModulePitch modulePitch;
|
||||
for (int ay = 0; ay < sqrtNumArea; ay++) {
|
||||
for (int ax = 0; ax < sqrtNumArea; ax++) {
|
||||
modulePitch = new ModulePitch(); //top left bottom rightの順に格納
|
||||
baseLineX = new Line();
|
||||
baseLineY = new Line();
|
||||
axis.setModulePitch(finderPattern.getModuleSize());
|
||||
if (ax == 0 && ay == 0) {
|
||||
axis.setOrigin(centers[0][0]);
|
||||
modulePitch.top = getAreaModulePitch(axis.translate(0,3), centers[1][0], logicalDistance + 3);
|
||||
modulePitch.left = getAreaModulePitch(axis.translate(3,0), centers[0][1], logicalDistance + 3);
|
||||
axis.setModulePitch(modulePitch.top);
|
||||
modulePitch.bottom = getAreaModulePitch(centers[0][1], centers[1][1], logicalDistance);
|
||||
axis.setModulePitch(modulePitch.left);
|
||||
modulePitch.right = getAreaModulePitch(centers[1][0], centers[1][1], logicalDistance);
|
||||
|
||||
axis.setModulePitch(modulePitch.top);
|
||||
baseLineX.setP1(axis.translate(-3,-3));
|
||||
|
||||
axis.setModulePitch(modulePitch.left);
|
||||
baseLineY.setP1(axis.translate(-3,-3));
|
||||
|
||||
axis.setOrigin(centers[0][1]);
|
||||
axis.setModulePitch(modulePitch.bottom);
|
||||
baseLineX.setP2(axis.translate(-6,0));
|
||||
|
||||
axis.setOrigin(centers[1][0]);
|
||||
axis.setModulePitch(modulePitch.right);
|
||||
baseLineY.setP2(axis.translate(0,-6));
|
||||
}
|
||||
else if (ax == 1 && ay == 0) {
|
||||
axis.setOrigin(centers[1][0]);
|
||||
modulePitch.top = getAreaModulePitch(axis.translate(0,-3), centers[2][0], logicalDistance + 3);
|
||||
modulePitch.left = getAreaModulePitch(centers[1][0], centers[1][1], logicalDistance);
|
||||
axis.setModulePitch(modulePitch.top);
|
||||
modulePitch.bottom = getAreaModulePitch(centers[1][1], centers[2][1], logicalDistance);
|
||||
axis.setModulePitch(modulePitch.left);
|
||||
axis.setOrigin(centers[2][0]);
|
||||
modulePitch.right = getAreaModulePitch(axis.translate(-3,0), centers[2][1], logicalDistance + 3);
|
||||
|
||||
axis.setOrigin(centers[1][0]);
|
||||
axis.setModulePitch(modulePitch.left);
|
||||
baseLineX.setP1(axis.translate(0,-6));
|
||||
|
||||
baseLineY.setP1(axis.translate(0,-6));
|
||||
|
||||
baseLineX.setP2(centers[1][1]);
|
||||
|
||||
axis.setOrigin(centers[2][0]);
|
||||
axis.setModulePitch(modulePitch.right);
|
||||
baseLineY.setP2(axis.translate(3,-3));
|
||||
}
|
||||
else if (ax == 0 && ay == 1) {
|
||||
modulePitch.top = getAreaModulePitch(centers[0][1], centers[1][1], logicalDistance);
|
||||
axis.setOrigin(centers[0][2]);
|
||||
modulePitch.left = getAreaModulePitch(centers[0][1], axis.translate(3,0), logicalDistance + 3);
|
||||
axis.setModulePitch(modulePitch.top);
|
||||
modulePitch.bottom = getAreaModulePitch(axis.translate(0,-3), centers[1][2], logicalDistance + 3);
|
||||
axis.setModulePitch(modulePitch.bottom);
|
||||
modulePitch.right = getAreaModulePitch(centers[1][1], centers[1][2], logicalDistance);
|
||||
|
||||
axis.setOrigin(centers[0][1]);
|
||||
axis.setModulePitch(modulePitch.top);
|
||||
baseLineX.setP1(axis.translate(-6,0));
|
||||
|
||||
baseLineY.setP1(axis.translate(-6,0));
|
||||
|
||||
axis.setOrigin(centers[0][2]);
|
||||
axis.setModulePitch(modulePitch.bottom);
|
||||
baseLineX.setP2(axis.translate(-3, 3));
|
||||
|
||||
baseLineY.setP2(centers[1][1]);
|
||||
}
|
||||
else if (ax == 1 && ay == 1) {
|
||||
modulePitch.top = getAreaModulePitch(centers[1][1], centers[2][1], logicalDistance);
|
||||
modulePitch.left = getAreaModulePitch(centers[1][1], centers[1][2], logicalDistance);
|
||||
modulePitch.bottom = getAreaModulePitch(centers[1][2], centers[2][2], logicalDistance);
|
||||
modulePitch.right = getAreaModulePitch(centers[2][1], centers[2][2], logicalDistance);
|
||||
|
||||
baseLineX.setP1(centers[1][1]);
|
||||
baseLineY.setP1(centers[1][1]);
|
||||
|
||||
axis.setOrigin(centers[1][2]);
|
||||
axis.setModulePitch(modulePitch.left);
|
||||
baseLineX.setP2(axis.translate(0,6));
|
||||
|
||||
axis.setOrigin(centers[2][1]);
|
||||
axis.setModulePitch(modulePitch.top);
|
||||
baseLineY.setP2(axis.translate(6,0));
|
||||
}
|
||||
|
||||
samplingGrid.initGrid(ax,ay, sqrtNumAreaModules, sqrtNumAreaModules);
|
||||
|
||||
for (int i = 0; i < sqrtNumAreaModules; i++) {
|
||||
gridLineX = new Line(baseLineX.getP1(), baseLineX.getP2());
|
||||
|
||||
axis.setOrigin(gridLineX.getP1());
|
||||
axis.setModulePitch(modulePitch.top);
|
||||
gridLineX.setP1(axis.translate(i,0));
|
||||
|
||||
axis.setOrigin(gridLineX.getP2());
|
||||
axis.setModulePitch(modulePitch.bottom);
|
||||
gridLineX.setP2(axis.translate(i,0));
|
||||
|
||||
|
||||
gridLineY = new Line(baseLineY.getP1(), baseLineY.getP2());
|
||||
|
||||
axis.setOrigin(gridLineY.getP1());
|
||||
axis.setModulePitch(modulePitch.left);
|
||||
gridLineY.setP1(axis.translate(0,i));
|
||||
|
||||
axis.setOrigin(gridLineY.getP2());
|
||||
axis.setModulePitch(modulePitch.right);
|
||||
gridLineY.setP2(axis.translate(0,i));
|
||||
|
||||
samplingGrid.setXLine(ax,ay,i,gridLineX);
|
||||
samplingGrid.setYLine(ax,ay,i,gridLineY);
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (int ay = 0; ay < samplingGrid.getHeight(); ay++) {
|
||||
for (int ax = 0; ax < samplingGrid.getWidth();ax++) {
|
||||
canvas.drawLines(samplingGrid.getXLines(ax,ay), java.awt.Color.BLUE);
|
||||
canvas.drawLines(samplingGrid.getYLines(ax,ay), java.awt.Color.BLUE);
|
||||
}
|
||||
}
|
||||
|
||||
return samplingGrid;
|
||||
}*/
|
||||
/// <summary> Generic Sampling grid method</summary>
|
||||
internal virtual SamplingGrid getSamplingGrid(FinderPattern finderPattern, AlignmentPattern alignmentPattern)
|
||||
{
|
||||
|
||||
Point[][] centers = alignmentPattern.getCenter();
|
||||
|
||||
int version = finderPattern.Version;
|
||||
int sqrtCenters = (version / 7) + 2;
|
||||
|
||||
centers[0][0] = finderPattern.getCenter(FinderPattern.UL);
|
||||
centers[sqrtCenters - 1][0] = finderPattern.getCenter(FinderPattern.UR);
|
||||
centers[0][sqrtCenters - 1] = finderPattern.getCenter(FinderPattern.DL);
|
||||
//int sqrtNumModules = finderPattern.getSqrtNumModules(); /// The number of modules per one side is obtained
|
||||
int sqrtNumArea = sqrtCenters - 1;
|
||||
|
||||
//--//--//--//--//--//--//--//--//--//--//--//--//--//--//--//--//--//--//--//--//--//--//--//--//--//--//--//--//--//--//
|
||||
SamplingGrid samplingGrid = new SamplingGrid(sqrtNumArea);
|
||||
|
||||
Line baseLineX, baseLineY, gridLineX, gridLineY;
|
||||
|
||||
///???
|
||||
//Point[] targetCenters;
|
||||
|
||||
//int logicalDistance = alignmentPattern.getLogicalDistance();
|
||||
Axis axis = new Axis(finderPattern.getAngle(), finderPattern.getModuleSize());
|
||||
ModulePitch modulePitch;
|
||||
|
||||
// for each area :
|
||||
for (int ay = 0; ay < sqrtNumArea; ay++)
|
||||
{
|
||||
for (int ax = 0; ax < sqrtNumArea; ax++)
|
||||
{
|
||||
modulePitch = new ModulePitch(this); /// Housing to order
|
||||
baseLineX = new Line();
|
||||
baseLineY = new Line();
|
||||
axis.ModulePitch = finderPattern.getModuleSize();
|
||||
|
||||
Point[][] logicalCenters = AlignmentPattern.getLogicalCenter(finderPattern);
|
||||
|
||||
Point upperLeftPoint = centers[ax][ay];
|
||||
Point upperRightPoint = centers[ax + 1][ay];
|
||||
Point lowerLeftPoint = centers[ax][ay + 1];
|
||||
Point lowerRightPoint = centers[ax + 1][ay + 1];
|
||||
|
||||
Point logicalUpperLeftPoint = logicalCenters[ax][ay];
|
||||
Point logicalUpperRightPoint = logicalCenters[ax + 1][ay];
|
||||
Point logicalLowerLeftPoint = logicalCenters[ax][ay + 1];
|
||||
Point logicalLowerRightPoint = logicalCenters[ax + 1][ay + 1];
|
||||
|
||||
if (ax == 0 && ay == 0)
|
||||
// left upper corner
|
||||
{
|
||||
if (sqrtNumArea == 1)
|
||||
{
|
||||
upperLeftPoint = axis.translate(upperLeftPoint, - 3, - 3);
|
||||
upperRightPoint = axis.translate(upperRightPoint, 3, - 3);
|
||||
lowerLeftPoint = axis.translate(lowerLeftPoint, - 3, 3);
|
||||
lowerRightPoint = axis.translate(lowerRightPoint, 6, 6);
|
||||
|
||||
logicalUpperLeftPoint.translate(- 6, - 6);
|
||||
logicalUpperRightPoint.translate(3, - 3);
|
||||
logicalLowerLeftPoint.translate(- 3, 3);
|
||||
logicalLowerRightPoint.translate(6, 6);
|
||||
}
|
||||
else
|
||||
{
|
||||
upperLeftPoint = axis.translate(upperLeftPoint, - 3, - 3);
|
||||
upperRightPoint = axis.translate(upperRightPoint, 0, - 6);
|
||||
lowerLeftPoint = axis.translate(lowerLeftPoint, - 6, 0);
|
||||
|
||||
logicalUpperLeftPoint.translate(- 6, - 6);
|
||||
logicalUpperRightPoint.translate(0, - 6);
|
||||
logicalLowerLeftPoint.translate(- 6, 0);
|
||||
}
|
||||
}
|
||||
else if (ax == 0 && ay == sqrtNumArea - 1)
|
||||
// left bottom corner
|
||||
{
|
||||
upperLeftPoint = axis.translate(upperLeftPoint, - 6, 0);
|
||||
lowerLeftPoint = axis.translate(lowerLeftPoint, - 3, 3);
|
||||
lowerRightPoint = axis.translate(lowerRightPoint, 0, 6);
|
||||
|
||||
|
||||
logicalUpperLeftPoint.translate(- 6, 0);
|
||||
logicalLowerLeftPoint.translate(- 6, 6);
|
||||
logicalLowerRightPoint.translate(0, 6);
|
||||
}
|
||||
else if (ax == sqrtNumArea - 1 && ay == 0)
|
||||
// right upper corner
|
||||
{
|
||||
upperLeftPoint = axis.translate(upperLeftPoint, 0, - 6);
|
||||
upperRightPoint = axis.translate(upperRightPoint, 3, - 3);
|
||||
lowerRightPoint = axis.translate(lowerRightPoint, 6, 0);
|
||||
|
||||
logicalUpperLeftPoint.translate(0, - 6);
|
||||
logicalUpperRightPoint.translate(6, - 6);
|
||||
logicalLowerRightPoint.translate(6, 0);
|
||||
}
|
||||
else if (ax == sqrtNumArea - 1 && ay == sqrtNumArea - 1)
|
||||
// right bottom corner
|
||||
{
|
||||
lowerLeftPoint = axis.translate(lowerLeftPoint, 0, 6);
|
||||
upperRightPoint = axis.translate(upperRightPoint, 6, 0);
|
||||
lowerRightPoint = axis.translate(lowerRightPoint, 6, 6);
|
||||
|
||||
logicalLowerLeftPoint.translate(0, 6);
|
||||
logicalUpperRightPoint.translate(6, 0);
|
||||
logicalLowerRightPoint.translate(6, 6);
|
||||
}
|
||||
else if (ax == 0)
|
||||
// left side
|
||||
{
|
||||
upperLeftPoint = axis.translate(upperLeftPoint, - 6, 0);
|
||||
lowerLeftPoint = axis.translate(lowerLeftPoint, - 6, 0);
|
||||
|
||||
logicalUpperLeftPoint.translate(- 6, 0);
|
||||
logicalLowerLeftPoint.translate(- 6, 0);
|
||||
}
|
||||
else if (ax == sqrtNumArea - 1)
|
||||
// right
|
||||
{
|
||||
upperRightPoint = axis.translate(upperRightPoint, 6, 0);
|
||||
lowerRightPoint = axis.translate(lowerRightPoint, 6, 0);
|
||||
|
||||
logicalUpperRightPoint.translate(6, 0);
|
||||
logicalLowerRightPoint.translate(6, 0);
|
||||
}
|
||||
else if (ay == 0)
|
||||
// top
|
||||
{
|
||||
upperLeftPoint = axis.translate(upperLeftPoint, 0, - 6);
|
||||
upperRightPoint = axis.translate(upperRightPoint, 0, - 6);
|
||||
|
||||
logicalUpperLeftPoint.translate(0, - 6);
|
||||
logicalUpperRightPoint.translate(0, - 6);
|
||||
}
|
||||
else if (ay == sqrtNumArea - 1)
|
||||
// bottom
|
||||
{
|
||||
lowerLeftPoint = axis.translate(lowerLeftPoint, 0, 6);
|
||||
lowerRightPoint = axis.translate(lowerRightPoint, 0, 6);
|
||||
|
||||
logicalLowerLeftPoint.translate(0, 6);
|
||||
logicalLowerRightPoint.translate(0, 6);
|
||||
}
|
||||
|
||||
if (ax == 0)
|
||||
{
|
||||
logicalUpperRightPoint.translate(1, 0);
|
||||
logicalLowerRightPoint.translate(1, 0);
|
||||
}
|
||||
else
|
||||
{
|
||||
logicalUpperLeftPoint.translate(- 1, 0);
|
||||
logicalLowerLeftPoint.translate(- 1, 0);
|
||||
}
|
||||
|
||||
if (ay == 0)
|
||||
{
|
||||
logicalLowerLeftPoint.translate(0, 1);
|
||||
logicalLowerRightPoint.translate(0, 1);
|
||||
}
|
||||
else
|
||||
{
|
||||
logicalUpperLeftPoint.translate(0, - 1);
|
||||
logicalUpperRightPoint.translate(0, - 1);
|
||||
}
|
||||
|
||||
int logicalWidth = logicalUpperRightPoint.X - logicalUpperLeftPoint.X;
|
||||
int logicalHeight = logicalLowerLeftPoint.Y - logicalUpperLeftPoint.Y;
|
||||
|
||||
if (version < 7)
|
||||
{
|
||||
logicalWidth += 3;
|
||||
logicalHeight += 3;
|
||||
}
|
||||
modulePitch.top = getAreaModulePitch(upperLeftPoint, upperRightPoint, logicalWidth - 1);
|
||||
modulePitch.left = getAreaModulePitch(upperLeftPoint, lowerLeftPoint, logicalHeight - 1);
|
||||
modulePitch.bottom = getAreaModulePitch(lowerLeftPoint, lowerRightPoint, logicalWidth - 1);
|
||||
modulePitch.right = getAreaModulePitch(upperRightPoint, lowerRightPoint, logicalHeight - 1);
|
||||
|
||||
baseLineX.setP1(upperLeftPoint);
|
||||
baseLineY.setP1(upperLeftPoint);
|
||||
baseLineX.setP2(lowerLeftPoint);
|
||||
baseLineY.setP2(upperRightPoint);
|
||||
|
||||
samplingGrid.initGrid(ax, ay, logicalWidth, logicalHeight);
|
||||
|
||||
for (int i = 0; i < logicalWidth; i++)
|
||||
{
|
||||
gridLineX = new Line(baseLineX.getP1(), baseLineX.getP2());
|
||||
|
||||
axis.Origin = gridLineX.getP1();
|
||||
axis.ModulePitch = modulePitch.top;
|
||||
gridLineX.setP1(axis.translate(i, 0));
|
||||
|
||||
axis.Origin = gridLineX.getP2();
|
||||
axis.ModulePitch = modulePitch.bottom;
|
||||
gridLineX.setP2(axis.translate(i, 0));
|
||||
|
||||
samplingGrid.setXLine(ax, ay, i, gridLineX);
|
||||
}
|
||||
|
||||
for (int i = 0; i < logicalHeight; i++)
|
||||
{
|
||||
|
||||
gridLineY = new Line(baseLineY.getP1(), baseLineY.getP2());
|
||||
|
||||
axis.Origin = gridLineY.getP1();
|
||||
axis.ModulePitch = modulePitch.left;
|
||||
gridLineY.setP1(axis.translate(0, i));
|
||||
|
||||
axis.Origin = gridLineY.getP2();
|
||||
axis.ModulePitch = modulePitch.right;
|
||||
gridLineY.setP2(axis.translate(0, i));
|
||||
|
||||
samplingGrid.setYLine(ax, ay, i, gridLineY);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return samplingGrid;
|
||||
}
|
||||
|
||||
|
||||
|
||||
//get module pitch in single area
|
||||
internal virtual int getAreaModulePitch(Point start, Point end, int logicalDistance)
|
||||
{
|
||||
Line tempLine;
|
||||
tempLine = new Line(start, end);
|
||||
int realDistance = tempLine.Length;
|
||||
int modulePitch = (realDistance << DECIMAL_POINT) / logicalDistance;
|
||||
return modulePitch;
|
||||
}
|
||||
|
||||
|
||||
//gridLines[areaX][areaY][direction(x=0,y=1)][EachLines]
|
||||
internal virtual bool[][] getQRCodeMatrix(bool[][] image, SamplingGrid gridLines)
|
||||
{
|
||||
//int gridSize = gridLines.getWidth() * gridLines.getWidth(0,0);
|
||||
int gridSize = gridLines.TotalWidth;
|
||||
|
||||
// now this is done within the SamplingGrid class...
|
||||
// if (gridLines.getWidth() >= 2)
|
||||
// gridSize-=1;
|
||||
|
||||
canvas.println("gridSize=" + gridSize);
|
||||
//canvas.println("gridLines.getWidth() * gridLines.getWidth(0,0) = "+gridLines.getWidth() * gridLines.getWidth(0,0));
|
||||
Point bottomRightPoint = null;
|
||||
bool[][] sampledMatrix = new bool[gridSize][];
|
||||
for (int i = 0; i < gridSize; i++)
|
||||
{
|
||||
sampledMatrix[i] = new bool[gridSize];
|
||||
}
|
||||
for (int ay = 0; ay < gridLines.getHeight(); ay++)
|
||||
{
|
||||
for (int ax = 0; ax < gridLines.getWidth(); ax++)
|
||||
{
|
||||
System.Collections.ArrayList sampledPoints = System.Collections.ArrayList.Synchronized(new System.Collections.ArrayList(10)); //only for visualiz;
|
||||
for (int y = 0; y < gridLines.getHeight(ax, ay); y++)
|
||||
{
|
||||
for (int x = 0; x < gridLines.getWidth(ax, ay); x++)
|
||||
{
|
||||
int x1 = gridLines.getXLine(ax, ay, x).getP1().X;
|
||||
int y1 = gridLines.getXLine(ax, ay, x).getP1().Y;
|
||||
int x2 = gridLines.getXLine(ax, ay, x).getP2().X;
|
||||
int y2 = gridLines.getXLine(ax, ay, x).getP2().Y;
|
||||
int x3 = gridLines.getYLine(ax, ay, y).getP1().X;
|
||||
int y3 = gridLines.getYLine(ax, ay, y).getP1().Y;
|
||||
int x4 = gridLines.getYLine(ax, ay, y).getP2().X;
|
||||
int y4 = gridLines.getYLine(ax, ay, y).getP2().Y;
|
||||
|
||||
int e = (y2 - y1) * (x3 - x4) - (y4 - y3) * (x1 - x2);
|
||||
int f = (x1 * y2 - x2 * y1) * (x3 - x4) - (x3 * y4 - x4 * y3) * (x1 - x2);
|
||||
int g = (x3 * y4 - x4 * y3) * (y2 - y1) - (x1 * y2 - x2 * y1) * (y4 - y3);
|
||||
sampledMatrix[gridLines.getX(ax, x)][gridLines.getY(ay, y)] = image[f / e][g / e];
|
||||
if ((ay == gridLines.getHeight() - 1 && ax == gridLines.getWidth() - 1) && y == gridLines.getHeight(ax, ay) - 1 && x == gridLines.getWidth(ax, ay) - 1)
|
||||
bottomRightPoint = new Point(f / e, g / e);
|
||||
//calling canvas.drawPoint in loop can be very slow.
|
||||
// use canvas.drawPoints if you need
|
||||
//canvas.drawPoint(new Point(f / e,g / e), Color.RED);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if (bottomRightPoint.X > image.Length - 1 || bottomRightPoint.Y > image[0].Length - 1)
|
||||
throw new System.IndexOutOfRangeException("Sampling grid pointed out of image");
|
||||
canvas.drawPoint(bottomRightPoint, ThoughtWorks.QRCode.Codec.Util.Color_Fields.BLUE);
|
||||
|
||||
return sampledMatrix;
|
||||
}
|
||||
}
|
||||
}
|
||||
245
reader/pattern/AlignmentPattern.cs
Normal file
245
reader/pattern/AlignmentPattern.cs
Normal file
@@ -0,0 +1,245 @@
|
||||
using System;
|
||||
using QRCodeDecoder = ThoughtWorks.QRCode.Codec.QRCodeDecoder;
|
||||
using ThoughtWorks.QRCode.Codec.Reader;
|
||||
using AlignmentPatternNotFoundException = ThoughtWorks.QRCode.ExceptionHandler.AlignmentPatternNotFoundException;
|
||||
using InvalidVersionException = ThoughtWorks.QRCode.ExceptionHandler.InvalidVersionException;
|
||||
using ThoughtWorks.QRCode.Geom;
|
||||
using ThoughtWorks.QRCode.Codec.Util;
|
||||
|
||||
namespace ThoughtWorks.QRCode.Codec.Reader.Pattern
|
||||
{
|
||||
|
||||
public class AlignmentPattern
|
||||
{
|
||||
internal const int RIGHT = 1;
|
||||
internal const int BOTTOM = 2;
|
||||
internal const int LEFT = 3;
|
||||
internal const int TOP = 4;
|
||||
|
||||
internal static DebugCanvas canvas;
|
||||
internal Point[][] center;
|
||||
internal int patternDistance;
|
||||
|
||||
virtual public int LogicalDistance
|
||||
{
|
||||
get
|
||||
{
|
||||
return patternDistance;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
internal AlignmentPattern(Point[][] center, int patternDistance)
|
||||
{
|
||||
this.center = center;
|
||||
this.patternDistance = patternDistance;
|
||||
}
|
||||
|
||||
public static AlignmentPattern findAlignmentPattern(bool[][] image, FinderPattern finderPattern)
|
||||
{
|
||||
Point[][] logicalCenters = getLogicalCenter(finderPattern);
|
||||
int logicalDistance = logicalCenters[1][0].X - logicalCenters[0][0].X;
|
||||
//With it converts in order to handle in the same way
|
||||
Point[][] centers = null;
|
||||
centers = getCenter(image, finderPattern, logicalCenters);
|
||||
return new AlignmentPattern(centers, logicalDistance);
|
||||
}
|
||||
|
||||
public virtual Point[][] getCenter()
|
||||
{
|
||||
return center;
|
||||
}
|
||||
|
||||
// for only trancparency access in version 1, which has no alignement pattern
|
||||
public virtual void setCenter(Point[][] center)
|
||||
{
|
||||
this.center = center;
|
||||
}
|
||||
|
||||
internal static Point[][] getCenter(bool[][] image, FinderPattern finderPattern, Point[][] logicalCenters)
|
||||
{
|
||||
int moduleSize = finderPattern.getModuleSize();
|
||||
|
||||
Axis axis = new Axis(finderPattern.getAngle(), moduleSize);
|
||||
int sqrtCenters = logicalCenters.Length;
|
||||
Point[][] centers = new Point[sqrtCenters][];
|
||||
for (int i = 0; i < sqrtCenters; i++)
|
||||
{
|
||||
centers[i] = new Point[sqrtCenters];
|
||||
}
|
||||
|
||||
axis.Origin = finderPattern.getCenter(FinderPattern.UL);
|
||||
centers[0][0] = axis.translate(3, 3);
|
||||
canvas.drawCross(centers[0][0], ThoughtWorks.QRCode.Codec.Util.Color_Fields.BLUE);
|
||||
|
||||
axis.Origin = finderPattern.getCenter(FinderPattern.UR);
|
||||
centers[sqrtCenters - 1][0] = axis.translate(- 3, 3);
|
||||
canvas.drawCross(centers[sqrtCenters - 1][0], ThoughtWorks.QRCode.Codec.Util.Color_Fields.BLUE);
|
||||
|
||||
axis.Origin = finderPattern.getCenter(FinderPattern.DL);
|
||||
centers[0][sqrtCenters - 1] = axis.translate(3, - 3);
|
||||
canvas.drawCross(centers[0][sqrtCenters - 1], ThoughtWorks.QRCode.Codec.Util.Color_Fields.BLUE);
|
||||
|
||||
Point tmpPoint = centers[0][0];
|
||||
|
||||
for (int y = 0; y < sqrtCenters; y++)
|
||||
{
|
||||
for (int x = 0; x < sqrtCenters; x++)
|
||||
{
|
||||
if ((x == 0 && y == 0) || (x == 0 && y == sqrtCenters - 1) || (x == sqrtCenters - 1 && y == 0))
|
||||
{
|
||||
// canvas.drawCross(centers[x][y], java.awt.Color.MAGENTA);
|
||||
continue;
|
||||
}
|
||||
Point target = null;
|
||||
if (y == 0)
|
||||
{
|
||||
if (x > 0 && x < sqrtCenters - 1)
|
||||
{
|
||||
target = axis.translate(centers[x - 1][y], logicalCenters[x][y].X - logicalCenters[x - 1][y].X, 0);
|
||||
}
|
||||
centers[x][y] = new Point(target.X, target.Y);
|
||||
canvas.drawCross(centers[x][y], ThoughtWorks.QRCode.Codec.Util.Color_Fields.RED);
|
||||
}
|
||||
else if (x == 0)
|
||||
{
|
||||
if (y > 0 && y < sqrtCenters - 1)
|
||||
{
|
||||
target = axis.translate(centers[x][y - 1], 0, logicalCenters[x][y].Y - logicalCenters[x][y - 1].Y);
|
||||
}
|
||||
centers[x][y] = new Point(target.X, target.Y);
|
||||
canvas.drawCross(centers[x][y], ThoughtWorks.QRCode.Codec.Util.Color_Fields.RED);
|
||||
}
|
||||
else
|
||||
{
|
||||
Point t1 = axis.translate(centers[x - 1][y], logicalCenters[x][y].X - logicalCenters[x - 1][y].X, 0);
|
||||
Point t2 = axis.translate(centers[x][y - 1], 0, logicalCenters[x][y].Y - logicalCenters[x][y - 1].Y);
|
||||
centers[x][y] = new Point((t1.X + t2.X) / 2, (t1.Y + t2.Y) / 2 + 1);
|
||||
}
|
||||
if (finderPattern.Version > 1)
|
||||
{
|
||||
Point precisionCenter = getPrecisionCenter(image, centers[x][y]);
|
||||
|
||||
if (centers[x][y].distanceOf(precisionCenter) < 6)
|
||||
{
|
||||
canvas.drawCross(centers[x][y], ThoughtWorks.QRCode.Codec.Util.Color_Fields.RED);
|
||||
int dx = precisionCenter.X - centers[x][y].X;
|
||||
int dy = precisionCenter.Y - centers[x][y].Y;
|
||||
canvas.println("Adjust AP(" + x + "," + y + ") to d(" + dx + "," + dy + ")");
|
||||
|
||||
centers[x][y] = precisionCenter;
|
||||
}
|
||||
}
|
||||
canvas.drawCross(centers[x][y], ThoughtWorks.QRCode.Codec.Util.Color_Fields.BLUE);
|
||||
canvas.drawLine(new Line(tmpPoint, centers[x][y]), ThoughtWorks.QRCode.Codec.Util.Color_Fields.LIGHTBLUE);
|
||||
tmpPoint = centers[x][y];
|
||||
}
|
||||
}
|
||||
return centers;
|
||||
}
|
||||
|
||||
|
||||
|
||||
internal static Point getPrecisionCenter(bool[][] image, Point targetPoint)
|
||||
{
|
||||
// find nearest dark point and update it as new rough center point
|
||||
// when original rough center points light point
|
||||
int tx = targetPoint.X, ty = targetPoint.Y;
|
||||
if ((tx < 0 || ty < 0) || (tx > image.Length - 1 || ty > image[0].Length - 1))
|
||||
throw new AlignmentPatternNotFoundException("Alignment Pattern finder exceeded out of image");
|
||||
|
||||
if (image[targetPoint.X][targetPoint.Y] == QRCodeImageReader.POINT_LIGHT)
|
||||
{
|
||||
int scope = 0;
|
||||
bool found = false;
|
||||
while (!found)
|
||||
{
|
||||
scope++;
|
||||
for (int dy = scope; dy > - scope; dy--)
|
||||
{
|
||||
for (int dx = scope; dx > - scope; dx--)
|
||||
{
|
||||
int x = targetPoint.X + dx;
|
||||
int y = targetPoint.Y + dy;
|
||||
if ((x < 0 || y < 0) || (x > image.Length - 1 || y > image[0].Length - 1))
|
||||
throw new AlignmentPatternNotFoundException("Alignment Pattern finder exceeded out of image");
|
||||
if (image[x][y] == QRCodeImageReader.POINT_DARK)
|
||||
{
|
||||
targetPoint = new Point(targetPoint.X + dx, targetPoint.Y + dy);
|
||||
found = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
int x2, lx, rx, y2, uy, dy2;
|
||||
x2 = lx = rx = targetPoint.X;
|
||||
y2 = uy = dy2 = targetPoint.Y;
|
||||
|
||||
// GuoQing Hu's FIX
|
||||
while (lx >= 1 && !targetPointOnTheCorner(image, lx, y2, lx - 1, y2))
|
||||
lx--;
|
||||
while (rx < image.Length - 1 && !targetPointOnTheCorner(image, rx, y2, rx + 1, y2))
|
||||
rx++;
|
||||
while (uy >= 1 && !targetPointOnTheCorner(image, x2, uy, x2, uy - 1))
|
||||
uy--;
|
||||
while (dy2 < image[0].Length - 1 && !targetPointOnTheCorner(image, x2, dy2, x2, dy2 + 1))
|
||||
dy2++;
|
||||
|
||||
return new Point((lx + rx + 1) / 2, (uy + dy2 + 1) / 2);
|
||||
}
|
||||
|
||||
internal static bool targetPointOnTheCorner(bool[][] image, int x, int y, int nx, int ny)
|
||||
{
|
||||
if (x < 0 || y < 0 || nx < 0 || ny < 0 || x > image.Length || y > image[0].Length || nx > image.Length || ny > image[0].Length)
|
||||
{
|
||||
// Console.out.println("Overflow: x="+x+", y="+y+" nx="+nx+" ny="+ny+" x.max="+image.length+", y.max="+image[0].length);
|
||||
throw new AlignmentPatternNotFoundException("Alignment Pattern Finder exceeded image edge");
|
||||
//return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
return (image[x][y] == QRCodeImageReader.POINT_LIGHT && image[nx][ny] == QRCodeImageReader.POINT_DARK);
|
||||
}
|
||||
}
|
||||
|
||||
//get logical center coordinates of each alignment patterns
|
||||
public static Point[][] getLogicalCenter(FinderPattern finderPattern)
|
||||
{
|
||||
int version = finderPattern.Version;
|
||||
Point[][] logicalCenters = new Point[1][];
|
||||
for (int i = 0; i < 1; i++)
|
||||
{
|
||||
logicalCenters[i] = new Point[1];
|
||||
}
|
||||
int[] logicalSeeds = new int[1];
|
||||
//create "column(row)-coordinates" which based on relative coordinates
|
||||
//int sqrtCenters = (version / 7) + 2;
|
||||
//logicalSeeds = new int[sqrtCenters];
|
||||
//for(int i=0 ; i<sqrtCenters ; i++) {
|
||||
// logicalSeeds[i] = 6 + i * (4 + 4 * version) / (sqrtCenters - 1);
|
||||
// logicalSeeds[i] -= (logicalSeeds[i] - 2) % 4;
|
||||
//}
|
||||
logicalSeeds = LogicalSeed.getSeed(version);
|
||||
logicalCenters = new Point[logicalSeeds.Length][];
|
||||
for (int i2 = 0; i2 < logicalSeeds.Length; i2++)
|
||||
{
|
||||
logicalCenters[i2] = new Point[logicalSeeds.Length];
|
||||
}
|
||||
|
||||
//create real relative coordinates
|
||||
for (int col = 0; col < logicalCenters.Length; col++)
|
||||
{
|
||||
for (int row = 0; row < logicalCenters.Length; row++)
|
||||
{
|
||||
logicalCenters[row][col] = new Point(logicalSeeds[row], logicalSeeds[col]);
|
||||
}
|
||||
}
|
||||
return logicalCenters;
|
||||
}
|
||||
static AlignmentPattern()
|
||||
{
|
||||
canvas = QRCodeDecoder.Canvas;
|
||||
}
|
||||
}
|
||||
}
|
||||
744
reader/pattern/FinderPattern.cs
Normal file
744
reader/pattern/FinderPattern.cs
Normal file
@@ -0,0 +1,744 @@
|
||||
using System;
|
||||
using QRCodeDecoder = ThoughtWorks.QRCode.Codec.QRCodeDecoder;
|
||||
using ThoughtWorks.QRCode.Codec.Reader;
|
||||
using FinderPatternNotFoundException = ThoughtWorks.QRCode.ExceptionHandler.FinderPatternNotFoundException;
|
||||
using InvalidVersionInfoException = ThoughtWorks.QRCode.ExceptionHandler.InvalidVersionInfoException;
|
||||
using InvalidVersionException = ThoughtWorks.QRCode.ExceptionHandler.InvalidVersionException;
|
||||
using VersionInformationException = ThoughtWorks.QRCode.ExceptionHandler.VersionInformationException;
|
||||
using ThoughtWorks.QRCode.Geom;
|
||||
using ThoughtWorks.QRCode.Codec.Util;
|
||||
|
||||
namespace ThoughtWorks.QRCode.Codec.Reader.Pattern
|
||||
{
|
||||
|
||||
public class FinderPattern
|
||||
{
|
||||
public const int UL = 0;
|
||||
public const int UR = 1;
|
||||
public const int DL = 2;
|
||||
|
||||
internal static readonly int[] VersionInfoBit = new int[] { 0x07C94, 0x085BC, 0x09A99, 0x0A4D3, 0x0BBF6, 0x0C762, 0x0D847, 0x0E60D, 0x0F928, 0x10B78, 0x1145D, 0x12A17, 0x13532, 0x149A6, 0x15683, 0x168C9, 0x177EC, 0x18EC4, 0x191E1, 0x1AFAB, 0x1B08E, 0x1CC1A, 0x1D33F, 0x1ED75, 0x1F250, 0x209D5, 0x216F0, 0x228BA, 0x2379F, 0x24B0B, 0x2542E, 0x26A64, 0x27541, 0x28C69 };
|
||||
|
||||
internal static DebugCanvas canvas;
|
||||
internal Point[] center;
|
||||
internal int version;
|
||||
internal int[] sincos;
|
||||
internal int[] width;
|
||||
internal int[] moduleSize;
|
||||
|
||||
virtual public int Version
|
||||
{
|
||||
get
|
||||
{
|
||||
return version;
|
||||
}
|
||||
|
||||
}
|
||||
virtual public int SqrtNumModules
|
||||
{
|
||||
get
|
||||
{
|
||||
return 17 + 4 * version;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public static FinderPattern findFinderPattern(bool[][] image)
|
||||
{
|
||||
Line[] lineAcross = findLineAcross(image);
|
||||
Line[] lineCross = findLineCross(lineAcross);
|
||||
Point[] center = null;
|
||||
try
|
||||
{
|
||||
center = getCenter(lineCross);
|
||||
}
|
||||
catch (FinderPatternNotFoundException e)
|
||||
{
|
||||
throw e;
|
||||
}
|
||||
int[] sincos = getAngle(center);
|
||||
center = sort(center, sincos);
|
||||
int[] width = getWidth(image, center, sincos);
|
||||
// moduleSize for version recognition
|
||||
int[] moduleSize = new int[]{(width[UL] << QRCodeImageReader.DECIMAL_POINT) / 7, (width[UR] << QRCodeImageReader.DECIMAL_POINT) / 7, (width[DL] << QRCodeImageReader.DECIMAL_POINT) / 7};
|
||||
int version = calcRoughVersion(center, width);
|
||||
if (version > 6)
|
||||
{
|
||||
try
|
||||
{
|
||||
version = calcExactVersion(center, sincos, moduleSize, image);
|
||||
}
|
||||
catch (VersionInformationException e)
|
||||
{
|
||||
//use rough version data
|
||||
// throw e;
|
||||
}
|
||||
}
|
||||
return new FinderPattern(center, version, sincos, width, moduleSize);
|
||||
}
|
||||
|
||||
internal FinderPattern(Point[] center, int version, int[] sincos, int[] width, int[] moduleSize)
|
||||
{
|
||||
this.center = center;
|
||||
this.version = version;
|
||||
this.sincos = sincos;
|
||||
this.width = width;
|
||||
this.moduleSize = moduleSize;
|
||||
}
|
||||
|
||||
public virtual Point[] getCenter()
|
||||
{
|
||||
return center;
|
||||
}
|
||||
|
||||
public virtual Point getCenter(int position)
|
||||
{
|
||||
if (position >= UL && position <= DL)
|
||||
return center[position];
|
||||
else
|
||||
return null;
|
||||
}
|
||||
|
||||
public virtual int getWidth(int position)
|
||||
{
|
||||
return width[position];
|
||||
}
|
||||
|
||||
public virtual int[] getAngle()
|
||||
{
|
||||
return sincos;
|
||||
}
|
||||
|
||||
public virtual int getModuleSize()
|
||||
{
|
||||
return moduleSize[UL];
|
||||
}
|
||||
public virtual int getModuleSize(int place)
|
||||
{
|
||||
return moduleSize[place];
|
||||
}
|
||||
|
||||
internal static Line[] findLineAcross(bool[][] image)
|
||||
{
|
||||
int READ_HORIZONTAL = 0;
|
||||
int READ_VERTICAL = 1;
|
||||
|
||||
int imageWidth = image.Length;
|
||||
int imageHeight = image[0].Length;
|
||||
|
||||
//int currentX = 0, currentY = 0;
|
||||
Point current = new Point();
|
||||
System.Collections.ArrayList lineAcross = System.Collections.ArrayList.Synchronized(new System.Collections.ArrayList(10));
|
||||
|
||||
//buffer contains recent length of modules which has same brightness
|
||||
int[] lengthBuffer = new int[5];
|
||||
int bufferPointer = 0;
|
||||
|
||||
int direction = READ_HORIZONTAL; //start to read horizontally
|
||||
bool lastElement = QRCodeImageReader.POINT_LIGHT;
|
||||
|
||||
while (true)
|
||||
{
|
||||
//check points in image
|
||||
bool currentElement = image[current.X][current.Y];
|
||||
if (currentElement == lastElement)
|
||||
{
|
||||
//target point has same brightness with last point
|
||||
lengthBuffer[bufferPointer]++;
|
||||
}
|
||||
else
|
||||
{
|
||||
//target point has different brightness with last point
|
||||
if (currentElement == QRCodeImageReader.POINT_LIGHT)
|
||||
{
|
||||
if (checkPattern(lengthBuffer, bufferPointer))
|
||||
{
|
||||
//detected pattern
|
||||
int x1, y1, x2, y2;
|
||||
if (direction == READ_HORIZONTAL)
|
||||
{
|
||||
//obtain X coordinates of both side of the detected horizontal pattern
|
||||
x1 = current.X;
|
||||
for (int j = 0; j < 5; j++)
|
||||
{
|
||||
x1 -= lengthBuffer[j];
|
||||
}
|
||||
x2 = current.X - 1; //right side is last X coordinate
|
||||
y1 = y2 = current.Y;
|
||||
}
|
||||
else
|
||||
{
|
||||
x1 = x2 = current.X;
|
||||
//obtain Y coordinates of both side of the detected vertical pattern
|
||||
// upper side is sum of length of buffer
|
||||
y1 = current.Y;
|
||||
for (int j = 0; j < 5; j++)
|
||||
{
|
||||
y1 -= lengthBuffer[j];
|
||||
}
|
||||
y2 = current.Y - 1; // bottom side is last Y coordinate
|
||||
}
|
||||
lineAcross.Add(new Line(x1, y1, x2, y2));
|
||||
}
|
||||
}
|
||||
bufferPointer = (bufferPointer + 1) % 5;
|
||||
lengthBuffer[bufferPointer] = 1;
|
||||
lastElement = !lastElement;
|
||||
}
|
||||
|
||||
// determine if read next, change read direction or terminate this loop
|
||||
if (direction == READ_HORIZONTAL)
|
||||
{
|
||||
if (current.X < imageWidth - 1)
|
||||
{
|
||||
current.translate(1, 0);
|
||||
}
|
||||
else if (current.Y < imageHeight - 1)
|
||||
{
|
||||
current.set_Renamed(0, current.Y + 1);
|
||||
lengthBuffer = new int[5];
|
||||
}
|
||||
else
|
||||
{
|
||||
current.set_Renamed(0, 0); //reset target point
|
||||
lengthBuffer = new int[5];
|
||||
direction = READ_VERTICAL; //start to read vertically
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
//reading vertically
|
||||
if (current.Y < imageHeight - 1)
|
||||
current.translate(0, 1);
|
||||
else if (current.X < imageWidth - 1)
|
||||
{
|
||||
current.set_Renamed(current.X + 1, 0);
|
||||
lengthBuffer = new int[5];
|
||||
}
|
||||
else
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Line[] foundLines = new Line[lineAcross.Count];
|
||||
|
||||
for (int i = 0; i < foundLines.Length; i++)
|
||||
foundLines[i] = (Line) lineAcross[i];
|
||||
|
||||
canvas.drawLines(foundLines, ThoughtWorks.QRCode.Codec.Util.Color_Fields.LIGHTGREEN);
|
||||
return foundLines;
|
||||
}
|
||||
|
||||
internal static bool checkPattern(int[] buffer, int pointer)
|
||||
{
|
||||
int[] modelRatio = new int[]{1, 1, 3, 1, 1};
|
||||
|
||||
int baselength = 0;
|
||||
for (int i = 0; i < 5; i++)
|
||||
{
|
||||
baselength += buffer[i];
|
||||
}
|
||||
// pseudo fixed point calculation. I think it needs smarter code
|
||||
baselength <<= QRCodeImageReader.DECIMAL_POINT;
|
||||
baselength /= 7;
|
||||
int i2;
|
||||
for (i2 = 0; i2 < 5; i2++)
|
||||
{
|
||||
int leastlength = baselength * modelRatio[i2] - baselength / 2;
|
||||
int mostlength = baselength * modelRatio[i2] + baselength / 2;
|
||||
|
||||
//TODO rough finder pattern detection
|
||||
|
||||
int targetlength = buffer[(pointer + i2 + 1) % 5] << QRCodeImageReader.DECIMAL_POINT;
|
||||
if (targetlength < leastlength || targetlength > mostlength)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
//obtain lines cross at the center of Finder Patterns
|
||||
|
||||
internal static Line[] findLineCross(Line[] lineAcross)
|
||||
{
|
||||
System.Collections.ArrayList crossLines = System.Collections.ArrayList.Synchronized(new System.Collections.ArrayList(10));
|
||||
System.Collections.ArrayList lineNeighbor = System.Collections.ArrayList.Synchronized(new System.Collections.ArrayList(10));
|
||||
System.Collections.ArrayList lineCandidate = System.Collections.ArrayList.Synchronized(new System.Collections.ArrayList(10));
|
||||
Line compareLine;
|
||||
for (int i = 0; i < lineAcross.Length; i++)
|
||||
lineCandidate.Add(lineAcross[i]);
|
||||
|
||||
for (int i = 0; i < lineCandidate.Count - 1; i++)
|
||||
{
|
||||
lineNeighbor.Clear();
|
||||
lineNeighbor.Add(lineCandidate[i]);
|
||||
for (int j = i + 1; j < lineCandidate.Count; j++)
|
||||
{
|
||||
if (Line.isNeighbor((Line) lineNeighbor[lineNeighbor.Count - 1], (Line) lineCandidate[j]))
|
||||
{
|
||||
lineNeighbor.Add(lineCandidate[j]);
|
||||
compareLine = (Line) lineNeighbor[lineNeighbor.Count - 1];
|
||||
if (lineNeighbor.Count * 5 > compareLine.Length && j == lineCandidate.Count - 1)
|
||||
{
|
||||
crossLines.Add(lineNeighbor[lineNeighbor.Count / 2]);
|
||||
for (int k = 0; k < lineNeighbor.Count; k++)
|
||||
lineCandidate.Remove(lineNeighbor[k]);
|
||||
}
|
||||
}
|
||||
//terminate comparison if there are no possibility for found neighbour lines
|
||||
else if (cantNeighbor((Line) lineNeighbor[lineNeighbor.Count - 1], (Line) lineCandidate[j]) || (j == lineCandidate.Count - 1))
|
||||
{
|
||||
compareLine = (Line) lineNeighbor[lineNeighbor.Count - 1];
|
||||
/*
|
||||
* determine lines across Finder Patterns when number of neighbour lines are
|
||||
* bigger than 1/6 length of theirselves
|
||||
*/
|
||||
if (lineNeighbor.Count * 6 > compareLine.Length)
|
||||
{
|
||||
crossLines.Add(lineNeighbor[lineNeighbor.Count / 2]);
|
||||
for (int k = 0; k < lineNeighbor.Count; k++)
|
||||
{
|
||||
lineCandidate.Remove(lineNeighbor[k]);
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Line[] foundLines = new Line[crossLines.Count];
|
||||
for (int i = 0; i < foundLines.Length; i++)
|
||||
{
|
||||
foundLines[i] = (Line) crossLines[i];
|
||||
}
|
||||
return foundLines;
|
||||
}
|
||||
|
||||
internal static bool cantNeighbor(Line line1, Line line2)
|
||||
{
|
||||
if (Line.isCross(line1, line2))
|
||||
return true;
|
||||
|
||||
if (line1.Horizontal)
|
||||
{
|
||||
if (System.Math.Abs(line1.getP1().Y - line2.getP1().Y) > 1)
|
||||
return true;
|
||||
else
|
||||
return false;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (System.Math.Abs(line1.getP1().X - line2.getP1().X) > 1)
|
||||
return true;
|
||||
else
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
//obtain slope of symbol
|
||||
internal static int[] getAngle(Point[] centers)
|
||||
{
|
||||
|
||||
Line[] additionalLine = new Line[3];
|
||||
|
||||
for (int i = 0; i < additionalLine.Length; i++)
|
||||
{
|
||||
additionalLine[i] = new Line(centers[i], centers[(i + 1) % additionalLine.Length]);
|
||||
}
|
||||
// remoteLine - does not contain UL center
|
||||
Line remoteLine = Line.getLongest(additionalLine);
|
||||
Point originPoint = new Point();
|
||||
for (int i = 0; i < centers.Length; i++)
|
||||
{
|
||||
if (!remoteLine.getP1().equals(centers[i]) && !remoteLine.getP2().equals(centers[i]))
|
||||
{
|
||||
originPoint = centers[i];
|
||||
break;
|
||||
}
|
||||
}
|
||||
canvas.println("originPoint is: " + originPoint);
|
||||
Point remotePoint = new Point();
|
||||
|
||||
//with origin that the center of Left-Up Finder Pattern, determine other two patterns center.
|
||||
//then calculate symbols angle
|
||||
if (originPoint.Y <= remoteLine.getP1().Y & originPoint.Y <= remoteLine.getP2().Y)
|
||||
if (remoteLine.getP1().X < remoteLine.getP2().X)
|
||||
remotePoint = remoteLine.getP2();
|
||||
else
|
||||
remotePoint = remoteLine.getP1();
|
||||
else if (originPoint.X >= remoteLine.getP1().X & originPoint.X >= remoteLine.getP2().X)
|
||||
if (remoteLine.getP1().Y < remoteLine.getP2().Y)
|
||||
remotePoint = remoteLine.getP2();
|
||||
else
|
||||
remotePoint = remoteLine.getP1();
|
||||
else if (originPoint.Y >= remoteLine.getP1().Y & originPoint.Y >= remoteLine.getP2().Y)
|
||||
if (remoteLine.getP1().X < remoteLine.getP2().X)
|
||||
remotePoint = remoteLine.getP1();
|
||||
else
|
||||
remotePoint = remoteLine.getP2();
|
||||
//1st or 4th quadrant
|
||||
else if (remoteLine.getP1().Y < remoteLine.getP2().Y)
|
||||
remotePoint = remoteLine.getP1();
|
||||
else
|
||||
remotePoint = remoteLine.getP2();
|
||||
|
||||
int r = new Line(originPoint, remotePoint).Length;
|
||||
//canvas.println(Integer.toString(((remotePoint.getX() - originPoint.getX()) << QRCodeImageReader.DECIMAL_POINT)));
|
||||
int[] angle = new int[2];
|
||||
angle[0] = ((remotePoint.Y - originPoint.Y) << QRCodeImageReader.DECIMAL_POINT) / r; //Sin
|
||||
angle[1] = ((remotePoint.X - originPoint.X) << (QRCodeImageReader.DECIMAL_POINT)) / r; //Cos
|
||||
|
||||
return angle;
|
||||
}
|
||||
|
||||
internal static Point[] getCenter(Line[] crossLines)
|
||||
{
|
||||
System.Collections.ArrayList centers = System.Collections.ArrayList.Synchronized(new System.Collections.ArrayList(10));
|
||||
for (int i = 0; i < crossLines.Length - 1; i++)
|
||||
{
|
||||
Line compareLine = crossLines[i];
|
||||
for (int j = i + 1; j < crossLines.Length; j++)
|
||||
{
|
||||
Line comparedLine = crossLines[j];
|
||||
if (Line.isCross(compareLine, comparedLine))
|
||||
{
|
||||
int x = 0;
|
||||
int y = 0;
|
||||
if (compareLine.Horizontal)
|
||||
{
|
||||
x = compareLine.Center.X;
|
||||
y = comparedLine.Center.Y;
|
||||
}
|
||||
else
|
||||
{
|
||||
x = comparedLine.Center.X;
|
||||
y = compareLine.Center.Y;
|
||||
}
|
||||
centers.Add(new Point(x, y));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Point[] foundPoints = new Point[centers.Count];
|
||||
|
||||
for (int i = 0; i < foundPoints.Length; i++)
|
||||
{
|
||||
foundPoints[i] = (Point) centers[i];
|
||||
//Console.out.println(foundPoints[i]);
|
||||
}
|
||||
//Console.out.println(foundPoints.length);
|
||||
|
||||
if (foundPoints.Length == 3)
|
||||
{
|
||||
canvas.drawPolygon(foundPoints, ThoughtWorks.QRCode.Codec.Util.Color_Fields.RED);
|
||||
return foundPoints;
|
||||
}
|
||||
else
|
||||
throw new FinderPatternNotFoundException("Invalid number of Finder Pattern detected");
|
||||
}
|
||||
|
||||
//sort center of finder patterns as Left-Up: points[0], Right-Up: points[1], Left-Down: points[2].
|
||||
internal static Point[] sort(Point[] centers, int[] angle)
|
||||
{
|
||||
|
||||
Point[] sortedCenters = new Point[3];
|
||||
|
||||
int quadant = getURQuadant(angle);
|
||||
switch (quadant)
|
||||
{
|
||||
|
||||
case 1:
|
||||
sortedCenters[1] = getPointAtSide(centers, Point.RIGHT, Point.BOTTOM);
|
||||
sortedCenters[2] = getPointAtSide(centers, Point.BOTTOM, Point.LEFT);
|
||||
break;
|
||||
|
||||
case 2:
|
||||
sortedCenters[1] = getPointAtSide(centers, Point.BOTTOM, Point.LEFT);
|
||||
sortedCenters[2] = getPointAtSide(centers, Point.TOP, Point.LEFT);
|
||||
break;
|
||||
|
||||
case 3:
|
||||
sortedCenters[1] = getPointAtSide(centers, Point.LEFT, Point.TOP);
|
||||
sortedCenters[2] = getPointAtSide(centers, Point.RIGHT, Point.TOP);
|
||||
break;
|
||||
|
||||
case 4:
|
||||
sortedCenters[1] = getPointAtSide(centers, Point.TOP, Point.RIGHT);
|
||||
sortedCenters[2] = getPointAtSide(centers, Point.BOTTOM, Point.RIGHT);
|
||||
break;
|
||||
}
|
||||
|
||||
//last of centers is Left-Up patterns one
|
||||
for (int i = 0; i < centers.Length; i++)
|
||||
{
|
||||
if (!centers[i].equals(sortedCenters[1]) && !centers[i].equals(sortedCenters[2]))
|
||||
{
|
||||
sortedCenters[0] = centers[i];
|
||||
}
|
||||
}
|
||||
|
||||
return sortedCenters;
|
||||
}
|
||||
|
||||
internal static int getURQuadant(int[] angle)
|
||||
{
|
||||
int sin = angle[0];
|
||||
int cos = angle[1];
|
||||
if (sin >= 0 && cos > 0)
|
||||
return 1;
|
||||
else if (sin > 0 && cos <= 0)
|
||||
return 2;
|
||||
else if (sin <= 0 && cos < 0)
|
||||
return 3;
|
||||
else if (sin < 0 && cos >= 0)
|
||||
return 4;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
internal static Point getPointAtSide(Point[] points, int side1, int side2)
|
||||
{
|
||||
Point sidePoint = new Point();
|
||||
int x = ((side1 == Point.RIGHT || side2 == Point.RIGHT)?0:System.Int32.MaxValue);
|
||||
int y = ((side1 == Point.BOTTOM || side2 == Point.BOTTOM)?0:System.Int32.MaxValue);
|
||||
sidePoint = new Point(x, y);
|
||||
|
||||
for (int i = 0; i < points.Length; i++)
|
||||
{
|
||||
switch (side1)
|
||||
{
|
||||
|
||||
case Point.RIGHT:
|
||||
if (sidePoint.X < points[i].X)
|
||||
{
|
||||
sidePoint = points[i];
|
||||
}
|
||||
else if (sidePoint.X == points[i].X)
|
||||
{
|
||||
if (side2 == Point.BOTTOM)
|
||||
{
|
||||
if (sidePoint.Y < points[i].Y)
|
||||
{
|
||||
sidePoint = points[i];
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (sidePoint.Y > points[i].Y)
|
||||
{
|
||||
sidePoint = points[i];
|
||||
}
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
case Point.BOTTOM:
|
||||
if (sidePoint.Y < points[i].Y)
|
||||
{
|
||||
sidePoint = points[i];
|
||||
}
|
||||
else if (sidePoint.Y == points[i].Y)
|
||||
{
|
||||
if (side2 == Point.RIGHT)
|
||||
{
|
||||
if (sidePoint.X < points[i].X)
|
||||
{
|
||||
sidePoint = points[i];
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (sidePoint.X > points[i].X)
|
||||
{
|
||||
sidePoint = points[i];
|
||||
}
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
case Point.LEFT:
|
||||
if (sidePoint.X > points[i].X)
|
||||
{
|
||||
sidePoint = points[i];
|
||||
}
|
||||
else if (sidePoint.X == points[i].X)
|
||||
{
|
||||
if (side2 == Point.BOTTOM)
|
||||
{
|
||||
if (sidePoint.Y < points[i].Y)
|
||||
{
|
||||
sidePoint = points[i];
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (sidePoint.Y > points[i].Y)
|
||||
{
|
||||
sidePoint = points[i];
|
||||
}
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
case Point.TOP:
|
||||
if (sidePoint.Y > points[i].Y)
|
||||
{
|
||||
sidePoint = points[i];
|
||||
}
|
||||
else if (sidePoint.Y == points[i].Y)
|
||||
{
|
||||
if (side2 == Point.RIGHT)
|
||||
{
|
||||
if (sidePoint.X < points[i].X)
|
||||
{
|
||||
sidePoint = points[i];
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (sidePoint.X > points[i].X)
|
||||
{
|
||||
sidePoint = points[i];
|
||||
}
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
return sidePoint;
|
||||
}
|
||||
|
||||
internal static int[] getWidth(bool[][] image, Point[] centers, int[] sincos)
|
||||
{
|
||||
|
||||
int[] width = new int[3];
|
||||
|
||||
for (int i = 0; i < 3; i++)
|
||||
{
|
||||
bool flag = false;
|
||||
int lx, rx;
|
||||
int y = centers[i].Y;
|
||||
for (lx = centers[i].X; lx > 0; lx--)
|
||||
{
|
||||
if (image[lx][y] == QRCodeImageReader.POINT_DARK && image[lx - 1][y] == QRCodeImageReader.POINT_LIGHT)
|
||||
{
|
||||
if (flag == false)
|
||||
flag = true;
|
||||
else
|
||||
break;
|
||||
}
|
||||
}
|
||||
flag = false;
|
||||
for (rx = centers[i].X; rx < image.Length; rx++)
|
||||
{
|
||||
if (image[rx][y] == QRCodeImageReader.POINT_DARK && image[rx + 1][y] == QRCodeImageReader.POINT_LIGHT)
|
||||
{
|
||||
if (flag == false)
|
||||
flag = true;
|
||||
else
|
||||
break;
|
||||
}
|
||||
}
|
||||
width[i] = (rx - lx + 1);
|
||||
}
|
||||
return width;
|
||||
}
|
||||
|
||||
internal static int calcRoughVersion(Point[] center, int[] width)
|
||||
{
|
||||
int dp = QRCodeImageReader.DECIMAL_POINT;
|
||||
int lengthAdditionalLine = (new Line(center[UL], center[UR]).Length) << dp;
|
||||
int avarageWidth = ((width[UL] + width[UR]) << dp) / 14;
|
||||
int roughVersion = ((lengthAdditionalLine / avarageWidth) - 10) / 4;
|
||||
if (((lengthAdditionalLine / avarageWidth) - 10) % 4 >= 2)
|
||||
{
|
||||
roughVersion++;
|
||||
}
|
||||
|
||||
return roughVersion;
|
||||
}
|
||||
|
||||
internal static int calcExactVersion(Point[] centers, int[] angle, int[] moduleSize, bool[][] image)
|
||||
{
|
||||
bool[] versionInformation = new bool[18];
|
||||
Point[] points = new Point[18];
|
||||
Point target;
|
||||
Axis axis = new Axis(angle, moduleSize[UR]); //UR
|
||||
axis.Origin = centers[UR];
|
||||
|
||||
for (int y = 0; y < 6; y++)
|
||||
{
|
||||
for (int x = 0; x < 3; x++)
|
||||
{
|
||||
target = axis.translate(x - 7, y - 3);
|
||||
versionInformation[x + y * 3] = image[target.X][target.Y];
|
||||
points[x + y * 3] = target;
|
||||
}
|
||||
}
|
||||
canvas.drawPoints(points, ThoughtWorks.QRCode.Codec.Util.Color_Fields.RED);
|
||||
|
||||
int exactVersion = 0;
|
||||
try
|
||||
{
|
||||
exactVersion = checkVersionInfo(versionInformation);
|
||||
}
|
||||
catch (InvalidVersionInfoException e)
|
||||
{
|
||||
canvas.println("Version info error. now retry with other place one.");
|
||||
axis.Origin = centers[DL];
|
||||
axis.ModulePitch = moduleSize[DL]; //DL
|
||||
|
||||
for (int x = 0; x < 6; x++)
|
||||
{
|
||||
for (int y = 0; y < 3; y++)
|
||||
{
|
||||
target = axis.translate(x - 3, y - 7);
|
||||
versionInformation[y + x * 3] = image[target.X][target.Y];
|
||||
points[x + y * 3] = target;
|
||||
}
|
||||
}
|
||||
canvas.drawPoints(points, ThoughtWorks.QRCode.Codec.Util.Color_Fields.RED);
|
||||
|
||||
try
|
||||
{
|
||||
exactVersion = checkVersionInfo(versionInformation);
|
||||
}
|
||||
catch (VersionInformationException e2)
|
||||
{
|
||||
throw e2;
|
||||
}
|
||||
}
|
||||
return exactVersion;
|
||||
}
|
||||
|
||||
internal static int checkVersionInfo(bool[] target)
|
||||
{
|
||||
// note that this method includes BCH 18-6 Error Correction
|
||||
// see page 67 on JIS-X-0510(2004)
|
||||
int errorCount = 0, versionBase;
|
||||
for (versionBase = 0; versionBase < VersionInfoBit.Length; versionBase++)
|
||||
{
|
||||
errorCount = 0;
|
||||
for (int j = 0; j < 18; j++)
|
||||
{
|
||||
if (target[j] ^ (VersionInfoBit[versionBase] >> j) % 2 == 1)
|
||||
errorCount++;
|
||||
}
|
||||
if (errorCount <= 3)
|
||||
break;
|
||||
}
|
||||
if (errorCount <= 3)
|
||||
return 7 + versionBase;
|
||||
else
|
||||
throw new InvalidVersionInfoException("Too many errors in version information");
|
||||
}
|
||||
static FinderPattern()
|
||||
{
|
||||
canvas = QRCodeDecoder.Canvas;
|
||||
}
|
||||
}
|
||||
}
|
||||
70
reader/pattern/LogicalSeed.cs
Normal file
70
reader/pattern/LogicalSeed.cs
Normal file
@@ -0,0 +1,70 @@
|
||||
using System;
|
||||
namespace ThoughtWorks.QRCode.Codec.Reader.Pattern
|
||||
{
|
||||
|
||||
/// <summary> This class returns the position of the position patterns</summary>
|
||||
public class LogicalSeed
|
||||
{
|
||||
/// <summary> The positions</summary>
|
||||
private static int[][] seed;
|
||||
|
||||
/// <summary> Returns all the seeds for a version</summary>
|
||||
public static int[] getSeed(int version)
|
||||
{
|
||||
return (seed[version - 1]);
|
||||
}
|
||||
|
||||
/// <summary> Returns a seed for a version and a pattern number</summary>
|
||||
public static int getSeed(int version, int patternNumber)
|
||||
{
|
||||
return (seed[version - 1][patternNumber]);
|
||||
}
|
||||
/// <summary> The static constructor instanciates the values</summary>
|
||||
static LogicalSeed()
|
||||
{
|
||||
{
|
||||
seed = new int[40][];
|
||||
seed[0] = new int[]{6, 14};
|
||||
seed[1] = new int[]{6, 18};
|
||||
seed[2] = new int[]{6, 22};
|
||||
seed[3] = new int[]{6, 26};
|
||||
seed[4] = new int[]{6, 30};
|
||||
seed[5] = new int[]{6, 34};
|
||||
seed[6] = new int[]{6, 22, 38};
|
||||
seed[7] = new int[]{6, 24, 42};
|
||||
seed[8] = new int[]{6, 26, 46};
|
||||
seed[9] = new int[]{6, 28, 50};
|
||||
seed[10] = new int[]{6, 30, 54};
|
||||
seed[11] = new int[]{6, 32, 58};
|
||||
seed[12] = new int[]{6, 34, 62};
|
||||
seed[13] = new int[]{6, 26, 46, 66};
|
||||
seed[14] = new int[]{6, 26, 48, 70};
|
||||
seed[15] = new int[]{6, 26, 50, 74};
|
||||
seed[16] = new int[]{6, 30, 54, 78};
|
||||
seed[17] = new int[]{6, 30, 56, 82};
|
||||
seed[18] = new int[]{6, 30, 58, 86};
|
||||
seed[19] = new int[]{6, 34, 62, 90};
|
||||
seed[20] = new int[]{6, 28, 50, 72, 94};
|
||||
seed[21] = new int[]{6, 26, 50, 74, 98};
|
||||
seed[22] = new int[]{6, 30, 54, 78, 102};
|
||||
seed[23] = new int[]{6, 28, 54, 80, 106};
|
||||
seed[24] = new int[]{6, 32, 58, 84, 110};
|
||||
seed[25] = new int[]{6, 30, 58, 86, 114};
|
||||
seed[26] = new int[]{6, 34, 62, 90, 118};
|
||||
seed[27] = new int[]{6, 26, 50, 74, 98, 122};
|
||||
seed[28] = new int[]{6, 30, 54, 78, 102, 126};
|
||||
seed[29] = new int[]{6, 26, 52, 78, 104, 130};
|
||||
seed[30] = new int[]{6, 30, 56, 82, 108, 134};
|
||||
seed[31] = new int[]{6, 34, 60, 86, 112, 138};
|
||||
seed[32] = new int[]{6, 30, 58, 86, 114, 142};
|
||||
seed[33] = new int[]{6, 34, 62, 90, 118, 146};
|
||||
seed[34] = new int[]{6, 30, 54, 78, 102, 126, 150};
|
||||
seed[35] = new int[]{6, 24, 50, 76, 102, 128, 154};
|
||||
seed[36] = new int[]{6, 28, 54, 80, 106, 132, 158};
|
||||
seed[37] = new int[]{6, 32, 58, 84, 110, 136, 162};
|
||||
seed[38] = new int[]{6, 26, 54, 82, 110, 138, 166};
|
||||
seed[39] = new int[]{6, 30, 58, 86, 114, 142, 170};
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user