use of com.google.zxing.Result in project weex-example by KalicyZhou.
the class UPCEANExtension2Support method decodeRow.
Result decodeRow(int rowNumber, BitArray row, int[] extensionStartRange) throws NotFoundException {
StringBuilder result = decodeRowStringBuffer;
result.setLength(0);
int end = decodeMiddle(row, extensionStartRange, result);
String resultString = result.toString();
Map<ResultMetadataType, Object> extensionData = parseExtensionString(resultString);
Result extensionResult = new Result(resultString, null, new ResultPoint[] { new ResultPoint((extensionStartRange[0] + extensionStartRange[1]) / 2.0f, (float) rowNumber), new ResultPoint((float) end, (float) rowNumber) }, BarcodeFormat.UPC_EAN_EXTENSION);
if (extensionData != null) {
extensionResult.putAllMetadata(extensionData);
}
return extensionResult;
}
use of com.google.zxing.Result in project weex-example by KalicyZhou.
the class Code39Reader method decodeRow.
@Override
public Result decodeRow(int rowNumber, BitArray row, Map<DecodeHintType, ?> hints) throws NotFoundException, ChecksumException, FormatException {
int[] theCounters = counters;
Arrays.fill(theCounters, 0);
StringBuilder result = decodeRowResult;
result.setLength(0);
int[] start = findAsteriskPattern(row, theCounters);
// Read off white space
int nextStart = row.getNextSet(start[1]);
int end = row.getSize();
char decodedChar;
int lastStart;
do {
recordPattern(row, nextStart, theCounters);
int pattern = toNarrowWidePattern(theCounters);
if (pattern < 0) {
throw NotFoundException.getNotFoundInstance();
}
decodedChar = patternToChar(pattern);
result.append(decodedChar);
lastStart = nextStart;
for (int counter : theCounters) {
nextStart += counter;
}
// Read off white space
nextStart = row.getNextSet(nextStart);
} while (decodedChar != '*');
// remove asterisk
result.setLength(result.length() - 1);
// Look for whitespace after pattern:
int lastPatternSize = 0;
for (int counter : theCounters) {
lastPatternSize += counter;
}
int whiteSpaceAfterEnd = nextStart - lastStart - lastPatternSize;
// (but if it's whitespace to the very end of the image, that's OK)
if (nextStart != end && (whiteSpaceAfterEnd * 2) < lastPatternSize) {
throw NotFoundException.getNotFoundInstance();
}
if (usingCheckDigit) {
int max = result.length() - 1;
int total = 0;
for (int i = 0; i < max; i++) {
total += CHECK_DIGIT_STRING.indexOf(decodeRowResult.charAt(i));
}
if (result.charAt(max) != CHECK_DIGIT_STRING.charAt(total % 43)) {
throw ChecksumException.getChecksumInstance();
}
result.setLength(max);
}
if (result.length() == 0) {
// false positive
throw NotFoundException.getNotFoundInstance();
}
String resultString;
if (extendedMode) {
resultString = decodeExtended(result);
} else {
resultString = result.toString();
}
float left = (float) (start[1] + start[0]) / 2.0f;
float right = lastStart + lastPatternSize / 2.0f;
return new Result(resultString, null, new ResultPoint[] { new ResultPoint(left, (float) rowNumber), new ResultPoint(right, (float) rowNumber) }, BarcodeFormat.CODE_39);
}
use of com.google.zxing.Result in project weex-example by KalicyZhou.
the class ITFReader method decodeRow.
@Override
public Result decodeRow(int rowNumber, BitArray row, Map<DecodeHintType, ?> hints) throws FormatException, NotFoundException {
// Find out where the Middle section (payload) starts & ends
int[] startRange = decodeStart(row);
int[] endRange = decodeEnd(row);
StringBuilder result = new StringBuilder(20);
decodeMiddle(row, startRange[1], endRange[0], result);
String resultString = result.toString();
int[] allowedLengths = null;
if (hints != null) {
allowedLengths = (int[]) hints.get(DecodeHintType.ALLOWED_LENGTHS);
}
if (allowedLengths == null) {
allowedLengths = DEFAULT_ALLOWED_LENGTHS;
}
// To avoid false positives with 2D barcodes (and other patterns), make
// an assumption that the decoded string must be a 'standard' length if it's short
int length = resultString.length();
boolean lengthOK = false;
int maxAllowedLength = 0;
for (int allowedLength : allowedLengths) {
if (length == allowedLength) {
lengthOK = true;
break;
}
if (allowedLength > maxAllowedLength) {
maxAllowedLength = allowedLength;
}
}
if (!lengthOK && length > maxAllowedLength) {
lengthOK = true;
}
if (!lengthOK) {
throw FormatException.getFormatInstance();
}
return new Result(resultString, // no natural byte representation for these barcodes
null, new ResultPoint[] { new ResultPoint(startRange[1], (float) rowNumber), new ResultPoint(endRange[0], (float) rowNumber) }, BarcodeFormat.ITF);
}
use of com.google.zxing.Result in project zxing by zxing.
the class DecodeServlet method processImage.
private static void processImage(BufferedImage image, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
LuminanceSource source = new BufferedImageLuminanceSource(image);
BinaryBitmap bitmap = new BinaryBitmap(new GlobalHistogramBinarizer(source));
Collection<Result> results = new ArrayList<>(1);
try {
Reader reader = new MultiFormatReader();
ReaderException savedException = null;
try {
// Look for multiple barcodes
MultipleBarcodeReader multiReader = new GenericMultipleBarcodeReader(reader);
Result[] theResults = multiReader.decodeMultiple(bitmap, HINTS);
if (theResults != null) {
results.addAll(Arrays.asList(theResults));
}
} catch (ReaderException re) {
savedException = re;
}
if (results.isEmpty()) {
try {
// Look for pure barcode
Result theResult = reader.decode(bitmap, HINTS_PURE);
if (theResult != null) {
results.add(theResult);
}
} catch (ReaderException re) {
savedException = re;
}
}
if (results.isEmpty()) {
try {
// Look for normal barcode in photo
Result theResult = reader.decode(bitmap, HINTS);
if (theResult != null) {
results.add(theResult);
}
} catch (ReaderException re) {
savedException = re;
}
}
if (results.isEmpty()) {
try {
// Try again with other binarizer
BinaryBitmap hybridBitmap = new BinaryBitmap(new HybridBinarizer(source));
Result theResult = reader.decode(hybridBitmap, HINTS);
if (theResult != null) {
results.add(theResult);
}
} catch (ReaderException re) {
savedException = re;
}
}
if (results.isEmpty()) {
try {
throw savedException == null ? NotFoundException.getNotFoundInstance() : savedException;
} catch (FormatException | ChecksumException e) {
log.info(e.toString());
errorResponse(request, response, "format");
} catch (ReaderException e) {
// Including NotFoundException
log.info(e.toString());
errorResponse(request, response, "notfound");
}
return;
}
} catch (RuntimeException re) {
// Call out unexpected errors in the log clearly
log.log(Level.WARNING, "Unexpected exception from library", re);
throw new ServletException(re);
}
String fullParameter = request.getParameter("full");
boolean minimalOutput = fullParameter != null && !Boolean.parseBoolean(fullParameter);
if (minimalOutput) {
response.setContentType(MediaType.PLAIN_TEXT_UTF_8.toString());
response.setCharacterEncoding(StandardCharsets.UTF_8.name());
try (Writer out = new OutputStreamWriter(response.getOutputStream(), StandardCharsets.UTF_8)) {
for (Result result : results) {
out.write(result.getText());
out.write('\n');
}
}
} else {
request.setAttribute("results", results);
request.getRequestDispatcher("decoderesult.jspx").forward(request, response);
}
}
use of com.google.zxing.Result in project zxing by zxing.
the class RSSExpandedStackedInternalTestCase method testDecodingRowByRow.
@Test
public void testDecodingRowByRow() throws Exception {
RSSExpandedReader rssExpandedReader = new RSSExpandedReader();
BinaryBitmap binaryMap = TestCaseUtil.getBinaryBitmap("src/test/resources/blackbox/rssexpandedstacked-2/1000.png");
int firstRowNumber = binaryMap.getHeight() / 3;
BitArray firstRow = binaryMap.getBlackRow(firstRowNumber, null);
try {
rssExpandedReader.decodeRow2pairs(firstRowNumber, firstRow);
fail(NotFoundException.class.getName() + " expected");
} catch (NotFoundException nfe) {
// ok
}
assertEquals(1, rssExpandedReader.getRows().size());
ExpandedRow firstExpandedRow = rssExpandedReader.getRows().get(0);
assertEquals(firstRowNumber, firstExpandedRow.getRowNumber());
assertEquals(2, firstExpandedRow.getPairs().size());
firstExpandedRow.getPairs().get(1).getFinderPattern().getStartEnd()[1] = 0;
int secondRowNumber = 2 * binaryMap.getHeight() / 3;
BitArray secondRow = binaryMap.getBlackRow(secondRowNumber, null);
secondRow.reverse();
List<ExpandedPair> totalPairs = rssExpandedReader.decodeRow2pairs(secondRowNumber, secondRow);
Result result = RSSExpandedReader.constructResult(totalPairs);
assertEquals("(01)98898765432106(3202)012345(15)991231", result.getText());
}
Aggregations