not really known
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

95 lines
2.7 KiB

  1. /*
  2. Ported to JavaScript by Lazar Laszlo 2011
  3. lazarsoft@gmail.com, www.lazarsoft.info
  4. */
  5. /*
  6. *
  7. * Copyright 2007 ZXing authors
  8. *
  9. * Licensed under the Apache License, Version 2.0 (the "License");
  10. * you may not use this file except in compliance with the License.
  11. * You may obtain a copy of the License at
  12. *
  13. * http://www.apache.org/licenses/LICENSE-2.0
  14. *
  15. * Unless required by applicable law or agreed to in writing, software
  16. * distributed under the License is distributed on an "AS IS" BASIS,
  17. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  18. * See the License for the specific language governing permissions and
  19. * limitations under the License.
  20. */
  21. var Decoder={};
  22. Decoder.rsDecoder = new ReedSolomonDecoder(GF256.QR_CODE_FIELD);
  23. Decoder.correctErrors=function( codewordBytes, numDataCodewords)
  24. {
  25. var numCodewords = codewordBytes.length;
  26. // First read into an array of ints
  27. var codewordsInts = new Array(numCodewords);
  28. for (var i = 0; i < numCodewords; i++)
  29. {
  30. codewordsInts[i] = codewordBytes[i] & 0xFF;
  31. }
  32. var numECCodewords = codewordBytes.length - numDataCodewords;
  33. try
  34. {
  35. Decoder.rsDecoder.decode(codewordsInts, numECCodewords);
  36. //var corrector = new ReedSolomon(codewordsInts, numECCodewords);
  37. //corrector.correct();
  38. }
  39. catch ( rse)
  40. {
  41. throw rse;
  42. }
  43. // Copy back into array of bytes -- only need to worry about the bytes that were data
  44. // We don't care about errors in the error-correction codewords
  45. for (var i = 0; i < numDataCodewords; i++)
  46. {
  47. codewordBytes[i] = codewordsInts[i];
  48. }
  49. }
  50. Decoder.decode=function(bits)
  51. {
  52. var parser = new BitMatrixParser(bits);
  53. var version = parser.readVersion();
  54. var ecLevel = parser.readFormatInformation().ErrorCorrectionLevel;
  55. // Read codewords
  56. var codewords = parser.readCodewords();
  57. // Separate into data blocks
  58. var dataBlocks = DataBlock.getDataBlocks(codewords, version, ecLevel);
  59. // Count total number of data bytes
  60. var totalBytes = 0;
  61. for (var i = 0; i < dataBlocks.length; i++)
  62. {
  63. totalBytes += dataBlocks[i].NumDataCodewords;
  64. }
  65. var resultBytes = new Array(totalBytes);
  66. var resultOffset = 0;
  67. // Error-correct and copy data blocks together into a stream of bytes
  68. for (var j = 0; j < dataBlocks.length; j++)
  69. {
  70. var dataBlock = dataBlocks[j];
  71. var codewordBytes = dataBlock.Codewords;
  72. var numDataCodewords = dataBlock.NumDataCodewords;
  73. Decoder.correctErrors(codewordBytes, numDataCodewords);
  74. for (var i = 0; i < numDataCodewords; i++)
  75. {
  76. resultBytes[resultOffset++] = codewordBytes[i];
  77. }
  78. }
  79. // Decode the contents of that stream of bytes
  80. var reader = new QRCodeDataBlockReader(resultBytes, version.VersionNumber, ecLevel.Bits);
  81. return reader;
  82. //return DecodedBitStreamParser.decode(resultBytes, version, ecLevel);
  83. }