Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

ZXDataMatrixDataBlock.m 3.7KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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 "ZXByteArray.h"
  17. #import "ZXDataMatrixDataBlock.h"
  18. #import "ZXDataMatrixVersion.h"
  19. @implementation ZXDataMatrixDataBlock
  20. - (id)initWithNumDataCodewords:(int)numDataCodewords codewords:(ZXByteArray *)codewords {
  21. if (self = [super init]) {
  22. _numDataCodewords = numDataCodewords;
  23. _codewords = codewords;
  24. }
  25. return self;
  26. }
  27. + (NSArray *)dataBlocks:(ZXByteArray *)rawCodewords version:(ZXDataMatrixVersion *)version {
  28. // Figure out the number and size of data blocks used by this version
  29. ZXDataMatrixECBlocks *ecBlocks = version.ecBlocks;
  30. // First count the total number of data blocks
  31. int totalBlocks = 0;
  32. NSArray *ecBlockArray = ecBlocks.ecBlocks;
  33. for (ZXDataMatrixECB *ecBlock in ecBlockArray) {
  34. totalBlocks += ecBlock.count;
  35. }
  36. // Now establish DataBlocks of the appropriate size and number of data codewords
  37. NSMutableArray *result = [NSMutableArray arrayWithCapacity:totalBlocks];
  38. for (ZXDataMatrixECB *ecBlock in ecBlockArray) {
  39. for (int i = 0; i < ecBlock.count; i++) {
  40. int numDataCodewords = ecBlock.dataCodewords;
  41. int numBlockCodewords = ecBlocks.ecCodewords + numDataCodewords;
  42. [result addObject:[[ZXDataMatrixDataBlock alloc] initWithNumDataCodewords:numDataCodewords codewords:[[ZXByteArray alloc] initWithLength:numBlockCodewords]]];
  43. }
  44. }
  45. // All blocks have the same amount of data, except that the last n
  46. // (where n may be 0) have 1 less byte. Figure out where these start.
  47. // TODO(bbrown): There is only one case where there is a difference for Data Matrix for size 144
  48. int longerBlocksTotalCodewords = [[(ZXDataMatrixDataBlock *)result[0] codewords] length];
  49. //int shorterBlocksTotalCodewords = longerBlocksTotalCodewords - 1;
  50. int longerBlocksNumDataCodewords = longerBlocksTotalCodewords - ecBlocks.ecCodewords;
  51. int shorterBlocksNumDataCodewords = longerBlocksNumDataCodewords - 1;
  52. // The last elements of result may be 1 element shorter for 144 matrix
  53. // first fill out as many elements as all of them have minus 1
  54. int rawCodewordsOffset = 0;
  55. for (int i = 0; i < shorterBlocksNumDataCodewords; i++) {
  56. for (ZXDataMatrixDataBlock *block in result) {
  57. block.codewords.array[i] = rawCodewords.array[rawCodewordsOffset++];
  58. }
  59. }
  60. // Fill out the last data block in the longer ones
  61. BOOL specialVersion = version.versionNumber == 24;
  62. int numLongerBlocks = specialVersion ? 8 : (int)[result count];
  63. for (int j = 0; j < numLongerBlocks; j++) {
  64. [(ZXDataMatrixDataBlock *)result[j] codewords].array[longerBlocksNumDataCodewords - 1] = rawCodewords.array[rawCodewordsOffset++];
  65. }
  66. NSUInteger max = [(ZXDataMatrixDataBlock *)result[0] codewords].length;
  67. for (int i = longerBlocksNumDataCodewords; i < max; i++) {
  68. for (int j = 0; j < [result count]; j++) {
  69. int jOffset = specialVersion ? (j + 8) % [result count] : j;
  70. int iOffset = specialVersion && jOffset > 7 ? i - 1 : i;
  71. [(ZXDataMatrixDataBlock *)result[jOffset] codewords].array[iOffset] = rawCodewords.array[rawCodewordsOffset++];
  72. }
  73. }
  74. if (rawCodewordsOffset != rawCodewords.length) {
  75. [NSException raise:NSInvalidArgumentException format:@"Codewords size mismatch"];
  76. }
  77. return result;
  78. }
  79. @end