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.

ZXBinaryBitmap.m 2.7KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. #import "ZXBinaryBitmap.h"
  18. #import "ZXBitArray.h"
  19. #import "ZXBitMatrix.h"
  20. @interface ZXBinaryBitmap ()
  21. @property (nonatomic, strong, readonly) ZXBinarizer *binarizer;
  22. @property (nonatomic, strong) ZXBitMatrix *matrix;
  23. @end
  24. @implementation ZXBinaryBitmap
  25. - (id)initWithBinarizer:(ZXBinarizer *)binarizer {
  26. if (self = [super init]) {
  27. if (binarizer == nil) {
  28. [NSException raise:NSInvalidArgumentException format:@"Binarizer must be non-null."];
  29. }
  30. _binarizer = binarizer;
  31. }
  32. return self;
  33. }
  34. + (id)binaryBitmapWithBinarizer:(ZXBinarizer *)binarizer {
  35. return [[self alloc] initWithBinarizer:binarizer];
  36. }
  37. - (int)width {
  38. return self.binarizer.width;
  39. }
  40. - (int)height {
  41. return self.binarizer.height;
  42. }
  43. - (ZXBitArray *)blackRow:(int)y row:(ZXBitArray *)row error:(NSError **)error {
  44. return [self.binarizer blackRow:y row:row error:error];
  45. }
  46. - (ZXBitMatrix *)blackMatrixWithError:(NSError **)error {
  47. if (self.matrix == nil) {
  48. self.matrix = [self.binarizer blackMatrixWithError:error];
  49. }
  50. return self.matrix;
  51. }
  52. - (BOOL)cropSupported {
  53. return [self.binarizer luminanceSource].cropSupported;
  54. }
  55. - (ZXBinaryBitmap *)crop:(int)left top:(int)top width:(int)aWidth height:(int)aHeight {
  56. ZXLuminanceSource *newSource = [[self.binarizer luminanceSource] crop:left top:top width:aWidth height:aHeight];
  57. return [[ZXBinaryBitmap alloc] initWithBinarizer:[self.binarizer createBinarizer:newSource]];
  58. }
  59. - (BOOL)rotateSupported {
  60. return [self.binarizer luminanceSource].rotateSupported;
  61. }
  62. - (ZXBinaryBitmap *)rotateCounterClockwise {
  63. ZXLuminanceSource *newSource = [[self.binarizer luminanceSource] rotateCounterClockwise];
  64. return [[ZXBinaryBitmap alloc] initWithBinarizer:[self.binarizer createBinarizer:newSource]];
  65. }
  66. - (ZXBinaryBitmap *)rotateCounterClockwise45 {
  67. ZXLuminanceSource *newSource = [[self.binarizer luminanceSource] rotateCounterClockwise45];
  68. return [[ZXBinaryBitmap alloc] initWithBinarizer:[self.binarizer createBinarizer:newSource]];
  69. }
  70. - (NSString *)description {
  71. ZXBitMatrix *matrix = [self blackMatrixWithError:nil];
  72. if (matrix) {
  73. return [matrix description];
  74. } else {
  75. return @"";
  76. }
  77. }
  78. @end