Ejemplo n.º 1
0
 /**
    * <p>Convenience method that can decode a Data Matrix Code represented as a 2D array of booleans.
    * "true" is taken to mean a black module.</p>
    *
    * @param image booleans representing white/black Data Matrix Code modules
    * @return text and bytes encoded within the Data Matrix Code
    * @throws ReaderException if the Data Matrix Code cannot be decoded
    */
 public DecoderResult decode(bool[][] image)
 {
     int dimension = image.Length;
     BitMatrix bits = new BitMatrix(dimension);
     for (int i = 0; i < dimension; i++) {
       for (int j = 0; j < dimension; j++) {
         if (image[i][j]) {
           bits.set(i, j);
         }
       }
     }
     return decode(bits);
 }
Ejemplo n.º 2
0
 /// <summary>
 /// Applies a single threshold to a block of pixels.
 /// </summary>
 private static void thresholdBlock(sbyte[] luminances, int xoffset, int yoffset, int threshold, int stride, BitMatrix matrix)
 {
     for (int y = 0, offset = yoffset * stride + xoffset; y < BLOCK_SIZE; y++, offset += stride)
     {
         for (int x = 0; x < BLOCK_SIZE; x++)
         {
             // Comparison needs to be <= so that black == 0 pixels are black even if the threshold is 0.
             if ((luminances[offset + x] & 0xFF) <= threshold)
             {
                 matrix.set(xoffset + x, yoffset + y);
             }
         }
     }
 }
Ejemplo n.º 3
0
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET:
//ORIGINAL LINE: public BitMatrix sampleGrid(BitMatrix image, int dimensionX, int dimensionY, PerspectiveTransform transform) throws com.google.zxing.NotFoundException
        public override BitMatrix sampleGrid(BitMatrix image, int dimensionX, int dimensionY, PerspectiveTransform transform)
        {
            if (dimensionX <= 0 || dimensionY <= 0)
            {
                throw NotFoundException.NotFoundInstance;
            }
            BitMatrix bits = new BitMatrix(dimensionX, dimensionY);

            float[] points = new float[dimensionX << 1];
            for (int y = 0; y < dimensionY; y++)
            {
                int   max    = points.Length;
                float iValue = (float)y + 0.5f;
                for (int x = 0; x < max; x += 2)
                {
                    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)
                    {
                        if (image.get((int)points[x], (int)points[x + 1]))
                        {
                            // Black(-ish) pixel
                            bits.set(x >> 1, y);
                        }
                    }
                }
                catch (System.IndexOutOfRangeException aioobe)
                {
                    // 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 NotFoundException.NotFoundInstance;
                }
            }
            return(bits);
        }
 //JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET:
 //ORIGINAL LINE: public BitMatrix sampleGrid(BitMatrix image, int dimensionX, int dimensionY, PerspectiveTransform transform) throws com.google.zxing.NotFoundException
 public override BitMatrix sampleGrid(BitMatrix image, int dimensionX, int dimensionY, PerspectiveTransform transform)
 {
     if (dimensionX <= 0 || dimensionY <= 0)
     {
       throw NotFoundException.NotFoundInstance;
     }
     BitMatrix bits = new BitMatrix(dimensionX, dimensionY);
     float[] points = new float[dimensionX << 1];
     for (int y = 0; y < dimensionY; y++)
     {
       int max = points.Length;
       float iValue = (float) y + 0.5f;
       for (int x = 0; x < max; x += 2)
       {
     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)
     {
       if (image.get((int) points[x], (int) points[x + 1]))
       {
         // Black(-ish) pixel
         bits.set(x >> 1, y);
       }
     }
       }
       catch (System.IndexOutOfRangeException aioobe)
       {
     // 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 NotFoundException.NotFoundInstance;
       }
     }
     return bits;
 }
Ejemplo n.º 5
0
 /**
    * <p>Convenience method that can decode a QR Code represented as a 2D array of booleans.
    * "true" is taken to mean a black module.</p>
    *
    * @param image booleans representing white/black QR Code modules
    * @return text and bytes encoded within the QR Code
    * @throws ReaderException if the QR Code cannot be decoded
    */
 public DecoderResult decode(bool[][] image)
 {
     try{
         int dimension = image.Length;
         BitMatrix bits = new BitMatrix(dimension);
         for (int i = 0; i < dimension; i++) {
           for (int j = 0; j < dimension; j++) {
             if (image[i][j]) {
               bits.set(i, j);
             }
           }
         }
         return decode(bits);
       }catch (Exception e){
         throw  new ReaderException(e.Message);
       }
 }
Ejemplo n.º 6
0
        public override BitMatrix sampleGrid(MonochromeBitmapSource 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);

            BitMatrix bits = new BitMatrix(dimension);

            float[] points = new float[dimension << 1];
            for (int i = 0; i < dimension; i++)
            {
                int   max    = points.Length;
                float iValue = (float)i + 0.5f;
                for (int j = 0; j < max; j += 2)
                {
                    points[j]     = (float)(j >> 1) + 0.5f;
                    points[j + 1] = iValue;
                }
                transform.transformPoints(points);
                // Quick check to see if points transformed to something inside the image;
                // sufficent to check the endpoints
                checkAndNudgePoints(image, points);
                try
                {
                    for (int j = 0; j < max; j += 2)
                    {
                        //UPGRADE_WARNING: Narrowing conversions may produce unexpected results in C#. 'ms-help://MS.VSCC.2003/commoner/redir/redirect.htm?keyword="jlca1042"'
                        if (image.isBlack((int)points[j], (int)points[j + 1]))
                        {
                            // Black(-ish) pixel
                            bits.set(i, j >> 1);
                        }
                    }
                }
                catch (System.IndexOutOfRangeException aioobe)
                {
                    // 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 new ReaderException(aioobe.Message);
                }
            }
            return(bits);
        }
        public override BitMatrix sampleGrid(MonochromeBitmapSource 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);

            BitMatrix bits = new BitMatrix(dimension);
            float[] points = new float[dimension << 1];
            for (int i = 0; i < dimension; i++)
            {
                int max = points.Length;
                float iValue = (float)i + 0.5f;
                for (int j = 0; j < max; j += 2)
                {
                    points[j] = (float)(j >> 1) + 0.5f;
                    points[j + 1] = iValue;
                }
                transform.transformPoints(points);
                // Quick check to see if points transformed to something inside the image;
                // sufficent to check the endpoints
                checkAndNudgePoints(image, points);
                try
                {
                    for (int j = 0; j < max; j += 2)
                    {
                        //UPGRADE_WARNING: Narrowing conversions may produce unexpected results in C#. 'ms-help://MS.VSCC.2003/commoner/redir/redirect.htm?keyword="jlca1042"'
                        if (image.isBlack((int)points[j], (int)points[j + 1]))
                        {
                            // Black(-ish) pixel
                            bits.set(i, j >> 1);
                        }
                    }
                }
                catch (System.IndexOutOfRangeException aioobe)
                {
                    // 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 new ReaderException(aioobe.Message);
                }
            }
            return bits;
        }
Ejemplo n.º 8
0
 /// <summary>
 /// Applies a single threshold to a block of pixels.
 /// </summary>
 private static void thresholdBlock(sbyte[] luminances, int xoffset, int yoffset, int threshold, int stride, BitMatrix matrix)
 {
     for (int y = 0, offset = yoffset * stride + xoffset; y < BLOCK_SIZE; y++, offset += stride)
     {
       for (int x = 0; x < BLOCK_SIZE; x++)
       {
     // Comparison needs to be <= so that black == 0 pixels are black even if the threshold is 0.
     if ((luminances[offset + x] & 0xFF) <= threshold)
     {
       matrix.set(xoffset + x, yoffset + y);
     }
       }
     }
 }
        /**
           * This method detects a Data Matrix code in a "pure" image -- that is, pure monochrome image
           * which contains only an unrotated, unskewed, image of a Data Matrix code, with some white border
           * around it. This is a specialized method that works exceptionally fast in this special
           * case.
           */
        private static BitMatrix extractPureBits(MonochromeBitmapSource image)
        {
            // Now need to determine module size in pixels

            int height = image.getHeight();
            int width = image.getWidth();
            int minDimension = Math.Min(height, width);

            // First, skip white border by tracking diagonally from the top left down and to the right:
            int borderWidth = 0;
            while (borderWidth < minDimension && !image.isBlack(borderWidth, borderWidth)) {
              borderWidth++;
            }
            if (borderWidth == minDimension) {
              throw new ReaderException();
            }

            // And then keep tracking across the top-left black module to determine module size
            int moduleEnd = borderWidth + 1;
            while (moduleEnd < width && image.isBlack(moduleEnd, borderWidth)) {
              moduleEnd++;
            }
            if (moduleEnd == width) {
              throw new ReaderException();
            }

            int moduleSize = moduleEnd - borderWidth;

            // And now find where the bottommost black module on the first column ends
            int columnEndOfSymbol = height - 1;
            while (columnEndOfSymbol >= 0 && !image.isBlack(borderWidth, columnEndOfSymbol)) {
                columnEndOfSymbol--;
            }
            if (columnEndOfSymbol < 0) {
              throw new ReaderException();
            }
            columnEndOfSymbol++;

            // Make sure width of barcode is a multiple of module size
            if ((columnEndOfSymbol - borderWidth) % moduleSize != 0) {
              throw new ReaderException();
            }
            int dimension = (columnEndOfSymbol - borderWidth) / moduleSize;

            // Push in the "border" by half the module width so that we start
            // sampling in the middle of the module. Just in case the image is a
            // little off, this will help recover.
            borderWidth += moduleSize >> 1;

            int sampleDimension = borderWidth + (dimension - 1) * moduleSize;
            if (sampleDimension >= width || sampleDimension >= height) {
              throw new ReaderException();
            }

            // Now just read off the bits
            BitMatrix bits = new BitMatrix(dimension);
            for (int i = 0; i < dimension; i++) {
              int iOffset = borderWidth + i * moduleSize;
              for (int j = 0; j < dimension; j++) {
                if (image.isBlack(borderWidth + j * moduleSize, iOffset)) {
                  bits.set(i, j);
                }
              }
            }
            return bits;
        }
Ejemplo n.º 10
0
        /**
           * <p>Extracts the data region from a {@link BitMatrix} that contains
           * alignment patterns.</p>
           *
           * @param bitMatrix Original {@link BitMatrix} with alignment patterns
           * @return BitMatrix that has the alignment patterns removed
           */
        BitMatrix extractDataRegion(BitMatrix bitMatrix)
        {
            int symbolSizeRows = version.getSymbolSizeRows();
            int symbolSizeColumns = version.getSymbolSizeColumns();

            // TODO(bbrown): Make this work with rectangular codes
            if (bitMatrix.getDimension() != symbolSizeRows) {
              throw new ArgumentException("Dimension of bitMarix must match the version size");
            }

            int dataRegionSizeRows = version.getDataRegionSizeRows();
            int dataRegionSizeColumns = version.getDataRegionSizeColumns();

            int numDataRegionsRow = symbolSizeRows / dataRegionSizeRows;
            int numDataRegionsColumn = symbolSizeColumns / dataRegionSizeColumns;

            int sizeDataRegionRow = numDataRegionsRow * dataRegionSizeRows;
            //int sizeDataRegionColumn = numDataRegionsColumn * dataRegionSizeColumns;

            // TODO(bbrown): Make this work with rectangular codes
            BitMatrix bitMatrixWithoutAlignment = new BitMatrix(sizeDataRegionRow);
            for (int dataRegionRow = 0; dataRegionRow < numDataRegionsRow; ++dataRegionRow) {
              int dataRegionRowOffset = dataRegionRow * dataRegionSizeRows;
              for (int dataRegionColumn = 0; dataRegionColumn < numDataRegionsColumn; ++dataRegionColumn) {
                int dataRegionColumnOffset = dataRegionColumn * dataRegionSizeColumns;
                for (int i = 0; i < dataRegionSizeRows; ++i) {
                  int readRowOffset = dataRegionRow * (dataRegionSizeRows + 2) + 1 + i;
                  int writeRowOffset = dataRegionRowOffset + i;
                  for (int j = 0; j < dataRegionSizeColumns; ++j) {
                    int readColumnOffset = dataRegionColumn * (dataRegionSizeColumns + 2) + 1 + j;
                    if (bitMatrix.get(readRowOffset, readColumnOffset)) {
                      int writeColumnOffset = dataRegionColumnOffset + j;
                      bitMatrixWithoutAlignment.set(writeRowOffset, writeColumnOffset);
                    }
                  }
                }
              }
            }
            return bitMatrixWithoutAlignment;
        }