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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /*
  2. * Copyright 2012 ZXing authors
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #import "ZXBitMatrix.h"
  17. #import "ZXBoolArray.h"
  18. #import "ZXByteArray.h"
  19. #import "ZXDataMatrixBitMatrixParser.h"
  20. #import "ZXDataMatrixDataBlock.h"
  21. #import "ZXDataMatrixDecodedBitStreamParser.h"
  22. #import "ZXDataMatrixDecoder.h"
  23. #import "ZXDataMatrixVersion.h"
  24. #import "ZXDecoderResult.h"
  25. #import "ZXErrors.h"
  26. #import "ZXGenericGF.h"
  27. #import "ZXIntArray.h"
  28. #import "ZXReedSolomonDecoder.h"
  29. @interface ZXDataMatrixDecoder ()
  30. @property (nonatomic, strong, readonly) ZXReedSolomonDecoder *rsDecoder;
  31. @end
  32. @implementation ZXDataMatrixDecoder
  33. - (id)init {
  34. if (self = [super init]) {
  35. _rsDecoder = [[ZXReedSolomonDecoder alloc] initWithField:[ZXGenericGF DataMatrixField256]];
  36. }
  37. return self;
  38. }
  39. - (ZXDecoderResult *)decode:(NSArray *)image error:(NSError **)error {
  40. int dimension = (int)[image count];
  41. ZXBitMatrix *bits = [[ZXBitMatrix alloc] initWithDimension:dimension];
  42. for (int i = 0; i < dimension; i++) {
  43. ZXBoolArray *b = image[i];
  44. for (int j = 0; j < dimension; j++) {
  45. if (b.array[j]) {
  46. [bits setX:j y:i];
  47. }
  48. }
  49. }
  50. return [self decodeMatrix:bits error:error];
  51. }
  52. - (ZXDecoderResult *)decodeMatrix:(ZXBitMatrix *)bits error:(NSError **)error {
  53. ZXDataMatrixBitMatrixParser *parser = [[ZXDataMatrixBitMatrixParser alloc] initWithBitMatrix:bits error:error];
  54. if (!parser) {
  55. return nil;
  56. }
  57. ZXDataMatrixVersion *version = [parser version];
  58. ZXByteArray *codewords = [parser readCodewords];
  59. NSArray *dataBlocks = [ZXDataMatrixDataBlock dataBlocks:codewords version:version];
  60. NSUInteger dataBlocksCount = [dataBlocks count];
  61. int totalBytes = 0;
  62. for (int i = 0; i < dataBlocksCount; i++) {
  63. totalBytes += [dataBlocks[i] numDataCodewords];
  64. }
  65. if (totalBytes == 0) {
  66. return nil;
  67. }
  68. ZXByteArray *resultBytes = [[ZXByteArray alloc] initWithLength:totalBytes];
  69. for (int j = 0; j < dataBlocksCount; j++) {
  70. ZXDataMatrixDataBlock *dataBlock = dataBlocks[j];
  71. ZXByteArray *codewordBytes = dataBlock.codewords;
  72. int numDataCodewords = [dataBlock numDataCodewords];
  73. if (![self correctErrors:codewordBytes numDataCodewords:numDataCodewords error:error]) {
  74. return nil;
  75. }
  76. for (int i = 0; i < numDataCodewords; i++) {
  77. // De-interlace data blocks.
  78. resultBytes.array[i * dataBlocksCount + j] = codewordBytes.array[i];
  79. }
  80. }
  81. return [ZXDataMatrixDecodedBitStreamParser decode:resultBytes error:error];
  82. }
  83. /**
  84. * Given data and error-correction codewords received, possibly corrupted by errors, attempts to
  85. * correct the errors in-place using Reed-Solomon error correction.
  86. *
  87. * @param codewordBytes data and error correction codewords
  88. * @param numDataCodewords number of codewords that are data bytes
  89. * @return NO if error correction fails
  90. */
  91. - (BOOL)correctErrors:(ZXByteArray *)codewordBytes numDataCodewords:(int)numDataCodewords error:(NSError **)error {
  92. int numCodewords = codewordBytes.length;
  93. // First read into an array of ints
  94. ZXIntArray *codewordsInts = [[ZXIntArray alloc] initWithLength:numCodewords];
  95. for (int i = 0; i < numCodewords; i++) {
  96. codewordsInts.array[i] = codewordBytes.array[i] & 0xFF;
  97. }
  98. int numECCodewords = codewordBytes.length - numDataCodewords;
  99. NSError *decodeError = nil;
  100. if (![self.rsDecoder decode:codewordsInts twoS:numECCodewords error:&decodeError]) {
  101. if (decodeError.code == ZXReedSolomonError) {
  102. if (error) *error = ZXChecksumErrorInstance();
  103. return NO;
  104. } else {
  105. if (error) *error = decodeError;
  106. return NO;
  107. }
  108. }
  109. for (int i = 0; i < numDataCodewords; i++) {
  110. codewordBytes.array[i] = (int8_t) codewordsInts.array[i];
  111. }
  112. return YES;
  113. }
  114. @end