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.

ZXBinarizer.m 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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 "ZXBinarizer.h"
  17. #if TARGET_OS_EMBEDDED || TARGET_IPHONE_SIMULATOR
  18. #import <UIKit/UIKit.h>
  19. #define ZXBlack [[UIColor blackColor] CGColor]
  20. #define ZXWhite [[UIColor whiteColor] CGColor]
  21. #else
  22. #define ZXBlack CGColorGetConstantColor(kCGColorBlack)
  23. #define ZXWhite CGColorGetConstantColor(kCGColorWhite)
  24. #endif
  25. @implementation ZXBinarizer
  26. - (id)initWithSource:(ZXLuminanceSource *)source {
  27. if (self = [super init]) {
  28. _luminanceSource = source;
  29. }
  30. return self;
  31. }
  32. - (id)initWithLuminanceSource:(ZXLuminanceSource *)source {
  33. return [self initWithSource:source];
  34. }
  35. + (id)binarizerWithSource:(ZXLuminanceSource *)source {
  36. return [[self alloc] initWithLuminanceSource:source];
  37. }
  38. - (ZXBitArray *)blackRow:(int)y row:(ZXBitArray *)row error:(NSError **)error {
  39. @throw [NSException exceptionWithName:NSInternalInconsistencyException
  40. reason:[NSString stringWithFormat:@"You must override %@ in a subclass", NSStringFromSelector(_cmd)]
  41. userInfo:nil];
  42. }
  43. - (ZXBitMatrix *)blackMatrixWithError:(NSError **)error {
  44. @throw [NSException exceptionWithName:NSInternalInconsistencyException
  45. reason:[NSString stringWithFormat:@"You must override %@ in a subclass", NSStringFromSelector(_cmd)]
  46. userInfo:nil];
  47. }
  48. - (ZXBinarizer *)createBinarizer:(ZXLuminanceSource *)source {
  49. @throw [NSException exceptionWithName:NSInternalInconsistencyException
  50. reason:[NSString stringWithFormat:@"You must override %@ in a subclass", NSStringFromSelector(_cmd)]
  51. userInfo:nil];
  52. }
  53. - (CGImageRef)createImage CF_RETURNS_RETAINED {
  54. ZXBitMatrix *matrix = [self blackMatrixWithError:nil];
  55. if (!matrix) {
  56. return nil;
  57. }
  58. ZXLuminanceSource *source = [self luminanceSource];
  59. int width = source.width;
  60. int height = source.height;
  61. int bytesPerRow = ((width&0xf)>>4)<<4;
  62. CGColorSpaceRef gray = CGColorSpaceCreateDeviceGray();
  63. CGContextRef context = CGBitmapContextCreate (
  64. 0,
  65. width,
  66. height,
  67. 8, // bits per component
  68. bytesPerRow,
  69. gray,
  70. kCGBitmapAlphaInfoMask & kCGImageAlphaNone);
  71. CGColorSpaceRelease(gray);
  72. CGRect r = CGRectZero;
  73. r.size.width = width;
  74. r.size.height = height;
  75. CGContextSetFillColorWithColor(context, ZXBlack);
  76. CGContextFillRect(context, r);
  77. r.size.width = 1;
  78. r.size.height = 1;
  79. CGContextSetFillColorWithColor(context, ZXWhite);
  80. for (int y=0; y<height; y++) {
  81. r.origin.y = height-1-y;
  82. for (int x=0; x<width; x++) {
  83. if (![matrix getX:x y:y]) {
  84. r.origin.x = x;
  85. CGContextFillRect(context, r);
  86. }
  87. }
  88. }
  89. CGImageRef binary = CGBitmapContextCreateImage(context);
  90. CGContextRelease(context);
  91. return binary;
  92. }
  93. - (int)width {
  94. return self.luminanceSource.width;
  95. }
  96. - (int)height {
  97. return self.luminanceSource.height;
  98. }
  99. @end