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

DefaultGridSampler.cs 4.2 kB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. /*
  2. * Copyright 2007 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. using System;
  17. namespace ZXing.Common
  18. {
  19. /// <author> Sean Owen
  20. /// </author>
  21. /// <author>www.Redivivus.in (suraj.supekar@redivivus.in) - Ported from ZXING Java Source
  22. /// </author>
  23. public sealed class DefaultGridSampler : GridSampler
  24. {
  25. public override BitMatrix sampleGrid(BitMatrix image, int dimensionX, int dimensionY, float p1ToX, float p1ToY, float p2ToX, float p2ToY, float p3ToX, float p3ToY, float p4ToX, float p4ToY, float p1FromX, float p1FromY, float p2FromX, float p2FromY, float p3FromX, float p3FromY, float p4FromX, float p4FromY)
  26. {
  27. PerspectiveTransform transform = PerspectiveTransform.quadrilateralToQuadrilateral(
  28. p1ToX, p1ToY, p2ToX, p2ToY, p3ToX, p3ToY, p4ToX, p4ToY,
  29. p1FromX, p1FromY, p2FromX, p2FromY, p3FromX, p3FromY, p4FromX, p4FromY);
  30. return sampleGrid(image, dimensionX, dimensionY, transform);
  31. }
  32. public override BitMatrix sampleGrid(BitMatrix image, int dimensionX, int dimensionY, PerspectiveTransform transform)
  33. {
  34. if (dimensionX <= 0 || dimensionY <= 0)
  35. {
  36. return null;
  37. }
  38. BitMatrix bits = new BitMatrix(dimensionX, dimensionY);
  39. float[] points = new float[dimensionX << 1];
  40. for (int y = 0; y < dimensionY; y++)
  41. {
  42. int max = points.Length;
  43. //UPGRADE_WARNING: Data types in Visual C# might be different. Verify the accuracy of narrowing conversions. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1042'"
  44. float iValue = (float)y + 0.5f;
  45. for (int x = 0; x < max; x += 2)
  46. {
  47. //UPGRADE_WARNING: Data types in Visual C# might be different. Verify the accuracy of narrowing conversions. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1042'"
  48. points[x] = (float)(x >> 1) + 0.5f;
  49. points[x + 1] = iValue;
  50. }
  51. transform.transformPoints(points);
  52. // Quick check to see if points transformed to something inside the image;
  53. // sufficient to check the endpoints
  54. if (!checkAndNudgePoints(image, points))
  55. return null;
  56. try
  57. {
  58. for (int x = 0; x < max; x += 2)
  59. {
  60. //UPGRADE_WARNING: Data types in Visual C# might be different. Verify the accuracy of narrowing conversions. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1042'"
  61. bits[x >> 1, y] = image[(int)points[x], (int)points[x + 1]];
  62. }
  63. }
  64. catch (System.IndexOutOfRangeException)
  65. {
  66. // This feels wrong, but, sometimes if the finder patterns are misidentified, the resulting
  67. // transform gets "twisted" such that it maps a straight line of points to a set of points
  68. // whose endpoints are in bounds, but others are not. There is probably some mathematical
  69. // way to detect this about the transformation that I don't know yet.
  70. // This results in an ugly runtime exception despite our clever checks above -- can't have
  71. // that. We could check each point's coordinates but that feels duplicative. We settle for
  72. // catching and wrapping ArrayIndexOutOfBoundsException.
  73. return null;
  74. }
  75. }
  76. return bits;
  77. }
  78. }
  79. }