use of com.google.zxing.Result in project weex-example by KalicyZhou.
the class OneDReader method doDecode.
/**
* 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) {
// Look at the whole image, not just the center
maxLines = height;
} else {
// 15 rows spaced 1/32 apart is roughly the middle half of the image
maxLines = 15;
}
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;
// i.e. is x even?
boolean isAbove = (x & 0x01) == 0;
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;
}
// handle decoding upside down barcodes.
for (int attempt = 0; attempt < 2; attempt++) {
if (attempt == 1) {
// trying again?
// reverse the row and continue
row.reverse();
// 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();
}
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 weex-example by KalicyZhou.
the class QRCodeEncoder method encodeFromStreamExtra.
// Handles send intents from the Contacts app, retrieving a contact as a VCARD.
private void encodeFromStreamExtra(Intent intent) throws WriterException {
format = BarcodeFormat.QR_CODE;
Bundle bundle = intent.getExtras();
if (bundle == null) {
throw new WriterException("No extras");
}
Uri uri = bundle.getParcelable(Intent.EXTRA_STREAM);
if (uri == null) {
throw new WriterException("No EXTRA_STREAM");
}
byte[] vcard;
String vcardString;
InputStream stream = null;
try {
stream = activity.getContentResolver().openInputStream(uri);
if (stream == null) {
throw new WriterException("Can't open stream for " + uri);
}
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] buffer = new byte[2048];
int bytesRead;
while ((bytesRead = stream.read(buffer)) > 0) {
baos.write(buffer, 0, bytesRead);
}
vcard = baos.toByteArray();
vcardString = new String(vcard, 0, vcard.length, "UTF-8");
} catch (IOException ioe) {
throw new WriterException(ioe);
} finally {
if (stream != null) {
try {
stream.close();
} catch (IOException e) {
// continue
}
}
}
Log.d(TAG, "Encoding share intent content:");
Log.d(TAG, vcardString);
Result result = new Result(vcardString, vcard, null, BarcodeFormat.QR_CODE);
ParsedResult parsedResult = ResultParser.parseResult(result);
if (!(parsedResult instanceof AddressBookParsedResult)) {
throw new WriterException("Result was not an address");
}
encodeQRCodeContents((AddressBookParsedResult) parsedResult);
if (contents == null || contents.isEmpty()) {
throw new WriterException("No content to encode");
}
}
Aggregations