com.google.zxing.BinaryBitmap#getWidth ( )源码实例Demo

下面列出了com.google.zxing.BinaryBitmap#getWidth ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

源代码1 项目: ScreenCapture   文件: OneDReader.java
/**
 * We're going to examine rows from the middle outward, searching alternately above and below the
 * middle, and farther out each time. rowStep is the number of rows between each successive
 * attempt above and below the middle. So we'd scan row middle, then middle - rowStep, then
 * middle + rowStep, then middle - (2 * rowStep), etc.
 * rowStep is bigger as the image is taller, but is always at least 1. We've somewhat arbitrarily
 * decided that moving up and down by about 1/16 of the image is pretty good; we try more of the
 * image if "trying harder".
 *
 * @param image The image to decode
 * @param hints Any hints that were requested
 * @return The contents of the decoded barcode
 * @throws NotFoundException Any spontaneous errors which occur
 */
private Result doDecode(BinaryBitmap image,
                        Map<DecodeHintType,?> hints) throws NotFoundException {
  int width = image.getWidth();
  int height = image.getHeight();
  BitArray row = new BitArray(width);

  boolean tryHarder = hints != null && hints.containsKey(DecodeHintType.TRY_HARDER);
  int rowStep = Math.max(1, height >> (tryHarder ? 8 : 5));
  int maxLines;
  if (tryHarder) {
    maxLines = height; // Look at the whole image, not just the center
  } else {
    maxLines = 15; // 15 rows spaced 1/32 apart is roughly the middle half of the image
  }

  int middle = height / 2;
  for (int x = 0; x < maxLines; x++) {

    // Scanning from the middle out. Determine which row we're looking at next:
    int rowStepsAboveOrBelow = (x + 1) / 2;
    boolean isAbove = (x & 0x01) == 0; // i.e. is x even?
    int rowNumber = middle + rowStep * (isAbove ? rowStepsAboveOrBelow : -rowStepsAboveOrBelow);
    if (rowNumber < 0 || rowNumber >= height) {
      // Oops, if we run off the top or bottom, stop
      break;
    }

    // Estimate black point for this row and load it:
    try {
      row = image.getBlackRow(rowNumber, row);
    } catch (NotFoundException ignored) {
      continue;
    }

    // While we have the image data in a BitArray, it's fairly cheap to reverse it in place to
    // handle decoding upside down barcodes.
    for (int attempt = 0; attempt < 2; attempt++) {
      if (attempt == 1) { // trying again?
        row.reverse(); // reverse the row and continue
        // This means we will only ever draw result points *once* in the life of this method
        // since we want to avoid drawing the wrong points after flipping the row, and,
        // don't want to clutter with noise from every single row scan -- just the scans
        // that start on the center line.
        if (hints != null && hints.containsKey(DecodeHintType.NEED_RESULT_POINT_CALLBACK)) {
          Map<DecodeHintType,Object> newHints = new EnumMap<>(DecodeHintType.class);
          newHints.putAll(hints);
          newHints.remove(DecodeHintType.NEED_RESULT_POINT_CALLBACK);
          hints = newHints;
        }
      }
      try {
        // Look for a barcode
        Result result = decodeRow(rowNumber, row, hints);
        // We found our barcode
        if (attempt == 1) {
          // But it was upside down, so note that
          result.putMetadata(ResultMetadataType.ORIENTATION, 180);
          // And remember to flip the result points horizontally.
          ResultPoint[] points = result.getResultPoints();
          if (points != null) {
            points[0] = new ResultPoint(width - points[0].getX() - 1, points[0].getY());
            points[1] = new ResultPoint(width - points[1].getX() - 1, points[1].getY());
          }
        }
        return result;
      } catch (ReaderException re) {
        // continue -- just couldn't decode this row
      }
    }
  }

  throw NotFoundException.getNotFoundInstance();
}
 
private void doDecodeMultiple(BinaryBitmap image,
                              Map<DecodeHintType,?> hints,
                              List<Result> results,
                              int xOffset,
                              int yOffset,
                              int currentDepth) {
  if (currentDepth > MAX_DEPTH) {
    return;
  }

  Result result;
  try {
    result = delegate.decode(image, hints);
  } catch (ReaderException ignored) {
    return;
  }
  boolean alreadyFound = false;
  for (Result existingResult : results) {
    if (existingResult.getText().equals(result.getText())) {
      alreadyFound = true;
      break;
    }
  }
  if (!alreadyFound) {
    results.add(translateResultPoints(result, xOffset, yOffset));
  }
  ResultPoint[] resultPoints = result.getResultPoints();
  if (resultPoints == null || resultPoints.length == 0) {
    return;
  }
  int width = image.getWidth();
  int height = image.getHeight();
  float minX = width;
  float minY = height;
  float maxX = 0.0f;
  float maxY = 0.0f;
  for (ResultPoint point : resultPoints) {
    if (point == null) {
      continue;
    }
    float x = point.getX();
    float y = point.getY();
    if (x < minX) {
      minX = x;
    }
    if (y < minY) {
      minY = y;
    }
    if (x > maxX) {
      maxX = x;
    }
    if (y > maxY) {
      maxY = y;
    }
  }

  // Decode left of barcode
  if (minX > MIN_DIMENSION_TO_RECUR) {
    doDecodeMultiple(image.crop(0, 0, (int) minX, height),
                     hints, results,
                     xOffset, yOffset,
                     currentDepth + 1);
  }
  // Decode above barcode
  if (minY > MIN_DIMENSION_TO_RECUR) {
    doDecodeMultiple(image.crop(0, 0, width, (int) minY),
                     hints, results,
                     xOffset, yOffset,
                     currentDepth + 1);
  }
  // Decode right of barcode
  if (maxX < width - MIN_DIMENSION_TO_RECUR) {
    doDecodeMultiple(image.crop((int) maxX, 0, width - (int) maxX, height),
                     hints, results,
                     xOffset + (int) maxX, yOffset,
                     currentDepth + 1);
  }
  // Decode below barcode
  if (maxY < height - MIN_DIMENSION_TO_RECUR) {
    doDecodeMultiple(image.crop(0, (int) maxY, width, height - (int) maxY),
                     hints, results,
                     xOffset, yOffset + (int) maxY,
                     currentDepth + 1);
  }
}
 
源代码3 项目: Tesseract-OCR-Scanner   文件: OneDReader.java
/**
 * We're going to examine rows from the middle outward, searching alternately above and below the
 * middle, and farther out each time. rowStep is the number of rows between each successive
 * attempt above and below the middle. So we'd scan row middle, then middle - rowStep, then
 * middle + rowStep, then middle - (2 * rowStep), etc.
 * rowStep is bigger as the image is taller, but is always at least 1. We've somewhat arbitrarily
 * decided that moving up and down by about 1/16 of the image is pretty good; we try more of the
 * image if "trying harder".
 *
 * @param image The image to decode
 * @param hints Any hints that were requested
 * @return The contents of the decoded barcode
 * @throws NotFoundException Any spontaneous errors which occur
 */
private Result doDecode(BinaryBitmap image,
                        Map<DecodeHintType,?> hints) throws NotFoundException {
  int width = image.getWidth();
  int height = image.getHeight();
  BitArray row = new BitArray(width);

  boolean tryHarder = hints != null && hints.containsKey(DecodeHintType.TRY_HARDER);
  int rowStep = Math.max(1, height >> (tryHarder ? 8 : 5));
  int maxLines;
  if (tryHarder) {
    maxLines = height; // Look at the whole image, not just the center
  } else {
    maxLines = 15; // 15 rows spaced 1/32 apart is roughly the middle half of the image
  }

  int middle = height / 2;
  for (int x = 0; x < maxLines; x++) {

    // Scanning from the middle out. Determine which row we're looking at next:
    int rowStepsAboveOrBelow = (x + 1) / 2;
    boolean isAbove = (x & 0x01) == 0; // i.e. is x even?
    int rowNumber = middle + rowStep * (isAbove ? rowStepsAboveOrBelow : -rowStepsAboveOrBelow);
    if (rowNumber < 0 || rowNumber >= height) {
      // Oops, if we run off the top or bottom, stop
      break;
    }

    // Estimate black point for this row and load it:
    try {
      row = image.getBlackRow(rowNumber, row);
    } catch (NotFoundException ignored) {
      continue;
    }

    // While we have the image data in a BitArray, it's fairly cheap to reverse it in place to
    // handle decoding upside down barcodes.
    for (int attempt = 0; attempt < 2; attempt++) {
      if (attempt == 1) { // trying again?
        row.reverse(); // reverse the row and continue
        // This means we will only ever draw result points *once* in the life of this method
        // since we want to avoid drawing the wrong points after flipping the row, and,
        // don't want to clutter with noise from every single row scan -- just the scans
        // that start on the center line.
        if (hints != null && hints.containsKey(DecodeHintType.NEED_RESULT_POINT_CALLBACK)) {
          Map<DecodeHintType,Object> newHints = new EnumMap<>(DecodeHintType.class);
          newHints.putAll(hints);
          newHints.remove(DecodeHintType.NEED_RESULT_POINT_CALLBACK);
          hints = newHints;
        }
      }
      try {
        // Look for a barcode
        Result result = decodeRow(rowNumber, row, hints);
        // We found our barcode
        if (attempt == 1) {
          // But it was upside down, so note that
          result.putMetadata(ResultMetadataType.ORIENTATION, 180);
          // And remember to flip the result points horizontally.
          ResultPoint[] points = result.getResultPoints();
          if (points != null) {
            points[0] = new ResultPoint(width - points[0].getX() - 1, points[0].getY());
            points[1] = new ResultPoint(width - points[1].getX() - 1, points[1].getY());
          }
        }
        return result;
      } catch (ReaderException re) {
        // continue -- just couldn't decode this row
      }
    }
  }

  throw NotFoundException.getNotFoundInstance();
}
 
private void doDecodeMultiple(BinaryBitmap image,
                              Map<DecodeHintType,?> hints,
                              List<Result> results,
                              int xOffset,
                              int yOffset,
                              int currentDepth) {
  if (currentDepth > MAX_DEPTH) {
    return;
  }

  Result result;
  try {
    result = delegate.decode(image, hints);
  } catch (ReaderException ignored) {
    return;
  }
  boolean alreadyFound = false;
  for (Result existingResult : results) {
    if (existingResult.getText().equals(result.getText())) {
      alreadyFound = true;
      break;
    }
  }
  if (!alreadyFound) {
    results.add(translateResultPoints(result, xOffset, yOffset));
  }
  ResultPoint[] resultPoints = result.getResultPoints();
  if (resultPoints == null || resultPoints.length == 0) {
    return;
  }
  int width = image.getWidth();
  int height = image.getHeight();
  float minX = width;
  float minY = height;
  float maxX = 0.0f;
  float maxY = 0.0f;
  for (ResultPoint point : resultPoints) {
    if (point == null) {
      continue;
    }
    float x = point.getX();
    float y = point.getY();
    if (x < minX) {
      minX = x;
    }
    if (y < minY) {
      minY = y;
    }
    if (x > maxX) {
      maxX = x;
    }
    if (y > maxY) {
      maxY = y;
    }
  }

  // Decode left of barcode
  if (minX > MIN_DIMENSION_TO_RECUR) {
    doDecodeMultiple(image.crop(0, 0, (int) minX, height),
                     hints, results,
                     xOffset, yOffset,
                     currentDepth + 1);
  }
  // Decode above barcode
  if (minY > MIN_DIMENSION_TO_RECUR) {
    doDecodeMultiple(image.crop(0, 0, width, (int) minY),
                     hints, results,
                     xOffset, yOffset,
                     currentDepth + 1);
  }
  // Decode right of barcode
  if (maxX < width - MIN_DIMENSION_TO_RECUR) {
    doDecodeMultiple(image.crop((int) maxX, 0, width - (int) maxX, height),
                     hints, results,
                     xOffset + (int) maxX, yOffset,
                     currentDepth + 1);
  }
  // Decode below barcode
  if (maxY < height - MIN_DIMENSION_TO_RECUR) {
    doDecodeMultiple(image.crop(0, (int) maxY, width, height - (int) maxY),
                     hints, results,
                     xOffset, yOffset + (int) maxY,
                     currentDepth + 1);
  }
}
 
源代码5 项目: QrCodeScanner   文件: OneDReader.java
/**
 * We're going to examine rows from the middle outward, searching alternately above and below the
 * middle, and farther out each time. rowStep is the number of rows between each successive
 * attempt above and below the middle. So we'd scan row middle, then middle - rowStep, then
 * middle + rowStep, then middle - (2 * rowStep), etc.
 * rowStep is bigger as the image is taller, but is always at least 1. We've somewhat arbitrarily
 * decided that moving up and down by about 1/16 of the image is pretty good; we try more of the
 * image if "trying harder".
 *
 * @param image The image to decode
 * @param hints Any hints that were requested
 * @return The contents of the decoded barcode
 * @throws NotFoundException Any spontaneous errors which occur
 */
private Result doDecode(BinaryBitmap image,
                        Map<DecodeHintType,?> hints) throws NotFoundException {
  int width = image.getWidth();
  int height = image.getHeight();
  BitArray row = new BitArray(width);

  boolean tryHarder = hints != null && hints.containsKey(DecodeHintType.TRY_HARDER);
  int rowStep = Math.max(1, height >> (tryHarder ? 8 : 5));
  int maxLines;
  if (tryHarder) {
    maxLines = height; // Look at the whole image, not just the center
  } else {
    maxLines = 15; // 15 rows spaced 1/32 apart is roughly the middle half of the image
  }

  int middle = height / 2;
  for (int x = 0; x < maxLines; x++) {

    // Scanning from the middle out. Determine which row we're looking at next:
    int rowStepsAboveOrBelow = (x + 1) / 2;
    boolean isAbove = (x & 0x01) == 0; // i.e. is x even?
    int rowNumber = middle + rowStep * (isAbove ? rowStepsAboveOrBelow : -rowStepsAboveOrBelow);
    if (rowNumber < 0 || rowNumber >= height) {
      // Oops, if we run off the top or bottom, stop
      break;
    }

    // Estimate black point for this row and load it:
    try {
      row = image.getBlackRow(rowNumber, row);
    } catch (NotFoundException ignored) {
      continue;
    }

    // While we have the image data in a BitArray, it's fairly cheap to reverse it in place to
    // handle decoding upside down barcodes.
    for (int attempt = 0; attempt < 2; attempt++) {
      if (attempt == 1) { // trying again?
        row.reverse(); // reverse the row and continue
        // This means we will only ever draw result points *once* in the life of this method
        // since we want to avoid drawing the wrong points after flipping the row, and,
        // don't want to clutter with noise from every single row scan -- just the scans
        // that start on the center line.
        if (hints != null && hints.containsKey(DecodeHintType.NEED_RESULT_POINT_CALLBACK)) {
          Map<DecodeHintType,Object> newHints = new EnumMap<>(DecodeHintType.class);
          newHints.putAll(hints);
          newHints.remove(DecodeHintType.NEED_RESULT_POINT_CALLBACK);
          hints = newHints;
        }
      }
      try {
        // Look for a barcode
        Result result = decodeRow(rowNumber, row, hints);
        // We found our barcode
        if (attempt == 1) {
          // But it was upside down, so note that
          result.putMetadata(ResultMetadataType.ORIENTATION, 180);
          // And remember to flip the result points horizontally.
          ResultPoint[] points = result.getResultPoints();
          if (points != null) {
            points[0] = new ResultPoint(width - points[0].getX() - 1, points[0].getY());
            points[1] = new ResultPoint(width - points[1].getX() - 1, points[1].getY());
          }
        }
        return result;
      } catch (ReaderException re) {
        // continue -- just couldn't decode this row
      }
    }
  }

  throw NotFoundException.getNotFoundInstance();
}
 
private void doDecodeMultiple(BinaryBitmap image,
                              Map<DecodeHintType,?> hints,
                              List<Result> results,
                              int xOffset,
                              int yOffset,
                              int currentDepth) {
  if (currentDepth > MAX_DEPTH) {
    return;
  }

  Result result;
  try {
    result = delegate.decode(image, hints);
  } catch (ReaderException ignored) {
    return;
  }
  boolean alreadyFound = false;
  for (Result existingResult : results) {
    if (existingResult.getText().equals(result.getText())) {
      alreadyFound = true;
      break;
    }
  }
  if (!alreadyFound) {
    results.add(translateResultPoints(result, xOffset, yOffset));
  }
  ResultPoint[] resultPoints = result.getResultPoints();
  if (resultPoints == null || resultPoints.length == 0) {
    return;
  }
  int width = image.getWidth();
  int height = image.getHeight();
  float minX = width;
  float minY = height;
  float maxX = 0.0f;
  float maxY = 0.0f;
  for (ResultPoint point : resultPoints) {
    if (point == null) {
      continue;
    }
    float x = point.getX();
    float y = point.getY();
    if (x < minX) {
      minX = x;
    }
    if (y < minY) {
      minY = y;
    }
    if (x > maxX) {
      maxX = x;
    }
    if (y > maxY) {
      maxY = y;
    }
  }

  // Decode left of barcode
  if (minX > MIN_DIMENSION_TO_RECUR) {
    doDecodeMultiple(image.crop(0, 0, (int) minX, height),
                     hints, results,
                     xOffset, yOffset,
                     currentDepth + 1);
  }
  // Decode above barcode
  if (minY > MIN_DIMENSION_TO_RECUR) {
    doDecodeMultiple(image.crop(0, 0, width, (int) minY),
                     hints, results,
                     xOffset, yOffset,
                     currentDepth + 1);
  }
  // Decode right of barcode
  if (maxX < width - MIN_DIMENSION_TO_RECUR) {
    doDecodeMultiple(image.crop((int) maxX, 0, width - (int) maxX, height),
                     hints, results,
                     xOffset + (int) maxX, yOffset,
                     currentDepth + 1);
  }
  // Decode below barcode
  if (maxY < height - MIN_DIMENSION_TO_RECUR) {
    doDecodeMultiple(image.crop(0, (int) maxY, width, height - (int) maxY),
                     hints, results,
                     xOffset, yOffset + (int) maxY,
                     currentDepth + 1);
  }
}
 
源代码7 项目: ZXing-Orient   文件: OneDReader.java
/**
 * We're going to examine rows from the middle outward, searching alternately above and below the
 * middle, and farther out each time. rowStep is the number of rows between each successive
 * attempt above and below the middle. So we'd scan row middle, then middle - rowStep, then
 * middle + rowStep, then middle - (2 * rowStep), etc.
 * rowStep is bigger as the image is taller, but is always at least 1. We've somewhat arbitrarily
 * decided that moving up and down by about 1/16 of the image is pretty good; we try more of the
 * image if "trying harder".
 *
 * @param image The image to decode
 * @param hints Any hints that were requested
 * @return The contents of the decoded barcode
 * @throws NotFoundException Any spontaneous errors which occur
 */
private Result doDecode(BinaryBitmap image,
                        Map<DecodeHintType,?> hints) throws NotFoundException {
  int width = image.getWidth();
  int height = image.getHeight();
  BitArray row = new BitArray(width);

  int middle = height >> 1;
  boolean tryHarder = hints != null && hints.containsKey(DecodeHintType.TRY_HARDER);
  int rowStep = Math.max(1, height >> (tryHarder ? 8 : 5));
  int maxLines;
  if (tryHarder) {
    maxLines = height; // Look at the whole image, not just the center
  } else {
    maxLines = 15; // 15 rows spaced 1/32 apart is roughly the middle half of the image
  }

  for (int x = 0; x < maxLines; x++) {

    // Scanning from the middle out. Determine which row we're looking at next:
    int rowStepsAboveOrBelow = (x + 1) / 2;
    boolean isAbove = (x & 0x01) == 0; // i.e. is x even?
    int rowNumber = middle + rowStep * (isAbove ? rowStepsAboveOrBelow : -rowStepsAboveOrBelow);
    if (rowNumber < 0 || rowNumber >= height) {
      // Oops, if we run off the top or bottom, stop
      break;
    }

    // Estimate black point for this row and load it:
    try {
      row = image.getBlackRow(rowNumber, row);
    } catch (NotFoundException ignored) {
      continue;
    }

    // While we have the image data in a BitArray, it's fairly cheap to reverse it in place to
    // handle decoding upside down barcodes.
    for (int attempt = 0; attempt < 2; attempt++) {
      if (attempt == 1) { // trying again?
        row.reverse(); // reverse the row and continue
        // This means we will only ever draw result points *once* in the life of this method
        // since we want to avoid drawing the wrong points after flipping the row, and,
        // don't want to clutter with noise from every single row scan -- just the scans
        // that start on the center line.
        if (hints != null && hints.containsKey(DecodeHintType.NEED_RESULT_POINT_CALLBACK)) {
          Map<DecodeHintType,Object> newHints = new EnumMap<>(DecodeHintType.class);
          newHints.putAll(hints);
          newHints.remove(DecodeHintType.NEED_RESULT_POINT_CALLBACK);
          hints = newHints;
        }
      }
      try {
        // Look for a barcode
        Result result = decodeRow(rowNumber, row, hints);
        // We found our barcode
        if (attempt == 1) {
          // But it was upside down, so note that
          result.putMetadata(ResultMetadataType.ORIENTATION, 180);
          // And remember to flip the result points horizontally.
          ResultPoint[] points = result.getResultPoints();
          if (points != null) {
            points[0] = new ResultPoint(width - points[0].getX() - 1, points[0].getY());
            points[1] = new ResultPoint(width - points[1].getX() - 1, points[1].getY());
          }
        }
        return result;
      } catch (ReaderException re) {
        // continue -- just couldn't decode this row
      }
    }
  }

  throw NotFoundException.getNotFoundInstance();
}
 
private void doDecodeMultiple(BinaryBitmap image,
                              Map<DecodeHintType,?> hints,
                              List<Result> results,
                              int xOffset,
                              int yOffset,
                              int currentDepth) {
  if (currentDepth > MAX_DEPTH) {
    return;
  }
  
  Result result;
  try {
    result = delegate.decode(image, hints);
  } catch (ReaderException ignored) {
    return;
  }
  boolean alreadyFound = false;
  for (Result existingResult : results) {
    if (existingResult.getText().equals(result.getText())) {
      alreadyFound = true;
      break;
    }
  }
  if (!alreadyFound) {
    results.add(translateResultPoints(result, xOffset, yOffset));
  }
  ResultPoint[] resultPoints = result.getResultPoints();
  if (resultPoints == null || resultPoints.length == 0) {
    return;
  }
  int width = image.getWidth();
  int height = image.getHeight();
  float minX = width;
  float minY = height;
  float maxX = 0.0f;
  float maxY = 0.0f;
  for (ResultPoint point : resultPoints) {
    float x = point.getX();
    float y = point.getY();
    if (x < minX) {
      minX = x;
    }
    if (y < minY) {
      minY = y;
    }
    if (x > maxX) {
      maxX = x;
    }
    if (y > maxY) {
      maxY = y;
    }
  }

  // Decode left of barcode
  if (minX > MIN_DIMENSION_TO_RECUR) {
    doDecodeMultiple(image.crop(0, 0, (int) minX, height),
                     hints, results, 
                     xOffset, yOffset, 
                     currentDepth + 1);
  }
  // Decode above barcode
  if (minY > MIN_DIMENSION_TO_RECUR) {
    doDecodeMultiple(image.crop(0, 0, width, (int) minY),
                     hints, results, 
                     xOffset, yOffset, 
                     currentDepth + 1);
  }
  // Decode right of barcode
  if (maxX < width - MIN_DIMENSION_TO_RECUR) {
    doDecodeMultiple(image.crop((int) maxX, 0, width - (int) maxX, height),
                     hints, results, 
                     xOffset + (int) maxX, yOffset, 
                     currentDepth + 1);
  }
  // Decode below barcode
  if (maxY < height - MIN_DIMENSION_TO_RECUR) {
    doDecodeMultiple(image.crop(0, (int) maxY, width, height - (int) maxY),
                     hints, results, 
                     xOffset, yOffset + (int) maxY, 
                     currentDepth + 1);
  }
}
 
/**
 * We're going to examine rows from the middle outward, searching alternately above and below the
 * middle, and farther out each time. rowStep is the number of rows between each successive
 * attempt above and below the middle. So we'd scan row middle, then middle - rowStep, then
 * middle + rowStep, then middle - (2 * rowStep), etc.
 * rowStep is bigger as the image is taller, but is always at least 1. We've somewhat arbitrarily
 * decided that moving up and down by about 1/16 of the image is pretty good; we try more of the
 * image if "trying harder".
 *
 * @param image The image to decode
 * @param hints Any hints that were requested
 * @return The contents of the decoded barcode
 * @throws NotFoundException Any spontaneous errors which occur
 */
private Result doDecode(BinaryBitmap image,
                        Map<DecodeHintType,?> hints) throws NotFoundException {
  int width = image.getWidth();
  int height = image.getHeight();
  BitArray row = new BitArray(width);

  int middle = height >> 1;
  boolean tryHarder = hints != null && hints.containsKey(DecodeHintType.TRY_HARDER);
  int rowStep = Math.max(1, height >> (tryHarder ? 8 : 5));
  int maxLines;
  if (tryHarder) {
    maxLines = height; // Look at the whole image, not just the center
  } else {
    maxLines = 15; // 15 rows spaced 1/32 apart is roughly the middle half of the image
  }

  for (int x = 0; x < maxLines; x++) {

    // Scanning from the middle out. Determine which row we're looking at next:
    int rowStepsAboveOrBelow = (x + 1) / 2;
    boolean isAbove = (x & 0x01) == 0; // i.e. is x even?
    int rowNumber = middle + rowStep * (isAbove ? rowStepsAboveOrBelow : -rowStepsAboveOrBelow);
    if (rowNumber < 0 || rowNumber >= height) {
      // Oops, if we run off the top or bottom, stop
      break;
    }

    // Estimate black point for this row and load it:
    try {
      row = image.getBlackRow(rowNumber, row);
    } catch (NotFoundException ignored) {
      continue;
    }

    // While we have the image data in a BitArray, it's fairly cheap to reverse it in place to
    // handle decoding upside down barcodes.
    for (int attempt = 0; attempt < 2; attempt++) {
      if (attempt == 1) { // trying again?
        row.reverse(); // reverse the row and continue
        // This means we will only ever draw result points *once* in the life of this method
        // since we want to avoid drawing the wrong points after flipping the row, and,
        // don't want to clutter with noise from every single row scan -- just the scans
        // that start on the center line.
        if (hints != null && hints.containsKey(DecodeHintType.NEED_RESULT_POINT_CALLBACK)) {
          Map<DecodeHintType,Object> newHints = new EnumMap<>(DecodeHintType.class);
          newHints.putAll(hints);
          newHints.remove(DecodeHintType.NEED_RESULT_POINT_CALLBACK);
          hints = newHints;
        }
      }
      try {
        // Look for a barcode
        Result result = decodeRow(rowNumber, row, hints);
        // We found our barcode
        if (attempt == 1) {
          // But it was upside down, so note that
          result.putMetadata(ResultMetadataType.ORIENTATION, 180);
          // And remember to flip the result points horizontally.
          ResultPoint[] points = result.getResultPoints();
          if (points != null) {
            points[0] = new ResultPoint(width - points[0].getX() - 1, points[0].getY());
            points[1] = new ResultPoint(width - points[1].getX() - 1, points[1].getY());
          }
        }
        return result;
      } catch (ReaderException re) {
        // continue -- just couldn't decode this row
      }
    }
  }

  throw NotFoundException.getNotFoundInstance();
}
 
private void doDecodeMultiple(BinaryBitmap image,
                              Map<DecodeHintType,?> hints,
                              List<Result> results,
                              int xOffset,
                              int yOffset,
                              int currentDepth) {
  if (currentDepth > MAX_DEPTH) {
    return;
  }
  
  Result result;
  try {
    result = delegate.decode(image, hints);
  } catch (ReaderException ignored) {
    return;
  }
  boolean alreadyFound = false;
  for (Result existingResult : results) {
    if (existingResult.getText().equals(result.getText())) {
      alreadyFound = true;
      break;
    }
  }
  if (!alreadyFound) {
    results.add(translateResultPoints(result, xOffset, yOffset));
  }
  ResultPoint[] resultPoints = result.getResultPoints();
  if (resultPoints == null || resultPoints.length == 0) {
    return;
  }
  int width = image.getWidth();
  int height = image.getHeight();
  float minX = width;
  float minY = height;
  float maxX = 0.0f;
  float maxY = 0.0f;
  for (ResultPoint point : resultPoints) {
    if (point == null) {
      continue;
    }
    float x = point.getX();
    float y = point.getY();
    if (x < minX) {
      minX = x;
    }
    if (y < minY) {
      minY = y;
    }
    if (x > maxX) {
      maxX = x;
    }
    if (y > maxY) {
      maxY = y;
    }
  }

  // Decode left of barcode
  if (minX > MIN_DIMENSION_TO_RECUR) {
    doDecodeMultiple(image.crop(0, 0, (int) minX, height),
                     hints, results, 
                     xOffset, yOffset, 
                     currentDepth + 1);
  }
  // Decode above barcode
  if (minY > MIN_DIMENSION_TO_RECUR) {
    doDecodeMultiple(image.crop(0, 0, width, (int) minY),
                     hints, results, 
                     xOffset, yOffset, 
                     currentDepth + 1);
  }
  // Decode right of barcode
  if (maxX < width - MIN_DIMENSION_TO_RECUR) {
    doDecodeMultiple(image.crop((int) maxX, 0, width - (int) maxX, height),
                     hints, results, 
                     xOffset + (int) maxX, yOffset, 
                     currentDepth + 1);
  }
  // Decode below barcode
  if (maxY < height - MIN_DIMENSION_TO_RECUR) {
    doDecodeMultiple(image.crop(0, (int) maxY, width, height - (int) maxY),
                     hints, results, 
                     xOffset, yOffset + (int) maxY, 
                     currentDepth + 1);
  }
}
 
源代码11 项目: weex   文件: OneDReader.java
/**
 * We're going to examine rows from the middle outward, searching alternately above and below the
 * middle, and farther out each time. rowStep is the number of rows between each successive
 * attempt above and below the middle. So we'd scan row middle, then middle - rowStep, then
 * middle + rowStep, then middle - (2 * rowStep), etc.
 * rowStep is bigger as the image is taller, but is always at least 1. We've somewhat arbitrarily
 * decided that moving up and down by about 1/16 of the image is pretty good; we try more of the
 * image if "trying harder".
 *
 * @param image The image to decode
 * @param hints Any hints that were requested
 * @return The contents of the decoded barcode
 * @throws NotFoundException Any spontaneous errors which occur
 */
private Result doDecode(BinaryBitmap image,
                        Map<DecodeHintType,?> hints) throws NotFoundException {
  int width = image.getWidth();
  int height = image.getHeight();
  BitArray row = new BitArray(width);

  int middle = height >> 1;
  boolean tryHarder = hints != null && hints.containsKey(DecodeHintType.TRY_HARDER);
  int rowStep = Math.max(1, height >> (tryHarder ? 8 : 5));
  int maxLines;
  if (tryHarder) {
    maxLines = height; // Look at the whole image, not just the center
  } else {
    maxLines = 15; // 15 rows spaced 1/32 apart is roughly the middle half of the image
  }

  for (int x = 0; x < maxLines; x++) {

    // Scanning from the middle out. Determine which row we're looking at next:
    int rowStepsAboveOrBelow = (x + 1) / 2;
    boolean isAbove = (x & 0x01) == 0; // i.e. is x even?
    int rowNumber = middle + rowStep * (isAbove ? rowStepsAboveOrBelow : -rowStepsAboveOrBelow);
    if (rowNumber < 0 || rowNumber >= height) {
      // Oops, if we run off the top or bottom, stop
      break;
    }

    // Estimate black point for this row and load it:
    try {
      row = image.getBlackRow(rowNumber, row);
    } catch (NotFoundException ignored) {
      continue;
    }

    // While we have the image data in a BitArray, it's fairly cheap to reverse it in place to
    // handle decoding upside down barcodes.
    for (int attempt = 0; attempt < 2; attempt++) {
      if (attempt == 1) { // trying again?
        row.reverse(); // reverse the row and continue
        // This means we will only ever draw result points *once* in the life of this method
        // since we want to avoid drawing the wrong points after flipping the row, and,
        // don't want to clutter with noise from every single row scan -- just the scans
        // that start on the center line.
        if (hints != null && hints.containsKey(DecodeHintType.NEED_RESULT_POINT_CALLBACK)) {
          Map<DecodeHintType,Object> newHints = new EnumMap<>(DecodeHintType.class);
          newHints.putAll(hints);
          newHints.remove(DecodeHintType.NEED_RESULT_POINT_CALLBACK);
          hints = newHints;
        }
      }
      try {
        // Look for a barcode
        Result result = decodeRow(rowNumber, row, hints);
        // We found our barcode
        if (attempt == 1) {
          // But it was upside down, so note that
          result.putMetadata(ResultMetadataType.ORIENTATION, 180);
          // And remember to flip the result points horizontally.
          ResultPoint[] points = result.getResultPoints();
          if (points != null) {
            points[0] = new ResultPoint(width - points[0].getX() - 1, points[0].getY());
            points[1] = new ResultPoint(width - points[1].getX() - 1, points[1].getY());
          }
        }
        return result;
      } catch (ReaderException re) {
        // continue -- just couldn't decode this row
      }
    }
  }

  throw NotFoundException.getNotFoundInstance();
}
 
源代码12 项目: weex   文件: GenericMultipleBarcodeReader.java
private void doDecodeMultiple(BinaryBitmap image,
                              Map<DecodeHintType,?> hints,
                              List<Result> results,
                              int xOffset,
                              int yOffset,
                              int currentDepth) {
  if (currentDepth > MAX_DEPTH) {
    return;
  }
  
  Result result;
  try {
    result = delegate.decode(image, hints);
  } catch (ReaderException ignored) {
    return;
  }
  boolean alreadyFound = false;
  for (Result existingResult : results) {
    if (existingResult.getText().equals(result.getText())) {
      alreadyFound = true;
      break;
    }
  }
  if (!alreadyFound) {
    results.add(translateResultPoints(result, xOffset, yOffset));
  }
  ResultPoint[] resultPoints = result.getResultPoints();
  if (resultPoints == null || resultPoints.length == 0) {
    return;
  }
  int width = image.getWidth();
  int height = image.getHeight();
  float minX = width;
  float minY = height;
  float maxX = 0.0f;
  float maxY = 0.0f;
  for (ResultPoint point : resultPoints) {
    if (point == null) {
      continue;
    }
    float x = point.getX();
    float y = point.getY();
    if (x < minX) {
      minX = x;
    }
    if (y < minY) {
      minY = y;
    }
    if (x > maxX) {
      maxX = x;
    }
    if (y > maxY) {
      maxY = y;
    }
  }

  // Decode left of barcode
  if (minX > MIN_DIMENSION_TO_RECUR) {
    doDecodeMultiple(image.crop(0, 0, (int) minX, height),
                     hints, results, 
                     xOffset, yOffset, 
                     currentDepth + 1);
  }
  // Decode above barcode
  if (minY > MIN_DIMENSION_TO_RECUR) {
    doDecodeMultiple(image.crop(0, 0, width, (int) minY),
                     hints, results, 
                     xOffset, yOffset, 
                     currentDepth + 1);
  }
  // Decode right of barcode
  if (maxX < width - MIN_DIMENSION_TO_RECUR) {
    doDecodeMultiple(image.crop((int) maxX, 0, width - (int) maxX, height),
                     hints, results, 
                     xOffset + (int) maxX, yOffset, 
                     currentDepth + 1);
  }
  // Decode below barcode
  if (maxY < height - MIN_DIMENSION_TO_RECUR) {
    doDecodeMultiple(image.crop(0, (int) maxY, width, height - (int) maxY),
                     hints, results, 
                     xOffset, yOffset + (int) maxY, 
                     currentDepth + 1);
  }
}
 
源代码13 项目: barcodescanner-lib-aar   文件: OneDReader.java
/**
 * We're going to examine rows from the middle outward, searching alternately above and below the
 * middle, and farther out each time. rowStep is the number of rows between each successive
 * attempt above and below the middle. So we'd scan row middle, then middle - rowStep, then
 * middle + rowStep, then middle - (2 * rowStep), etc.
 * rowStep is bigger as the image is taller, but is always at least 1. We've somewhat arbitrarily
 * decided that moving up and down by about 1/16 of the image is pretty good; we try more of the
 * image if "trying harder".
 *
 * @param image The image to decode
 * @param hints Any hints that were requested
 * @return The contents of the decoded barcode
 * @throws NotFoundException Any spontaneous errors which occur
 */
private Result doDecode(BinaryBitmap image,
                        Map<DecodeHintType,?> hints) throws NotFoundException {
  int width = image.getWidth();
  int height = image.getHeight();
  BitArray row = new BitArray(width);

  int middle = height >> 1;
  boolean tryHarder = hints != null && hints.containsKey(DecodeHintType.TRY_HARDER);
  int rowStep = Math.max(1, height >> (tryHarder ? 8 : 5));
  int maxLines;
  if (tryHarder) {
    maxLines = height; // Look at the whole image, not just the center
  } else {
    maxLines = 15; // 15 rows spaced 1/32 apart is roughly the middle half of the image
  }

  for (int x = 0; x < maxLines; x++) {

    // Scanning from the middle out. Determine which row we're looking at next:
    int rowStepsAboveOrBelow = (x + 1) / 2;
    boolean isAbove = (x & 0x01) == 0; // i.e. is x even?
    int rowNumber = middle + rowStep * (isAbove ? rowStepsAboveOrBelow : -rowStepsAboveOrBelow);
    if (rowNumber < 0 || rowNumber >= height) {
      // Oops, if we run off the top or bottom, stop
      break;
    }

    // Estimate black point for this row and load it:
    try {
      row = image.getBlackRow(rowNumber, row);
    } catch (NotFoundException ignored) {
      continue;
    }

    // While we have the image data in a BitArray, it's fairly cheap to reverse it in place to
    // handle decoding upside down barcodes.
    for (int attempt = 0; attempt < 2; attempt++) {
      if (attempt == 1) { // trying again?
        row.reverse(); // reverse the row and continue
        // This means we will only ever draw result points *once* in the life of this method
        // since we want to avoid drawing the wrong points after flipping the row, and,
        // don't want to clutter with noise from every single row scan -- just the scans
        // that start on the center line.
        if (hints != null && hints.containsKey(DecodeHintType.NEED_RESULT_POINT_CALLBACK)) {
          Map<DecodeHintType,Object> newHints = new EnumMap<>(DecodeHintType.class);
          newHints.putAll(hints);
          newHints.remove(DecodeHintType.NEED_RESULT_POINT_CALLBACK);
          hints = newHints;
        }
      }
      try {
        // Look for a barcode
        Result result = decodeRow(rowNumber, row, hints);
        // We found our barcode
        if (attempt == 1) {
          // But it was upside down, so note that
          result.putMetadata(ResultMetadataType.ORIENTATION, 180);
          // And remember to flip the result points horizontally.
          ResultPoint[] points = result.getResultPoints();
          if (points != null) {
            points[0] = new ResultPoint(width - points[0].getX() - 1, points[0].getY());
            points[1] = new ResultPoint(width - points[1].getX() - 1, points[1].getY());
          }
        }
        return result;
      } catch (ReaderException re) {
        // continue -- just couldn't decode this row
      }
    }
  }

  throw NotFoundException.getNotFoundInstance();
}
 
private void doDecodeMultiple(BinaryBitmap image,
                              Map<DecodeHintType,?> hints,
                              List<Result> results,
                              int xOffset,
                              int yOffset,
                              int currentDepth) {
  if (currentDepth > MAX_DEPTH) {
    return;
  }
  
  Result result;
  try {
    result = delegate.decode(image, hints);
  } catch (ReaderException ignored) {
    return;
  }
  boolean alreadyFound = false;
  for (Result existingResult : results) {
    if (existingResult.getText().equals(result.getText())) {
      alreadyFound = true;
      break;
    }
  }
  if (!alreadyFound) {
    results.add(translateResultPoints(result, xOffset, yOffset));
  }
  ResultPoint[] resultPoints = result.getResultPoints();
  if (resultPoints == null || resultPoints.length == 0) {
    return;
  }
  int width = image.getWidth();
  int height = image.getHeight();
  float minX = width;
  float minY = height;
  float maxX = 0.0f;
  float maxY = 0.0f;
  for (ResultPoint point : resultPoints) {
    if (point == null) {
      continue;
    }
    float x = point.getX();
    float y = point.getY();
    if (x < minX) {
      minX = x;
    }
    if (y < minY) {
      minY = y;
    }
    if (x > maxX) {
      maxX = x;
    }
    if (y > maxY) {
      maxY = y;
    }
  }

  // Decode left of barcode
  if (minX > MIN_DIMENSION_TO_RECUR) {
    doDecodeMultiple(image.crop(0, 0, (int) minX, height),
                     hints, results, 
                     xOffset, yOffset, 
                     currentDepth + 1);
  }
  // Decode above barcode
  if (minY > MIN_DIMENSION_TO_RECUR) {
    doDecodeMultiple(image.crop(0, 0, width, (int) minY),
                     hints, results, 
                     xOffset, yOffset, 
                     currentDepth + 1);
  }
  // Decode right of barcode
  if (maxX < width - MIN_DIMENSION_TO_RECUR) {
    doDecodeMultiple(image.crop((int) maxX, 0, width - (int) maxX, height),
                     hints, results, 
                     xOffset + (int) maxX, yOffset, 
                     currentDepth + 1);
  }
  // Decode below barcode
  if (maxY < height - MIN_DIMENSION_TO_RECUR) {
    doDecodeMultiple(image.crop(0, (int) maxY, width, height - (int) maxY),
                     hints, results, 
                     xOffset, yOffset + (int) maxY, 
                     currentDepth + 1);
  }
}
 
源代码15 项目: reacteu-app   文件: OneDReader.java
/**
 * We're going to examine rows from the middle outward, searching alternately above and below the
 * middle, and farther out each time. rowStep is the number of rows between each successive
 * attempt above and below the middle. So we'd scan row middle, then middle - rowStep, then
 * middle + rowStep, then middle - (2 * rowStep), etc.
 * rowStep is bigger as the image is taller, but is always at least 1. We've somewhat arbitrarily
 * decided that moving up and down by about 1/16 of the image is pretty good; we try more of the
 * image if "trying harder".
 *
 * @param image The image to decode
 * @param hints Any hints that were requested
 * @return The contents of the decoded barcode
 * @throws NotFoundException Any spontaneous errors which occur
 */
private Result doDecode(BinaryBitmap image,
                        Map<DecodeHintType,?> hints) throws NotFoundException {
  int width = image.getWidth();
  int height = image.getHeight();
  BitArray row = new BitArray(width);

  int middle = height >> 1;
  boolean tryHarder = hints != null && hints.containsKey(DecodeHintType.TRY_HARDER);
  int rowStep = Math.max(1, height >> (tryHarder ? 8 : 5));
  int maxLines;
  if (tryHarder) {
    maxLines = height; // Look at the whole image, not just the center
  } else {
    maxLines = 15; // 15 rows spaced 1/32 apart is roughly the middle half of the image
  }

  for (int x = 0; x < maxLines; x++) {

    // Scanning from the middle out. Determine which row we're looking at next:
    int rowStepsAboveOrBelow = (x + 1) >> 1;
    boolean isAbove = (x & 0x01) == 0; // i.e. is x even?
    int rowNumber = middle + rowStep * (isAbove ? rowStepsAboveOrBelow : -rowStepsAboveOrBelow);
    if (rowNumber < 0 || rowNumber >= height) {
      // Oops, if we run off the top or bottom, stop
      break;
    }

    // Estimate black point for this row and load it:
    try {
      row = image.getBlackRow(rowNumber, row);
    } catch (NotFoundException nfe) {
      continue;
    }

    // While we have the image data in a BitArray, it's fairly cheap to reverse it in place to
    // handle decoding upside down barcodes.
    for (int attempt = 0; attempt < 2; attempt++) {
      if (attempt == 1) { // trying again?
        row.reverse(); // reverse the row and continue
        // This means we will only ever draw result points *once* in the life of this method
        // since we want to avoid drawing the wrong points after flipping the row, and,
        // don't want to clutter with noise from every single row scan -- just the scans
        // that start on the center line.
        if (hints != null && hints.containsKey(DecodeHintType.NEED_RESULT_POINT_CALLBACK)) {
          Map<DecodeHintType,Object> newHints = new EnumMap<DecodeHintType,Object>(DecodeHintType.class);
          newHints.putAll(hints);
          newHints.remove(DecodeHintType.NEED_RESULT_POINT_CALLBACK);
          hints = newHints;
        }
      }
      try {
        // Look for a barcode
        Result result = decodeRow(rowNumber, row, hints);
        // We found our barcode
        if (attempt == 1) {
          // But it was upside down, so note that
          result.putMetadata(ResultMetadataType.ORIENTATION, 180);
          // And remember to flip the result points horizontally.
          ResultPoint[] points = result.getResultPoints();
          if (points != null) {
            points[0] = new ResultPoint(width - points[0].getX() - 1, points[0].getY());
            points[1] = new ResultPoint(width - points[1].getX() - 1, points[1].getY());
          }
        }
        return result;
      } catch (ReaderException re) {
        // continue -- just couldn't decode this row
      }
    }
  }

  throw NotFoundException.getNotFoundInstance();
}
 
源代码16 项目: reacteu-app   文件: GenericMultipleBarcodeReader.java
private void doDecodeMultiple(BinaryBitmap image,
                              Map<DecodeHintType,?> hints,
                              List<Result> results,
                              int xOffset,
                              int yOffset) {
  Result result;
  try {
    result = delegate.decode(image, hints);
  } catch (ReaderException re) {
    return;
  }
  boolean alreadyFound = false;
  for (Result existingResult : results) {
    if (existingResult.getText().equals(result.getText())) {
      alreadyFound = true;
      break;
    }
  }
  if (alreadyFound) {
    return;
  }
  results.add(translateResultPoints(result, xOffset, yOffset));
  ResultPoint[] resultPoints = result.getResultPoints();
  if (resultPoints == null || resultPoints.length == 0) {
    return;
  }
  int width = image.getWidth();
  int height = image.getHeight();
  float minX = width;
  float minY = height;
  float maxX = 0.0f;
  float maxY = 0.0f;
  for (ResultPoint point : resultPoints) {
    float x = point.getX();
    float y = point.getY();
    if (x < minX) {
      minX = x;
    }
    if (y < minY) {
      minY = y;
    }
    if (x > maxX) {
      maxX = x;
    }
    if (y > maxY) {
      maxY = y;
    }
  }

  // Decode left of barcode
  if (minX > MIN_DIMENSION_TO_RECUR) {
    doDecodeMultiple(image.crop(0, 0, (int) minX, height),
                     hints, results, xOffset, yOffset);
  }
  // Decode above barcode
  if (minY > MIN_DIMENSION_TO_RECUR) {
    doDecodeMultiple(image.crop(0, 0, width, (int) minY),
                     hints, results, xOffset, yOffset);
  }
  // Decode right of barcode
  if (maxX < width - MIN_DIMENSION_TO_RECUR) {
    doDecodeMultiple(image.crop((int) maxX, 0, width - (int) maxX, height),
                     hints, results, xOffset + (int) maxX, yOffset);
  }
  // Decode below barcode
  if (maxY < height - MIN_DIMENSION_TO_RECUR) {
    doDecodeMultiple(image.crop(0, (int) maxY, width, height - (int) maxY),
                     hints, results, xOffset, yOffset + (int) maxY);
  }
}
 
源代码17 项目: MiBandDecompiled   文件: ByQuadrantReader.java
public Result decode(BinaryBitmap binarybitmap, Map map)
{
    int i = binarybitmap.getWidth();
    int j = binarybitmap.getHeight();
    int k = i / 2;
    int l = j / 2;
    BinaryBitmap binarybitmap1 = binarybitmap.crop(0, 0, k, l);
    Result result3;
    try
    {
        result3 = a.decode(binarybitmap1, map);
    }
    catch (NotFoundException notfoundexception)
    {
        BinaryBitmap binarybitmap2 = binarybitmap.crop(k, 0, k, l);
        Result result2;
        try
        {
            result2 = a.decode(binarybitmap2, map);
        }
        catch (NotFoundException notfoundexception1)
        {
            BinaryBitmap binarybitmap3 = binarybitmap.crop(0, l, k, l);
            Result result1;
            try
            {
                result1 = a.decode(binarybitmap3, map);
            }
            catch (NotFoundException notfoundexception2)
            {
                BinaryBitmap binarybitmap4 = binarybitmap.crop(k, l, k, l);
                Result result;
                try
                {
                    result = a.decode(binarybitmap4, map);
                }
                catch (NotFoundException notfoundexception3)
                {
                    BinaryBitmap binarybitmap5 = binarybitmap.crop(k / 2, l / 2, k, l);
                    return a.decode(binarybitmap5, map);
                }
                return result;
            }
            return result1;
        }
        return result2;
    }
    return result3;
}
 
源代码18 项目: RipplePower   文件: OneDReader.java
/**
 * We're going to examine rows from the middle outward, searching
 * alternately above and below the middle, and farther out each time.
 * rowStep is the number of rows between each successive attempt above and
 * below the middle. So we'd scan row middle, then middle - rowStep, then
 * middle + rowStep, then middle - (2 * rowStep), etc. rowStep is bigger as
 * the image is taller, but is always at least 1. We've somewhat arbitrarily
 * decided that moving up and down by about 1/16 of the image is pretty
 * good; we try more of the image if "trying harder".
 * 
 * @param image
 *            The image to decode
 * @param hints
 *            Any hints that were requested
 * @return The contents of the decoded barcode
 * @throws NotFoundException
 *             Any spontaneous errors which occur
 */
private Result doDecode(BinaryBitmap image, Map<DecodeHintType, ?> hints) throws NotFoundException {
	int width = image.getWidth();
	int height = image.getHeight();
	BitArray row = new BitArray(width);

	int middle = height >> 1;
	boolean tryHarder = hints != null && hints.containsKey(DecodeHintType.TRY_HARDER);
	int rowStep = Math.max(1, height >> (tryHarder ? 8 : 5));
	int maxLines;
	if (tryHarder) {
		maxLines = height; // Look at the whole image, not just the center
	} else {
		maxLines = 15; // 15 rows spaced 1/32 apart is roughly the middle
						// half of the image
	}

	for (int x = 0; x < maxLines; x++) {

		// Scanning from the middle out. Determine which row we're looking
		// at next:
		int rowStepsAboveOrBelow = (x + 1) / 2;
		boolean isAbove = (x & 0x01) == 0; // i.e. is x even?
		int rowNumber = middle + rowStep * (isAbove ? rowStepsAboveOrBelow : -rowStepsAboveOrBelow);
		if (rowNumber < 0 || rowNumber >= height) {
			// Oops, if we run off the top or bottom, stop
			break;
		}

		// Estimate black point for this row and load it:
		try {
			row = image.getBlackRow(rowNumber, row);
		} catch (NotFoundException ignored) {
			continue;
		}

		// While we have the image data in a BitArray, it's fairly cheap to
		// reverse it in place to
		// handle decoding upside down barcodes.
		for (int attempt = 0; attempt < 2; attempt++) {
			if (attempt == 1) { // trying again?
				row.reverse(); // reverse the row and continue
				// This means we will only ever draw result points *once* in
				// the life of this method
				// since we want to avoid drawing the wrong points after
				// flipping the row, and,
				// don't want to clutter with noise from every single row
				// scan -- just the scans
				// that start on the center line.
				if (hints != null && hints.containsKey(DecodeHintType.NEED_RESULT_POINT_CALLBACK)) {
					Map<DecodeHintType, Object> newHints = new EnumMap<>(DecodeHintType.class);
					newHints.putAll(hints);
					newHints.remove(DecodeHintType.NEED_RESULT_POINT_CALLBACK);
					hints = newHints;
				}
			}
			try {
				// Look for a barcode
				Result result = decodeRow(rowNumber, row, hints);
				// We found our barcode
				if (attempt == 1) {
					// But it was upside down, so note that
					result.putMetadata(ResultMetadataType.ORIENTATION, 180);
					// And remember to flip the result points horizontally.
					ResultPoint[] points = result.getResultPoints();
					if (points != null) {
						points[0] = new ResultPoint(width - points[0].getX() - 1, points[0].getY());
						points[1] = new ResultPoint(width - points[1].getX() - 1, points[1].getY());
					}
				}
				return result;
			} catch (ReaderException re) {
				// continue -- just couldn't decode this row
			}
		}
	}

	throw NotFoundException.getNotFoundInstance();
}
 
private void doDecodeMultiple(BinaryBitmap image,
                              Map<DecodeHintType,?> hints,
                              List<Result> results,
                              int xOffset,
                              int yOffset,
                              int currentDepth) {
  if (currentDepth > MAX_DEPTH) {
    return;
  }

  Result result;
  try {
    result = delegate.decode(image, hints);
  } catch (ReaderException ignored) {
    return;
  }
  boolean alreadyFound = false;
  for (Result existingResult : results) {
    if (existingResult.getText().equals(result.getText())) {
      alreadyFound = true;
      break;
    }
  }
  if (!alreadyFound) {
    results.add(translateResultPoints(result, xOffset, yOffset));
  }
  ResultPoint[] resultPoints = result.getResultPoints();
  if (resultPoints == null || resultPoints.length == 0) {
    return;
  }
  int width = image.getWidth();
  int height = image.getHeight();
  float minX = width;
  float minY = height;
  float maxX = 0.0f;
  float maxY = 0.0f;
  for (ResultPoint point : resultPoints) {
    if (point == null) {
      continue;
    }
    float x = point.getX();
    float y = point.getY();
    if (x < minX) {
      minX = x;
    }
    if (y < minY) {
      minY = y;
    }
    if (x > maxX) {
      maxX = x;
    }
    if (y > maxY) {
      maxY = y;
    }
  }

  // Decode left of barcode
  if (minX > MIN_DIMENSION_TO_RECUR) {
    doDecodeMultiple(image.crop(0, 0, (int) minX, height),
                     hints, results,
                     xOffset, yOffset,
                     currentDepth + 1);
  }
  // Decode above barcode
  if (minY > MIN_DIMENSION_TO_RECUR) {
    doDecodeMultiple(image.crop(0, 0, width, (int) minY),
                     hints, results,
                     xOffset, yOffset,
                     currentDepth + 1);
  }
  // Decode right of barcode
  if (maxX < width - MIN_DIMENSION_TO_RECUR) {
    doDecodeMultiple(image.crop((int) maxX, 0, width - (int) maxX, height),
                     hints, results,
                     xOffset + (int) maxX, yOffset,
                     currentDepth + 1);
  }
  // Decode below barcode
  if (maxY < height - MIN_DIMENSION_TO_RECUR) {
    doDecodeMultiple(image.crop(0, (int) maxY, width, height - (int) maxY),
                     hints, results,
                     xOffset, yOffset + (int) maxY,
                     currentDepth + 1);
  }
}
 
源代码20 项目: Telegram   文件: GenericMultipleBarcodeReader.java
private void doDecodeMultiple(BinaryBitmap image,
                              Map<DecodeHintType,?> hints,
                              List<Result> results,
                              int xOffset,
                              int yOffset,
                              int currentDepth) {
  if (currentDepth > MAX_DEPTH) {
    return;
  }

  Result result;
  try {
    result = delegate.decode(image, hints);
  } catch (ReaderException ignored) {
    return;
  }
  boolean alreadyFound = false;
  for (Result existingResult : results) {
    if (existingResult.getText().equals(result.getText())) {
      alreadyFound = true;
      break;
    }
  }
  if (!alreadyFound) {
    results.add(translateResultPoints(result, xOffset, yOffset));
  }
  ResultPoint[] resultPoints = result.getResultPoints();
  if (resultPoints == null || resultPoints.length == 0) {
    return;
  }
  int width = image.getWidth();
  int height = image.getHeight();
  float minX = width;
  float minY = height;
  float maxX = 0.0f;
  float maxY = 0.0f;
  for (ResultPoint point : resultPoints) {
    if (point == null) {
      continue;
    }
    float x = point.getX();
    float y = point.getY();
    if (x < minX) {
      minX = x;
    }
    if (y < minY) {
      minY = y;
    }
    if (x > maxX) {
      maxX = x;
    }
    if (y > maxY) {
      maxY = y;
    }
  }

  // Decode left of barcode
  if (minX > MIN_DIMENSION_TO_RECUR) {
    doDecodeMultiple(image.crop(0, 0, (int) minX, height),
                     hints, results,
                     xOffset, yOffset,
                     currentDepth + 1);
  }
  // Decode above barcode
  if (minY > MIN_DIMENSION_TO_RECUR) {
    doDecodeMultiple(image.crop(0, 0, width, (int) minY),
                     hints, results,
                     xOffset, yOffset,
                     currentDepth + 1);
  }
  // Decode right of barcode
  if (maxX < width - MIN_DIMENSION_TO_RECUR) {
    doDecodeMultiple(image.crop((int) maxX, 0, width - (int) maxX, height),
                     hints, results,
                     xOffset + (int) maxX, yOffset,
                     currentDepth + 1);
  }
  // Decode below barcode
  if (maxY < height - MIN_DIMENSION_TO_RECUR) {
    doDecodeMultiple(image.crop(0, (int) maxY, width, height - (int) maxY),
                     hints, results,
                     xOffset, yOffset + (int) maxY,
                     currentDepth + 1);
  }
}