📄 qrcodedecoder.java
字号:
if (x % 3 == 0)
maskPattern[x][y] = true;
break;
case 3: // 011
if ((x + y) % 3 == 0)
maskPattern[x][y] = true;
break;
case 4: // 100
if ((x / 3 + y / 2) % 2 == 0)
maskPattern[x][y] = true;
break;
case 5: // 101
if ((x * y) % 2 + (x * y) % 3 == 0)
maskPattern[x][y] = true;
break;
case 6: // 110
if (((x * y) % 2 + (x * y) % 3) % 2 == 0)
maskPattern[x][y] = true;
break;
case 7: // 111
if (((x * y) % 3 + (x + y) % 2) % 2 == 0)
maskPattern[x][y] = true;
break;
}
}
}
return maskPattern;
}
int[] getBlocks(QRCodeSymbol symbol) {
int width = symbol.getWidth();
//System.out.println("SymbolWidth:" + Integer.toString(symbol.getWidth()));
//System.out.println("SymbolHeight:" + Integer.toString(symbol.getHeight()));
int height = symbol.getHeight();
int x = width - 1;
int y = height - 1;
Vector codeBits = new Vector();
Vector codeWords = new Vector();
int tempWord = 0;
int figure = 7;
int isNearFinish = 0;
final boolean READ_UP = true;
final boolean READ_DOWN = false;
boolean direction = READ_UP;
do {
//canvas.drawPoint(new Point(x * 4 +8 , y * 4 + 47), Color.RED);
codeBits.addElement(new Boolean(symbol.getElement(x, y)));
//System.out.println(Integer.toString(codeBits.size()));
//canvas.drawPoint(new Point(x*3 + 120,y*3 + 190), Color.RED);
//int ratio = canvas.getWidth() / symbol.getWidth();
//int offsetX = (canvas.getWidth() - symbol.getWidth() * ratio) / 2;
//int offsetY = (canvas.getHeight() - symbol.getHeight() * ratio) / 2;
//canvas.drawPoint(new Point(offsetX + x * ratio + 3, offsetY + y * ratio + 3), 0xFF0000);
if (symbol.getElement(x, y) == true) {
tempWord += 1 << figure;
}
//System.out.println(new Point(x, y).toString() + " " + symbol.getElement(x, y));
figure--;
if (figure == -1) {
codeWords.addElement(new Integer(tempWord));
//System.out.print(codeWords.size() + ": ");
//System.out.println(tempWord);
figure = 7;
tempWord = 0;
}
//師偵撉傓儌僕儏乕儖傪寛掕偡傞
do {
if (direction == READ_UP) {
if ((x + isNearFinish) % 2 == 0) //擇楍偺偆偪塃懁側傜
x--; //嵍懁
else {
if (y > 0) { //忋偵恑傔傞
x++;
y--;
}
else { //恑傔側偄
x--; //曽岦揮姺偟
if (x == 6){
x--;
isNearFinish=1; // 廲偺僞僀儈儞僌僷僞乕儞傪捠夁偡傞偙偲偵傛偭偰敾掕傪曄偊傞
}
direction = READ_DOWN;
}
}
}
else {
if ((x + isNearFinish) % 2 == 0) //擇楍偺偆偪嵍懁側傜
x--;
else {
if (y < height - 1) {
x++;
y++;
}
else {
x--;
if (x == 6){
x--;
isNearFinish=1;
}
direction = READ_UP;
}
}
}
} while (symbol.isInFunctionPattern(x, y));
} while (x != -1);
int[] gotWords = new int[codeWords.size()];
for (int i = 0; i < codeWords.size(); i++) {
Integer temp = (Integer)codeWords.elementAt(i);
gotWords[i] = temp.intValue();
}
return gotWords;
}
int[] getCorrectedDataBlocks(int[] blocks) {
int numErrors = 0;
//System.out.println(":");
//System.out.println("blockLength: " + blocks.length);
int version = symbol.getVersion();
//System.out.println("Version: " + version);
int errorCollectionLevel = symbol.getErrorCollectionLevel();
//System.out.println("errorCollectionLevel:" + errorCollectionLevel);
int dataCapacity = symbol.getDataCapacity();
int[] dataBlocks = new int[dataCapacity];
//System.out.println("dataCapacity: " + dataCapacity);
int numErrorCollectionCode = symbol.getNumErrorCollectionCode();
//System.out.println("numErrorCollectionCode:" + numErrorCollectionCode);
int numRSBlocks = symbol.getNumRSBlocks();
int eccPerRSBlock = numErrorCollectionCode / numRSBlocks;
//System.out.println("numRSBlocks: " + numRSBlocks);
if (numRSBlocks == 1) {
//[TODO]僄儔乕掶惓張棟
// for (int k = 0; k < blocks.length; k++) {
// System.out.print(String.valueOf(blocks[k]) + ",");
// }
// System.out.println("");
// System.out.println("numWords="+String.valueOf(blocks.length));
ReedSolomon corrector = new ReedSolomon(blocks);
corrector.correct();
numErrors += corrector.getNumCorrectedErrors();
if (numErrors > 0)
canvas.println(String.valueOf(numErrors) + " data errors corrected.");
else
canvas.println("No errors found.");
return blocks;
}
else { //RS僽儘僢僋偑2偮埲忋偺偨傔丄僨乕僞僽儘僢僋暲傃懼偊偁傝
int numLongerRSBlocks = dataCapacity % numRSBlocks;
if (numLongerRSBlocks == 0) { //RS僽儘僢僋偼1庬椶
int lengthRSBlock = dataCapacity / numRSBlocks;
int[][] RSBlocks = new int[numRSBlocks][lengthRSBlock];
//RS僽儘僢僋傪摼傞
for (int i = 0; i < numRSBlocks; i++) {
//System.out.println("i = " + i);
//for (int j = 0; j < lengthRSBlock - numErrorCollectionCode / numRSBlocks ; j++) {
for (int j = 0; j < lengthRSBlock; j++) {
//System.out.println("j = " + j);
//try {
RSBlocks[i][j] = blocks[j * numRSBlocks + i];
//} catch (ArrayIndexOutOfBoundsException e) {}
}
//[TODO]僄儔乕掶惓張棟
// for (int k = 0; k < RSBlocks[i].length; k++) {
// System.out.print(String.valueOf(RSBlocks[i][k]) + ",");
// }
//System.out.println("");
//System.out.println("numWords="+String.valueOf(RSBlocks[i].length));
ReedSolomon corrector = new ReedSolomon(RSBlocks[i]);
corrector.correct();
numErrors += corrector.getNumCorrectedErrors();
}
//僨乕僞晹暘偺傒敳偒弌偡
int p = 0;
for (int i = 0; i < numRSBlocks; i++) {
for (int j = 0; j < lengthRSBlock - eccPerRSBlock; j++) {
dataBlocks[p++] = RSBlocks[i][j];
}
}
}
else { //RS僽儘僢僋偼2庬椶
int lengthShorterRSBlock = dataCapacity / numRSBlocks;
//System.out.println("lengthShorterRSBlock : " + lengthShorterRSBlock);
int lengthLongerRSBlock = dataCapacity / numRSBlocks + 1;
//System.out.println("lengthLongerRSBlock: " + lengthLongerRSBlock);
int numShorterRSBlocks = numRSBlocks - numLongerRSBlocks;
//System.out.println("numShorterRSBlocks: " + numShorterRSBlocks);
//System.out.println("numLongerRSBlocks: " + numLongerRSBlocks);
int[][] shorterRSBlocks = new int[numShorterRSBlocks][lengthShorterRSBlock];
int[][] longerRSBlocks = new int[numLongerRSBlocks][lengthLongerRSBlock];
for (int i = 0; i < numRSBlocks; i++) {
//System.out.println("i = " + i);
if (i < numShorterRSBlocks) {
//抁偄曽偺RS僽儘僢僋傪摼傞
//for (int j = 0; j < lengthShorterRSBlock - numErrorCollectionCode / numRSBlocks ; j++) {
int mod = 0;
for (int j = 0; j < lengthShorterRSBlock; j++) {
//System.out.println(" j = " + j);
if (j == lengthShorterRSBlock - eccPerRSBlock) mod = numLongerRSBlocks;
//System.out.print(String.valueOf(j * numRSBlocks + i + mod) + ",");
shorterRSBlocks[i][j] = blocks[j * numRSBlocks + i + mod];
}
//System.out.println("");
//[TODO]僄儔乕掶惓張棟
// for (int k = 0; k < shorterRSBlocks[i].length; k++) {
// System.out.print(String.valueOf(shorterRSBlocks[i][k]) + ",");
// }
//System.out.println("");
//System.out.println("numWords="+String.valueOf(shorterRSBlocks[i].length));
ReedSolomon corrector = new ReedSolomon(shorterRSBlocks[i]);
corrector.correct();
numErrors += corrector.getNumCorrectedErrors();
}
else {
//System.out.println("Debug" + String.valueOf(numShorterRSBlocks));
//挿偄曽偺RS僽儘僢僋傪摼傞
//for (int j = 0; j < lengthLongerRSBlock - numErrorCollectionCode / numRSBlocks ; j++) {
int mod = 0;
for (int j = 0; j < lengthLongerRSBlock; j++) {
//System.out.println(" j = " + j);
//try {
if (j == lengthShorterRSBlock - eccPerRSBlock) mod = numShorterRSBlocks;
// System.out.print("," + String.valueOf(j * numRSBlocks + i - mod));
longerRSBlocks[i - numShorterRSBlocks][j] = blocks[j * numRSBlocks + i - mod];
//} catch (Exception e) {e.getMessage();}
}
//System.out.println("debug2");
//[TODO]僄儔乕掶惓張棟
// for (int k = 0; k < longerRSBlocks[i - numShorterRSBlocks].length; k++) {
// System.out.print(String.valueOf(longerRSBlocks[i - numShorterRSBlocks][k]) + ",");
// }
//System.out.println("");
//System.out.println("numWords="+String.valueOf(longerRSBlocks[i - numShorterRSBlocks].length));
ReedSolomon corrector = new ReedSolomon(longerRSBlocks[i - numShorterRSBlocks]);
corrector.correct();
numErrors += corrector.getNumCorrectedErrors();
}
}
int p = 0;
for (int i = 0; i < numRSBlocks; i++) {
if (i < numShorterRSBlocks) {
for (int j = 0; j < lengthShorterRSBlock - eccPerRSBlock; j++) {
dataBlocks[p++] = shorterRSBlocks[i][j];
}
}
else {
for (int j = 0; j < lengthLongerRSBlock - eccPerRSBlock; j++) {
dataBlocks[p++] = longerRSBlocks[i - numShorterRSBlocks][j];
}
}
}
}
if (numErrors > 0)
canvas.println(String.valueOf(numErrors) + " data errors corrected.");
return dataBlocks;
}
}
String getDecodedString(int[] blocks, int version) throws IllegalDataBlockException {
//canvas.println("Reading data.");
String dataString = null;
QRCodeDataBlockReader reader = new QRCodeDataBlockReader(blocks, version);
try {
dataString = reader.getDataString();
} catch (ArrayIndexOutOfBoundsException e) {
//canvas.println("ERROR: Data block error");
throw new IllegalDataBlockException();
}
return dataString;
}
public DebugCanvas getCanvas() {
return canvas;
}
public void setCanvas(DebugCanvas canvas) {
this.canvas = canvas;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -