public final class BitMatrix extends Object implements 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.
| Constructor and Description |
|---|
BitMatrix(int dimension)Creates an empty square BitMatrix. |
BitMatrix(int width,
int height)Creates an empty BitMatrix. |
| Modifier and Type | Method and Description |
|---|---|
void | clear()Clears all bits (sets to false). |
BitMatrix | clone() |
boolean | equals(Object o) |
void | flip(int x,
int y)Flips the given bit. |
boolean | get(int x,
int y)Gets the requested bit, where true means black. |
int[] | getBottomRightOnBit()This is useful in detecting a corner of a 'pure' barcode. |
int[] | getEnclosingRectangle()This is useful in detecting the enclosing rectangle of a 'pure' barcode. |
int | getHeight()Gets the height of the matrix. |
BitArray | getRow(int y,
BitArray row)A fast method to retrieve one row of data from the matrix as a BitArray. |
int | getRowSize()Gets the row size of the matrix. |
int[] | getTopLeftOnBit()This is useful in detecting a corner of a 'pure' barcode. |
int | getWidth()Gets the width of the matrix. |
int | hashCode() |
static BitMatrix | parse(boolean[][] image)Interprets a 2D array of booleans as a BitMatrix, where "true" means an "on" bit. |
static BitMatrix | parse(String stringRepresentation,
String setString,
String unsetString)Parses a string representation of a bit matrix. |
void | rotate180()Modifies this BitMatrix to represent the same but rotated 180 degrees |
void | set(int x,
int y)Sets the given bit to true. |
void | setRegion(int left,
int top,
int width,
int height)Sets a square region of the bit matrix to true. |
void | setRow(int y,
BitArray row)Sets a row of the matrix from a BitArray. |
String | toString()Gets a string representation using "X" for set and " " for unset bits. |
String | toString(String setString,
String unsetString)Returns string representation of entire matrix utilizing given strings. |
void | unset(int x,
int y)Sets the given bit to false. |
void | xor(BitMatrix mask)Exclusive-or (XOR): Flip the bit in this BitMatrix if the corresponding
mask bit is set. |
public BitMatrix(int dimension)
BitMatrix.dimension - height and widthpublic BitMatrix(int width,
int height)BitMatrix.width - bit matrix widthheight - bit matrix heightpublic void clear()
public void flip(int x,
int y)Flips the given bit.
x - The horizontal component (i.e. which column)y - The vertical component (i.e. which row)public boolean get(int x,
int y)Gets the requested bit, where true means black.
x - The horizontal component (i.e. which column)y - The vertical component (i.e. which row)public int[] getBottomRightOnBit()
This is useful in detecting a corner of a 'pure' barcode.
x,y coordinate of bottom-right-most 1 bit, or null if it is all whitepublic int[] getEnclosingRectangle()
left,top,width,height enclosing rectangle of all 1 bits, or null if it is all whitepublic int getHeight()
Gets the height of the matrix.
public BitArray getRow(int y, BitArray row)
y - The row to retrieverow - An optional caller-allocated BitArray, will be allocated if null or too smallpublic int getRowSize()
Gets the row size of the matrix.
public int[] getTopLeftOnBit()
x,y coordinate of top-left-most 1 bit, or null if it is all whitepublic int getWidth()
Gets the width of the matrix.
public static BitMatrix parse(boolean[][] image)
BitMatrix, where "true" means an "on" bit.image - bits of the image, as a row-major 2D array. Elements are arrays representing rowsBitMatrix representation of imagepublic static BitMatrix parse(String stringRepresentation, String setString, String unsetString)
Parses a string representation of a bit matrix.
stringRepresentation - the full string representationsetString - the string for a set bitunsetString - the string for an unset bitpublic void rotate180()
BitMatrix to represent the same but rotated 180 degreespublic void set(int x,
int y)Sets the given bit to true.
x - The horizontal component (i.e. which column)y - The vertical component (i.e. which row)public void setRegion(int left,
int top,
int width,
int height)Sets a square region of the bit matrix to true.
left - The horizontal position to begin at (inclusive)top - The vertical position to begin at (inclusive)width - The width of the regionheight - The height of the regionpublic void setRow(int y,
BitArray row)Sets a row of the matrix from a BitArray.
y - row to setrow - BitArray to copy frompublic String toString()
Gets a string representation using "X" for set and " " for unset bits.
public String toString(String setString, String unsetString)
Returns string representation of entire matrix utilizing given strings.
setString - representation of a set bitunsetString - representation of an unset bitpublic void unset(int x,
int y)Sets the given bit to false.
x - The horizontal component (i.e. which column)y - The vertical component (i.e. which row)public void xor(BitMatrix mask)
BitMatrix if the corresponding
mask bit is set.mask - XOR mask