/// <summary> /// <p>Reads a bit of the mapping matrix accounting for boundary wrapping.</p> /// </summary> /// <param name="row"> Row to read in the mapping matrix </param> /// <param name="column"> Column to read in the mapping matrix </param> /// <param name="numRows"> Number of rows in the mapping matrix </param> /// <param name="numColumns"> Number of columns in the mapping matrix </param> /// <returns> value of the given bit in the mapping matrix </returns> internal bool readModule(int row, int column, int numRows, int numColumns) { // Adjust the row and column indices based on boundary wrapping if (row < 0) { row += numRows; column += 4 - ((numRows + 4) & 0x07); } if (column < 0) { column += numColumns; row += 4 - ((numColumns + 4) & 0x07); } readMappingMatrix.set(column, row); return(mappingBitMatrix.get(column, row)); }
/// <summary> /// <p>Extracts the data region from a <seealso cref="BitMatrix"/> that contains /// alignment patterns.</p> /// </summary> /// <param name="bitMatrix"> Original <seealso cref="BitMatrix"/> with alignment patterns </param> /// <returns> BitMatrix that has the alignment patterns removed </returns> internal BitMatrix extractDataRegion(BitMatrix bitMatrix) { int symbolSizeRows = version.SymbolSizeRows; int symbolSizeColumns = version.SymbolSizeColumns; if (bitMatrix.Height != symbolSizeRows) { throw new System.ArgumentException("Dimension of bitMarix must match the version size"); } int dataRegionSizeRows = version.DataRegionSizeRows; int dataRegionSizeColumns = version.DataRegionSizeColumns; int numDataRegionsRow = symbolSizeRows / dataRegionSizeRows; int numDataRegionsColumn = symbolSizeColumns / dataRegionSizeColumns; int sizeDataRegionRow = numDataRegionsRow * dataRegionSizeRows; int sizeDataRegionColumn = numDataRegionsColumn * dataRegionSizeColumns; BitMatrix bitMatrixWithoutAlignment = new BitMatrix(sizeDataRegionColumn, 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(readColumnOffset, readRowOffset)) { int writeColumnOffset = dataRegionColumnOffset + j; bitMatrixWithoutAlignment.set(writeColumnOffset, writeRowOffset); } } } } } return(bitMatrixWithoutAlignment); }
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET: //ORIGINAL LINE: private static int moduleSize(int[] leftTopBlack, com.google.zxing.common.BitMatrix image) throws com.google.zxing.NotFoundException private static int getModuleSize(int[] leftTopBlack, BitMatrix image) { int x = leftTopBlack[0]; int y = leftTopBlack[1]; int width = image.Width; while (x < width && image.get(x, y)) { x++; } if (x == width) { throw NotFoundException.NotFoundInstance; } int moduleSize = (int)((uint)(x - leftTopBlack[0]) >> 3); // We've crossed left first bar, which is 8x if (moduleSize == 0) { throw NotFoundException.NotFoundInstance; } return moduleSize; }
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET: //ORIGINAL LINE: private static int findPatternStart(int x, int y, com.google.zxing.common.BitMatrix image) throws com.google.zxing.NotFoundException private static int findPatternStart(int x, int y, BitMatrix image) { int width = image.Width; int start = x; // start should be on black int transitions = 0; bool black = true; while (start < width - 1 && transitions < 8) { start++; bool newBlack = image.get(start, y); if (black != newBlack) { transitions++; } black = newBlack; } if (start == width - 1) { throw NotFoundException.NotFoundInstance; } return start; }
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET: //ORIGINAL LINE: private static int findPatternEnd(int x, int y, com.google.zxing.common.BitMatrix image) throws com.google.zxing.NotFoundException private static int findPatternEnd(int x, int y, BitMatrix image) { int width = image.Width; int end = width - 1; // end should be on black while (end > x && !image.get(end, y)) { end--; } int transitions = 0; bool black = true; while (end > x && transitions < 9) { end--; bool newBlack = image.get(end, y); if (black != newBlack) { transitions++; } black = newBlack; } if (end == x) { throw NotFoundException.NotFoundInstance; } return end; }
/// <summary> /// This method detects a code in a "pure" image -- that is, pure monochrome image /// which contains only an unrotated, unskewed, image of a code, with some white border /// around it. This is a specialized method that works exceptionally fast in this special /// case. /// </summary> /// <seealso cref= com.google.zxing.qrcode.QRCodeReader#extractPureBits(BitMatrix) </seealso> /// <seealso cref= com.google.zxing.datamatrix.DataMatrixReader#extractPureBits(BitMatrix) </seealso> //JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET: //ORIGINAL LINE: private static com.google.zxing.common.BitMatrix extractPureBits(com.google.zxing.common.BitMatrix image) throws com.google.zxing.NotFoundException private static BitMatrix extractPureBits(BitMatrix image) { int[] leftTopBlack = image.TopLeftOnBit; int[] rightBottomBlack = image.BottomRightOnBit; if (leftTopBlack == null || rightBottomBlack == null) { throw NotFoundException.NotFoundInstance; } int moduleSize = getModuleSize(leftTopBlack, image); int top = leftTopBlack[1]; int bottom = rightBottomBlack[1]; int left = findPatternStart(leftTopBlack[0], top, image); int right = findPatternEnd(leftTopBlack[0], top, image); int matrixWidth = (right - left + 1) / moduleSize; int matrixHeight = (bottom - top + 1) / moduleSize; if (matrixWidth <= 0 || matrixHeight <= 0) { throw NotFoundException.NotFoundInstance; } // 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. int nudge = moduleSize >> 1; top += nudge; left += nudge; // Now just read off the bits BitMatrix bits = new BitMatrix(matrixWidth, matrixHeight); for (int y = 0; y < matrixHeight; y++) { int iOffset = top + y * moduleSize; for (int x = 0; x < matrixWidth; x++) { if (image.get(left + x * moduleSize, iOffset)) { bits.set(x, y); } } } return bits; }
/// <summary> /// <p>Reads the bits in the <seealso cref="BitMatrix"/> representing the mapping matrix (No alignment patterns) /// in the correct order in order to reconstitute the codewords bytes contained within the /// Data Matrix Code.</p> /// </summary> /// <returns> bytes encoded within the Data Matrix Code </returns> /// <exception cref="FormatException"> if the exact number of bytes expected is not read </exception> //JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET: //ORIGINAL LINE: byte[] readCodewords() throws com.google.zxing.FormatException internal sbyte[] readCodewords() { sbyte[] result = new sbyte[version.TotalCodewords]; int resultOffset = 0; int row = 4; int column = 0; int numRows = mappingBitMatrix.Height; int numColumns = mappingBitMatrix.Width; bool corner1Read = false; bool corner2Read = false; bool corner3Read = false; bool corner4Read = false; // Read all of the codewords do { // Check the four corner cases if ((row == numRows) && (column == 0) && !corner1Read) { result[resultOffset++] = (sbyte)readCorner1(numRows, numColumns); row -= 2; column += 2; corner1Read = true; } else if ((row == numRows - 2) && (column == 0) && ((numColumns & 0x03) != 0) && !corner2Read) { result[resultOffset++] = (sbyte)readCorner2(numRows, numColumns); row -= 2; column += 2; corner2Read = true; } else if ((row == numRows + 4) && (column == 2) && ((numColumns & 0x07) == 0) && !corner3Read) { result[resultOffset++] = (sbyte)readCorner3(numRows, numColumns); row -= 2; column += 2; corner3Read = true; } else if ((row == numRows - 2) && (column == 0) && ((numColumns & 0x07) == 4) && !corner4Read) { result[resultOffset++] = (sbyte)readCorner4(numRows, numColumns); row -= 2; column += 2; corner4Read = true; } else { // Sweep upward diagonally to the right do { if ((row < numRows) && (column >= 0) && !readMappingMatrix.get(column, row)) { result[resultOffset++] = (sbyte)readUtah(row, column, numRows, numColumns); } row -= 2; column += 2; } while ((row >= 0) && (column < numColumns)); row += 1; column += 3; // Sweep downward diagonally to the left do { if ((row >= 0) && (column < numColumns) && !readMappingMatrix.get(column, row)) { result[resultOffset++] = (sbyte)readUtah(row, column, numRows, numColumns); } row += 2; column -= 2; } while ((row < numRows) && (column >= 0)); row += 3; column += 1; } } while ((row < numRows) || (column < numColumns)); if (resultOffset != version.TotalCodewords) { throw FormatException.FormatInstance; } return(result); }
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET: //ORIGINAL LINE: private static int moduleSize(int[] leftTopBlack, com.google.zxing.common.BitMatrix image) throws com.google.zxing.NotFoundException private static int getModuleSize(int[] leftTopBlack, BitMatrix image) { int width = image.Width; int x = leftTopBlack[0]; int y = leftTopBlack[1]; while (x < width && image.get(x, y)) { x++; } if (x == width) { throw NotFoundException.NotFoundInstance; } int moduleSize = x - leftTopBlack[0]; if (moduleSize == 0) { throw NotFoundException.NotFoundInstance; } return moduleSize; }
/// <summary> /// <p>Reads the bits in the <seealso cref="BitMatrix"/> representing the finder pattern in the /// correct order in order to reconstitute the codewords bytes contained within the /// QR Code.</p> /// </summary> /// <returns> bytes encoded within the QR Code </returns> /// <exception cref="FormatException"> if the exact number of bytes expected is not read </exception> //JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET: //ORIGINAL LINE: byte[] readCodewords() throws com.google.zxing.FormatException internal sbyte[] readCodewords() { FormatInformation formatInfo = readFormatInformation(); Version version = readVersion(); // Get the data mask for the format used in this QR Code. This will exclude // some bits from reading as we wind through the bit matrix. DataMask dataMask = DataMask.forReference((int)formatInfo.DataMask); int dimension = bitMatrix.Height; dataMask.unmaskBitMatrix(bitMatrix, dimension); BitMatrix functionPattern = version.buildFunctionPattern(); bool readingUp = true; sbyte[] result = new sbyte[version.TotalCodewords]; int resultOffset = 0; int currentByte = 0; int bitsRead = 0; // Read columns in pairs, from right to left for (int j = dimension - 1; j > 0; j -= 2) { if (j == 6) { // Skip whole column with vertical alignment pattern; // saves time and makes the other code proceed more cleanly j--; } // Read alternatingly from bottom to top then top to bottom for (int count = 0; count < dimension; count++) { int i = readingUp ? dimension - 1 - count : count; for (int col = 0; col < 2; col++) { // Ignore bits covered by the function pattern if (!functionPattern.get(j - col, i)) { // Read a bit bitsRead++; currentByte <<= 1; if (bitMatrix.get(j - col, i)) { currentByte |= 1; } // If we've made a whole byte, save it off if (bitsRead == 8) { result[resultOffset++] = (sbyte)currentByte; bitsRead = 0; currentByte = 0; } } } } readingUp ^= true; // readingUp = !readingUp; // switch directions } if (resultOffset != version.TotalCodewords) { throw FormatException.FormatInstance; } return(result); }
private int copyBit(int i, int j, int versionBits) { return(bitMatrix.get(i, j) ? (versionBits << 1) | 0x1 : versionBits << 1); }
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET: //ORIGINAL LINE: private static float moduleSize(int[] leftTopBlack, com.google.zxing.common.BitMatrix image) throws com.google.zxing.NotFoundException private static float getModuleSize(int[] leftTopBlack, BitMatrix image) { int height = image.Height; int width = image.Width; int x = leftTopBlack[0]; int y = leftTopBlack[1]; bool inBlack = true; int transitions = 0; while (x < width && y < height) { if (inBlack != image.get(x, y)) { if (++transitions == 5) { break; } inBlack = !inBlack; } x++; y++; } if (x == width || y == height) { throw NotFoundException.NotFoundInstance; } return (x - leftTopBlack[0]) / 7.0f; }
/// <summary> /// This method detects a code in a "pure" image -- that is, pure monochrome image /// which contains only an unrotated, unskewed, image of a code, with some white border /// around it. This is a specialized method that works exceptionally fast in this special /// case. /// </summary> /// <seealso cref= com.google.zxing.pdf417.PDF417Reader#extractPureBits(BitMatrix) </seealso> /// <seealso cref= com.google.zxing.datamatrix.DataMatrixReader#extractPureBits(BitMatrix) </seealso> //JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET: //ORIGINAL LINE: private static com.google.zxing.common.BitMatrix extractPureBits(com.google.zxing.common.BitMatrix image) throws com.google.zxing.NotFoundException private static BitMatrix extractPureBits(BitMatrix image) { int[] leftTopBlack = image.TopLeftOnBit; int[] rightBottomBlack = image.BottomRightOnBit; if (leftTopBlack == null || rightBottomBlack == null) { throw NotFoundException.NotFoundInstance; } float moduleSize = getModuleSize(leftTopBlack, image); int top = leftTopBlack[1]; int bottom = rightBottomBlack[1]; int left = leftTopBlack[0]; int right = rightBottomBlack[0]; if (bottom - top != right - left) { // Special case, where bottom-right module wasn't black so we found something else in the last row // Assume it's a square, so use height as the width right = left + (bottom - top); } int matrixWidth = (int)Math.Round((right - left + 1) / moduleSize); int matrixHeight = (int) Math.Round((bottom - top + 1) / moduleSize); if (matrixWidth <= 0 || matrixHeight <= 0) { throw NotFoundException.NotFoundInstance; } if (matrixHeight != matrixWidth) { // Only possibly decode square regions throw NotFoundException.NotFoundInstance; } // 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. int nudge = (int)(moduleSize / 2.0f); top += nudge; left += nudge; // Now just read off the bits BitMatrix bits = new BitMatrix(matrixWidth, matrixHeight); for (int y = 0; y < matrixHeight; y++) { int iOffset = top + (int)(y * moduleSize); for (int x = 0; x < matrixWidth; x++) { if (image.get(left + (int)(x * moduleSize), iOffset)) { bits.set(x, y); } } } return bits; }
/// <summary> /// <p>Extracts the data region from a <seealso cref="BitMatrix"/> that contains /// alignment patterns.</p> /// </summary> /// <param name="bitMatrix"> Original <seealso cref="BitMatrix"/> with alignment patterns </param> /// <returns> BitMatrix that has the alignment patterns removed </returns> internal BitMatrix extractDataRegion(BitMatrix bitMatrix) { int symbolSizeRows = version.SymbolSizeRows; int symbolSizeColumns = version.SymbolSizeColumns; if (bitMatrix.Height != symbolSizeRows) { throw new System.ArgumentException("Dimension of bitMarix must match the version size"); } int dataRegionSizeRows = version.DataRegionSizeRows; int dataRegionSizeColumns = version.DataRegionSizeColumns; int numDataRegionsRow = symbolSizeRows / dataRegionSizeRows; int numDataRegionsColumn = symbolSizeColumns / dataRegionSizeColumns; int sizeDataRegionRow = numDataRegionsRow * dataRegionSizeRows; int sizeDataRegionColumn = numDataRegionsColumn * dataRegionSizeColumns; BitMatrix bitMatrixWithoutAlignment = new BitMatrix(sizeDataRegionColumn, 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(readColumnOffset, readRowOffset)) { int writeColumnOffset = dataRegionColumnOffset + j; bitMatrixWithoutAlignment.set(writeColumnOffset, writeRowOffset); } } } } } return bitMatrixWithoutAlignment; }