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.

ZXDefaultGridSampler.m 3.5KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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 "ZXPerspectiveTransform.h"
  20. @implementation ZXDefaultGridSampler
  21. - (ZXBitMatrix *)sampleGrid:(ZXBitMatrix *)image
  22. dimensionX:(int)dimensionX
  23. dimensionY:(int)dimensionY
  24. p1ToX:(float)p1ToX p1ToY:(float)p1ToY
  25. p2ToX:(float)p2ToX p2ToY:(float)p2ToY
  26. p3ToX:(float)p3ToX p3ToY:(float)p3ToY
  27. p4ToX:(float)p4ToX p4ToY:(float)p4ToY
  28. p1FromX:(float)p1FromX p1FromY:(float)p1FromY
  29. p2FromX:(float)p2FromX p2FromY:(float)p2FromY
  30. p3FromX:(float)p3FromX p3FromY:(float)p3FromY
  31. p4FromX:(float)p4FromX p4FromY:(float)p4FromY
  32. error:(NSError **)error {
  33. ZXPerspectiveTransform *transform =
  34. [ZXPerspectiveTransform quadrilateralToQuadrilateral:p1ToX y0:p1ToY
  35. x1:p2ToX y1:p2ToY
  36. x2:p3ToX y2:p3ToY
  37. x3:p4ToX y3:p4ToY
  38. x0p:p1FromX y0p:p1FromY
  39. x1p:p2FromX y1p:p2FromY
  40. x2p:p3FromX y2p:p3FromY
  41. x3p:p4FromX y3p:p4FromY];
  42. return [self sampleGrid:image dimensionX:dimensionX dimensionY:dimensionY transform:transform error:error];
  43. }
  44. - (ZXBitMatrix *)sampleGrid:(ZXBitMatrix *)image
  45. dimensionX:(int)dimensionX
  46. dimensionY:(int)dimensionY
  47. transform:(ZXPerspectiveTransform *)transform
  48. error:(NSError **)error {
  49. if (dimensionX <= 0 || dimensionY <= 0) {
  50. if (error) *error = ZXNotFoundErrorInstance();
  51. return nil;
  52. }
  53. ZXBitMatrix *bits = [[ZXBitMatrix alloc] initWithWidth:dimensionX height:dimensionY];
  54. int pointsLen = 2 * dimensionX;
  55. float pointsf[pointsLen];
  56. memset(pointsf, 0, pointsLen * sizeof(float));
  57. for (int y = 0; y < dimensionY; y++) {
  58. int max = dimensionX << 1;
  59. float iValue = (float)y + 0.5f;
  60. for (int x = 0; x < max; x += 2) {
  61. pointsf[x] = (float) (x / 2) + 0.5f;
  62. pointsf[x + 1] = iValue;
  63. }
  64. [transform transformPoints:pointsf pointsLen:pointsLen];
  65. if (![ZXGridSampler checkAndNudgePoints:image points:pointsf pointsLen:pointsLen error:error]) {
  66. return nil;
  67. }
  68. for (int x = 0; x < max; x += 2) {
  69. int xx = (int)pointsf[x];
  70. int yy = (int)pointsf[x + 1];
  71. if (xx < 0 || yy < 0 || xx >= image.width || yy >= image.height) {
  72. if (error) *error = ZXNotFoundErrorInstance();
  73. return nil;
  74. }
  75. if ([image getX:xx y:yy]) {
  76. [bits setX:x / 2 y:y];
  77. }
  78. }
  79. }
  80. return bits;
  81. }
  82. @end