Пример #1
0
  /**
   * Determines the symbol's unit length by counting the number of pixels between the outer edges of
   * the first black ring. North, south, east, and west readings are taken and the average is
   * returned.
   */
  protected float readUnit(Scanner scanner) {
    int sx = (int) Math.round(x);
    int sy = (int) Math.round(y);
    int iwidth = scanner.getImageWidth();
    int iheight = scanner.getImageHeight();

    boolean whiteL = true;
    boolean whiteR = true;
    boolean whiteU = true;
    boolean whiteD = true;
    int sample;
    int distL = 0, distR = 0, distU = 0, distD = 0;

    for (int i = 1; true; i++) {
      if (sx - i < 1 || sx + i >= iwidth - 1 || sy - i < 1 || sy + i >= iheight - 1 || i > 100) {
        return -1;
      }

      // Left sample
      sample = scanner.getBW3x3(sx - i, sy);
      if (distL <= 0) {
        if (whiteL && sample == 0) {
          whiteL = false;
        } else if (!whiteL && sample == 1) {
          distL = i;
        }
      }

      // Right sample
      sample = scanner.getBW3x3(sx + i, sy);
      if (distR <= 0) {
        if (whiteR && sample == 0) {
          whiteR = false;
        } else if (!whiteR && sample == 1) {
          distR = i;
        }
      }

      // Up sample
      sample = scanner.getBW3x3(sx, sy - i);
      if (distU <= 0) {
        if (whiteU && sample == 0) {
          whiteU = false;
        } else if (!whiteU && sample == 1) {
          distU = i;
        }
      }

      // Down sample
      sample = scanner.getBW3x3(sx, sy + i);
      if (distD <= 0) {
        if (whiteD && sample == 0) {
          whiteD = false;
        } else if (!whiteD && sample == 1) {
          distD = i;
        }
      }

      if (distR > 0 && distL > 0 && distU > 0 && distD > 0) {
        float u = (distR + distL + distU + distD) / 8.0f;
        if (Math.abs(distR + distL - distU - distD) > u) {
          return -1;
        } else {
          return u;
        }
      }
    }
  }