Beispiel #1
0
  @Test
  public void testXYRC() {
    BitMatrix bm = new BitMatrix(4, 4);

    bm.setRC(1, 2, true);
    Assert.assertTrue(bm.getXY(2, 2));
  }
Beispiel #2
0
  @Test
  public void testSetGet() {
    BitMatrix bm = new BitMatrix(4, 3);

    bm.setRC(1, 2, true);
    Assert.assertTrue(bm.getRC(1, 2));
    bm.setXY(2, 1, true);
    Assert.assertTrue(bm.getXY(2, 1));
  }
Beispiel #3
0
  @Test
  public void testNotEmpty() {
    BitMatrix bm = new BitMatrix(4, 3);

    Assert.assertFalse(bm.isEmpty());
    Assert.assertTrue(bm.getWidth() == 4);
    Assert.assertTrue(bm.getHeight() == 3);
    Assert.assertTrue(bm.getSize() == 12);
  }
Beispiel #4
0
  @Test
  public void testEmpty() {
    BitMatrix bm = new BitMatrix();

    Assert.assertTrue(bm.isEmpty());
    Assert.assertTrue(bm.getWidth() == 0);
    Assert.assertTrue(bm.getHeight() == 0);
    Assert.assertTrue(bm.getSize() == 0);
  }
 /** Applies a single threshold to a block of pixels. */
 private static void thresholdBlock(
     byte[] luminances, int xoffset, int yoffset, int threshold, int stride, BitMatrix matrix) {
   for (int y = 0, offset = yoffset * stride + xoffset; y < BLOCK_SIZE; y++, offset += stride) {
     for (int x = 0; x < BLOCK_SIZE; x++) {
       // Comparison needs to be <= so that black == 0 pixels are black even if the threshold is 0.
       if ((luminances[offset + x] & 0xFF) <= threshold) {
         matrix.set(xoffset + x, yoffset + y);
       }
     }
   }
 }
Beispiel #6
0
  @Test
  public void testRealloc() {
    BitMatrix bm = new BitMatrix(4, 4);

    Assert.assertFalse(bm.resize(16, 1));
    Assert.assertTrue(bm.getWidth() == 16);
    Assert.assertTrue(bm.getHeight() == 1);
    Assert.assertTrue(bm.resize(8, 3));
    Assert.assertTrue(bm.getWidth() == 8);
    Assert.assertTrue(bm.getHeight() == 3);
  }
  // Does not sharpen the data, as this call is intended to only be used by 2D Readers.
  @Override
  public BitMatrix getBlackMatrix() throws NotFoundException {
    LuminanceSource source = getLuminanceSource();
    int width = source.getWidth();
    int height = source.getHeight();
    BitMatrix matrix = new BitMatrix(width, height);

    // Quickly calculates the histogram by sampling four rows from the image. This proved to be
    // more robust on the blackbox tests than sampling a diagonal as we used to do.
    initArrays(width);
    int[] localBuckets = buckets;
    for (int y = 1; y < 5; y++) {
      int row = height * y / 5;
      byte[] localLuminances = source.getRow(row, luminances);
      int right = (width * 4) / 5;
      for (int x = width / 5; x < right; x++) {
        int pixel = localLuminances[x] & 0xff;
        localBuckets[pixel >> LUMINANCE_SHIFT]++;
      }
    }
    int blackPoint = estimateBlackPoint(localBuckets);

    // We delay reading the entire image luminance until the black point estimation succeeds.
    // Although we end up reading four rows twice, it is consistent with our motto of
    // "fail quickly" which is necessary for continuous scanning.
    byte[] localLuminances = source.getMatrix();
    for (int y = 0; y < height; y++) {
      int offset = y * width;
      for (int x = 0; x < width; x++) {
        int pixel = localLuminances[offset + x] & 0xff;
        if (pixel < blackPoint) {
          matrix.set(x, y);
        }
      }
    }

    return matrix;
  }
Beispiel #8
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;
  }
Beispiel #9
0
  @Test
  public void testClip() {
    BitMatrix bm = new BitMatrix(2, 4);

    Assert.assertTrue(bm.clipX(-1) == 0);
    Assert.assertTrue(bm.clipX(1) == 1);
    Assert.assertTrue(bm.clipX(3) == 1);

    Assert.assertTrue(bm.clipC(-1) == 0);
    Assert.assertTrue(bm.clipC(1) == 1);
    Assert.assertTrue(bm.clipC(3) == 1);

    Assert.assertTrue(bm.clipY(-1) == 0);
    Assert.assertTrue(bm.clipY(4) == 3);
    Assert.assertTrue(bm.clipY(1) == 1);

    Assert.assertTrue(bm.clipR(-1) == 0);
    Assert.assertTrue(bm.clipR(4) == 3);
    Assert.assertTrue(bm.clipR(1) == 1);
  }
Beispiel #10
0
  @Test
  public void testIsInside() {
    BitMatrix bm = new BitMatrix(2, 4);

    Assert.assertTrue(bm.isInsideRC(1, 1));
    Assert.assertFalse(bm.isInsideRC(-1, 1));
    Assert.assertFalse(bm.isInsideRC(1, -1));
    Assert.assertFalse(bm.isInsideRC(2, 2));
    Assert.assertFalse(bm.isInsideRC(1, 4));

    Assert.assertTrue(bm.isInsideXY(1, 1));
    Assert.assertFalse(bm.isInsideXY(-1, 1));
    Assert.assertFalse(bm.isInsideXY(1, -1));
    Assert.assertFalse(bm.isInsideXY(2, 2));
    Assert.assertFalse(bm.isInsideXY(1, 4));
  }
  public void testGeneral() {
    BitMatrix blah = new BitMatrix(5000);
    assertEquals(0, BigMat.mostOnes(blah));

    blah.set(0, 0, 1);
    assertEquals(0, BigMat.mostOnes(blah));

    blah.set(5, 1, 1);
    assertEquals(5, BigMat.mostOnes(blah));

    blah.set(7, 455, 1);
    assertEquals(7, BigMat.mostOnes(blah));

    blah.set(466, 460, 1);
    assertEquals(466, BigMat.mostOnes(blah));

    blah.set(468, 460, 1);
    assertEquals(466, BigMat.mostOnes(blah));

    blah.set(470, 460, 1);
    assertEquals(466, BigMat.mostOnes(blah));
  }
Beispiel #12
0
  /**
   * This method detects a Data Matrix code in a "pure" image -- that is, pure monochrome image
   * which contains only an unrotated, unskewed, image of a Data Matrix code, with some white border
   * around it. This is a specialized method that works exceptionally fast in this special case.
   */
  private static BitMatrix extractPureBits(BitMatrix image) throws NotFoundException {
    // Now need to determine module size in pixels

    int height = image.getHeight();
    int width = image.getWidth();
    int minDimension = Math.min(height, width);

    // First, skip white border by tracking diagonally from the top left down and to the right:
    int borderWidth = 0;
    while (borderWidth < minDimension && !image.get(borderWidth, borderWidth)) {
      borderWidth++;
    }
    if (borderWidth == minDimension) {
      throw NotFoundException.getNotFoundInstance();
    }

    // And then keep tracking across the top-left black module to determine module size
    int moduleEnd = borderWidth + 1;
    while (moduleEnd < width && image.get(moduleEnd, borderWidth)) {
      moduleEnd++;
    }
    if (moduleEnd == width) {
      throw NotFoundException.getNotFoundInstance();
    }

    int moduleSize = moduleEnd - borderWidth;

    // And now find where the bottommost black module on the first column ends
    int columnEndOfSymbol = height - 1;
    while (columnEndOfSymbol >= 0 && !image.get(borderWidth, columnEndOfSymbol)) {
      columnEndOfSymbol--;
    }
    if (columnEndOfSymbol < 0) {
      throw NotFoundException.getNotFoundInstance();
    }
    columnEndOfSymbol++;

    // Make sure width of barcode is a multiple of module size
    if ((columnEndOfSymbol - borderWidth) % moduleSize != 0) {
      throw NotFoundException.getNotFoundInstance();
    }
    int dimension = (columnEndOfSymbol - borderWidth) / moduleSize;

    // Push in the "border" by half the module width so that we start
    // sampling in the middle of the module. Just in case the image is a
    // little off, this will help recover.
    borderWidth += moduleSize >> 1;

    int sampleDimension = borderWidth + (dimension - 1) * moduleSize;
    if (sampleDimension >= width || sampleDimension >= height) {
      throw NotFoundException.getNotFoundInstance();
    }

    // Now just read off the bits
    BitMatrix bits = new BitMatrix(dimension);
    for (int i = 0; i < dimension; i++) {
      int iOffset = borderWidth + i * moduleSize;
      for (int j = 0; j < dimension; j++) {
        if (image.get(borderWidth + j * moduleSize, iOffset)) {
          bits.set(j, i);
        }
      }
    }
    return bits;
  }