java.lang.Object
com.google.zxing.common.BitMatrix
All Implemented Interfaces:
Cloneable

public final class BitMatrixextends 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.

  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    private final int[]
    The bits, packed into ints.
    private final int
    Height of the matrix, in bits.
    private final int
    Ints used per row of the packed storage.
    private final int
    Width of the matrix, in bits.
  • Constructor Summary

    Modifier
    Constructor
    Description
    BitMatrix(int dimension)
    Creates an empty square BitMatrix.
    BitMatrix(int width, int height)
    Creates an empty BitMatrix.
    private
    BitMatrix(int width, int height, int rowSize, int[] bits)
    Wraps dimensions and storage already packed into ints.
  • Method Summary

    Modifier and Type
    Method
    Description
    private String
    buildToString(String setString, String unsetString, String lineSeparator)
    Renders the matrix as text.
    void
    Clears all bits (sets to false).
     
    boolean
     
    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[]
    This is useful in detecting a corner of a 'pure' barcode.
    int[]
    This is useful in detecting the enclosing rectangle of a 'pure' barcode.
    int
    Gets the height of the matrix.
    getRow(int y, BitArray row)
    A fast method to retrieve one row of data from the matrix as a BitArray.
    int
    Gets the row size of the matrix.
    int[]
    This is useful in detecting a corner of a 'pure' barcode.
    int
    Gets the width of the matrix.
    int
     
    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
    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.
    Gets a string representation using "X" for set and " " for unset bits.
    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.

    Methods inherited from class Object

    getClass, notify, notifyAll, wait, wait, wait
  • Field Details

    • width

      private final int width
      Width of the matrix, in bits.
    • height

      private final int height
      Height of the matrix, in bits.
    • rowSize

      private final int rowSize
      Ints used per row of the packed storage.
    • bits

      private final int[] bits
      The bits, packed into ints.
  • Constructor Details

    • BitMatrix

      public BitMatrix(int dimension)
      Creates an empty square BitMatrix.
      Parameters:
      dimension - height and width
    • BitMatrix

      public BitMatrix(int width, int height)
      Creates an empty BitMatrix.
      Parameters:
      width - bit matrix width
      height - bit matrix height
    • BitMatrix

      private BitMatrix(int width, int height, int rowSize, int[] bits)
      Wraps dimensions and storage already packed into ints.
      Parameters:
      width - the width in bits
      height - the height in bits
      rowSize - ints used per row
      bits - the packed bits
  • Method Details

    • parse

      public static BitMatrix parse(boolean[][] image)
      Interprets a 2D array of booleans as a BitMatrix, where "true" means an "on" bit.
      Parameters:
      image - bits of the image, as a row-major 2D array. Elements are arrays representing rows
      Returns:
      BitMatrix representation of image
    • parse

      public static BitMatrix parse(String stringRepresentation, String setString, String unsetString)

      Parses a string representation of a bit matrix.

      Parameters:
      stringRepresentation - the full string representation
      setString - the string for a set bit
      unsetString - the string for an unset bit
      Returns:
      the parsed bit matrix
    • get

      public 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
    • set

      public 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)
    • unset

      public void unset(int x, int y)

      Sets the given bit to false.

      Parameters:
      x - The horizontal component (i.e. which column)
      y - The vertical component (i.e. which row)
    • flip

      public 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)
    • xor

      public void xor(BitMatrix mask)
      Exclusive-or (XOR): Flip the bit in this BitMatrix if the corresponding mask bit is set.
      Parameters:
      mask - the matrix to XOR in, which must have the same width, height and row size as this one
    • clear

      public void clear()
      Clears all bits (sets to false).
    • setRegion

      public 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
    • getRow

      public BitArray getRow(int y, BitArray row)
      A 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

      public void setRow(int y, BitArray row)

      Sets a row of the matrix from a BitArray.

      Parameters:
      y - row to set
      row - BitArray to copy from
    • rotate180

      public void rotate180()
      Modifies this BitMatrix to represent the same but rotated 180 degrees
    • getEnclosingRectangle

      public int[] getEnclosingRectangle()
      This is useful in detecting the enclosing rectangle of a 'pure' barcode.
      Returns:
      left,top,width,height enclosing rectangle of all 1 bits, or null if it is all white
    • getTopLeftOnBit

      public int[] getTopLeftOnBit()
      This is useful in detecting a corner of a 'pure' barcode.
      Returns:
      x,y coordinate of top-left-most 1 bit, or null if it is all white
    • getBottomRightOnBit

      public int[] getBottomRightOnBit()

      This is useful in detecting a corner of a 'pure' barcode.

      Returns:
      x,y coordinate of bottom-right-most 1 bit, or null if it is all white
    • getWidth

      public int getWidth()

      Gets the width of the matrix.

      Returns:
      The width of the matrix
    • getHeight

      public int getHeight()

      Gets the height of the matrix.

      Returns:
      The height of the matrix
    • getRowSize

      public int getRowSize()

      Gets the row size of the matrix.

      Returns:
      The row size of the matrix
    • equals

      public boolean equals(Object o)
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • toString

      public String toString()

      Gets a string representation using "X" for set and " " for unset bits.

      Overrides:
      toString in class Object
      Returns:
      string representation using "X" for set and " " for unset bits
    • toString

      public String toString(String setString, String unsetString)

      Returns string representation of entire matrix utilizing given strings.

      Parameters:
      setString - representation of a set bit
      unsetString - representation of an unset bit
      Returns:
      string representation of entire matrix utilizing given strings
    • buildToString

      private String buildToString(String setString, String unsetString, String lineSeparator)
      Renders the matrix as text.
      Parameters:
      setString - the text for a set bit
      unsetString - the text for an unset bit
      lineSeparator - the text between rows
      Returns:
      the text rendering
    • clone

      public BitMatrix clone()
      Overrides:
      clone in class Object