Class BitMatrix
- All Implemented Interfaces:
- Cloneable
Represents a 2D matrix of bits. In function arguments below, and throughout the common module, x is the column position, and y is the row position. The ordering is always x, y. The origin is at the top-left.
Internally the bits are represented in a 1-D array of 32-bit ints. However, each row begins with a new int. This is done intentionally so that we can copy out a row into a BitArray very efficiently.
The ordering of bits is row-major. Within each int, the least significant bits are used first, meaning they represent lower x values. This is compatible with BitArray's implementation.
- Author:
- Sean Owen, dswitkin@google.com (Daniel Switkin)
- 
Constructor SummaryConstructors
- 
Method SummaryModifier and TypeMethodDescriptionvoidclear()Clears all bits (sets to false).clone()booleanvoidflip()Flips every bit in the matrix.voidflip(int x, int y) Flips the given bit.booleanget(int x, int y) Gets the requested bit, where true means black.int[]int[]This is useful in detecting the enclosing rectangle of a 'pure' barcode.intA fast method to retrieve one row of data from the matrix as a BitArray.intint[]This is useful in detecting a corner of a 'pure' barcode.intgetWidth()inthashCode()static BitMatrixparse(boolean[][] image) Interprets a 2D array of booleans as aBitMatrix, where "true" means an "on" bit.static BitMatrixvoidrotate(int degrees) Modifies thisBitMatrixto represent the same but rotated the given degrees (0, 90, 180, 270)voidModifies thisBitMatrixto represent the same but rotated 180 degreesvoidrotate90()Modifies thisBitMatrixto represent the same but rotated 90 degrees counterclockwisevoidset(int x, int y) Sets the given bit to true.voidsetRegion(int left, int top, int width, int height) Sets a square region of the bit matrix to true.voidtoString()Deprecated.voidunset(int x, int y) voidExclusive-or (XOR): Flip the bit in thisBitMatrixif the corresponding mask bit is set.
- 
Constructor Details- 
BitMatrixpublic BitMatrix(int dimension) Creates an empty squareBitMatrix.- Parameters:
- dimension- height and width
 
- 
BitMatrixpublic BitMatrix(int width, int height) Creates an emptyBitMatrix.- Parameters:
- width- bit matrix width
- height- bit matrix height
 
 
- 
- 
Method Details- 
parseInterprets a 2D array of booleans as aBitMatrix, where "true" means an "on" bit.- Parameters:
- image- bits of the image, as a row-major 2D array. Elements are arrays representing rows
- Returns:
- BitMatrixrepresentation of image
 
- 
parse
- 
getpublic boolean get(int x, int y) Gets the requested bit, where true means black. - Parameters:
- x- The horizontal component (i.e. which column)
- y- The vertical component (i.e. which row)
- Returns:
- value of given bit in matrix
 
- 
setpublic void set(int x, int y) Sets the given bit to true. - Parameters:
- x- The horizontal component (i.e. which column)
- y- The vertical component (i.e. which row)
 
- 
unsetpublic void unset(int x, int y) 
- 
flippublic void flip(int x, int y) Flips the given bit. - Parameters:
- x- The horizontal component (i.e. which column)
- y- The vertical component (i.e. which row)
 
- 
flippublic void flip()Flips every bit in the matrix. 
- 
xorExclusive-or (XOR): Flip the bit in thisBitMatrixif the corresponding mask bit is set.- Parameters:
- mask- XOR mask
 
- 
clearpublic void clear()Clears all bits (sets to false).
- 
setRegionpublic void setRegion(int left, int top, int width, int height) Sets a square region of the bit matrix to true. - Parameters:
- left- The horizontal position to begin at (inclusive)
- top- The vertical position to begin at (inclusive)
- width- The width of the region
- height- The height of the region
 
- 
getRowA fast method to retrieve one row of data from the matrix as a BitArray.- Parameters:
- y- The row to retrieve
- row- An optional caller-allocated BitArray, will be allocated if null or too small
- Returns:
- The resulting BitArray - this reference should always be used even when passing your own row
 
- 
setRow- Parameters:
- y- row to set
- row-- BitArrayto copy from
 
- 
rotatepublic void rotate(int degrees) Modifies thisBitMatrixto represent the same but rotated the given degrees (0, 90, 180, 270)- Parameters:
- degrees- number of degrees to rotate through counter-clockwise (0, 90, 180, 270)
 
- 
rotate180public void rotate180()Modifies thisBitMatrixto represent the same but rotated 180 degrees
- 
rotate90public void rotate90()Modifies thisBitMatrixto represent the same but rotated 90 degrees counterclockwise
- 
getEnclosingRectanglepublic int[] getEnclosingRectangle()This is useful in detecting the enclosing rectangle of a 'pure' barcode.- Returns:
- left,top,width,heightenclosing rectangle of all 1 bits, or null if it is all white
 
- 
getTopLeftOnBitpublic int[] getTopLeftOnBit()This is useful in detecting a corner of a 'pure' barcode.- Returns:
- x,ycoordinate of top-left-most 1 bit, or null if it is all white
 
- 
getBottomRightOnBitpublic int[] getBottomRightOnBit()
- 
getWidthpublic int getWidth()- Returns:
- The width of the matrix
 
- 
getHeightpublic int getHeight()- Returns:
- The height of the matrix
 
- 
getRowSizepublic int getRowSize()- Returns:
- The row size of the matrix
 
- 
equals
- 
hashCodepublic int hashCode()
- 
toString
- 
toString- Parameters:
- setString- representation of a set bit
- unsetString- representation of an unset bit
- Returns:
- string representation of entire matrix utilizing given strings
 
- 
toStringDeprecated.calltoString(String,String)only, which uses \n line separator always- Parameters:
- setString- representation of a set bit
- unsetString- representation of an unset bit
- lineSeparator- newline character in string representation
- Returns:
- string representation of entire matrix utilizing given strings and line separator
 
- 
clone
 
- 
toString(String,String)only, which uses \n line separator always