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

The following examples show how to use com.google.zxing.Result#getRawBytes() . 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 ScreenCapture with MIT License 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: GenericMultipleBarcodeReader.java    From MiBandDecompiled with Apache License 2.0 6 votes vote down vote up
private static Result a(Result result, int i, int j)
{
    ResultPoint aresultpoint[] = result.getResultPoints();
    if (aresultpoint == null)
    {
        return result;
    }
    ResultPoint aresultpoint1[] = new ResultPoint[aresultpoint.length];
    for (int k = 0; k < aresultpoint.length; k++)
    {
        ResultPoint resultpoint = aresultpoint[k];
        aresultpoint1[k] = new ResultPoint(resultpoint.getX() + (float)i, resultpoint.getY() + (float)j);
    }

    return new Result(result.getText(), result.getRawBytes(), aresultpoint1, result.getBarcodeFormat());
}
 
Example 3
Source File: GenericMultipleBarcodeReader.java    From Tesseract-OCR-Scanner with Apache License 2.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 4
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 5
Source File: GenericMultipleBarcodeReader.java    From Telegram-FOSS 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 6
Source File: GenericMultipleBarcodeReader.java    From barcodescanner-lib-aar with MIT License 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 7
Source File: GenericMultipleBarcodeReader.java    From weex with Apache License 2.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(), newResultPoints, result.getBarcodeFormat());
  newResult.putAllMetadata(result.getResultMetadata());
  return newResult;
}
 
Example 8
Source File: QRCodeMultiReader.java    From Telegram-FOSS with GNU General Public License v2.0 5 votes vote down vote up
static List<Result> processStructuredAppend(List<Result> results) {
  List<Result> newResults = new ArrayList<>();
  List<Result> saResults = new ArrayList<>();
  for (Result result : results) {
    if (result.getResultMetadata().containsKey(ResultMetadataType.STRUCTURED_APPEND_SEQUENCE)) {
      saResults.add(result);
    } else {
      newResults.add(result);
    }
  }
  if (saResults.isEmpty()) {
    return results;
  }

  // sort and concatenate the SA list items
  Collections.sort(saResults, new SAComparator());
  StringBuilder newText = new StringBuilder();
  ByteArrayOutputStream newRawBytes = new ByteArrayOutputStream();
  ByteArrayOutputStream newByteSegment = new ByteArrayOutputStream();
  for (Result saResult : saResults) {
    newText.append(saResult.getText());
    byte[] saBytes = saResult.getRawBytes();
    newRawBytes.write(saBytes, 0, saBytes.length);
    @SuppressWarnings("unchecked")
    Iterable<byte[]> byteSegments =
        (Iterable<byte[]>) saResult.getResultMetadata().get(ResultMetadataType.BYTE_SEGMENTS);
    if (byteSegments != null) {
      for (byte[] segment : byteSegments) {
        newByteSegment.write(segment, 0, segment.length);
      }
    }
  }

  Result newResult = new Result(newText.toString(), newRawBytes.toByteArray(), NO_POINTS, BarcodeFormat.QR_CODE);
  if (newByteSegment.size() > 0) {
    newResult.putMetadata(ResultMetadataType.BYTE_SEGMENTS, Collections.singletonList(newByteSegment.toByteArray()));
  }
  newResults.add(newResult);
  return newResults;
}
 
Example 9
Source File: GenericMultipleBarcodeReader.java    From ZXing-Orient with Apache License 2.0 5 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];
    newResultPoints[i] = new ResultPoint(oldPoint.getX() + xOffset, oldPoint.getY() + yOffset);
  }
  Result newResult = new Result(result.getText(), result.getRawBytes(), newResultPoints, result.getBarcodeFormat());
  newResult.putAllMetadata(result.getResultMetadata());
  return newResult;
}
 
Example 10
Source File: QRCodeMultiReader.java    From Telegram with GNU General Public License v2.0 5 votes vote down vote up
static List<Result> processStructuredAppend(List<Result> results) {
  List<Result> newResults = new ArrayList<>();
  List<Result> saResults = new ArrayList<>();
  for (Result result : results) {
    if (result.getResultMetadata().containsKey(ResultMetadataType.STRUCTURED_APPEND_SEQUENCE)) {
      saResults.add(result);
    } else {
      newResults.add(result);
    }
  }
  if (saResults.isEmpty()) {
    return results;
  }

  // sort and concatenate the SA list items
  Collections.sort(saResults, new SAComparator());
  StringBuilder newText = new StringBuilder();
  ByteArrayOutputStream newRawBytes = new ByteArrayOutputStream();
  ByteArrayOutputStream newByteSegment = new ByteArrayOutputStream();
  for (Result saResult : saResults) {
    newText.append(saResult.getText());
    byte[] saBytes = saResult.getRawBytes();
    newRawBytes.write(saBytes, 0, saBytes.length);
    @SuppressWarnings("unchecked")
    Iterable<byte[]> byteSegments =
        (Iterable<byte[]>) saResult.getResultMetadata().get(ResultMetadataType.BYTE_SEGMENTS);
    if (byteSegments != null) {
      for (byte[] segment : byteSegments) {
        newByteSegment.write(segment, 0, segment.length);
      }
    }
  }

  Result newResult = new Result(newText.toString(), newRawBytes.toByteArray(), NO_POINTS, BarcodeFormat.QR_CODE);
  if (newByteSegment.size() > 0) {
    newResult.putMetadata(ResultMetadataType.BYTE_SEGMENTS, Collections.singletonList(newByteSegment.toByteArray()));
  }
  newResults.add(newResult);
  return newResults;
}
 
Example 11
Source File: MultiFormatUPCEANReader.java    From RipplePower 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 12
Source File: MultiFormatUPCEANReader.java    From ScreenCapture 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) {
    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 13
Source File: MultiFormatUPCEANReader.java    From reacteu-app 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 re) {
      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';
    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: 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 15
Source File: MultiFormatUPCEANReader.java    From weex 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 16
Source File: MultiFormatUPCEANReader.java    From analyzer-of-android-for-Apache-Weex 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 17
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 18
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 19
Source File: MultiFormatUPCEANReader.java    From Tesseract-OCR-Scanner 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) {
    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: AppInvCaptureActivity.java    From appinventor-extensions with Apache License 2.0 4 votes vote down vote up
private void handleDecodeExternally(Result rawResult, ResultHandler resultHandler, Bitmap barcode) {

    if (barcode != null) {
      viewfinderView.drawResultBitmap(barcode);
    }

    long resultDurationMS;
    if (getIntent() == null) {
      resultDurationMS = DEFAULT_INTENT_RESULT_DURATION_MS;
    } else {
      resultDurationMS = getIntent().getLongExtra(Intents.Scan.RESULT_DISPLAY_DURATION_MS,
                                                  DEFAULT_INTENT_RESULT_DURATION_MS);
    }

    if (copyToClipboard && !resultHandler.areContentsSecure()) {
      ClipboardManager clipboard = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
      CharSequence text = resultHandler.getDisplayContents();
      if (text != null) {
        clipboard.setText(text);
      }
    }

    if (source == IntentSource.NATIVE_APP_INTENT) {
      
      // Hand back whatever action they requested - this can be changed to Intents.Scan.ACTION when
      // the deprecated intent is retired.
      Intent intent = new Intent(getIntent().getAction());
      intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
      intent.putExtra(Intents.Scan.RESULT, rawResult.toString());
      intent.putExtra(Intents.Scan.RESULT_FORMAT, rawResult.getBarcodeFormat().toString());
      byte[] rawBytes = rawResult.getRawBytes();
      if (rawBytes != null && rawBytes.length > 0) {
        intent.putExtra(Intents.Scan.RESULT_BYTES, rawBytes);
      }
      Map<ResultMetadataType,?> metadata = rawResult.getResultMetadata();
      if (metadata != null) {
        if (metadata.containsKey(ResultMetadataType.UPC_EAN_EXTENSION)) {
          intent.putExtra(Intents.Scan.RESULT_UPC_EAN_EXTENSION,
                          metadata.get(ResultMetadataType.UPC_EAN_EXTENSION).toString());
        }
        Integer orientation = (Integer) metadata.get(ResultMetadataType.ORIENTATION);
        if (orientation != null) {
          intent.putExtra(Intents.Scan.RESULT_ORIENTATION, orientation.intValue());
        }
        String ecLevel = (String) metadata.get(ResultMetadataType.ERROR_CORRECTION_LEVEL);
        if (ecLevel != null) {
          intent.putExtra(Intents.Scan.RESULT_ERROR_CORRECTION_LEVEL, ecLevel);
        }
        Iterable<byte[]> byteSegments = (Iterable<byte[]>) metadata.get(ResultMetadataType.BYTE_SEGMENTS);
        if (byteSegments != null) {
          int i = 0;
          for (byte[] byteSegment : byteSegments) {
            intent.putExtra(Intents.Scan.RESULT_BYTE_SEGMENTS_PREFIX + i, byteSegment);
            i++;
          }
        }
      }
      sendReplyMessage(Constants.return_scan_result, intent, resultDurationMS);
    }
  }