Exemplo n.º 1
0
 /**
  * Estimates module size (pixels in a module) based on the Start and End finder patterns.
  *
  * @param vertices an array of vertices: vertices[0] x, y top left barcode vertices[1] x, y bottom
  *     left barcode vertices[2] x, y top right barcode vertices[3] x, y bottom right barcode
  *     vertices[4] x, y top left codeword area vertices[5] x, y bottom left codeword area
  *     vertices[6] x, y top right codeword area vertices[7] x, y bottom right codeword area
  * @return the module size.
  */
 private static float computeModuleWidth(ResultPoint[] vertices) {
   float pixels1 = ResultPoint.distance(vertices[0], vertices[4]);
   float pixels2 = ResultPoint.distance(vertices[1], vertices[5]);
   float moduleWidth1 = (pixels1 + pixels2) / (17 * 2.0f);
   float pixels3 = ResultPoint.distance(vertices[6], vertices[2]);
   float pixels4 = ResultPoint.distance(vertices[7], vertices[3]);
   float moduleWidth2 = (pixels3 + pixels4) / (18 * 2.0f);
   return (moduleWidth1 + moduleWidth2) / 2.0f;
 }
Exemplo n.º 2
0
 /**
  * Computes the dimension (number of modules in a row) of the PDF417 Code based on vertices of the
  * codeword area and estimated module size.
  *
  * @param topLeft of codeword area
  * @param topRight of codeword area
  * @param bottomLeft of codeword area
  * @param bottomRight of codeword are
  * @param moduleWidth estimated module size
  * @return the number of modules in a row.
  */
 private static int computeDimension(
     ResultPoint topLeft,
     ResultPoint topRight,
     ResultPoint bottomLeft,
     ResultPoint bottomRight,
     float moduleWidth) {
   int topRowDimension = round(ResultPoint.distance(topLeft, topRight) / moduleWidth);
   int bottomRowDimension = round(ResultPoint.distance(bottomLeft, bottomRight) / moduleWidth);
   return ((((topRowDimension + bottomRowDimension) >> 1) + 8) / 17) * 17;
   /*
    * int topRowDimension = round(ResultPoint.distance(topLeft, topRight));
    * //moduleWidth); int bottomRowDimension =
    * round(ResultPoint.distance(bottomLeft, bottomRight)); //
    * moduleWidth); int dimension = ((topRowDimension + bottomRowDimension)
    * >> 1); // Round up to nearest 17 modules i.e. there are 17 modules
    * per codeword //int dimension = ((((topRowDimension +
    * bottomRowDimension) >> 1) + 8) / 17) * 17; return dimension;
    */
 }
Exemplo n.º 3
0
  /**
   * @return the 3 best {@link FinderPattern}s from our list of candidates. The "best" are those
   *     that have been detected at least {@link #CENTER_QUORUM} times, and whose module size
   *     differs from the average among those patterns the least
   * @throws NotFoundException if 3 such finder patterns do not exist
   */
  private FinderPattern[][] selectMutipleBestPatterns() throws NotFoundException {
    List<FinderPattern> possibleCenters = getPossibleCenters();
    int size = possibleCenters.size();

    if (size < 3) {
      // Couldn't find enough finder patterns
      throw NotFoundException.getNotFoundInstance();
    }

    /*
     * Begin HE modifications to safely detect multiple codes of equal size
     */
    if (size == 3) {
      return new FinderPattern[][] {
        new FinderPattern[] {possibleCenters.get(0), possibleCenters.get(1), possibleCenters.get(2)}
      };
    }

    // Sort by estimated module size to speed up the upcoming checks
    Collections.sort(possibleCenters, new ModuleSizeComparator());

    /*
     * Now lets start: build a list of tuples of three finder locations that
     *  - feature similar module sizes
     *  - are placed in a distance so the estimated module count is within the QR specification
     *  - have similar distance between upper left/right and left top/bottom finder patterns
     *  - form a triangle with 90° angle (checked by comparing top right/bottom left distance
     *    with pythagoras)
     *
     * Note: we allow each point to be used for more than one code region: this might seem
     * counterintuitive at first, but the performance penalty is not that big. At this point,
     * we cannot make a good quality decision whether the three finders actually represent
     * a QR code, or are just by chance layouted so it looks like there might be a QR code there.
     * So, if the layout seems right, lets have the decoder try to decode.
     */

    List<FinderPattern[]> results = new ArrayList<>(); // holder for the results

    for (int i1 = 0; i1 < (size - 2); i1++) {
      FinderPattern p1 = possibleCenters.get(i1);
      if (p1 == null) {
        continue;
      }

      for (int i2 = i1 + 1; i2 < (size - 1); i2++) {
        FinderPattern p2 = possibleCenters.get(i2);
        if (p2 == null) {
          continue;
        }

        // Compare the expected module sizes; if they are really off, skip
        float vModSize12 =
            (p1.getEstimatedModuleSize() - p2.getEstimatedModuleSize())
                / Math.min(p1.getEstimatedModuleSize(), p2.getEstimatedModuleSize());
        float vModSize12A = Math.abs(p1.getEstimatedModuleSize() - p2.getEstimatedModuleSize());
        if (vModSize12A > DIFF_MODSIZE_CUTOFF && vModSize12 >= DIFF_MODSIZE_CUTOFF_PERCENT) {
          // break, since elements are ordered by the module size deviation there cannot be
          // any more interesting elements for the given p1.
          break;
        }

        for (int i3 = i2 + 1; i3 < size; i3++) {
          FinderPattern p3 = possibleCenters.get(i3);
          if (p3 == null) {
            continue;
          }

          // Compare the expected module sizes; if they are really off, skip
          float vModSize23 =
              (p2.getEstimatedModuleSize() - p3.getEstimatedModuleSize())
                  / Math.min(p2.getEstimatedModuleSize(), p3.getEstimatedModuleSize());
          float vModSize23A = Math.abs(p2.getEstimatedModuleSize() - p3.getEstimatedModuleSize());
          if (vModSize23A > DIFF_MODSIZE_CUTOFF && vModSize23 >= DIFF_MODSIZE_CUTOFF_PERCENT) {
            // break, since elements are ordered by the module size deviation there cannot be
            // any more interesting elements for the given p1.
            break;
          }

          FinderPattern[] test = {p1, p2, p3};
          ResultPoint.orderBestPatterns(test);

          // Calculate the distances: a = topleft-bottomleft, b=topleft-topright, c = diagonal
          FinderPatternInfo info = new FinderPatternInfo(test);
          float dA = ResultPoint.distance(info.getTopLeft(), info.getBottomLeft());
          float dC = ResultPoint.distance(info.getTopRight(), info.getBottomLeft());
          float dB = ResultPoint.distance(info.getTopLeft(), info.getTopRight());

          // Check the sizes
          float estimatedModuleCount = (dA + dB) / (p1.getEstimatedModuleSize() * 2.0f);
          if (estimatedModuleCount > MAX_MODULE_COUNT_PER_EDGE
              || estimatedModuleCount < MIN_MODULE_COUNT_PER_EDGE) {
            continue;
          }

          // Calculate the difference of the edge lengths in percent
          float vABBC = Math.abs((dA - dB) / Math.min(dA, dB));
          if (vABBC >= 0.1f) {
            continue;
          }

          // Calculate the diagonal length by assuming a 90° angle at topleft
          float dCpy = (float) Math.sqrt(dA * dA + dB * dB);
          // Compare to the real distance in %
          float vPyC = Math.abs((dC - dCpy) / Math.min(dC, dCpy));

          if (vPyC >= 0.1f) {
            continue;
          }

          // All tests passed!
          results.add(test);
        } // end iterate p3
      } // end iterate p2
    } // end iterate p1

    if (!results.isEmpty()) {
      return results.toArray(new FinderPattern[results.size()][]);
    }

    // Nothing found!
    throw NotFoundException.getNotFoundInstance();
  }