com.google.zxing.common.detector.MathUtils Java Examples

The following examples show how to use com.google.zxing.common.detector.MathUtils. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: Detector.java    From ZXing-Orient with Apache License 2.0 6 votes vote down vote up
/**
 * 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;
}
 
Example #2
Source File: Detector.java    From RipplePower with Apache License 2.0 6 votes vote down vote up
/**
 * <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;
}
 
Example #3
Source File: Detector.java    From Telegram-FOSS with GNU General Public License v2.0 6 votes vote down vote up
/**
 * <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;
}
 
Example #4
Source File: Detector.java    From QrCodeScanner with GNU General Public License v3.0 6 votes vote down vote up
/**
 * <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;
}
 
Example #5
Source File: Detector.java    From Tesseract-OCR-Scanner with Apache License 2.0 6 votes vote down vote up
/**
 * <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;
}
 
Example #6
Source File: Detector.java    From ScreenCapture with MIT License 6 votes vote down vote up
/**
 * <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;
}
 
Example #7
Source File: Detector.java    From Telegram with GNU General Public License v2.0 6 votes vote down vote up
/**
 * <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;
}
 
Example #8
Source File: Detector.java    From ZXing-Orient with Apache License 2.0 6 votes vote down vote up
/**
 * <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;
}
 
Example #9
Source File: Detector.java    From reacteu-app with MIT License 6 votes vote down vote up
/**
 *
 * 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;
}
 
Example #10
Source File: Detector.java    From reacteu-app with MIT License 6 votes vote down vote up
/**
 * <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;
}
 
Example #11
Source File: Detector.java    From analyzer-of-android-for-Apache-Weex with Apache License 2.0 6 votes vote down vote up
/**
 * <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;
}
 
Example #12
Source File: Detector.java    From analyzer-of-android-for-Apache-Weex with Apache License 2.0 6 votes vote down vote up
/**
 * 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;
}
 
Example #13
Source File: Detector.java    From barcodescanner-lib-aar with MIT License 6 votes vote down vote up
/**
 * 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;
}
 
Example #14
Source File: Detector.java    From weex with Apache License 2.0 6 votes vote down vote up
/**
 * <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;
}
 
Example #15
Source File: Detector.java    From weex with Apache License 2.0 6 votes vote down vote up
/**
 * 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;
}
 
Example #16
Source File: Detector.java    From barcodescanner-lib-aar with MIT License 6 votes vote down vote up
/**
 * <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;
}
 
Example #17
Source File: PDF417CodewordDecoder.java    From barcodescanner-lib-aar with MIT License 6 votes vote down vote up
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;
}
 
Example #18
Source File: PDF417CodewordDecoder.java    From barcodescanner-lib-aar with MIT License 6 votes vote down vote up
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;
}
 
Example #19
Source File: Detector.java    From weex with Apache License 2.0 5 votes vote down vote up
/**
 * 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;
}
 
Example #20
Source File: Detector.java    From reacteu-app with MIT License 5 votes vote down vote up
/**
 *
 * <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)}; 
}
 
Example #21
Source File: Detector.java    From ZXing-Orient with Apache License 2.0 5 votes vote down vote up
/**
 * 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;
}
 
Example #22
Source File: Detector.java    From analyzer-of-android-for-Apache-Weex with Apache License 2.0 5 votes vote down vote up
/**
 * 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;
}
 
Example #23
Source File: Detector.java    From barcodescanner-lib-aar with MIT License 5 votes vote down vote up
/**
 * 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;
}
 
Example #24
Source File: Detector.java    From reacteu-app with MIT License 5 votes vote down vote up
/**
 * 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;
  }
}
 
Example #25
Source File: Detector.java    From reacteu-app with MIT License 4 votes vote down vote up
private static int distance(ResultPoint a, ResultPoint b) {
  return MathUtils.round(ResultPoint.distance(a, b));
}
 
Example #26
Source File: Detector.java    From ScreenCapture with MIT License 4 votes vote down vote up
/**
 * <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;
}
 
Example #27
Source File: Detector.java    From barcodescanner-lib-aar with MIT License 4 votes vote down vote up
/**
 * <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;
}
 
Example #28
Source File: Detector.java    From Telegram-FOSS with GNU General Public License v2.0 4 votes vote down vote up
/**
 * <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;
}
 
Example #29
Source File: PDF417ScanningDecoder.java    From barcodescanner-lib-aar with MIT License 4 votes vote down vote up
private static Codeword detectCodeword(BitMatrix image,
                                       int minColumn,
                                       int maxColumn,
                                       boolean leftToRight,
                                       int startColumn,
                                       int imageRow,
                                       int minCodewordWidth,
                                       int maxCodewordWidth) {
  startColumn = adjustCodewordStartColumn(image, minColumn, maxColumn, leftToRight, startColumn, imageRow);
  // we usually know fairly exact now how long a codeword is. We should provide minimum and maximum expected length
  // and try to adjust the read pixels, e.g. remove single pixel errors or try to cut off exceeding pixels.
  // min and maxCodewordWidth should not be used as they are calculated for the whole barcode an can be inaccurate
  // for the current position
  int[] moduleBitCount = getModuleBitCount(image, minColumn, maxColumn, leftToRight, startColumn, imageRow);
  if (moduleBitCount == null) {
    return null;
  }
  int endColumn;
  int codewordBitCount = MathUtils.sum(moduleBitCount);
  if (leftToRight) {
    endColumn = startColumn + codewordBitCount;
  } else {
    for (int i = 0; i < moduleBitCount.length / 2; i++) {
      int tmpCount = moduleBitCount[i];
      moduleBitCount[i] = moduleBitCount[moduleBitCount.length - 1 - i];
      moduleBitCount[moduleBitCount.length - 1 - i] = tmpCount;
    }
    endColumn = startColumn;
    startColumn = endColumn - codewordBitCount;
  }
  // TODO implement check for width and correction of black and white bars
  // use start (and maybe stop pattern) to determine if black bars are wider than white bars. If so, adjust.
  // should probably done only for codewords with a lot more than 17 bits. 
  // The following fixes 10-1.png, which has wide black bars and small white bars
  //    for (int i = 0; i < moduleBitCount.length; i++) {
  //      if (i % 2 == 0) {
  //        moduleBitCount[i]--;
  //      } else {
  //        moduleBitCount[i]++;
  //      }
  //    }

  // We could also use the width of surrounding codewords for more accurate results, but this seems
  // sufficient for now
  if (!checkCodewordSkew(codewordBitCount, minCodewordWidth, maxCodewordWidth)) {
    // We could try to use the startX and endX position of the codeword in the same column in the previous row,
    // create the bit count from it and normalize it to 8. This would help with single pixel errors.
    return null;
  }

  int decodedValue = PDF417CodewordDecoder.getDecodedValue(moduleBitCount);
  int codeword = PDF417Common.getCodeword(decodedValue);
  if (codeword == -1) {
    return null;
  }
  return new Codeword(startColumn, endColumn, getCodewordBucketNumber(decodedValue), codeword);
}
 
Example #30
Source File: Detector.java    From reacteu-app with MIT License 4 votes vote down vote up
/**
 * <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 >> 1;
  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;
}