Java源码示例:com.google.zxing.common.detector.MathUtils

示例1
/**
 * <p>Computes the dimension (number of modules on a size) of the QR Code based on the position
 * of the finder patterns and estimated module size.</p>
 */
private static int computeDimension(ResultPoint topLeft,
                                    ResultPoint topRight,
                                    ResultPoint bottomLeft,
                                    float moduleSize) throws NotFoundException {
  int tltrCentersDimension = MathUtils.round(ResultPoint.distance(topLeft, topRight) / moduleSize);
  int tlblCentersDimension = MathUtils.round(ResultPoint.distance(topLeft, bottomLeft) / moduleSize);
  int dimension = ((tltrCentersDimension + tlblCentersDimension) / 2) + 7;
  switch (dimension & 0x03) { // mod 4
    case 0:
      dimension++;
      break;
      // 1? do nothing
    case 2:
      dimension--;
      break;
    case 3:
      throw NotFoundException.getNotFoundInstance();
  }
  return dimension;
}
 
示例2
/**
 * <p>Computes the dimension (number of modules on a size) of the QR Code based on the position
 * of the finder patterns and estimated module size.</p>
 */
private static int computeDimension(ResultPoint topLeft,
                                    ResultPoint topRight,
                                    ResultPoint bottomLeft,
                                    float moduleSize) throws NotFoundException {
  int tltrCentersDimension = MathUtils.round(ResultPoint.distance(topLeft, topRight) / moduleSize);
  int tlblCentersDimension = MathUtils.round(ResultPoint.distance(topLeft, bottomLeft) / moduleSize);
  int dimension = ((tltrCentersDimension + tlblCentersDimension) / 2) + 7;
  switch (dimension & 0x03) { // mod 4
    case 0:
      dimension++;
      break;
      // 1? do nothing
    case 2:
      dimension--;
      break;
    case 3:
      throw NotFoundException.getNotFoundInstance();
  }
  return dimension;
}
 
示例3
/**
 * <p>Computes the dimension (number of modules on a size) of the QR Code based on the position
 * of the finder patterns and estimated module size.</p>
 */
private static int computeDimension(ResultPoint topLeft,
                                    ResultPoint topRight,
                                    ResultPoint bottomLeft,
                                    float moduleSize) throws NotFoundException {
  int tltrCentersDimension = MathUtils.round(ResultPoint.distance(topLeft, topRight) / moduleSize);
  int tlblCentersDimension = MathUtils.round(ResultPoint.distance(topLeft, bottomLeft) / moduleSize);
  int dimension = ((tltrCentersDimension + tlblCentersDimension) / 2) + 7;
  switch (dimension & 0x03) { // mod 4
    case 0:
      dimension++;
      break;
      // 1? do nothing
    case 2:
      dimension--;
      break;
    case 3:
      throw NotFoundException.getNotFoundInstance();
  }
  return dimension;
}
 
示例4
/**
 * <p>Computes the dimension (number of modules on a size) of the QR Code based on the position
 * of the finder patterns and estimated module size.</p>
 */
private static int computeDimension(ResultPoint topLeft,
                                    ResultPoint topRight,
                                    ResultPoint bottomLeft,
                                    float moduleSize) throws NotFoundException {
  int tltrCentersDimension = MathUtils.round(ResultPoint.distance(topLeft, topRight) / moduleSize);
  int tlblCentersDimension = MathUtils.round(ResultPoint.distance(topLeft, bottomLeft) / moduleSize);
  int dimension = ((tltrCentersDimension + tlblCentersDimension) / 2) + 7;
  switch (dimension & 0x03) { // mod 4
    case 0:
      dimension++;
      break;
      // 1? do nothing
    case 2:
      dimension--;
      break;
    case 3:
      throw NotFoundException.getNotFoundInstance();
  }
  return dimension;
}
 
示例5
/**
 * Samples a line.
 *
 * @param p1   start point (inclusive)
 * @param p2   end point (exclusive)
 * @param size number of bits
 * @return the array of bits as an int (first bit is high-order bit of result)
 */
private int sampleLine(ResultPoint p1, ResultPoint p2, int size) {
  int result = 0;

  float d = distance(p1, p2);
  float moduleSize = d / size;
  float px = p1.getX();
  float py = p1.getY();
  float dx = moduleSize * (p2.getX() - p1.getX()) / d;
  float dy = moduleSize * (p2.getY() - p1.getY()) / d;
  for (int i = 0; i < size; i++) {
    if (image.get(MathUtils.round(px + i * dx), MathUtils.round(py + i * dy))) {
      result |= 1 << (size - i - 1);
    }
  }
  return result;
}
 
示例6
/**
 * <p>Computes the dimension (number of modules on a size) of the QR Code based on the position
 * of the finder patterns and estimated module size.</p>
 */
private static int computeDimension(ResultPoint topLeft,
                                    ResultPoint topRight,
                                    ResultPoint bottomLeft,
                                    float moduleSize) throws NotFoundException {
  int tltrCentersDimension = MathUtils.round(ResultPoint.distance(topLeft, topRight) / moduleSize);
  int tlblCentersDimension = MathUtils.round(ResultPoint.distance(topLeft, bottomLeft) / moduleSize);
  int dimension = ((tltrCentersDimension + tlblCentersDimension) / 2) + 7;
  switch (dimension & 0x03) { // mod 4
    case 0:
      dimension++;
      break;
      // 1? do nothing
    case 2:
      dimension--;
      break;
    case 3:
      throw NotFoundException.getNotFoundInstance();
  }
  return dimension;
}
 
示例7
/**
 * Samples a line.
 *
 * @param p1   start point (inclusive)
 * @param p2   end point (exclusive)
 * @param size number of bits
 * @return the array of bits as an int (first bit is high-order bit of result)
 */
private int sampleLine(ResultPoint p1, ResultPoint p2, int size) {
  int result = 0;

  float d = distance(p1, p2);
  float moduleSize = d / size;
  float px = p1.getX();
  float py = p1.getY();
  float dx = moduleSize * (p2.getX() - p1.getX()) / d;
  float dy = moduleSize * (p2.getY() - p1.getY()) / d;
  for (int i = 0; i < size; i++) {
    if (image.get(MathUtils.round(px + i * dx), MathUtils.round(py + i * dy))) {
      result |= 1 << (size - i - 1);
    }
  }
  return result;
}
 
示例8
/**
 * <p>Computes the dimension (number of modules on a size) of the QR Code based on the position
 * of the finder patterns and estimated module size.</p>
 */
private static int computeDimension(ResultPoint topLeft,
                                    ResultPoint topRight,
                                    ResultPoint bottomLeft,
                                    float moduleSize) throws NotFoundException {
  int tltrCentersDimension = MathUtils.round(ResultPoint.distance(topLeft, topRight) / moduleSize);
  int tlblCentersDimension = MathUtils.round(ResultPoint.distance(topLeft, bottomLeft) / moduleSize);
  int dimension = ((tltrCentersDimension + tlblCentersDimension) / 2) + 7;
  switch (dimension & 0x03) { // mod 4
    case 0:
      dimension++;
      break;
      // 1? do nothing
    case 2:
      dimension--;
      break;
    case 3:
      throw NotFoundException.getNotFoundInstance();
  }
  return dimension;
}
 
示例9
/**
 * Samples a line.
 *
 * @param p1   start point (inclusive)
 * @param p2   end point (exclusive)
 * @param size number of bits
 * @return the array of bits as an int (first bit is high-order bit of result)
 */
private int sampleLine(ResultPoint p1, ResultPoint p2, int size) {
  int result = 0;

  float d = distance(p1, p2);
  float moduleSize = d / size;
  float px = p1.getX();
  float py = p1.getY();
  float dx = moduleSize * (p2.getX() - p1.getX()) / d;
  float dy = moduleSize * (p2.getY() - p1.getY()) / d;
  for (int i = 0; i < size; i++) {
    if (image.get(MathUtils.round(px + i * dx), MathUtils.round(py + i * dy))) {
      result |= 1 << (size - i - 1);
    }
  }
  return result;
}
 
示例10
private static int[] sampleBitCounts(int[] moduleBitCount) {
  float bitCountSum = MathUtils.sum(moduleBitCount);
  int[] result = new int[PDF417Common.BARS_IN_MODULE];
  int bitCountIndex = 0;
  int sumPreviousBits = 0;
  for (int i = 0; i < PDF417Common.MODULES_IN_CODEWORD; i++) {
    float sampleIndex = 
        bitCountSum / (2 * PDF417Common.MODULES_IN_CODEWORD) + 
        (i * bitCountSum) / PDF417Common.MODULES_IN_CODEWORD;
    if (sumPreviousBits + moduleBitCount[bitCountIndex] <= sampleIndex) {
      sumPreviousBits += moduleBitCount[bitCountIndex];
      bitCountIndex++;
    }
    result[bitCountIndex]++;
  }
  return result;
}
 
示例11
private static int getClosestDecodedValue(int[] moduleBitCount) {
  int bitCountSum = MathUtils.sum(moduleBitCount);
  float[] bitCountRatios = new float[PDF417Common.BARS_IN_MODULE];
  for (int i = 0; i < bitCountRatios.length; i++) {
    bitCountRatios[i] = moduleBitCount[i] / (float) bitCountSum;
  }
  float bestMatchError = Float.MAX_VALUE;
  int bestMatch = -1;
  for (int j = 0; j < RATIOS_TABLE.length; j++) {
    float error = 0.0f;
    float[] ratioTableRow = RATIOS_TABLE[j];
    for (int k = 0; k < PDF417Common.BARS_IN_MODULE; k++) {
      float diff = ratioTableRow[k] - bitCountRatios[k];
      error += diff * diff;
      if (error >= bestMatchError) {
        break;
      }
    }
    if (error < bestMatchError) {
      bestMatchError = error;
      bestMatch = PDF417Common.SYMBOL_TABLE[j];
    }
  }
  return bestMatch;
}
 
示例12
/**
 * <p>Computes the dimension (number of modules on a size) of the QR Code based on the position
 * of the finder patterns and estimated module size.</p>
 */
private static int computeDimension(ResultPoint topLeft,
                                    ResultPoint topRight,
                                    ResultPoint bottomLeft,
                                    float moduleSize) throws NotFoundException {
  int tltrCentersDimension = MathUtils.round(ResultPoint.distance(topLeft, topRight) / moduleSize);
  int tlblCentersDimension = MathUtils.round(ResultPoint.distance(topLeft, bottomLeft) / moduleSize);
  int dimension = ((tltrCentersDimension + tlblCentersDimension) / 2) + 7;
  switch (dimension & 0x03) { // mod 4
    case 0:
      dimension++;
      break;
      // 1? do nothing
    case 2:
      dimension--;
      break;
    case 3:
      throw NotFoundException.getNotFoundInstance();
  }
  return dimension;
}
 
示例13
/**
 * Samples a line.
 *
 * @param p1   start point (inclusive)
 * @param p2   end point (exclusive)
 * @param size number of bits
 * @return the array of bits as an int (first bit is high-order bit of result)
 */
private int sampleLine(ResultPoint p1, ResultPoint p2, int size) {
  int result = 0;

  float d = distance(p1, p2);
  float moduleSize = d / size;
  float px = p1.getX();
  float py = p1.getY();
  float dx = moduleSize * (p2.getX() - p1.getX()) / d;
  float dy = moduleSize * (p2.getY() - p1.getY()) / d;
  for (int i = 0; i < size; i++) {
    if (image.get(MathUtils.round(px + i * dx), MathUtils.round(py + i * dy))) {
      result |= 1 << (size - i - 1);
    }
  }
  return result;
}
 
示例14
/**
 * <p>Computes the dimension (number of modules on a size) of the QR Code based on the position
 * of the finder patterns and estimated module size.</p>
 */
private static int computeDimension(ResultPoint topLeft,
                                    ResultPoint topRight,
                                    ResultPoint bottomLeft,
                                    float moduleSize) throws NotFoundException {
  int tltrCentersDimension = MathUtils.round(ResultPoint.distance(topLeft, topRight) / moduleSize);
  int tlblCentersDimension = MathUtils.round(ResultPoint.distance(topLeft, bottomLeft) / moduleSize);
  int dimension = ((tltrCentersDimension + tlblCentersDimension) >> 1) + 7;
  switch (dimension & 0x03) { // mod 4
    case 0:
      dimension++;
      break;
      // 1? do nothing
    case 2:
      dimension--;
      break;
    case 3:
      throw NotFoundException.getNotFoundInstance();
  }
  return dimension;
}
 
示例15
/**
 *
 * Samples a line
 *
 * @param p1 first point
 * @param p2 second point
 * @param size number of bits
 * @return the array of bits
 */
private boolean[] sampleLine(Point p1, Point p2, int size) {

  boolean[] res = new boolean[size];
  float d = distance(p1,p2);
  float moduleSize = d/(size-1);
  float dx = moduleSize*(p2.x - p1.x)/d;
  float dy = moduleSize*(p2.y - p1.y)/d;

  float px = p1.x;
  float py = p1.y;

  for (int i = 0; i < size; i++) {
    res[i] = image.get(MathUtils.round(px), MathUtils.round(py));
    px+=dx;
    py+=dy;
  }

  return res;
}
 
示例16
/**
 * <p>
 * Computes the dimension (number of modules on a size) of the QR Code based
 * on the position of the finder patterns and estimated module size.
 * </p>
 */
private static int computeDimension(ResultPoint topLeft, ResultPoint topRight, ResultPoint bottomLeft,
		float moduleSize) throws NotFoundException {
	int tltrCentersDimension = MathUtils.round(ResultPoint.distance(topLeft, topRight) / moduleSize);
	int tlblCentersDimension = MathUtils.round(ResultPoint.distance(topLeft, bottomLeft) / moduleSize);
	int dimension = ((tltrCentersDimension + tlblCentersDimension) / 2) + 7;
	switch (dimension & 0x03) { // mod 4
	case 0:
		dimension++;
		break;
	// 1? do nothing
	case 2:
		dimension--;
		break;
	case 3:
		throw NotFoundException.getNotFoundInstance();
	}
	return dimension;
}
 
示例17
/**
 * <p>Computes the dimension (number of modules on a size) of the QR Code based on the position
 * of the finder patterns and estimated module size.</p>
 */
private static int computeDimension(ResultPoint topLeft,
                                    ResultPoint topRight,
                                    ResultPoint bottomLeft,
                                    float moduleSize) throws NotFoundException {
  int tltrCentersDimension = MathUtils.round(ResultPoint.distance(topLeft, topRight) / moduleSize);
  int tlblCentersDimension = MathUtils.round(ResultPoint.distance(topLeft, bottomLeft) / moduleSize);
  int dimension = ((tltrCentersDimension + tlblCentersDimension) / 2) + 7;
  switch (dimension & 0x03) { // mod 4
    case 0:
      dimension++;
      break;
      // 1? do nothing
    case 2:
      dimension--;
      break;
    case 3:
      dimension += 2;
      break;
  }
  return dimension;
}
 
示例18
/**
 * <p>Computes the dimension (number of modules on a size) of the QR Code based on the position
 * of the finder patterns and estimated module size.</p>
 */
private static int computeDimension(ResultPoint topLeft,
                                    ResultPoint topRight,
                                    ResultPoint bottomLeft,
                                    float moduleSize) throws NotFoundException {
  int tltrCentersDimension = MathUtils.round(ResultPoint.distance(topLeft, topRight) / moduleSize);
  int tlblCentersDimension = MathUtils.round(ResultPoint.distance(topLeft, bottomLeft) / moduleSize);
  int dimension = ((tltrCentersDimension + tlblCentersDimension) / 2) + 7;
  switch (dimension & 0x03) { // mod 4
    case 0:
      dimension++;
      break;
      // 1? do nothing
    case 2:
      dimension--;
      break;
    case 3:
      dimension += 2;
      break;
  }
  return dimension;
}
 
示例19
/**
 * Gets the color of a segment
 *
 * @return 1 if segment more than 90% black, -1 if segment is more than 90% white, 0 else
 */
private int getColor(Point p1, Point p2) {
  float d = distance(p1, p2);
  float dx = (p2.getX() - p1.getX()) / d;
  float dy = (p2.getY() - p1.getY()) / d;
  int error = 0;

  float px = p1.getX();
  float py = p1.getY();

  boolean colorModel = image.get(p1.getX(), p1.getY());

  for (int i = 0; i < d; i++) {
    px += dx;
    py += dy;
    if (image.get(MathUtils.round(px), MathUtils.round(py)) != colorModel) {
      error++;
    }
  }

  float errRatio = error / d;

  if (errRatio > 0.1f && errRatio < 0.9f) {
    return 0;
  }

  return (errRatio <= 0.1f) == colorModel ? 1 : -1;
}
 
示例20
/**
 * Gets the color of a segment
 *
 * @return 1 if segment more than 90% black, -1 if segment is more than 90% white, 0 else
 */
private int getColor(Point p1, Point p2) {
  float d = distance(p1, p2);
  float dx = (p2.getX() - p1.getX()) / d;
  float dy = (p2.getY() - p1.getY()) / d;
  int error = 0;

  float px = p1.getX();
  float py = p1.getY();

  boolean colorModel = image.get(p1.getX(), p1.getY());

  for (int i = 0; i < d; i++) {
    px += dx;
    py += dy;
    if (image.get(MathUtils.round(px), MathUtils.round(py)) != colorModel) {
      error++;
    }
  }

  float errRatio = error / d;

  if (errRatio > 0.1f && errRatio < 0.9f) {
    return 0;
  }

  return (errRatio <= 0.1f) == colorModel ? 1 : -1;
}
 
示例21
/**
 * Gets the color of a segment
 *
 * @return 1 if segment more than 90% black, -1 if segment is more than 90% white, 0 else
 */
private int getColor(Point p1, Point p2) {
  float d = distance(p1, p2);
  float dx = (p2.getX() - p1.getX()) / d;
  float dy = (p2.getY() - p1.getY()) / d;
  int error = 0;

  float px = p1.getX();
  float py = p1.getY();

  boolean colorModel = image.get(p1.getX(), p1.getY());

  for (int i = 0; i < d; i++) {
    px += dx;
    py += dy;
    if (image.get(MathUtils.round(px), MathUtils.round(py)) != colorModel) {
      error++;
    }
  }

  float errRatio = error / d;

  if (errRatio > 0.1f && errRatio < 0.9f) {
    return 0;
  }

  return (errRatio <= 0.1f) == colorModel ? 1 : -1;
}
 
示例22
/**
 * Gets the color of a segment
 *
 * @return 1 if segment more than 90% black, -1 if segment is more than 90% white, 0 else
 */
private int getColor(Point p1, Point p2) {
  float d = distance(p1, p2);
  float dx = (p2.getX() - p1.getX()) / d;
  float dy = (p2.getY() - p1.getY()) / d;
  int error = 0;

  float px = p1.getX();
  float py = p1.getY();

  boolean colorModel = image.get(p1.getX(), p1.getY());

  int iMax = (int) Math.ceil(d);
  for (int i = 0; i < iMax; i++) {
    px += dx;
    py += dy;
    if (image.get(MathUtils.round(px), MathUtils.round(py)) != colorModel) {
      error++;
    }
  }

  float errRatio = error / d;

  if (errRatio > 0.1f && errRatio < 0.9f) {
    return 0;
  }

  return (errRatio <= 0.1f) == colorModel ? 1 : -1;
}
 
示例23
/**
 *
 * <p>Gets the Aztec code corners from the bull's eye corners and the parameters </p>
 *
 * @param bullEyeCornerPoints the array of bull's eye corners
 * @return the array of aztec code corners
 * @throws NotFoundException if the corner points do not fit in the image
 */
private ResultPoint[] getMatrixCornerPoints(Point[] bullEyeCornerPoints) throws NotFoundException {

  float ratio = (2 * nbLayers + (nbLayers > 4 ? 1 : 0) + (nbLayers - 4) / 8)
      / (2.0f * nbCenterLayers);

  int dx = bullEyeCornerPoints[0].x-bullEyeCornerPoints[2].x;
  dx+=dx>0?1:-1;
  int dy = bullEyeCornerPoints[0].y-bullEyeCornerPoints[2].y;
  dy+=dy>0?1:-1;
  
  int targetcx = MathUtils.round(bullEyeCornerPoints[2].x - ratio * dx);
  int targetcy = MathUtils.round(bullEyeCornerPoints[2].y - ratio * dy);
  
  int targetax = MathUtils.round(bullEyeCornerPoints[0].x + ratio * dx);
  int targetay = MathUtils.round(bullEyeCornerPoints[0].y + ratio * dy);
  
  dx = bullEyeCornerPoints[1].x-bullEyeCornerPoints[3].x;
  dx+=dx>0?1:-1;
  dy = bullEyeCornerPoints[1].y-bullEyeCornerPoints[3].y;
  dy+=dy>0?1:-1;
  
  int targetdx = MathUtils.round(bullEyeCornerPoints[3].x - ratio * dx);
  int targetdy = MathUtils.round(bullEyeCornerPoints[3].y - ratio * dy);
  int targetbx = MathUtils.round(bullEyeCornerPoints[1].x + ratio * dx);
  int targetby = MathUtils.round(bullEyeCornerPoints[1].y+ratio*dy);
  
  if (!isValid(targetax, targetay) || !isValid(targetbx, targetby) || !isValid(targetcx, targetcy) || !isValid(targetdx, targetdy)) {
    throw NotFoundException.getNotFoundInstance();
  }
  
  return new ResultPoint[]{new ResultPoint(targetax, targetay), new ResultPoint(targetbx, targetby), new ResultPoint(targetcx, targetcy), new ResultPoint(targetdx, targetdy)}; 
}
 
示例24
/**
 * Gets the color of a segment
 *
 * @return 1 if segment more than 90% black, -1 if segment is more than 90% white, 0 else
 */
private int getColor(Point p1, Point p2) {
  float d = distance(p1,p2);
  float dx = (p2.x - p1.x)/d;
  float dy = (p2.y - p1.y)/d;
  int error = 0;

  float px = p1.x;
  float py = p1.y;

  boolean colorModel = image.get(p1.x, p1.y);

  for (int i = 0; i < d; i++) {
    px+=dx;
    py+=dy;
    if (image.get(MathUtils.round(px), MathUtils.round(py)) != colorModel) {
      error++;
    }
  }

  float errRatio = (float)error/d;

  if (errRatio > 0.1 && errRatio < 0.9) {
    return 0;
  }

  if (errRatio <= 0.1) {
    return colorModel?1:-1;
  } else {
    return colorModel?-1:1;
  }
}
 
示例25
/**
 * <p>This method traces a line from a point in the image, in the direction towards another point.
 * It begins in a black region, and keeps going until it finds white, then black, then white again.
 * It reports the distance from the start to this point.</p>
 *
 * <p>This is used when figuring out how wide a finder pattern is, when the finder pattern
 * may be skewed or rotated.</p>
 */
private float sizeOfBlackWhiteBlackRun(int fromX, int fromY, int toX, int toY) {
  // Mild variant of Bresenham's algorithm;
  // see http://en.wikipedia.org/wiki/Bresenham's_line_algorithm
  boolean steep = Math.abs(toY - fromY) > Math.abs(toX - fromX);
  if (steep) {
    int temp = fromX;
    fromX = fromY;
    fromY = temp;
    temp = toX;
    toX = toY;
    toY = temp;
  }

  int dx = Math.abs(toX - fromX);
  int dy = Math.abs(toY - fromY);
  int error = -dx / 2;
  int xstep = fromX < toX ? 1 : -1;
  int ystep = fromY < toY ? 1 : -1;

  // In black pixels, looking for white, first or second time.
  int state = 0;
  // Loop up until x == toX, but not beyond
  int xLimit = toX + xstep;
  for (int x = fromX, y = fromY; x != xLimit; x += xstep) {
    int realX = steep ? y : x;
    int realY = steep ? x : y;

    // Does current pixel mean we have moved white to black or vice versa?
    // Scanning black in state 0,2 and white in state 1, so if we find the wrong
    // color, advance to next state or end if we are in state 2 already
    if ((state == 1) == image.get(realX, realY)) {
      if (state == 2) {
        return MathUtils.distance(x, y, fromX, fromY);
      }
      state++;
    }

    error += dy;
    if (error > 0) {
      if (y == toY) {
        break;
      }
      y += ystep;
      error -= dx;
    }
  }
  // Found black-white-black; give the benefit of the doubt that the next pixel outside the image
  // is "white" so this last point at (toX+xStep,toY) is the right ending. This is really a
  // small approximation; (toX+xStep,toY+yStep) might be really correct. Ignore this.
  if (state == 2) {
    return MathUtils.distance(toX + xstep, toY, fromX, fromY);
  }
  // else we didn't find even black-white-black; no estimate is really possible
  return Float.NaN;
}
 
示例26
private void adjustOddEvenCounts(int numModules) throws NotFoundException {

    int oddSum = MathUtils.sum(this.getOddCounts());
    int evenSum = MathUtils.sum(this.getEvenCounts());

    boolean incrementOdd = false;
    boolean decrementOdd = false;

    if (oddSum > 13) {
      decrementOdd = true;
    } else if (oddSum < 4) {
      incrementOdd = true;
    }
    boolean incrementEven = false;
    boolean decrementEven = false;
    if (evenSum > 13) {
      decrementEven = true;
    } else if (evenSum < 4) {
      incrementEven = true;
    }

    int mismatch = oddSum + evenSum - numModules;
    boolean oddParityBad = (oddSum & 0x01) == 1;
    boolean evenParityBad = (evenSum & 0x01) == 0;
    if (mismatch == 1) {
      if (oddParityBad) {
        if (evenParityBad) {
          throw NotFoundException.getNotFoundInstance();
        }
        decrementOdd = true;
      } else {
        if (!evenParityBad) {
          throw NotFoundException.getNotFoundInstance();
        }
        decrementEven = true;
      }
    } else if (mismatch == -1) {
      if (oddParityBad) {
        if (evenParityBad) {
          throw NotFoundException.getNotFoundInstance();
        }
        incrementOdd = true;
      } else {
        if (!evenParityBad) {
          throw NotFoundException.getNotFoundInstance();
        }
        incrementEven = true;
      }
    } else if (mismatch == 0) {
      if (oddParityBad) {
        if (!evenParityBad) {
          throw NotFoundException.getNotFoundInstance();
        }
        // Both bad
        if (oddSum < evenSum) {
          incrementOdd = true;
          decrementEven = true;
        } else {
          decrementOdd = true;
          incrementEven = true;
        }
      } else {
        if (evenParityBad) {
          throw NotFoundException.getNotFoundInstance();
        }
        // Nothing to do!
      }
    } else {
      throw NotFoundException.getNotFoundInstance();
    }

    if (incrementOdd) {
      if (decrementOdd) {
        throw NotFoundException.getNotFoundInstance();
      }
      increment(this.getOddCounts(), this.getOddRoundingErrors());
    }
    if (decrementOdd) {
      decrement(this.getOddCounts(), this.getOddRoundingErrors());
    }
    if (incrementEven) {
      if (decrementEven) {
        throw NotFoundException.getNotFoundInstance();
      }
      increment(this.getEvenCounts(), this.getOddRoundingErrors());
    }
    if (decrementEven) {
      decrement(this.getEvenCounts(), this.getEvenRoundingErrors());
    }
  }
 
示例27
/**
 * <p>This method traces a line from a point in the image, in the direction towards another point.
 * It begins in a black region, and keeps going until it finds white, then black, then white again.
 * It reports the distance from the start to this point.</p>
 *
 * <p>This is used when figuring out how wide a finder pattern is, when the finder pattern
 * may be skewed or rotated.</p>
 */
private float sizeOfBlackWhiteBlackRun(int fromX, int fromY, int toX, int toY) {
  // Mild variant of Bresenham's algorithm;
  // see http://en.wikipedia.org/wiki/Bresenham's_line_algorithm
  boolean steep = Math.abs(toY - fromY) > Math.abs(toX - fromX);
  if (steep) {
    int temp = fromX;
    fromX = fromY;
    fromY = temp;
    temp = toX;
    toX = toY;
    toY = temp;
  }

  int dx = Math.abs(toX - fromX);
  int dy = Math.abs(toY - fromY);
  int error = -dx / 2;
  int xstep = fromX < toX ? 1 : -1;
  int ystep = fromY < toY ? 1 : -1;

  // In black pixels, looking for white, first or second time.
  int state = 0;
  // Loop up until x == toX, but not beyond
  int xLimit = toX + xstep;
  for (int x = fromX, y = fromY; x != xLimit; x += xstep) {
    int realX = steep ? y : x;
    int realY = steep ? x : y;

    // Does current pixel mean we have moved white to black or vice versa?
    // Scanning black in state 0,2 and white in state 1, so if we find the wrong
    // color, advance to next state or end if we are in state 2 already
    if ((state == 1) == image.get(realX, realY)) {
      if (state == 2) {
        return MathUtils.distance(x, y, fromX, fromY);
      }
      state++;
    }

    error += dy;
    if (error > 0) {
      if (y == toY) {
        break;
      }
      y += ystep;
      error -= dx;
    }
  }
  // Found black-white-black; give the benefit of the doubt that the next pixel outside the image
  // is "white" so this last point at (toX+xStep,toY) is the right ending. This is really a
  // small approximation; (toX+xStep,toY+yStep) might be really correct. Ignore this.
  if (state == 2) {
    return MathUtils.distance(toX + xstep, toY, fromX, fromY);
  }
  // else we didn't find even black-white-black; no estimate is really possible
  return Float.NaN;
}
 
示例28
private void adjustOddEvenCounts(int numModules) throws NotFoundException {

    int oddSum = MathUtils.sum(this.getOddCounts());
    int evenSum = MathUtils.sum(this.getEvenCounts());

    boolean incrementOdd = false;
    boolean decrementOdd = false;

    if (oddSum > 13) {
      decrementOdd = true;
    } else if (oddSum < 4) {
      incrementOdd = true;
    }
    boolean incrementEven = false;
    boolean decrementEven = false;
    if (evenSum > 13) {
      decrementEven = true;
    } else if (evenSum < 4) {
      incrementEven = true;
    }

    int mismatch = oddSum + evenSum - numModules;
    boolean oddParityBad = (oddSum & 0x01) == 1;
    boolean evenParityBad = (evenSum & 0x01) == 0;
    if (mismatch == 1) {
      if (oddParityBad) {
        if (evenParityBad) {
          throw NotFoundException.getNotFoundInstance();
        }
        decrementOdd = true;
      } else {
        if (!evenParityBad) {
          throw NotFoundException.getNotFoundInstance();
        }
        decrementEven = true;
      }
    } else if (mismatch == -1) {
      if (oddParityBad) {
        if (evenParityBad) {
          throw NotFoundException.getNotFoundInstance();
        }
        incrementOdd = true;
      } else {
        if (!evenParityBad) {
          throw NotFoundException.getNotFoundInstance();
        }
        incrementEven = true;
      }
    } else if (mismatch == 0) {
      if (oddParityBad) {
        if (!evenParityBad) {
          throw NotFoundException.getNotFoundInstance();
        }
        // Both bad
        if (oddSum < evenSum) {
          incrementOdd = true;
          decrementEven = true;
        } else {
          decrementOdd = true;
          incrementEven = true;
        }
      } else {
        if (evenParityBad) {
          throw NotFoundException.getNotFoundInstance();
        }
        // Nothing to do!
      }
    } else {
      throw NotFoundException.getNotFoundInstance();
    }

    if (incrementOdd) {
      if (decrementOdd) {
        throw NotFoundException.getNotFoundInstance();
      }
      increment(this.getOddCounts(), this.getOddRoundingErrors());
    }
    if (decrementOdd) {
      decrement(this.getOddCounts(), this.getOddRoundingErrors());
    }
    if (incrementEven) {
      if (decrementEven) {
        throw NotFoundException.getNotFoundInstance();
      }
      increment(this.getEvenCounts(), this.getOddRoundingErrors());
    }
    if (decrementEven) {
      decrement(this.getEvenCounts(), this.getEvenRoundingErrors());
    }
  }
 
示例29
/**
 * <p>This method traces a line from a point in the image, in the direction towards another point.
 * It begins in a black region, and keeps going until it finds white, then black, then white again.
 * It reports the distance from the start to this point.</p>
 *
 * <p>This is used when figuring out how wide a finder pattern is, when the finder pattern
 * may be skewed or rotated.</p>
 */
private float sizeOfBlackWhiteBlackRun(int fromX, int fromY, int toX, int toY) {
  // Mild variant of Bresenham's algorithm;
  // see http://en.wikipedia.org/wiki/Bresenham's_line_algorithm
  boolean steep = Math.abs(toY - fromY) > Math.abs(toX - fromX);
  if (steep) {
    int temp = fromX;
    fromX = fromY;
    fromY = temp;
    temp = toX;
    toX = toY;
    toY = temp;
  }

  int dx = Math.abs(toX - fromX);
  int dy = Math.abs(toY - fromY);
  int error = -dx / 2;
  int xstep = fromX < toX ? 1 : -1;
  int ystep = fromY < toY ? 1 : -1;

  // In black pixels, looking for white, first or second time.
  int state = 0;
  // Loop up until x == toX, but not beyond
  int xLimit = toX + xstep;
  for (int x = fromX, y = fromY; x != xLimit; x += xstep) {
    int realX = steep ? y : x;
    int realY = steep ? x : y;

    // Does current pixel mean we have moved white to black or vice versa?
    // Scanning black in state 0,2 and white in state 1, so if we find the wrong
    // color, advance to next state or end if we are in state 2 already
    if ((state == 1) == image.get(realX, realY)) {
      if (state == 2) {
        return MathUtils.distance(x, y, fromX, fromY);
      }
      state++;
    }

    error += dy;
    if (error > 0) {
      if (y == toY) {
        break;
      }
      y += ystep;
      error -= dx;
    }
  }
  // Found black-white-black; give the benefit of the doubt that the next pixel outside the image
  // is "white" so this last point at (toX+xStep,toY) is the right ending. This is really a
  // small approximation; (toX+xStep,toY+yStep) might be really correct. Ignore this.
  if (state == 2) {
    return MathUtils.distance(toX + xstep, toY, fromX, fromY);
  }
  // else we didn't find even black-white-black; no estimate is really possible
  return Float.NaN;
}
 
示例30
private void adjustOddEvenCounts(int numModules) throws NotFoundException {

    int oddSum = MathUtils.sum(this.getOddCounts());
    int evenSum = MathUtils.sum(this.getEvenCounts());

    boolean incrementOdd = false;
    boolean decrementOdd = false;

    if (oddSum > 13) {
      decrementOdd = true;
    } else if (oddSum < 4) {
      incrementOdd = true;
    }
    boolean incrementEven = false;
    boolean decrementEven = false;
    if (evenSum > 13) {
      decrementEven = true;
    } else if (evenSum < 4) {
      incrementEven = true;
    }

    int mismatch = oddSum + evenSum - numModules;
    boolean oddParityBad = (oddSum & 0x01) == 1;
    boolean evenParityBad = (evenSum & 0x01) == 0;
    if (mismatch == 1) {
      if (oddParityBad) {
        if (evenParityBad) {
          throw NotFoundException.getNotFoundInstance();
        }
        decrementOdd = true;
      } else {
        if (!evenParityBad) {
          throw NotFoundException.getNotFoundInstance();
        }
        decrementEven = true;
      }
    } else if (mismatch == -1) {
      if (oddParityBad) {
        if (evenParityBad) {
          throw NotFoundException.getNotFoundInstance();
        }
        incrementOdd = true;
      } else {
        if (!evenParityBad) {
          throw NotFoundException.getNotFoundInstance();
        }
        incrementEven = true;
      }
    } else if (mismatch == 0) {
      if (oddParityBad) {
        if (!evenParityBad) {
          throw NotFoundException.getNotFoundInstance();
        }
        // Both bad
        if (oddSum < evenSum) {
          incrementOdd = true;
          decrementEven = true;
        } else {
          decrementOdd = true;
          incrementEven = true;
        }
      } else {
        if (evenParityBad) {
          throw NotFoundException.getNotFoundInstance();
        }
        // Nothing to do!
      }
    } else {
      throw NotFoundException.getNotFoundInstance();
    }

    if (incrementOdd) {
      if (decrementOdd) {
        throw NotFoundException.getNotFoundInstance();
      }
      increment(this.getOddCounts(), this.getOddRoundingErrors());
    }
    if (decrementOdd) {
      decrement(this.getOddCounts(), this.getOddRoundingErrors());
    }
    if (incrementEven) {
      if (decrementEven) {
        throw NotFoundException.getNotFoundInstance();
      }
      increment(this.getEvenCounts(), this.getOddRoundingErrors());
    }
    if (decrementEven) {
      decrement(this.getEvenCounts(), this.getEvenRoundingErrors());
    }
  }