您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

ZXITFWriter.m 2.5KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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 "ZXITFReader.h"
  18. #import "ZXITFWriter.h"
  19. const int ZX_ITF_WRITER_START_PATTERN[] = {1, 1, 1, 1};
  20. const int ZX_ITF_WRITER_END_PATTERN[] = {3, 1, 1};
  21. @implementation ZXITFWriter
  22. - (ZXBitMatrix *)encode:(NSString *)contents format:(ZXBarcodeFormat)format width:(int)width height:(int)height hints:(ZXEncodeHints *)hints error:(NSError **)error {
  23. if (format != kBarcodeFormatITF) {
  24. [NSException raise:NSInvalidArgumentException format:@"Can only encode ITF"];
  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 % 2 != 0) {
  31. [NSException raise:NSInvalidArgumentException format:@"The length of the input should be even"];
  32. }
  33. if (length > 80) {
  34. [NSException raise:NSInvalidArgumentException format:@"Requested contents should be less than 80 digits long, but got %d", length];
  35. }
  36. ZXBoolArray *result = [[ZXBoolArray alloc] initWithLength:9 + 9 * length];
  37. int pos = [self appendPattern:result pos:0 pattern:ZX_ITF_WRITER_START_PATTERN patternLen:sizeof(ZX_ITF_WRITER_START_PATTERN)/sizeof(int) startColor:YES];
  38. for (int i = 0; i < length; i += 2) {
  39. int one = [[contents substringWithRange:NSMakeRange(i, 1)] intValue];
  40. int two = [[contents substringWithRange:NSMakeRange(i + 1, 1)] intValue];
  41. const int encodingLen = 18;
  42. int encoding[encodingLen];
  43. memset(encoding, 0, encodingLen * sizeof(int));
  44. for (int j = 0; j < 5; j++) {
  45. encoding[2 * j] = ZX_ITF_PATTERNS[one][j];
  46. encoding[2 * j + 1] = ZX_ITF_PATTERNS[two][j];
  47. }
  48. pos += [super appendPattern:result pos:pos pattern:encoding patternLen:encodingLen startColor:YES];
  49. }
  50. [self appendPattern:result pos:pos pattern:ZX_ITF_WRITER_END_PATTERN patternLen:sizeof(ZX_ITF_WRITER_END_PATTERN)/sizeof(int) startColor:YES];
  51. return result;
  52. }
  53. @end