private static sbyte[] calculateRSECC(sbyte[] codewords, sbyte rsEccCodewords, sbyte[] rsBlockOrder, int maxDataCodewords, int maxCodewords) { sbyte[][] rsCalTableArray = new sbyte[256][]; for (int i = 0; i < 256; i++) { rsCalTableArray[i] = new sbyte[rsEccCodewords]; } try { //String filename = QRCODE_DATA_PATH + @"\rsc" + rsEccCodewords.ToString() + ".dat"; //StreamReader reader = new StreamReader(filename); String fileName = DATA_PATH + "." + "rsc" + rsEccCodewords.ToString() + ".dat"; //MemoryStream memoryStream = new MemoryStream(Resources.GetResource(fileName)); Stream stream = GetEmbeddedFile(fileName); BufferedStream bis = new BufferedStream(stream); for (int i = 0; i < 256; i++) { SystemUtils.ReadInput(bis, rsCalTableArray[i], 0, rsCalTableArray[i].Length); } bis.Close(); //reader.Close(); stream.Close(); } catch (Exception e) { SystemUtils.WriteStackTrace(e, Console.Error); } /* ---- RS-ECC prepare */ int i2 = 0; int j = 0; int rsBlockNumber = 0; sbyte[][] rsTemp = new sbyte[rsBlockOrder.Length][]; /* * for (int i = 0; i < rsBlockOrder.Length; i++) * { * rsTemp[i] = new sbyte[rsBlockOrder.Length]; * } */ sbyte[] res = new sbyte[maxCodewords]; Array.Copy(codewords, 0, res, 0, codewords.Length); i2 = 0; while (i2 < rsBlockOrder.Length) { rsTemp[i2] = new sbyte[(rsBlockOrder[i2] & 0xFF) - rsEccCodewords]; i2++; } i2 = 0; while (i2 < maxDataCodewords) { rsTemp[rsBlockNumber][j] = codewords[i2]; j++; if (j >= (rsBlockOrder[rsBlockNumber] & 0xFF) - rsEccCodewords) { j = 0; rsBlockNumber++; } i2++; } /* --- RS-ECC main --- */ rsBlockNumber = 0; while (rsBlockNumber < rsBlockOrder.Length) { sbyte[] rsTempData; rsTempData = new sbyte[rsTemp[rsBlockNumber].Length]; rsTemp[rsBlockNumber].CopyTo(rsTempData, 0); int rsCodewords = (rsBlockOrder[rsBlockNumber] & 0xFF); int rsDataCodewords = rsCodewords - rsEccCodewords; j = rsDataCodewords; while (j > 0) { sbyte first = rsTempData[0]; if (first != 0) { sbyte[] leftChr = new sbyte[rsTempData.Length - 1]; Array.Copy(rsTempData, 1, leftChr, 0, rsTempData.Length - 1); sbyte[] cal = rsCalTableArray[(first & 0xFF)]; rsTempData = calculateByteArrayBits(leftChr, cal, "xor"); } else { if (rsEccCodewords < rsTempData.Length) { sbyte[] rsTempNew = new sbyte[rsTempData.Length - 1]; Array.Copy(rsTempData, 1, rsTempNew, 0, rsTempData.Length - 1); rsTempData = new sbyte[rsTempNew.Length]; rsTempNew.CopyTo(rsTempData, 0); } else { sbyte[] rsTempNew = new sbyte[rsEccCodewords]; Array.Copy(rsTempData, 1, rsTempNew, 0, rsTempData.Length - 1); rsTempNew[rsEccCodewords - 1] = 0; rsTempData = new sbyte[rsTempNew.Length]; rsTempNew.CopyTo(rsTempData, 0); } } j--; } Array.Copy(rsTempData, 0, res, codewords.Length + rsBlockNumber * rsEccCodewords, (byte)rsEccCodewords); rsBlockNumber++; } return(res); }
public virtual bool[][] calQrcode(byte[] qrcodeData) { int dataLength; int dataCounter = 0; dataLength = qrcodeData.Length; int[] dataValue = new int[dataLength + 32]; sbyte[] dataBits = new sbyte[dataLength + 32]; if (dataLength <= 0) { bool[][] ret = new bool[][] { new bool[] { false } }; return(ret); } if (qrcodeStructureappendN > 1) { dataValue[0] = 3; dataBits[0] = 4; dataValue[1] = qrcodeStructureappendM - 1; dataBits[1] = 4; dataValue[2] = qrcodeStructureappendN - 1; dataBits[2] = 4; dataValue[3] = qrcodeStructureappendParity; dataBits[3] = 8; dataCounter = 4; } dataBits[dataCounter] = 4; /* --- determine encode mode --- */ int[] codewordNumPlus; int codewordNumCounterValue; switch (qrcodeEncodeMode) { /* ---- alphanumeric mode --- */ case ENCODE_MODE.ALPHA_NUMERIC: codewordNumPlus = new int[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4 }; dataValue[dataCounter] = 2; dataCounter++; dataValue[dataCounter] = dataLength; dataBits[dataCounter] = 9; codewordNumCounterValue = dataCounter; dataCounter++; for (int i = 0; i < dataLength; i++) { char chr = (char)qrcodeData[i]; sbyte chrValue = 0; if (chr >= 48 && chr < 58) { chrValue = (sbyte)(chr - 48); } else { if (chr >= 65 && chr < 91) { chrValue = (sbyte)(chr - 55); } else { if (chr == 32) { chrValue = 36; } if (chr == 36) { chrValue = 37; } if (chr == 37) { chrValue = 38; } if (chr == 42) { chrValue = 39; } if (chr == 43) { chrValue = 40; } if (chr == 45) { chrValue = 41; } if (chr == 46) { chrValue = 42; } if (chr == 47) { chrValue = 43; } if (chr == 58) { chrValue = 44; } } } if ((i % 2) == 0) { dataValue[dataCounter] = chrValue; dataBits[dataCounter] = 6; } else { dataValue[dataCounter] = dataValue[dataCounter] * 45 + chrValue; dataBits[dataCounter] = 11; if (i < dataLength - 1) { dataCounter++; } } } dataCounter++; break; /* ---- numeric mode ---- */ case ENCODE_MODE.NUMERIC: codewordNumPlus = new int[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4 }; dataValue[dataCounter] = 1; dataCounter++; dataValue[dataCounter] = dataLength; dataBits[dataCounter] = 10; /* #version 1-9*/ codewordNumCounterValue = dataCounter; dataCounter++; for (int i = 0; i < dataLength; i++) { if ((i % 3) == 0) { dataValue[dataCounter] = (int)(qrcodeData[i] - 0x30); dataBits[dataCounter] = 4; } else { dataValue[dataCounter] = dataValue[dataCounter] * 10 + (int)(qrcodeData[i] - 0x30); if ((i % 3) == 1) { dataBits[dataCounter] = 7; } else { dataBits[dataCounter] = 10; if (i < dataLength - 1) { dataCounter++; } } } } dataCounter++; break; /* ---- 8bit byte ---- */ default: codewordNumPlus = new int[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8 }; dataValue[dataCounter] = 4; dataCounter++; dataValue[dataCounter] = dataLength; dataBits[dataCounter] = 8; /* #version 1-9 */ codewordNumCounterValue = dataCounter; dataCounter++; for (int i = 0; i < dataLength; i++) { dataValue[i + dataCounter] = (qrcodeData[i] & 0xFF); dataBits[i + dataCounter] = 8; } dataCounter += dataLength; break; } int totalDataBits = 0; for (int i = 0; i < dataCounter; i++) { totalDataBits += dataBits[i]; } int ec; switch (qrcodeErrorCorrect) { case ERROR_CORRECTION.L: ec = 1; break; case ERROR_CORRECTION.Q: ec = 3; break; case ERROR_CORRECTION.H: ec = 2; break; default: ec = 0; break; } int[][] maxDataBitsArray = new int[][] { new int[] { 0, 128, 224, 352, 512, 688, 864, 992, 1232, 1456, 1728, 2032, 2320, 2672, 2920, 3320, 3624, 4056, 4504, 5016, 5352, 5712, 6256, 6880, 7312, 8000, 8496, 9024, 9544, 10136, 10984, 11640, 12328, 13048, 13800, 14496, 15312, 15936, 16816, 17728, 18672 }, new int[] { 0, 152, 272, 440, 640, 864, 1088, 1248, 1552, 1856, 2192, 2592, 2960, 3424, 3688, 4184, 4712, 5176, 5768, 6360, 6888, 7456, 8048, 8752, 9392, 10208, 10960, 11744, 12248, 13048, 13880, 14744, 15640, 16568, 17528, 18448, 19472, 20528, 21616, 22496, 23648 }, new int[] { 0, 72, 128, 208, 288, 368, 480, 528, 688, 800, 976, 1120, 1264, 1440, 1576, 1784, 2024, 2264, 2504, 2728, 3080, 3248, 3536, 3712, 4112, 4304, 4768, 5024, 5288, 5608, 5960, 6344, 6760, 7208, 7688, 7888, 8432, 8768, 9136, 9776, 10208 }, new int[] { 0, 104, 176, 272, 384, 496, 608, 704, 880, 1056, 1232, 1440, 1648, 1952, 2088, 2360, 2600, 2936, 3176, 3560, 3880, 4096, 4544, 4912, 5312, 5744, 6032, 6464, 6968, 7288, 7880, 8264, 8920, 9368, 9848, 10288, 10832, 11408, 12016, 12656, 13328 } }; int maxDataBits = 0; if (qrcodeVersion == 0) { /* auto version select */ qrcodeVersion = 1; for (int i = 1; i <= 40; i++) { if ((maxDataBitsArray[ec][i]) >= totalDataBits + codewordNumPlus[qrcodeVersion]) { maxDataBits = maxDataBitsArray[ec][i]; break; } qrcodeVersion++; } } else { maxDataBits = maxDataBitsArray[ec][qrcodeVersion]; } totalDataBits += codewordNumPlus[qrcodeVersion]; dataBits[codewordNumCounterValue] = (sbyte)(dataBits[codewordNumCounterValue] + codewordNumPlus[qrcodeVersion]); int[] maxCodewordsArray = new int[] { 0, 26, 44, 70, 100, 134, 172, 196, 242, 292, 346, 404, 466, 532, 581, 655, 733, 815, 901, 991, 1085, 1156, 1258, 1364, 1474, 1588, 1706, 1828, 1921, 2051, 2185, 2323, 2465, 2611, 2761, 2876, 3034, 3196, 3362, 3532, 3706 }; int maxCodewords = maxCodewordsArray[qrcodeVersion]; int maxModules1side = 17 + (qrcodeVersion << 2); int[] matrixRemainBit = new int[] { 0, 0, 7, 7, 7, 7, 7, 0, 0, 0, 0, 0, 0, 0, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 3, 3, 3, 3, 3, 3, 3, 0, 0, 0, 0, 0, 0 }; /* ---- read version ECC data file */ int byte_num = matrixRemainBit[qrcodeVersion] + (maxCodewords << 3); sbyte[] matrixX = new sbyte[byte_num]; sbyte[] matrixY = new sbyte[byte_num]; sbyte[] maskArray = new sbyte[byte_num]; sbyte[] formatInformationX2 = new sbyte[15]; sbyte[] formatInformationY2 = new sbyte[15]; sbyte[] rsEccCodewords = new sbyte[1]; sbyte[] rsBlockOrderTemp = new sbyte[128]; try { //String filename = QRCODE_DATA_PATH + @"\qrv" + System.Convert.ToString(qrcodeVersion) + "_" + System.Convert.ToString(ec) + ".dat"; //StreamReader reader = new StreamReader(filename); //BufferedStream bis = new BufferedStream(reader.BaseStream); String fileName = DATA_PATH + "." + "qrv" + Convert.ToString(qrcodeVersion) + "_" + Convert.ToString(ec) + ".dat"; //MemoryStream memoryStream = new MemoryStream(Resources.GetResource(fileName)); Stream stream = GetEmbeddedFile(fileName); BufferedStream bis = new BufferedStream(stream); SystemUtils.ReadInput(bis, matrixX, 0, matrixX.Length); SystemUtils.ReadInput(bis, matrixY, 0, matrixY.Length); SystemUtils.ReadInput(bis, maskArray, 0, maskArray.Length); SystemUtils.ReadInput(bis, formatInformationX2, 0, formatInformationX2.Length); SystemUtils.ReadInput(bis, formatInformationY2, 0, formatInformationY2.Length); SystemUtils.ReadInput(bis, rsEccCodewords, 0, rsEccCodewords.Length); SystemUtils.ReadInput(bis, rsBlockOrderTemp, 0, rsBlockOrderTemp.Length); bis.Close(); stream.Close(); // reader.Close(); /* * fis.Close(); */ } catch (Exception e) { SystemUtils.WriteStackTrace(e, Console.Error); } sbyte rsBlockOrderLength = 1; for (sbyte i = 1; i < 128; i++) { if (rsBlockOrderTemp[i] == 0) { rsBlockOrderLength = i; break; } } sbyte[] rsBlockOrder = new sbyte[rsBlockOrderLength]; Array.Copy(rsBlockOrderTemp, 0, rsBlockOrder, 0, (byte)rsBlockOrderLength); sbyte[] formatInformationX1 = new sbyte[] { 0, 1, 2, 3, 4, 5, 7, 8, 8, 8, 8, 8, 8, 8, 8 }; sbyte[] formatInformationY1 = new sbyte[] { 8, 8, 8, 8, 8, 8, 8, 8, 7, 5, 4, 3, 2, 1, 0 }; int maxDataCodewords = maxDataBits >> 3; /* -- read frame data -- */ int modules1Side = 4 * qrcodeVersion + 17; int matrixTotalBits = modules1Side * modules1Side; sbyte[] frameData = new sbyte[matrixTotalBits + modules1Side]; try { //String filename = QRCODE_DATA_PATH + "/qrvfr" + System.Convert.ToString(qrcodeVersion) + ".dat"; //StreamReader reader = new StreamReader(filename); String fileName = DATA_PATH + "." + "qrvfr" + Convert.ToString(qrcodeVersion) + ".dat"; //MemoryStream memoryStream = new MemoryStream(Resources.GetResource(fileName)); Stream stream = GetEmbeddedFile(fileName); BufferedStream bis = new BufferedStream(stream); SystemUtils.ReadInput(bis, frameData, 0, frameData.Length); bis.Close(); stream.Close(); //reader.Close(); //fis.Close(); } catch (Exception e) { SystemUtils.WriteStackTrace(e, Console.Error); } /* --- set terminator */ if (totalDataBits <= maxDataBits - 4) { dataValue[dataCounter] = 0; dataBits[dataCounter] = 4; } else { if (totalDataBits < maxDataBits) { dataValue[dataCounter] = 0; dataBits[dataCounter] = (sbyte)(maxDataBits - totalDataBits); } else { if (totalDataBits > maxDataBits) { System.Console.Out.WriteLine("overflow"); } } } sbyte[] dataCodewords = divideDataBy8Bits(dataValue, dataBits, maxDataCodewords); sbyte[] codewords = calculateRSECC(dataCodewords, rsEccCodewords[0], rsBlockOrder, maxDataCodewords, maxCodewords); /* ---- flash matrix */ sbyte[][] matrixContent = new sbyte[modules1Side][]; for (int i2 = 0; i2 < modules1Side; i2++) { matrixContent[i2] = new sbyte[modules1Side]; } for (int i = 0; i < modules1Side; i++) { for (int j = 0; j < modules1Side; j++) { matrixContent[j][i] = 0; } } /* --- attach data */ for (int i = 0; i < maxCodewords; i++) { sbyte codeword_i = codewords[i]; for (int j = 7; j >= 0; j--) { int codewordBitsNumber = (i * 8) + j; matrixContent[matrixX[codewordBitsNumber] & 0xFF][matrixY[codewordBitsNumber] & 0xFF] = (sbyte)((255 * (codeword_i & 1)) ^ maskArray[codewordBitsNumber]); codeword_i = (sbyte)(SystemUtils.URShift((codeword_i & 0xFF), 1)); } } for (int matrixRemain = matrixRemainBit[qrcodeVersion]; matrixRemain > 0; matrixRemain--) { int remainBitTemp = matrixRemain + (maxCodewords * 8) - 1; matrixContent[matrixX[remainBitTemp] & 0xFF][matrixY[remainBitTemp] & 0xFF] = (sbyte)(255 ^ maskArray[remainBitTemp]); } /* --- mask select --- */ sbyte maskNumber = selectMask(matrixContent, matrixRemainBit[qrcodeVersion] + maxCodewords * 8); sbyte maskContent = (sbyte)(1 << maskNumber); /* --- format information --- */ sbyte formatInformationValue = (sbyte)(ec << 3 | maskNumber); String[] formatInformationArray = new String[] { "101010000010010", "101000100100101", "101111001111100", "101101101001011", "100010111111001", "100000011001110", "100111110010111", "100101010100000", "111011111000100", "111001011110011", "111110110101010", "111100010011101", "110011000101111", "110001100011000", "110110001000001", "110100101110110", "001011010001001", "001001110111110", "001110011100111", "001100111010000", "000011101100010", "000001001010101", "000110100001100", "000100000111011", "011010101011111", "011000001101000", "011111100110001", "011101000000110", "010010010110100", "010000110000011", "010111011011010", "010101111101101" }; for (int i = 0; i < 15; i++) { sbyte content = (sbyte)System.SByte.Parse(formatInformationArray[formatInformationValue].Substring(i, (i + 1) - (i))); matrixContent[formatInformationX1[i] & 0xFF][formatInformationY1[i] & 0xFF] = (sbyte)(content * 255); matrixContent[formatInformationX2[i] & 0xFF][formatInformationY2[i] & 0xFF] = (sbyte)(content * 255); } bool[][] out_Renamed = new bool[modules1Side][]; for (int i3 = 0; i3 < modules1Side; i3++) { out_Renamed[i3] = new bool[modules1Side]; } int c = 0; for (int i = 0; i < modules1Side; i++) { for (int j = 0; j < modules1Side; j++) { if ((matrixContent[j][i] & maskContent) != 0 || frameData[c] == (char)49) { out_Renamed[j][i] = true; } else { out_Renamed[j][i] = false; } c++; } c++; } return(out_Renamed); }