public override BitMatrix sampleGrid(BitMatrix image, int dimension, PerspectiveTransform transform)
 {
     BitMatrix bits = new BitMatrix(dimension);
     float[] points = new float[dimension << 1];
     for (int y = 0; y < dimension; y++)
     {
         int max = points.Length;
         //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'"
         float iValue = (float) y + 0.5f;
         for (int x = 0; x < max; x += 2)
         {
             //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'"
             points[x] = (float) (x >> 1) + 0.5f;
             points[x + 1] = iValue;
         }
         transform.transformPoints(points);
         // Quick check to see if points transformed to something inside the image;
         // sufficient to check the endpoints
         checkAndNudgePoints(image, points);
         try
         {
             for (int x = 0; x < max; x += 2)
             {
                 //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'"
                 if (image.get_Renamed((int) points[x], (int) points[x + 1]))
                 {
                     // Black(-ish) pixel
                     bits.set_Renamed(x >> 1, y);
                 }
             }
         }
         catch (System.IndexOutOfRangeException)
         {
             // This feels wrong, but, sometimes if the finder patterns are misidentified, the resulting
             // transform gets "twisted" such that it maps a straight line of points to a set of points
             // whose endpoints are in bounds, but others are not. There is probably some mathematical
             // way to detect this about the transformation that I don't know yet.
             // This results in an ugly runtime exception despite our clever checks above -- can't have
             // that. We could check each point's coordinates but that feels duplicative. We settle for
             // catching and wrapping ArrayIndexOutOfBoundsException.
             throw ReaderException.Instance;
         }
     }
     return bits;
 }
Пример #2
0
 /// <summary> <p>Checks a set of points that have been transformed to sample points on an image against
 /// the image's dimensions to see if the point are even within the image.</p>
 /// 
 /// <p>This method will actually "nudge" the endpoints back onto the image if they are found to be
 /// barely (less than 1 pixel) off the image. This accounts for imperfect detection of finder
 /// patterns in an image where the QR Code runs all the way to the image border.</p>
 /// 
 /// <p>For efficiency, the method will check points from either end of the line until one is found
 /// to be within the image. Because the set of points are assumed to be linear, this is valid.</p>
 /// 
 /// </summary>
 /// <param name="image">image into which the points should map
 /// </param>
 /// <param name="points">actual points in x1,y1,...,xn,yn form
 /// </param>
 /// <throws>  ReaderException if an endpoint is lies outside the image boundaries </throws>
 protected internal static void checkAndNudgePoints(BitMatrix image, float[] points)
 {
     int width = image.Width;
     int height = image.Height;
     // Check and nudge points from start until we see some that are OK:
     bool nudged = true;
     for (int offset = 0; offset < points.Length && nudged; offset += 2)
     {
         //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'"
         int x = (int) points[offset];
         //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'"
         int y = (int) points[offset + 1];
         if (x < - 1 || x > width || y < - 1 || y > height)
         {
             throw ReaderException.Instance;
         }
         nudged = false;
         if (x == - 1)
         {
             points[offset] = 0.0f;
             nudged = true;
         }
         else if (x == width)
         {
             points[offset] = width - 1;
             nudged = true;
         }
         if (y == - 1)
         {
             points[offset + 1] = 0.0f;
             nudged = true;
         }
         else if (y == height)
         {
             points[offset + 1] = height - 1;
             nudged = true;
         }
     }
     // Check and nudge points from end:
     nudged = true;
     for (int offset = points.Length - 2; offset >= 0 && nudged; offset -= 2)
     {
         //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'"
         int x = (int) points[offset];
         //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'"
         int y = (int) points[offset + 1];
         if (x < - 1 || x > width || y < - 1 || y > height)
         {
             throw ReaderException.Instance;
         }
         nudged = false;
         if (x == - 1)
         {
             points[offset] = 0.0f;
             nudged = true;
         }
         else if (x == width)
         {
             points[offset] = width - 1;
             nudged = true;
         }
         if (y == - 1)
         {
             points[offset + 1] = 0.0f;
             nudged = true;
         }
         else if (y == height)
         {
             points[offset + 1] = height - 1;
             nudged = true;
         }
     }
 }
Пример #3
0
 public virtual BitMatrix sampleGrid(BitMatrix image, int dimension, PerspectiveTransform transform)
 {
     throw new System.NotSupportedException();
 }
Пример #4
0
 /// <summary> <p>Samples an image for a square matrix of bits of the given dimension. This is used to extract
 /// the black/white modules of a 2D barcode like a QR Code found in an image. Because this barcode
 /// may be rotated or perspective-distorted, the caller supplies four points in the source image
 /// that define known points in the barcode, so that the image may be sampled appropriately.</p>
 /// 
 /// <p>The last eight "from" parameters are four X/Y coordinate pairs of locations of points in
 /// the image that define some significant points in the image to be sample. For example,
 /// these may be the location of finder pattern in a QR Code.</p>
 /// 
 /// <p>The first eight "to" parameters are four X/Y coordinate pairs measured in the destination
 /// {@link BitMatrix}, from the top left, where the known points in the image given by the "from"
 /// parameters map to.</p>
 /// 
 /// <p>These 16 parameters define the transformation needed to sample the image.</p>
 /// 
 /// </summary>
 /// <param name="image">image to sample
 /// </param>
 /// <param name="dimension">width/height of {@link BitMatrix} to sample from image
 /// </param>
 /// <returns> {@link BitMatrix} representing a grid of points sampled from the image within a region
 /// defined by the "from" parameters
 /// </returns>
 /// <throws>  ReaderException if image can't be sampled, for example, if the transformation defined </throws>
 /// <summary>   by the given points is invalid or results in sampling outside the image boundaries
 /// </summary>
 public abstract BitMatrix sampleGrid(BitMatrix image, int dimension, 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);
        public override BitMatrix sampleGrid(BitMatrix image, int dimension, 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)
        {
            PerspectiveTransform transform = PerspectiveTransform.quadrilateralToQuadrilateral(p1ToX, p1ToY, p2ToX, p2ToY, p3ToX, p3ToY, p4ToX, p4ToY, p1FromX, p1FromY, p2FromX, p2FromY, p3FromX, p3FromY, p4FromX, p4FromY);

            return sampleGrid(image, dimension, transform);
        }
Пример #6
0
        // Calculates the final BitMatrix once for all requests. This could be called once from the
        // constructor instead, but there are some advantages to doing it lazily, such as making
        // profiling easier, and not doing heavy lifting when callers don't expect it.
        private void binarizeEntireImage()
        {
            if (matrix == null)
            {
                LuminanceSource source = LuminanceSource;
                if (source.Width >= MINIMUM_DIMENSION && source.Height >= MINIMUM_DIMENSION)
                {
                    sbyte[] luminances = source.Matrix;
                    int width = source.Width;
                    int height = source.Height;
                    int subWidth = width >> 3;
                    int subHeight = height >> 3;
                    int[][] blackPoints = calculateBlackPoints(luminances, subWidth, subHeight, width);

                    matrix = new BitMatrix(width, height);
                    calculateThresholdForBlock(luminances, subWidth, subHeight, width, blackPoints, matrix);
                }
                else
                {
                    // If the image is too small, fall back to the global histogram approach.
                    matrix = base.BlackMatrix;
                }
            }
        }
Пример #7
0
 // Applies a single threshold to an 8x8 block of pixels.
 private static void threshold8x8Block(sbyte[] luminances, int xoffset, int yoffset, int threshold, int stride, BitMatrix matrix)
 {
     for (int y = 0; y < 8; y++)
     {
         int offset = (yoffset + y) * stride + xoffset;
         for (int x = 0; x < 8; x++)
         {
             int pixel = luminances[offset + x] & 0xff;
             if (pixel < threshold)
             {
                 matrix.set_Renamed(xoffset + x, yoffset + y);
             }
         }
     }
 }
Пример #8
0
 // For each 8x8 block in the image, calculate the average black point using a 5x5 grid
 // of the blocks around it. Also handles the corner cases, but will ignore up to 7 pixels
 // on the right edge and 7 pixels at the bottom of the image if the overall dimensions are not
 // multiples of eight. In practice, leaving those pixels white does not seem to be a problem.
 private static void calculateThresholdForBlock(sbyte[] luminances, int subWidth, int subHeight, int stride, int[][] blackPoints, BitMatrix matrix)
 {
     for (int y = 0; y < subHeight; y++)
     {
         for (int x = 0; x < subWidth; x++)
         {
             int left = (x > 1)?x:2;
             left = (left < subWidth - 2)?left:subWidth - 3;
             int top = (y > 1)?y:2;
             top = (top < subHeight - 2)?top:subHeight - 3;
             int sum = 0;
             for (int z = - 2; z <= 2; z++)
             {
                 int[] blackRow = blackPoints[top + z];
                 sum += blackRow[left - 2];
                 sum += blackRow[left - 1];
                 sum += blackRow[left];
                 sum += blackRow[left + 1];
                 sum += blackRow[left + 2];
             }
             int average = sum / 25;
             threshold8x8Block(luminances, x << 3, y << 3, average, stride, matrix);
         }
     }
 }
Пример #9
0
 public DetectorResult(BitMatrix bits, ResultPoint[] points)
 {
     this.bits = bits;
     this.points = points;
 }