You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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 "ZXEncodeHints.h"
  19. #import "ZXOneDimensionalCodeWriter.h"
  20. @implementation ZXOneDimensionalCodeWriter
  21. - (ZXBitMatrix *)encode:(NSString *)contents format:(ZXBarcodeFormat)format width:(int)width height:(int)height error:(NSError **)error {
  22. return [self encode:contents format:format width:width height:height hints:nil error:error];
  23. }
  24. /**
  25. * Encode the contents following specified format.
  26. * width and height are required size. This method may return bigger size
  27. * ZXBitMatrix when specified size is too small. The user can set both {width and
  28. * height to zero to get minimum size barcode. If negative value is set to width
  29. * or height, IllegalArgumentException is thrown.
  30. */
  31. - (ZXBitMatrix *)encode:(NSString *)contents format:(ZXBarcodeFormat)format width:(int)width height:(int)height
  32. hints:(ZXEncodeHints *)hints error:(NSError **)error {
  33. if (contents.length == 0) {
  34. @throw [NSException exceptionWithName:NSInvalidArgumentException reason:@"Found empty contents" userInfo:nil];
  35. }
  36. if (width < 0 || height < 0) {
  37. @throw [NSException exceptionWithName:NSInvalidArgumentException
  38. reason:[NSString stringWithFormat:@"Negative size is not allowed. Input: %dx%d", width, height]
  39. userInfo:nil];
  40. }
  41. int sidesMargin = [self defaultMargin];
  42. if (hints && hints.margin) {
  43. sidesMargin = hints.margin.intValue;
  44. }
  45. ZXBoolArray *code = [self encode:contents];
  46. return [self renderResult:code width:width height:height sidesMargin:sidesMargin];
  47. }
  48. /**
  49. * @return a byte array of horizontal pixels (0 = white, 1 = black)
  50. */
  51. - (ZXBitMatrix *)renderResult:(ZXBoolArray *)code width:(int)width height:(int)height sidesMargin:(int)sidesMargin {
  52. int inputWidth = code.length;
  53. // Add quiet zone on both sides.
  54. int fullWidth = inputWidth + sidesMargin;
  55. int outputWidth = MAX(width, fullWidth);
  56. int outputHeight = MAX(1, height);
  57. int multiple = outputWidth / fullWidth;
  58. int leftPadding = (outputWidth - (inputWidth * multiple)) / 2;
  59. ZXBitMatrix *output = [[ZXBitMatrix alloc] initWithWidth:outputWidth height:outputHeight];
  60. for (int inputX = 0, outputX = leftPadding; inputX < inputWidth; inputX++, outputX += multiple) {
  61. if (code.array[inputX]) {
  62. [output setRegionAtLeft:outputX top:0 width:multiple height:outputHeight];
  63. }
  64. }
  65. return output;
  66. }
  67. /**
  68. * Appends the given pattern to the target array starting at pos.
  69. *
  70. * @param startColor starting color - false for white, true for black
  71. * @return the number of elements added to target.
  72. */
  73. - (int)appendPattern:(ZXBoolArray *)target pos:(int)pos pattern:(const int[])pattern patternLen:(int)patternLen startColor:(BOOL)startColor {
  74. BOOL color = startColor;
  75. int numAdded = 0;
  76. for (int i = 0; i < patternLen; i++) {
  77. for (int j = 0; j < pattern[i]; j++) {
  78. target.array[pos++] = color;
  79. }
  80. numAdded += pattern[i];
  81. color = !color; // flip color after each segment
  82. }
  83. return numAdded;
  84. }
  85. - (int)defaultMargin {
  86. // CodaBar spec requires a side margin to be more than ten times wider than narrow space.
  87. // This seems like a decent idea for a default for all formats.
  88. return 10;
  89. }
  90. /**
  91. * Encode the contents to boolean array expression of one-dimensional barcode.
  92. * Start code and end code should be included in result, and side margins should not be included.
  93. *
  94. * @return a ZXBoolArray of horizontal pixels (false = white, true = black)
  95. */
  96. - (ZXBoolArray *)encode:(NSString *)contents {
  97. @throw [NSException exceptionWithName:NSInternalInconsistencyException
  98. reason:[NSString stringWithFormat:@"You must override %@ in a subclass", NSStringFromSelector(_cmd)]
  99. userInfo:nil];
  100. }
  101. @end