Esempio n. 1
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 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);
      }
    }
  }
Esempio n. 3
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;
 }
  private static void embedHorizontalSeparationPattern(int xStart, int yStart, ByteMatrix matrix)
      throws WriterException {

    if (HORIZONTAL_SEPARATION_PATTERN[0].length != 8 || HORIZONTAL_SEPARATION_PATTERN.length != 1) {
      throw new WriterException("Bad horizontal separation pattern");
    }
    for (int x = 0; x < 8; ++x) {
      if (!isEmpty(matrix.get(xStart + x, yStart))) {
        throw new WriterException();
      }
      matrix.set(xStart + x, yStart, HORIZONTAL_SEPARATION_PATTERN[0][x]);
    }
  }
  private static void embedVerticalSeparationPattern(int xStart, int yStart, ByteMatrix matrix)
      throws WriterException {

    if (VERTICAL_SEPARATION_PATTERN[0].length != 1 || VERTICAL_SEPARATION_PATTERN.length != 7) {
      throw new WriterException("Bad vertical separation pattern");
    }
    for (int y = 0; y < 7; ++y) {
      if (!isEmpty(matrix.get(xStart, yStart + y))) {
        throw new WriterException();
      }
      matrix.set(xStart, yStart + y, VERTICAL_SEPARATION_PATTERN[y][0]);
    }
  }
Esempio n. 6
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());
    }
  }
Esempio n. 7
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;
 }
  private static void embedPositionAdjustmentPattern(int xStart, int yStart, ByteMatrix matrix)
      throws WriterException {

    if (POSITION_ADJUSTMENT_PATTERN[0].length != 5 || POSITION_ADJUSTMENT_PATTERN.length != 5) {
      throw new WriterException("Bad position adjustment");
    }
    for (int y = 0; y < 5; ++y) {
      for (int x = 0; x < 5; ++x) {
        if (!isEmpty(matrix.get(xStart + x, yStart + y))) {
          throw new WriterException();
        }
        matrix.set(xStart + x, yStart + y, POSITION_ADJUSTMENT_PATTERN[y][x]);
      }
    }
  }
  private static void embedPositionDetectionPattern(int xStart, int yStart, ByteMatrix matrix)
      throws WriterException {

    if (POSITION_DETECTION_PATTERN[0].length != 7 || POSITION_DETECTION_PATTERN.length != 7) {
      throw new WriterException("Bad position detection pattern");
    }
    for (int y = 0; y < 7; ++y) {
      for (int x = 0; x < 7; ++x) {
        if (!isEmpty(matrix.get(xStart + x, yStart + y))) {
          throw new WriterException();
        }
        matrix.set(xStart + x, yStart + y, POSITION_DETECTION_PATTERN[y][x]);
      }
    }
  }
Esempio n. 10
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;
 }
Esempio n. 11
0
 public String toString() {
   StringBuilder stringbuilder = new StringBuilder(200);
   stringbuilder.append("<<\n");
   stringbuilder.append(" mode: ");
   stringbuilder.append(a);
   stringbuilder.append("\n ecLevel: ");
   stringbuilder.append(b);
   stringbuilder.append("\n version: ");
   stringbuilder.append(c);
   stringbuilder.append("\n matrixWidth: ");
   stringbuilder.append(d);
   stringbuilder.append("\n maskPattern: ");
   stringbuilder.append(e);
   stringbuilder.append("\n numTotalBytes: ");
   stringbuilder.append(f);
   stringbuilder.append("\n numDataBytes: ");
   stringbuilder.append(g);
   stringbuilder.append("\n numECBytes: ");
   stringbuilder.append(h);
   stringbuilder.append("\n numRSBlocks: ");
   stringbuilder.append(i);
   if (j == null) {
     stringbuilder.append("\n matrix: null\n");
   } else {
     stringbuilder.append("\n matrix:\n");
     stringbuilder.append(j.toString());
   }
   stringbuilder.append(">>\n");
   return stringbuilder.toString();
 }
Esempio n. 12
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();
 }
Esempio n. 13
0
 // Embed position adjustment patterns if need be.
 private static void maybeEmbedPositionAdjustmentPatterns(int version, ByteMatrix matrix)
     throws WriterException {
   if (version < 2) { // The patterns appear if version >= 2
     return;
   }
   int index = version - 1;
   int[] coordinates = POSITION_ADJUSTMENT_PATTERN_COORDINATE_TABLE[index];
   int numCoordinates = POSITION_ADJUSTMENT_PATTERN_COORDINATE_TABLE[index].length;
   for (int i = 0; i < numCoordinates; ++i) {
     for (int j = 0; j < numCoordinates; ++j) {
       int y = coordinates[i];
       int x = coordinates[j];
       if (x == -1 || y == -1) {
         continue;
       }
       // If the cell is unset, we embed the position adjustment
       // pattern here.
       if (isEmpty(matrix.get(x, y))) {
         // -2 is necessary since the x/y coordinates point to the
         // center of the pattern,
         // not the
         // left top corner.
         embedPositionAdjustmentPattern(x - 2, y - 2, matrix);
       }
     }
   }
 }
  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());
    }
  }
Esempio n. 15
0
 public int at(int k, int l) {
   byte byte0 = j.get(k, l);
   if (byte0 != 0 && byte0 != 1) {
     throw new IllegalStateException("Bad value");
   } else {
     return byte0;
   }
 }
Esempio n. 16
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);
      }
    }
  }
  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);
      }
    }
  }
  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);
  }
Esempio n. 19
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);
  }
Esempio n. 20
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;
 }
Esempio n. 21
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 embedTimingPatterns(ByteMatrix matrix) throws WriterException {

    for (int i = 8; i < matrix.getWidth() - 8; ++i) {
      int bit = (i + 1) % 2;

      if (!isValidValue(matrix.get(i, 6))) {
        throw new WriterException();
      }
      if (isEmpty(matrix.get(i, 6))) {
        matrix.set(i, 6, bit);
      }

      if (!isValidValue(matrix.get(6, i))) {
        throw new WriterException();
      }
      if (isEmpty(matrix.get(6, i))) {
        matrix.set(6, i, bit);
      }
    }
  }
Esempio n. 23
0
 private static void embedTimingPatterns(ByteMatrix matrix) throws WriterException {
   // -8 is for skipping position detection patterns (size 7), and two horizontal/vertical
   // separation patterns (size 1). Thus, 8 = 7 + 1.
   for (int i = 8; i < matrix.getWidth() - 8; ++i) {
     int bit = (i + 1) % 2;
     // Horizontal line.
     if (!isValidValue(matrix.get(i, 6))) {
       throw new WriterException();
     }
     if (isEmpty(matrix.get(i, 6))) {
       matrix.set(i, 6, bit);
     }
     // Vertical line.
     if (!isValidValue(matrix.get(6, i))) {
       throw new WriterException();
     }
     if (isEmpty(matrix.get(6, i))) {
       matrix.set(6, i, bit);
     }
   }
 }
  private static void maybeEmbedPositionAdjustmentPatterns(int version, ByteMatrix matrix)
      throws WriterException {
    if (version < 2) {
      return;
    }
    int index = version - 1;
    int[] coordinates = POSITION_ADJUSTMENT_PATTERN_COORDINATE_TABLE[index];
    int numCoordinates = POSITION_ADJUSTMENT_PATTERN_COORDINATE_TABLE[index].length;
    for (int i = 0; i < numCoordinates; ++i) {
      for (int j = 0; j < numCoordinates; ++j) {
        int y = coordinates[i];
        int x = coordinates[j];
        if (x == -1 || y == -1) {
          continue;
        }

        if (isEmpty(matrix.get(x, y))) {

          embedPositionAdjustmentPattern(x - 2, y - 2, matrix);
        }
      }
    }
  }
 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);
 }
 static void clearMatrix(ByteMatrix matrix) {
   matrix.clear((byte) -1);
 }