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

ZXCode39Writer.m 3.2KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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 "ZXCode39Reader.h"
  19. #import "ZXCode39Writer.h"
  20. #import "ZXIntArray.h"
  21. @implementation ZXCode39Writer
  22. - (ZXBitMatrix *)encode:(NSString *)contents format:(ZXBarcodeFormat)format width:(int)width height:(int)height hints:(ZXEncodeHints *)hints error:(NSError **)error {
  23. if (format != kBarcodeFormatCode39) {
  24. [NSException raise:NSInvalidArgumentException format:@"Can only encode CODE_39."];
  25. }
  26. return [super encode:contents format:format width:width height:height hints:hints error:error];
  27. }
  28. - (ZXBoolArray *)encode:(NSString *)contents {
  29. int length = (int)[contents length];
  30. if (length > 80) {
  31. [NSException raise:NSInvalidArgumentException
  32. format:@"Requested contents should be less than 80 digits long, but got %d", length];
  33. }
  34. ZXIntArray *widths = [[ZXIntArray alloc] initWithLength:9];
  35. int codeWidth = 24 + 1 + length;
  36. for (int i = 0; i < length; i++) {
  37. NSUInteger indexInString = [ZX_CODE39_ALPHABET_STRING rangeOfString:[contents substringWithRange:NSMakeRange(i, 1)]].location;
  38. if (indexInString == NSNotFound) {
  39. [NSException raise:NSInvalidArgumentException format:@"Bad contents: %@", contents];
  40. }
  41. [self toIntArray:ZX_CODE39_CHARACTER_ENCODINGS[indexInString] toReturn:widths];
  42. codeWidth += [widths sum];
  43. }
  44. ZXBoolArray *result = [[ZXBoolArray alloc] initWithLength:codeWidth];
  45. [self toIntArray:ZX_CODE39_CHARACTER_ENCODINGS[39] toReturn:widths];
  46. int pos = [self appendPattern:result pos:0 pattern:widths.array patternLen:widths.length startColor:YES];
  47. ZXIntArray *narrowWhite = [[ZXIntArray alloc] initWithInts:1, -1];
  48. pos += [self appendPattern:result pos:pos pattern:narrowWhite.array patternLen:narrowWhite.length startColor:NO];
  49. //append next character to byte matrix
  50. for (int i = 0; i < length; i++) {
  51. NSUInteger indexInString = [ZX_CODE39_ALPHABET_STRING rangeOfString:[contents substringWithRange:NSMakeRange(i, 1)]].location;
  52. [self toIntArray:ZX_CODE39_CHARACTER_ENCODINGS[indexInString] toReturn:widths];
  53. pos += [self appendPattern:result pos:pos pattern:widths.array patternLen:widths.length startColor:YES];
  54. pos += [self appendPattern:result pos:pos pattern:narrowWhite.array patternLen:narrowWhite.length startColor:NO];
  55. }
  56. [self toIntArray:ZX_CODE39_CHARACTER_ENCODINGS[39] toReturn:widths];
  57. [self appendPattern:result pos:pos pattern:widths.array patternLen:widths.length startColor:YES];
  58. return result;
  59. }
  60. - (void)toIntArray:(int)a toReturn:(ZXIntArray *)toReturn {
  61. for (int i = 0; i < 9; i++) {
  62. int temp = a & (1 << (8 - i));
  63. toReturn.array[i] = temp == 0 ? 1 : 2;
  64. }
  65. }
  66. @end