Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

ZXDataMatrixBase256Encoder.m 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. /*
  2. * Copyright 2013 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 "ZXDataMatrixBase256Encoder.h"
  17. #import "ZXDataMatrixEncoderContext.h"
  18. #import "ZXDataMatrixHighLevelEncoder.h"
  19. #import "ZXDataMatrixSymbolInfo.h"
  20. @implementation ZXDataMatrixBase256Encoder
  21. - (int)encodingMode {
  22. return [ZXDataMatrixHighLevelEncoder base256Encodation];
  23. }
  24. - (void)encode:(ZXDataMatrixEncoderContext *)context {
  25. NSMutableString *buffer = [NSMutableString string];
  26. [buffer appendString:@"\0"]; //Initialize length field
  27. while ([context hasMoreCharacters]) {
  28. unichar c = [context currentChar];
  29. [buffer appendFormat:@"%C", c];
  30. context.pos++;
  31. int newMode = [ZXDataMatrixHighLevelEncoder lookAheadTest:context.message startpos:context.pos currentMode:[self encodingMode]];
  32. if (newMode != [self encodingMode]) {
  33. [context signalEncoderChange:newMode];
  34. break;
  35. }
  36. }
  37. int dataCount = (int)buffer.length - 1;
  38. int lengthFieldSize = 1;
  39. int currentSize = [context codewordCount] + dataCount + lengthFieldSize;
  40. [context updateSymbolInfoWithLength:currentSize];
  41. BOOL mustPad = (context.symbolInfo.dataCapacity - currentSize) > 0;
  42. if ([context hasMoreCharacters] || mustPad) {
  43. if (dataCount <= 249) {
  44. [buffer replaceCharactersInRange:NSMakeRange(0, 1)
  45. withString:[NSString stringWithFormat:@"%C", (unichar) dataCount]];
  46. } else if (dataCount > 249 && dataCount <= 1555) {
  47. [buffer replaceCharactersInRange:NSMakeRange(0, 1)
  48. withString:[NSString stringWithFormat:@"%C", (unichar) ((dataCount / 250) + 249)]];
  49. [buffer insertString:[NSString stringWithFormat:@"%C", (unichar) (dataCount % 250)]
  50. atIndex:1];
  51. } else {
  52. @throw [NSException exceptionWithName:@"IllegalStateException"
  53. reason:[NSString stringWithFormat:@"Message length not in valid ranges: %d", dataCount]
  54. userInfo:nil];
  55. }
  56. }
  57. for (int i = 0, c = (int)buffer.length; i < c; i++) {
  58. [context writeCodeword:[self randomize255State:[buffer characterAtIndex:i] codewordPosition:context.codewordCount + 1]];
  59. }
  60. }
  61. - (unichar)randomize255State:(unichar)ch codewordPosition:(int)codewordPosition {
  62. int pseudoRandom = ((149 * codewordPosition) % 255) + 1;
  63. int tempVariable = ch + pseudoRandom;
  64. if (tempVariable <= 255) {
  65. return (unichar) tempVariable;
  66. } else {
  67. return (unichar) (tempVariable - 256);
  68. }
  69. }
  70. @end