Ejemplo n.º 1
0
  // Embed position detection patterns and surrounding vertical/horizontal separators.
  private static void embedPositionDetectionPatternsAndSeparators(ByteMatrix matrix)
      throws WriterException {
    // Embed three big squares at corners.
    int pdpWidth = POSITION_DETECTION_PATTERN[0].length;
    // Left top corner.
    embedPositionDetectionPattern(0, 0, matrix);
    // Right top corner.
    embedPositionDetectionPattern(matrix.getWidth() - pdpWidth, 0, matrix);
    // Left bottom corner.
    embedPositionDetectionPattern(0, matrix.getWidth() - pdpWidth, matrix);

    // Embed horizontal separation patterns around the squares.
    int hspWidth = HORIZONTAL_SEPARATION_PATTERN[0].length;
    // Left top corner.
    embedHorizontalSeparationPattern(0, hspWidth - 1, matrix);
    // Right top corner.
    embedHorizontalSeparationPattern(matrix.getWidth() - hspWidth, hspWidth - 1, matrix);
    // Left bottom corner.
    embedHorizontalSeparationPattern(0, matrix.getWidth() - hspWidth, matrix);

    // Embed vertical separation patterns around the squares.
    int vspSize = VERTICAL_SEPARATION_PATTERN.length;
    // Left top corner.
    embedVerticalSeparationPattern(vspSize, 0, matrix);
    // Right top corner.
    embedVerticalSeparationPattern(matrix.getHeight() - vspSize - 1, 0, matrix);
    // Left bottom corner.
    embedVerticalSeparationPattern(vspSize, matrix.getHeight() - vspSize, matrix);
  }
  private static void embedPositionDetectionPatternsAndSeparators(ByteMatrix matrix)
      throws WriterException {

    int pdpWidth = POSITION_DETECTION_PATTERN[0].length;

    embedPositionDetectionPattern(0, 0, matrix);

    embedPositionDetectionPattern(matrix.getWidth() - pdpWidth, 0, matrix);

    embedPositionDetectionPattern(0, matrix.getWidth() - pdpWidth, matrix);

    int hspWidth = HORIZONTAL_SEPARATION_PATTERN[0].length;

    embedHorizontalSeparationPattern(0, hspWidth - 1, matrix);

    embedHorizontalSeparationPattern(matrix.getWidth() - hspWidth, hspWidth - 1, matrix);

    embedHorizontalSeparationPattern(0, matrix.getWidth() - hspWidth, matrix);

    int vspSize = VERTICAL_SEPARATION_PATTERN.length;

    embedVerticalSeparationPattern(vspSize, 0, matrix);

    embedVerticalSeparationPattern(matrix.getHeight() - vspSize - 1, 0, matrix);

    embedVerticalSeparationPattern(vspSize, matrix.getHeight() - vspSize, matrix);
  }
Ejemplo n.º 3
0
  // Embed "dataBits" using "getMaskPattern". On success, modify the matrix
  // and return true.
  // For debugging purposes, it skips masking process if "getMaskPattern" is
  // -1.
  // See 8.7 of JISX0510:2004 (p.38) for how to embed data bits.
  static void embedDataBits(BitArray dataBits, int maskPattern, ByteMatrix matrix)
      throws WriterException {
    int bitIndex = 0;
    int direction = -1;
    // Start from the right bottom cell.
    int x = matrix.getWidth() - 1;
    int y = matrix.getHeight() - 1;
    while (x > 0) {
      // Skip the vertical timing pattern.
      if (x == 6) {
        x -= 1;
      }
      while (y >= 0 && y < matrix.getHeight()) {
        for (int i = 0; i < 2; ++i) {
          int xx = x - i;
          // Skip the cell if it's not empty.
          if (!isEmpty(matrix.get(xx, y))) {
            continue;
          }
          boolean bit;
          if (bitIndex < dataBits.getSize()) {
            bit = dataBits.get(bitIndex);
            ++bitIndex;
          } else {
            // Padding bit. If there is no bit left, we'll fill the
            // left cells with 0,
            // as described
            // in 8.4.9 of JISX0510:2004 (p. 24).
            bit = false;
          }

          // Skip masking if mask_pattern is -1.
          if (maskPattern != -1) {
            if (MaskUtil.getDataMaskBit(maskPattern, xx, y)) {
              bit = !bit;
            }
          }
          matrix.set(xx, y, bit);
        }
        y += direction;
      }
      direction = -direction; // Reverse the direction.
      y += direction;
      x -= 2; // Move to the left.
    }
    // All bits should be consumed.
    if (bitIndex != dataBits.getSize()) {
      throw new WriterException("Not all bits consumed: " + bitIndex + '/' + dataBits.getSize());
    }
  }
  static void embedTypeInfo(ErrorCorrectionLevel ecLevel, int maskPattern, ByteMatrix matrix)
      throws WriterException {
    BitArray typeInfoBits = new BitArray();
    makeTypeInfoBits(ecLevel, maskPattern, typeInfoBits);

    for (int i = 0; i < typeInfoBits.getSize(); ++i) {

      boolean bit = typeInfoBits.get(typeInfoBits.getSize() - 1 - i);

      int x1 = TYPE_INFO_COORDINATES[i][0];
      int y1 = TYPE_INFO_COORDINATES[i][1];
      matrix.set(x1, y1, bit);

      if (i < 8) {

        int x2 = matrix.getWidth() - i - 1;
        int y2 = 8;
        matrix.set(x2, y2, bit);
      } else {

        int x2 = 8;
        int y2 = matrix.getHeight() - 7 + (i - 8);
        matrix.set(x2, y2, bit);
      }
    }
  }
Ejemplo n.º 5
0
 // Apply mask penalty rule 4 and return the penalty. Calculate the ratio of
 // dark cells and give
 // penalty if the ratio is far from 50%. It gives 10 penalty for 5%
 // distance. Examples:
 // - 0% => 100
 // - 40% => 20
 // - 45% => 10
 // - 50% => 0
 // - 55% => 10
 // - 55% => 20
 // - 100% => 100
 public static int applyMaskPenaltyRule4(ByteMatrix matrix) {
   int numDarkCells = 0;
   byte[][] array = matrix.getArray();
   int width = matrix.getWidth();
   int height = matrix.getHeight();
   for (int y = 0; y < height; ++y) {
     for (int x = 0; x < width; ++x) {
       if (array[y][x] == 1) {
         numDarkCells += 1;
       }
     }
   }
   int numTotalCells = matrix.getHeight() * matrix.getWidth();
   double darkRatio = (double) numDarkCells / numTotalCells;
   return Math.abs((int) (darkRatio * 100 - 50)) / 5 * 10;
 }
Ejemplo n.º 6
0
  // Embed type information. On success, modify the matrix.
  public static void embedTypeInfo(ErrorCorrectionLevel ecLevel, int maskPattern, ByteMatrix matrix)
      throws WriterException {
    BitArray typeInfoBits = new BitArray();
    makeTypeInfoBits(ecLevel, maskPattern, typeInfoBits);

    for (int i = 0; i < typeInfoBits.getSize(); ++i) {
      // Place bits in LSB to MSB order.  LSB (least significant bit) is the last value in
      // "typeInfoBits".
      boolean bit = typeInfoBits.get(typeInfoBits.getSize() - 1 - i);

      // Type info bits at the left top corner. See 8.9 of JISX0510:2004 (p.46).
      int x1 = TYPE_INFO_COORDINATES[i][0];
      int y1 = TYPE_INFO_COORDINATES[i][1];
      matrix.set(x1, y1, bit);

      if (i < 8) {
        // Right top corner.
        int x2 = matrix.getWidth() - i - 1;
        int y2 = 8;
        matrix.set(x2, y2, bit);
      } else {
        // Left bottom corner.
        int x2 = 8;
        int y2 = matrix.getHeight() - 7 + (i - 8);
        matrix.set(x2, y2, bit);
      }
    }
  }
  static void embedDataBits(BitArray dataBits, int maskPattern, ByteMatrix matrix)
      throws WriterException {
    int bitIndex = 0;
    int direction = -1;

    int x = matrix.getWidth() - 1;
    int y = matrix.getHeight() - 1;
    while (x > 0) {

      if (x == 6) {
        x -= 1;
      }
      while (y >= 0 && y < matrix.getHeight()) {
        for (int i = 0; i < 2; ++i) {
          int xx = x - i;

          if (!isEmpty(matrix.get(xx, y))) {
            continue;
          }
          boolean bit;
          if (bitIndex < dataBits.getSize()) {
            bit = dataBits.get(bitIndex);
            ++bitIndex;
          } else {

            bit = false;
          }

          if (maskPattern != -1) {
            if (MaskUtil.getDataMaskBit(maskPattern, xx, y)) {
              bit = !bit;
            }
          }
          matrix.set(xx, y, bit);
        }
        y += direction;
      }
      direction = -direction;
      y += direction;
      x -= 2;
    }

    if (bitIndex != dataBits.getSize()) {
      throw new WriterException("Not all bits consumed: " + bitIndex + '/' + dataBits.getSize());
    }
  }
  static void maybeEmbedVersionInfo(int version, ByteMatrix matrix) throws WriterException {
    if (version < 7) {
      return;
    }
    BitArray versionInfoBits = new BitArray();
    makeVersionInfoBits(version, versionInfoBits);

    int bitIndex = 6 * 3 - 1;
    for (int i = 0; i < 6; ++i) {
      for (int j = 0; j < 3; ++j) {

        boolean bit = versionInfoBits.get(bitIndex);
        bitIndex--;

        matrix.set(i, matrix.getHeight() - 11 + j, bit);

        matrix.set(matrix.getHeight() - 11 + j, i, bit);
      }
    }
  }
Ejemplo n.º 9
0
  // Embed version information if need be. On success, modify the matrix and return true.
  // See 8.10 of JISX0510:2004 (p.47) for how to embed version information.
  public static void maybeEmbedVersionInfo(int version, ByteMatrix matrix) throws WriterException {
    if (version < 7) { // Version info is necessary if version >= 7.
      return; // Don't need version info.
    }
    BitArray versionInfoBits = new BitArray();
    makeVersionInfoBits(version, versionInfoBits);

    int bitIndex = 6 * 3 - 1; // It will decrease from 17 to 0.
    for (int i = 0; i < 6; ++i) {
      for (int j = 0; j < 3; ++j) {
        // Place bits in LSB (least significant bit) to MSB order.
        boolean bit = versionInfoBits.get(bitIndex);
        bitIndex--;
        // Left bottom corner.
        matrix.set(i, matrix.getHeight() - 11 + j, bit);
        // Right bottom corner.
        matrix.set(matrix.getHeight() - 11 + j, i, bit);
      }
    }
  }
Ejemplo n.º 10
0
 // Apply mask penalty rule 3 and return the penalty. Find consecutive cells
 // of 00001011101 or
 // 10111010000, and give penalty to them. If we find patterns like
 // 000010111010000, we give
 // penalties twice (i.e. 40 * 2).
 public static int applyMaskPenaltyRule3(ByteMatrix matrix) {
   int penalty = 0;
   byte[][] array = matrix.getArray();
   int width = matrix.getWidth();
   int height = matrix.getHeight();
   for (int y = 0; y < height; ++y) {
     for (int x = 0; x < width; ++x) {
       // Tried to simplify following conditions but failed.
       if (x + 6 < width
           && array[y][x] == 1
           && array[y][x + 1] == 0
           && array[y][x + 2] == 1
           && array[y][x + 3] == 1
           && array[y][x + 4] == 1
           && array[y][x + 5] == 0
           && array[y][x + 6] == 1
           && ((x + 10 < width
                   && array[y][x + 7] == 0
                   && array[y][x + 8] == 0
                   && array[y][x + 9] == 0
                   && array[y][x + 10] == 0)
               || (x - 4 >= 0
                   && array[y][x - 1] == 0
                   && array[y][x - 2] == 0
                   && array[y][x - 3] == 0
                   && array[y][x - 4] == 0))) {
         penalty += 40;
       }
       if (y + 6 < height
           && array[y][x] == 1
           && array[y + 1][x] == 0
           && array[y + 2][x] == 1
           && array[y + 3][x] == 1
           && array[y + 4][x] == 1
           && array[y + 5][x] == 0
           && array[y + 6][x] == 1
           && ((y + 10 < height
                   && array[y + 7][x] == 0
                   && array[y + 8][x] == 0
                   && array[y + 9][x] == 0
                   && array[y + 10][x] == 0)
               || (y - 4 >= 0
                   && array[y - 1][x] == 0
                   && array[y - 2][x] == 0
                   && array[y - 3][x] == 0
                   && array[y - 4][x] == 0))) {
         penalty += 40;
       }
     }
   }
   return penalty;
 }
Ejemplo n.º 11
0
 // Helper function for applyMaskPenaltyRule1. We need this for doing this
 // calculation in both
 // vertical and horizontal orders respectively.
 private static int applyMaskPenaltyRule1Internal(ByteMatrix matrix, boolean isHorizontal) {
   int penalty = 0;
   int numSameBitCells = 0;
   int prevBit = -1;
   // Horizontal mode:
   // for (int i = 0; i < matrix.height(); ++i) {
   // for (int j = 0; j < matrix.width(); ++j) {
   // int bit = matrix.get(i, j);
   // Vertical mode:
   // for (int i = 0; i < matrix.width(); ++i) {
   // for (int j = 0; j < matrix.height(); ++j) {
   // int bit = matrix.get(j, i);
   int iLimit = isHorizontal ? matrix.getHeight() : matrix.getWidth();
   int jLimit = isHorizontal ? matrix.getWidth() : matrix.getHeight();
   byte[][] array = matrix.getArray();
   for (int i = 0; i < iLimit; ++i) {
     for (int j = 0; j < jLimit; ++j) {
       int bit = isHorizontal ? array[i][j] : array[j][i];
       if (bit == prevBit) {
         numSameBitCells += 1;
         // Found five repetitive cells with the same color (bit).
         // We'll give penalty of 3.
         if (numSameBitCells == 5) {
           penalty += 3;
         } else if (numSameBitCells > 5) {
           // After five repetitive cells, we'll add the penalty
           // one
           // by one.
           penalty += 1;
         }
       } else {
         numSameBitCells = 1; // Include the cell itself.
         prevBit = bit;
       }
     }
     numSameBitCells = 0; // Clear at each row/column.
   }
   return penalty;
 }
Ejemplo n.º 12
0
 // Apply mask penalty rule 2 and return the penalty. Find 2x2 blocks with
 // the same color and give
 // penalty to them.
 public static int applyMaskPenaltyRule2(ByteMatrix matrix) {
   int penalty = 0;
   byte[][] array = matrix.getArray();
   int width = matrix.getWidth();
   int height = matrix.getHeight();
   for (int y = 0; y < height - 1; ++y) {
     for (int x = 0; x < width - 1; ++x) {
       int value = array[y][x];
       if (value == array[y][x + 1] && value == array[y + 1][x] && value == array[y + 1][x + 1]) {
         penalty += 3;
       }
     }
   }
   return penalty;
 }
Ejemplo n.º 13
0
 public boolean isValid() {
   return a != null
       && b != null
       && c != -1
       && d != -1
       && e != -1
       && f != -1
       && g != -1
       && h != -1
       && i != -1
       && isValidMaskPattern(e)
       && f == g + h
       && j != null
       && d == j.getWidth()
       && j.getWidth() == j.getHeight();
 }
Ejemplo n.º 14
0
  // Note that the input matrix uses 0 == white, 1 == black, while the output matrix uses
  // 0 == black, 255 == white (i.e. an 8 bit greyscale bitmap).
  private static BitMatrix renderResult(QRCode code, int width, int height, int quietZone) {
    ByteMatrix input = code.getMatrix();
    if (input == null) {
      throw new IllegalStateException();
    }
    int inputWidth = input.getWidth();
    int inputHeight = input.getHeight();
    int qrWidth = inputWidth + (quietZone << 1);
    int qrHeight = inputHeight + (quietZone << 1);
    int outputWidth = Math.max(width, qrWidth);
    int outputHeight = Math.max(height, qrHeight);

    int multiple = Math.min(outputWidth / qrWidth, outputHeight / qrHeight);
    // Padding includes both the quiet zone and the extra white pixels to accommodate the requested
    // dimensions. For example, if input is 25x25 the QR will be 33x33 including the quiet zone.
    // If the requested size is 200x160, the multiple will be 4, for a QR of 132x132. These will
    // handle all the padding from 100x100 (the actual QR) up to 200x160.
    int leftPadding = (outputWidth - (inputWidth * multiple)) / 2;
    int topPadding = (outputHeight - (inputHeight * multiple)) / 2;

    BitMatrix output = new BitMatrix(outputWidth, outputHeight);

    for (int inputY = 0, outputY = topPadding;
        inputY < inputHeight;
        inputY++, outputY += multiple) {
      // Write the contents of this row of the barcode
      for (int inputX = 0, outputX = leftPadding;
          inputX < inputWidth;
          inputX++, outputX += multiple) {
        if (input.get(inputX, inputY) == 1) {
          output.setRegion(outputX, outputY, multiple, multiple);
        }
      }
    }

    return output;
  }
 private static void embedDarkDotAtLeftBottomCorner(ByteMatrix matrix) throws WriterException {
   if (matrix.get(8, matrix.getHeight() - 8) == 0) {
     throw new WriterException();
   }
   matrix.set(8, matrix.getHeight() - 8, 1);
 }