From: srowen Date: Wed, 11 Aug 2010 13:59:48 +0000 (+0000) Subject: Add result points for UPC EAN metadata extension X-Git-Url: http://git.rot13.org/?p=zxing.git;a=commitdiff_plain;h=118cc9f113634f4d6227e7ccc2ec6137c52999af Add result points for UPC EAN metadata extension git-svn-id: http://zxing.googlecode.com/svn/trunk@1519 59b500cc-1b3d-0410-9834-0bbf25fbcc57 --- diff --git a/android/src/com/google/zxing/client/android/CaptureActivity.java b/android/src/com/google/zxing/client/android/CaptureActivity.java index 687be895..02437ea9 100755 --- a/android/src/com/google/zxing/client/android/CaptureActivity.java +++ b/android/src/com/google/zxing/client/android/CaptureActivity.java @@ -506,8 +506,13 @@ public final class CaptureActivity extends Activity implements SurfaceHolder.Cal paint.setColor(getResources().getColor(R.color.result_points)); if (points.length == 2) { paint.setStrokeWidth(4.0f); - canvas.drawLine(points[0].getX(), points[0].getY(), points[1].getX(), - points[1].getY(), paint); + drawLine(canvas, paint, points[0], points[1]); + } else if (points.length == 4 && + (rawResult.getBarcodeFormat().equals(BarcodeFormat.UPC_A)) || + (rawResult.getBarcodeFormat().equals(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) { @@ -517,6 +522,10 @@ public final class CaptureActivity extends Activity implements SurfaceHolder.Cal } } + private static void drawLine(Canvas canvas, Paint paint, ResultPoint a, ResultPoint b) { + canvas.drawLine(a.getX(), a.getY(), b.getX(), b.getY(), paint); + } + // Put up our own UI for how to handle the decoded contents. private void handleDecodeInternally(Result rawResult, Bitmap barcode) { statusView.setVisibility(View.GONE); diff --git a/core/src/com/google/zxing/Result.java b/core/src/com/google/zxing/Result.java index 9223ceb7..ee1af527 100644 --- a/core/src/com/google/zxing/Result.java +++ b/core/src/com/google/zxing/Result.java @@ -28,7 +28,7 @@ public final class Result { private final String text; private final byte[] rawBytes; - private final ResultPoint[] resultPoints; + private ResultPoint[] resultPoints; private final BarcodeFormat format; private Hashtable resultMetadata; private final long timestamp; @@ -117,6 +117,17 @@ public final class Result { } } + public void addResultPoints(ResultPoint[] newPoints) { + if (resultPoints == null) { + resultPoints = newPoints; + } else if (newPoints != null && newPoints.length > 0) { + ResultPoint[] allPoints = new ResultPoint[resultPoints.length + newPoints.length]; + System.arraycopy(resultPoints, 0, allPoints, 0, resultPoints.length); + System.arraycopy(newPoints, 0, allPoints, resultPoints.length, newPoints.length); + resultPoints = allPoints; + } + } + public long getTimestamp() { return timestamp; } diff --git a/core/src/com/google/zxing/oned/UPCEANExtensionSupport.java b/core/src/com/google/zxing/oned/UPCEANExtensionSupport.java index f883566d..dea116d6 100644 --- a/core/src/com/google/zxing/oned/UPCEANExtensionSupport.java +++ b/core/src/com/google/zxing/oned/UPCEANExtensionSupport.java @@ -22,6 +22,7 @@ import com.google.zxing.BarcodeFormat; import com.google.zxing.NotFoundException; import com.google.zxing.Result; import com.google.zxing.ResultMetadataType; +import com.google.zxing.ResultPoint; import com.google.zxing.common.BitArray; final class UPCEANExtensionSupport { @@ -34,18 +35,25 @@ final class UPCEANExtensionSupport { private final int[] decodeMiddleCounters = new int[4]; private final StringBuffer decodeRowStringBuffer = new StringBuffer(); - Result decodeRow(BitArray row, int rowOffset) throws NotFoundException { + Result decodeRow(int rowNumber, BitArray row, int rowOffset) throws NotFoundException { int[] extensionStartRange = UPCEANReader.findGuardPattern(row, rowOffset, false, EXTENSION_START_PATTERN); StringBuffer result = decodeRowStringBuffer; result.setLength(0); - decodeMiddle(row, extensionStartRange, result); + int end = decodeMiddle(row, extensionStartRange, result); String resultString = result.toString(); Hashtable extensionData = parseExtensionString(resultString); - Result extensionResult = new Result(resultString, null, null, BarcodeFormat.UPC_EAN_EXTENSION); + 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); } @@ -72,12 +80,14 @@ final class UPCEANExtensionSupport { if (bestMatch >= 10) { lgPatternFound |= 1 << (4 - x); } - // Read off separator - while (rowOffset < end && !row.get(rowOffset)) { - rowOffset++; - } - while (rowOffset < end && row.get(rowOffset)) { - rowOffset++; + if (x != 4) { + // Read off separator if not last + while (rowOffset < end && !row.get(rowOffset)) { + rowOffset++; + } + while (rowOffset < end && row.get(rowOffset)) { + rowOffset++; + } } } diff --git a/core/src/com/google/zxing/oned/UPCEANReader.java b/core/src/com/google/zxing/oned/UPCEANReader.java index 7b4ea5e4..6de318ee 100644 --- a/core/src/com/google/zxing/oned/UPCEANReader.java +++ b/core/src/com/google/zxing/oned/UPCEANReader.java @@ -186,8 +186,9 @@ public abstract class UPCEANReader extends OneDReader { format); try { - Result extensionResult = extensionReader.decodeRow(row, endRange[1]); + Result extensionResult = extensionReader.decodeRow(rowNumber, row, endRange[1]); decodeResult.putAllMetadata(extensionResult.getResultMetadata()); + decodeResult.addResultPoints(extensionResult.getResultPoints()); } catch (ReaderException re) { // continue }