Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

ZXGridSampler.m 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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 "ZXDefaultGridSampler.h"
  18. #import "ZXErrors.h"
  19. #import "ZXGridSampler.h"
  20. #import "ZXPerspectiveTransform.h"
  21. static ZXGridSampler *gridSampler = nil;
  22. @implementation ZXGridSampler
  23. + (void)setGridSampler:(ZXGridSampler *)newGridSampler {
  24. gridSampler = newGridSampler;
  25. }
  26. + (ZXGridSampler *)instance {
  27. if (!gridSampler) {
  28. gridSampler = [[ZXDefaultGridSampler alloc] init];
  29. }
  30. return gridSampler;
  31. }
  32. - (ZXBitMatrix *)sampleGrid:(ZXBitMatrix *)image
  33. dimensionX:(int)dimensionX
  34. dimensionY:(int)dimensionY
  35. p1ToX:(float)p1ToX p1ToY:(float)p1ToY
  36. p2ToX:(float)p2ToX p2ToY:(float)p2ToY
  37. p3ToX:(float)p3ToX p3ToY:(float)p3ToY
  38. p4ToX:(float)p4ToX p4ToY:(float)p4ToY
  39. p1FromX:(float)p1FromX p1FromY:(float)p1FromY
  40. p2FromX:(float)p2FromX p2FromY:(float)p2FromY
  41. p3FromX:(float)p3FromX p3FromY:(float)p3FromY
  42. p4FromX:(float)p4FromX p4FromY:(float)p4FromY
  43. error:(NSError **)error {
  44. @throw [NSException exceptionWithName:NSInternalInconsistencyException
  45. reason:[NSString stringWithFormat:@"You must override %@ in a subclass", NSStringFromSelector(_cmd)]
  46. userInfo:nil];
  47. }
  48. - (ZXBitMatrix *)sampleGrid:(ZXBitMatrix *)image
  49. dimensionX:(int)dimensionX
  50. dimensionY:(int)dimensionY
  51. transform:(ZXPerspectiveTransform *)transform
  52. error:(NSError **)error {
  53. @throw [NSException exceptionWithName:NSInternalInconsistencyException
  54. reason:[NSString stringWithFormat:@"You must override %@ in a subclass", NSStringFromSelector(_cmd)]
  55. userInfo:nil];
  56. }
  57. + (BOOL)checkAndNudgePoints:(ZXBitMatrix *)image points:(float *)points pointsLen:(int)pointsLen error:(NSError **)error {
  58. int width = image.width;
  59. int height = image.height;
  60. // Check and nudge points from start until we see some that are OK:
  61. BOOL nudged = YES;
  62. for (int offset = 0; offset < pointsLen && nudged; offset += 2) {
  63. int x = (int) points[offset];
  64. int y = (int) points[offset + 1];
  65. if (x < -1 || x > width || y < -1 || y > height) {
  66. if (error) *error = ZXNotFoundErrorInstance();
  67. return NO;
  68. }
  69. nudged = NO;
  70. if (x == -1) {
  71. points[offset] = 0.0f;
  72. nudged = YES;
  73. } else if (x == width) {
  74. points[offset] = width - 1;
  75. nudged = YES;
  76. }
  77. if (y == -1) {
  78. points[offset + 1] = 0.0f;
  79. nudged = YES;
  80. } else if (y == height) {
  81. points[offset + 1] = height - 1;
  82. nudged = YES;
  83. }
  84. }
  85. // Check and nudge points from end:
  86. nudged = YES;
  87. for (int offset = pointsLen - 2; offset >= 0 && nudged; offset -= 2) {
  88. int x = (int) points[offset];
  89. int y = (int) points[offset + 1];
  90. if (x < -1 || x > width || y < -1 || y > height) {
  91. if (error) *error = ZXNotFoundErrorInstance();
  92. return NO;
  93. }
  94. nudged = NO;
  95. if (x == -1) {
  96. points[offset] = 0.0f;
  97. nudged = YES;
  98. } else if (x == width) {
  99. points[offset] = width - 1;
  100. nudged = YES;
  101. }
  102. if (y == -1) {
  103. points[offset + 1] = 0.0f;
  104. nudged = YES;
  105. } else if (y == height) {
  106. points[offset + 1] = height - 1;
  107. nudged = YES;
  108. }
  109. }
  110. return YES;
  111. }
  112. @end