選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

ZXCodaBarWriter.m 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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 "ZXBoolArray.h"
  17. #import "ZXCodaBarReader.h"
  18. #import "ZXCodaBarWriter.h"
  19. const unichar ZX_CODA_START_END_CHARS[] = {'A', 'B', 'C', 'D'};
  20. const unichar ZX_CODA_ALT_START_END_CHARS[] = {'T', 'N', '*', 'E'};
  21. const unichar ZX_CHARS_WHICH_ARE_TEN_LENGTH_EACH_AFTER_DECODED[] = {'/', ':', '+', '.'};
  22. static unichar ZX_CODA_DEFAULT_GUARD;
  23. @implementation ZXCodaBarWriter
  24. + (void)initialize {
  25. if ([self class] != [ZXCodaBarWriter class]) return;
  26. ZX_CODA_DEFAULT_GUARD = ZX_CODA_START_END_CHARS[0];
  27. }
  28. - (ZXBoolArray *)encode:(NSString *)contents {
  29. if ([contents length] < 2) {
  30. // Can't have a start/end guard, so tentatively add default guards
  31. contents = [NSString stringWithFormat:@"%C%@%C", ZX_CODA_DEFAULT_GUARD, contents, ZX_CODA_DEFAULT_GUARD];
  32. } else {
  33. // Verify input and calculate decoded length.
  34. unichar firstChar = [[contents uppercaseString] characterAtIndex:0];
  35. unichar lastChar = [[contents uppercaseString] characterAtIndex:contents.length - 1];
  36. BOOL startsNormal = [ZXCodaBarReader arrayContains:ZX_CODA_START_END_CHARS length:sizeof(ZX_CODA_START_END_CHARS) / sizeof(unichar) key:firstChar];
  37. BOOL endsNormal = [ZXCodaBarReader arrayContains:ZX_CODA_START_END_CHARS length:sizeof(ZX_CODA_START_END_CHARS) / sizeof(unichar) key:lastChar];
  38. BOOL startsAlt = [ZXCodaBarReader arrayContains:ZX_CODA_ALT_START_END_CHARS length:sizeof(ZX_CODA_ALT_START_END_CHARS) / sizeof(unichar) key:firstChar];
  39. BOOL endsAlt = [ZXCodaBarReader arrayContains:ZX_CODA_ALT_START_END_CHARS length:sizeof(ZX_CODA_ALT_START_END_CHARS) / sizeof(unichar) key:lastChar];
  40. if (startsNormal) {
  41. if (!endsNormal) {
  42. @throw [NSException exceptionWithName:NSInvalidArgumentException
  43. reason:[NSString stringWithFormat:@"Invalid start/end guards: %@", contents]
  44. userInfo:nil];
  45. }
  46. // else already has valid start/end
  47. } else if (startsAlt) {
  48. if (!endsAlt) {
  49. @throw [NSException exceptionWithName:NSInvalidArgumentException
  50. reason:[NSString stringWithFormat:@"Invalid start/end guards: %@", contents]
  51. userInfo:nil];
  52. }
  53. // else already has valid start/end
  54. } else {
  55. // Doesn't start with a guard
  56. if (endsNormal || endsAlt) {
  57. @throw [NSException exceptionWithName:NSInvalidArgumentException
  58. reason:[NSString stringWithFormat:@"Invalid start/end guards: %@", contents]
  59. userInfo:nil];
  60. }
  61. // else doesn't end with guard either, so add a default
  62. contents = [NSString stringWithFormat:@"%C%@%C", ZX_CODA_DEFAULT_GUARD, contents, ZX_CODA_DEFAULT_GUARD];
  63. }
  64. }
  65. // The start character and the end character are decoded to 10 length each.
  66. int resultLength = 20;
  67. for (int i = 1; i < contents.length - 1; i++) {
  68. if (([contents characterAtIndex:i] >= '0' && [contents characterAtIndex:i] <= '9') ||
  69. [contents characterAtIndex:i] == '-' || [contents characterAtIndex:i] == '$') {
  70. resultLength += 9;
  71. } else if ([ZXCodaBarReader arrayContains:ZX_CHARS_WHICH_ARE_TEN_LENGTH_EACH_AFTER_DECODED length:4 key:[contents characterAtIndex:i]]) {
  72. resultLength += 10;
  73. } else {
  74. @throw [NSException exceptionWithName:NSInvalidArgumentException
  75. reason:[NSString stringWithFormat:@"Cannot encode : '%C'", [contents characterAtIndex:i]]
  76. userInfo:nil];
  77. }
  78. }
  79. // A blank is placed between each character.
  80. resultLength += contents.length - 1;
  81. ZXBoolArray *result = [[ZXBoolArray alloc] initWithLength:resultLength];
  82. int position = 0;
  83. for (int index = 0; index < contents.length; index++) {
  84. unichar c = [[contents uppercaseString] characterAtIndex:index];
  85. if (index == 0 || index == contents.length - 1) {
  86. // The start/end chars are not in the CodaBarReader.ALPHABET.
  87. switch (c) {
  88. case 'T':
  89. c = 'A';
  90. break;
  91. case 'N':
  92. c = 'B';
  93. break;
  94. case '*':
  95. c = 'C';
  96. break;
  97. case 'E':
  98. c = 'D';
  99. break;
  100. }
  101. }
  102. int code = 0;
  103. for (int i = 0; i < ZX_CODA_ALPHABET_LEN; i++) {
  104. // Found any, because I checked above.
  105. if (c == ZX_CODA_ALPHABET[i]) {
  106. code = ZX_CODA_CHARACTER_ENCODINGS[i];
  107. break;
  108. }
  109. }
  110. BOOL color = YES;
  111. int counter = 0;
  112. int bit = 0;
  113. while (bit < 7) { // A character consists of 7 digit.
  114. result.array[position] = color;
  115. position++;
  116. if (((code >> (6 - bit)) & 1) == 0 || counter == 1) {
  117. color = !color; // Flip the color.
  118. bit++;
  119. counter = 0;
  120. } else {
  121. counter++;
  122. }
  123. }
  124. if (index < contents.length - 1) {
  125. result.array[position] = NO;
  126. position++;
  127. }
  128. }
  129. return result;
  130. }
  131. @end