Java Code Examples for com.google.zxing.Result#getResultPoints()

The following examples show how to use com.google.zxing.Result#getResultPoints() . 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: GenericMultipleBarcodeReader.java    From Telegram with GNU General Public License v2.0 6 votes vote down vote up
private static Result translateResultPoints(Result result, int xOffset, int yOffset) {
  ResultPoint[] oldResultPoints = result.getResultPoints();
  if (oldResultPoints == null) {
    return result;
  }
  ResultPoint[] newResultPoints = new ResultPoint[oldResultPoints.length];
  for (int i = 0; i < oldResultPoints.length; i++) {
    ResultPoint oldPoint = oldResultPoints[i];
    if (oldPoint != null) {
      newResultPoints[i] = new ResultPoint(oldPoint.getX() + xOffset, oldPoint.getY() + yOffset);
    }
  }
  Result newResult = new Result(result.getText(),
                                result.getRawBytes(),
                                result.getNumBits(),
                                newResultPoints,
                                result.getBarcodeFormat(),
                                result.getTimestamp());
  newResult.putAllMetadata(result.getResultMetadata());
  return newResult;
}
 
Example 2
Source File: ScanForResultActivity.java    From green_android with GNU General Public License v3.0 6 votes vote down vote up
public void handleResult(final Result scanResult, final Bitmap thumbnailImage, final float thumbnailScaleFactor)
{
    // superimpose dots to highlight the key features of the qr code
    final ResultPoint[] points = scanResult.getResultPoints();
    if (points != null && points.length > 0)
    {
        final Paint paint = new Paint();
        paint.setColor(getResources().getColor(R.color.scan_result_dots));
        paint.setStrokeWidth(10.0f);

        final Canvas canvas = new Canvas(thumbnailImage);
        canvas.scale(thumbnailScaleFactor, thumbnailScaleFactor);
        for (final ResultPoint point : points)
            canvas.drawPoint(point.getX(), point.getY(), paint);
    }

    scannerView.setIsResult(true);
    final Intent result = new Intent();
    result.putExtra(INTENT_EXTRA_RESULT, scanResult.getText());
    setResult(RESULT_OK, result);
    finish();
}
 
Example 3
Source File: BarCodeScannerFragment.java    From zxingfragmentlib with Apache License 2.0 6 votes vote down vote up
/**
 * Superimpose a line for 1D or dots for 2D to highlight the key features of the barcode.
 *
 * @param barcode   A bitmap of the captured image.
 * @param scaleFactor amount by which thumbnail was scaled
 * @param rawResult The decoded results which contains the points to draw.
 */
private void drawResultPoints(Bitmap barcode, float scaleFactor, Result rawResult) {
  ResultPoint[] points = rawResult.getResultPoints();
  if (points != null && points.length > 0) {
    Canvas canvas = new Canvas(barcode);
    Paint paint = new Paint();
    paint.setColor(getResources().getColor(R.color.result_points));
    if (points.length == 2) {
      paint.setStrokeWidth(4.0f);
      drawLine(canvas, paint, points[0], points[1], scaleFactor);
    } else if (points.length == 4 &&
            (rawResult.getBarcodeFormat() == BarcodeFormat.UPC_A ||
                    rawResult.getBarcodeFormat() == BarcodeFormat.EAN_13)) {
      // Hacky special case -- draw two lines, for the barcode and metadata
      drawLine(canvas, paint, points[0], points[1], scaleFactor);
      drawLine(canvas, paint, points[2], points[3], scaleFactor);
    } else {
      paint.setStrokeWidth(10.0f);
      for (ResultPoint point : points) {
        if (point != null) {
          canvas.drawPoint(scaleFactor * point.getX(), scaleFactor * point.getY(), paint);
        }
      }
    }
  }
}
 
Example 4
Source File: CaptureActivity.java    From reacteu-app with MIT License 6 votes vote down vote up
/**
 * Superimpose a line for 1D or dots for 2D to highlight the key features of the barcode.
 *
 * @param barcode   A bitmap of the captured image.
 * @param rawResult The decoded results which contains the points to draw.
 */
private void drawResultPoints(Bitmap barcode, Result rawResult) {
  ResultPoint[] points = rawResult.getResultPoints();
  if (points != null && points.length > 0) {
    Canvas canvas = new Canvas(barcode);
    Paint paint = new Paint();
    paint.setColor(getResources().getColor(fakeR.getId("color", "result_points")));
    if (points.length == 2) {
      paint.setStrokeWidth(4.0f);
      drawLine(canvas, paint, points[0], points[1]);
    } else if (points.length == 4 &&
               (rawResult.getBarcodeFormat() == BarcodeFormat.UPC_A ||
                rawResult.getBarcodeFormat() == BarcodeFormat.EAN_13)) {
      // Hacky special case -- draw two lines, for the barcode and metadata
      drawLine(canvas, paint, points[0], points[1]);
      drawLine(canvas, paint, points[2], points[3]);
    } else {
      paint.setStrokeWidth(10.0f);
      for (ResultPoint point : points) {
        canvas.drawPoint(point.getX(), point.getY(), paint);
      }
    }
  }
}
 
Example 5
Source File: GenericMultipleBarcodeReader.java    From QrCodeScanner with GNU General Public License v3.0 6 votes vote down vote up
private static Result translateResultPoints(Result result, int xOffset, int yOffset) {
  ResultPoint[] oldResultPoints = result.getResultPoints();
  if (oldResultPoints == null) {
    return result;
  }
  ResultPoint[] newResultPoints = new ResultPoint[oldResultPoints.length];
  for (int i = 0; i < oldResultPoints.length; i++) {
    ResultPoint oldPoint = oldResultPoints[i];
    if (oldPoint != null) {
      newResultPoints[i] = new ResultPoint(oldPoint.getX() + xOffset, oldPoint.getY() + yOffset);
    }
  }
  Result newResult = new Result(result.getText(),
                                result.getRawBytes(),
                                result.getNumBits(),
                                newResultPoints,
                                result.getBarcodeFormat(),
                                result.getTimestamp());
  newResult.putAllMetadata(result.getResultMetadata());
  return newResult;
}
 
Example 6
Source File: UPCAReader.java    From analyzer-of-android-for-Apache-Weex with Apache License 2.0 5 votes vote down vote up
private static Result maybeReturnResult(Result result) throws FormatException {
  String text = result.getText();
  if (text.charAt(0) == '0') {
    return new Result(text.substring(1), null, result.getResultPoints(), BarcodeFormat.UPC_A);
  } else {
    throw FormatException.getFormatInstance();
  }
}
 
Example 7
Source File: CaptureActivity.java    From weex with Apache License 2.0 5 votes vote down vote up
/**
 * Superimpose a line for 1D or dots for 2D to highlight the key features of
 * the barcode.
 * 
 * @param barcode
 *            A bitmap of the captured image.
 * @param scaleFactor
 *            amount by which thumbnail was scaled
 * @param rawResult
 *            The decoded results which contains the points to draw.
 */
private void drawResultPoints(Bitmap barcode, float scaleFactor,
		Result rawResult) {
	ResultPoint[] points = rawResult.getResultPoints();
	if (points != null && points.length > 0) {
		Canvas canvas = new Canvas(barcode);
		Paint paint = new Paint();
		paint.setColor(getResources().getColor(R.color.result_points));
		if (points.length == 2) {
			paint.setStrokeWidth(4.0f);
			drawLine(canvas, paint, points[0], points[1], scaleFactor);
		} else if (points.length == 4
				&& (rawResult.getBarcodeFormat() == BarcodeFormat.UPC_A || rawResult
						.getBarcodeFormat() == BarcodeFormat.EAN_13)) {
			// Hacky special case -- draw two lines, for the barcode and
			// metadata
			drawLine(canvas, paint, points[0], points[1], scaleFactor);
			drawLine(canvas, paint, points[2], points[3], scaleFactor);
		} else {
			paint.setStrokeWidth(10.0f);
			for (ResultPoint point : points) {
				if (point != null) {
					canvas.drawPoint(scaleFactor * point.getX(),
							scaleFactor * point.getY(), paint);
				}
			}
		}
	}
}
 
Example 8
Source File: UPCAReader.java    From Tesseract-OCR-Scanner with Apache License 2.0 5 votes vote down vote up
private static Result maybeReturnResult(Result result) throws FormatException {
  String text = result.getText();
  if (text.charAt(0) == '0') {
    return new Result(text.substring(1), null, result.getResultPoints(), BarcodeFormat.UPC_A);
  } else {
    throw FormatException.getFormatInstance();
  }
}
 
Example 9
Source File: UPCAReader.java    From reacteu-app with MIT License 5 votes vote down vote up
private static Result maybeReturnResult(Result result) throws FormatException {
  String text = result.getText();
  if (text.charAt(0) == '0') {
    return new Result(text.substring(1), null, result.getResultPoints(), BarcodeFormat.UPC_A);
  } else {
    throw FormatException.getFormatInstance();
  }
}
 
Example 10
Source File: ZxingScanView.java    From ProjectX with Apache License 2.0 5 votes vote down vote up
/**
 * 为结果图添加识别效果
 *
 * @param result      扫描结果
 * @param barcode     结果图片
 * @param scaleFactor 缩放比
 * @param color       颜色
 */
@SuppressWarnings("unused")
public static void addResultPoints(Result result, Bitmap barcode, float scaleFactor, int color) {
    ResultPoint[] points = result.getResultPoints();
    if (points != null && points.length > 0) {
        Canvas canvas = new Canvas(barcode);
        Paint paint = new Paint();
        paint.setColor(color);
        if (points.length == 2) {
            paint.setStrokeWidth(4.0f);
            drawLine(canvas, paint, points[0], points[1], scaleFactor);
        } else if (points.length == 4 &&
                (result.getBarcodeFormat() == BarcodeFormat.UPC_A ||
                        result.getBarcodeFormat() == BarcodeFormat.EAN_13)) {
            // Hacky special case -- draw two lines, for the barcode and metadata
            drawLine(canvas, paint, points[0], points[1], scaleFactor);
            drawLine(canvas, paint, points[2], points[3], scaleFactor);
        } else {
            paint.setStrokeWidth(10.0f);
            for (ResultPoint point : points) {
                if (point != null) {
                    canvas.drawPoint(scaleFactor * point.getX(), scaleFactor * point.getY(), paint);
                }
            }
        }
    }
}
 
Example 11
Source File: GenericMultipleBarcodeReader.java    From Telegram with GNU General Public License v2.0 4 votes vote down vote up
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);
  }
}
 
Example 12
Source File: MultiFormatUPCEANReader.java    From ZXing-Orient with Apache License 2.0 4 votes vote down vote up
@Override
public Result decodeRow(int rowNumber,
                        BitArray row,
                        Map<DecodeHintType,?> hints) throws NotFoundException {
  // Compute this location once and reuse it on multiple implementations
  int[] startGuardPattern = UPCEANReader.findStartGuardPattern(row);
  for (UPCEANReader reader : readers) {
    Result result;
    try {
      result = reader.decodeRow(rowNumber, row, startGuardPattern, hints);
    } catch (ReaderException ignored) {
      continue;
    }
    // Special case: a 12-digit code encoded in UPC-A is identical to a "0"
    // followed by those 12 digits encoded as EAN-13. Each will recognize such a code,
    // UPC-A as a 12-digit string and EAN-13 as a 13-digit string starting with "0".
    // Individually these are correct and their readers will both read such a code
    // and correctly call it EAN-13, or UPC-A, respectively.
    //
    // In this case, if we've been looking for both types, we'd like to call it
    // a UPC-A code. But for efficiency we only run the EAN-13 decoder to also read
    // UPC-A. So we special case it here, and convert an EAN-13 result to a UPC-A
    // result if appropriate.
    //
    // But, don't return UPC-A if UPC-A was not a requested format!
    boolean ean13MayBeUPCA =
        result.getBarcodeFormat() == BarcodeFormat.EAN_13 &&
            result.getText().charAt(0) == '0';
    @SuppressWarnings("unchecked")      
    Collection<BarcodeFormat> possibleFormats =
        hints == null ? null : (Collection<BarcodeFormat>) hints.get(DecodeHintType.POSSIBLE_FORMATS);
    boolean canReturnUPCA = possibleFormats == null || possibleFormats.contains(BarcodeFormat.UPC_A);

    if (ean13MayBeUPCA && canReturnUPCA) {
      // Transfer the metdata across
      Result resultUPCA = new Result(result.getText().substring(1),
                                     result.getRawBytes(),
                                     result.getResultPoints(),
                                     BarcodeFormat.UPC_A);
      resultUPCA.putAllMetadata(result.getResultMetadata());
      return resultUPCA;
    }
    return result;
  }

  throw NotFoundException.getNotFoundInstance();
}
 
Example 13
Source File: MultiFormatUPCEANReader.java    From barcodescanner-lib-aar with MIT License 4 votes vote down vote up
@Override
public Result decodeRow(int rowNumber,
                        BitArray row,
                        Map<DecodeHintType,?> hints) throws NotFoundException {
  // Compute this location once and reuse it on multiple implementations
  int[] startGuardPattern = UPCEANReader.findStartGuardPattern(row);
  for (UPCEANReader reader : readers) {
    Result result;
    try {
      result = reader.decodeRow(rowNumber, row, startGuardPattern, hints);
    } catch (ReaderException ignored) {
      continue;
    }
    // Special case: a 12-digit code encoded in UPC-A is identical to a "0"
    // followed by those 12 digits encoded as EAN-13. Each will recognize such a code,
    // UPC-A as a 12-digit string and EAN-13 as a 13-digit string starting with "0".
    // Individually these are correct and their readers will both read such a code
    // and correctly call it EAN-13, or UPC-A, respectively.
    //
    // In this case, if we've been looking for both types, we'd like to call it
    // a UPC-A code. But for efficiency we only run the EAN-13 decoder to also read
    // UPC-A. So we special case it here, and convert an EAN-13 result to a UPC-A
    // result if appropriate.
    //
    // But, don't return UPC-A if UPC-A was not a requested format!
    boolean ean13MayBeUPCA =
        result.getBarcodeFormat() == BarcodeFormat.EAN_13 &&
            result.getText().charAt(0) == '0';
    @SuppressWarnings("unchecked")      
    Collection<BarcodeFormat> possibleFormats =
        hints == null ? null : (Collection<BarcodeFormat>) hints.get(DecodeHintType.POSSIBLE_FORMATS);
    boolean canReturnUPCA = possibleFormats == null || possibleFormats.contains(BarcodeFormat.UPC_A);

    if (ean13MayBeUPCA && canReturnUPCA) {
      // Transfer the metdata across
      Result resultUPCA = new Result(result.getText().substring(1),
                                     result.getRawBytes(),
                                     result.getResultPoints(),
                                     BarcodeFormat.UPC_A);
      resultUPCA.putAllMetadata(result.getResultMetadata());
      return resultUPCA;
    }
    return result;
  }

  throw NotFoundException.getNotFoundInstance();
}
 
Example 14
Source File: OneDReader.java    From analyzer-of-android-for-Apache-Weex with Apache License 2.0 4 votes vote down vote up
/**
 * 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();
}
 
Example 15
Source File: OneDReader.java    From Tesseract-OCR-Scanner with Apache License 2.0 4 votes vote down vote up
/**
 * 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();
}
 
Example 16
Source File: GenericMultipleBarcodeReader.java    From ZXing-Orient with Apache License 2.0 4 votes vote down vote up
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);
  }
}
 
Example 17
Source File: OneDReader.java    From MiBandDecompiled with Apache License 2.0 4 votes vote down vote up
public Result decode(BinaryBitmap binarybitmap, Map map)
{
    Result result1;
    try
    {
        result1 = a(binarybitmap, map);
    }
    catch (NotFoundException notfoundexception)
    {
        boolean flag;
        if (map != null && map.containsKey(DecodeHintType.TRY_HARDER))
        {
            flag = true;
        } else
        {
            flag = false;
        }
        if (flag && binarybitmap.isRotateSupported())
        {
            BinaryBitmap binarybitmap1 = binarybitmap.rotateCounterClockwise();
            Result result = a(binarybitmap1, map);
            Map map1 = result.getResultMetadata();
            int i;
            ResultPoint aresultpoint[];
            if (map1 != null && map1.containsKey(ResultMetadataType.ORIENTATION))
            {
                i = (270 + ((Integer)map1.get(ResultMetadataType.ORIENTATION)).intValue()) % 360;
            } else
            {
                i = 270;
            }
            result.putMetadata(ResultMetadataType.ORIENTATION, Integer.valueOf(i));
            aresultpoint = result.getResultPoints();
            if (aresultpoint != null)
            {
                int j = binarybitmap1.getHeight();
                for (int k = 0; k < aresultpoint.length; k++)
                {
                    aresultpoint[k] = new ResultPoint((float)j - aresultpoint[k].getY() - 1.0F, aresultpoint[k].getX());
                }

            }
            return result;
        } else
        {
            throw notfoundexception;
        }
    }
    return result1;
}
 
Example 18
Source File: GenericMultipleBarcodeReader.java    From reacteu-app with MIT License 4 votes vote down vote up
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);
  }
}
 
Example 19
Source File: MultiFormatUPCEANReader.java    From QrCodeScanner with GNU General Public License v3.0 4 votes vote down vote up
@Override
public Result decodeRow(int rowNumber,
                        BitArray row,
                        Map<DecodeHintType,?> hints) throws NotFoundException {
  // Compute this location once and reuse it on multiple implementations
  int[] startGuardPattern = UPCEANReader.findStartGuardPattern(row);
  for (UPCEANReader reader : readers) {
    try {
      Result result = reader.decodeRow(rowNumber, row, startGuardPattern, hints);
      // Special case: a 12-digit code encoded in UPC-A is identical to a "0"
      // followed by those 12 digits encoded as EAN-13. Each will recognize such a code,
      // UPC-A as a 12-digit string and EAN-13 as a 13-digit string starting with "0".
      // Individually these are correct and their readers will both read such a code
      // and correctly call it EAN-13, or UPC-A, respectively.
      //
      // In this case, if we've been looking for both types, we'd like to call it
      // a UPC-A code. But for efficiency we only run the EAN-13 decoder to also read
      // UPC-A. So we special case it here, and convert an EAN-13 result to a UPC-A
      // result if appropriate.
      //
      // But, don't return UPC-A if UPC-A was not a requested format!
      boolean ean13MayBeUPCA =
          result.getBarcodeFormat() == BarcodeFormat.EAN_13 &&
              result.getText().charAt(0) == '0';
      @SuppressWarnings("unchecked")
      Collection<BarcodeFormat> possibleFormats =
          hints == null ? null : (Collection<BarcodeFormat>) hints.get(DecodeHintType.POSSIBLE_FORMATS);
      boolean canReturnUPCA = possibleFormats == null || possibleFormats.contains(BarcodeFormat.UPC_A);

      if (ean13MayBeUPCA && canReturnUPCA) {
        // Transfer the metdata across
        Result resultUPCA = new Result(result.getText().substring(1),
                                       result.getRawBytes(),
                                       result.getResultPoints(),
                                       BarcodeFormat.UPC_A);
        resultUPCA.putAllMetadata(result.getResultMetadata());
        return resultUPCA;
      }
      return result;
    } catch (ReaderException ignored) {
      // continue
    }
  }

  throw NotFoundException.getNotFoundInstance();
}
 
Example 20
Source File: OneDReader.java    From reacteu-app with MIT License 4 votes vote down vote up
/**
 * 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();
}