Added result metadata mechanism: now, reports orientation for 1D barcodes if it's not an upright orientation

git-svn-id: https://zxing.googlecode.com/svn/trunk@281 59b500cc-1b3d-0410-9834-0bbf25fbcc57
This commit is contained in:
srowen 2008-03-14 20:49:46 +00:00
parent b6e14d880d
commit 1215cc3512
3 changed files with 82 additions and 2 deletions

View file

@ -16,6 +16,8 @@
package com.google.zxing; package com.google.zxing;
import java.util.Hashtable;
/** /**
* <p>Encapsulates the result of decoding a barcode within an image.</p> * <p>Encapsulates the result of decoding a barcode within an image.</p>
* *
@ -27,12 +29,17 @@ public final class Result {
private final byte[] rawBytes; private final byte[] rawBytes;
private final ResultPoint[] resultPoints; private final ResultPoint[] resultPoints;
private final BarcodeFormat format; private final BarcodeFormat format;
private Hashtable resultMetadata;
public Result(String text, byte[] rawBytes, ResultPoint[] resultPoints, BarcodeFormat format) { public Result(String text,
byte[] rawBytes,
ResultPoint[] resultPoints,
BarcodeFormat format) {
this.text = text; this.text = text;
this.rawBytes = rawBytes; this.rawBytes = rawBytes;
this.resultPoints = resultPoints; this.resultPoints = resultPoints;
this.format = format; this.format = format;
this.resultMetadata = null;
} }
/** /**
@ -65,4 +72,19 @@ public final class Result {
return format; return format;
} }
/**
* @return {@link Hashtable} mapping {@link ResultMetadataType} keys to values. May be <code>null</code>.
* This contains optional metadata about what was detected about the barcode, like orientation.
*/
public Hashtable getResultMetadata() {
return resultMetadata;
}
public void putMetadata(ResultMetadataType type, Object value) {
if (resultMetadata == null) {
resultMetadata = new Hashtable(3);
}
resultMetadata.put(type, value);
}
} }

View file

@ -0,0 +1,46 @@
/*
* Copyright 2008 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.zxing;
/**
* Represents some type of metadata about the result of the decoding that the decoder
* wishes to communicate back to the caller.
*
* @author srowen@google.com (Sean Owen)
*/
public final class ResultMetadataType {
// No, we can't use an enum here. J2ME doesn't support it.
/**
* Unspecified, application-specific metadata. Maps to an unspecified {@link Object}.
*/
public static final ResultMetadataType OTHER = new ResultMetadataType();
/**
* Denotes the likely approximate orientation of the barcode in the image. This value
* is given as degrees rotated clockwise from the normal, upright orientation.
* For example a 1D barcode which was found by reading top-to-bottom would be
* said to have orientation "90". This key maps to an {@link Integer} whose
* value is in the range [0,360).
*/
public static final ResultMetadataType ORIENTATION = new ResultMetadataType();
private ResultMetadataType() {
}
}

View file

@ -21,6 +21,7 @@ import com.google.zxing.DecodeHintType;
import com.google.zxing.MonochromeBitmapSource; import com.google.zxing.MonochromeBitmapSource;
import com.google.zxing.ReaderException; import com.google.zxing.ReaderException;
import com.google.zxing.Result; import com.google.zxing.Result;
import com.google.zxing.ResultMetadataType;
import com.google.zxing.common.BitArray; import com.google.zxing.common.BitArray;
import java.util.Hashtable; import java.util.Hashtable;
@ -45,7 +46,16 @@ public abstract class AbstractOneDReader implements OneDReader {
} catch (ReaderException re) { } catch (ReaderException re) {
if (tryHarder && image.isRotateSupported()) { if (tryHarder && image.isRotateSupported()) {
MonochromeBitmapSource rotatedImage = image.rotateCounterClockwise(); MonochromeBitmapSource rotatedImage = image.rotateCounterClockwise();
return doDecode(rotatedImage, hints, tryHarder); Result result = doDecode(rotatedImage, hints, tryHarder);
// Record that we found it rotated 90 degrees CCW / 270 degrees CW
Hashtable metadata = result.getResultMetadata();
int orientation = 270;
if (metadata != null && metadata.containsKey(ResultMetadataType.ORIENTATION)) {
// But if we found it reversed in doDecode(), add in that result here:
orientation = (orientation + ((Integer) metadata.get(ResultMetadataType.ORIENTATION)).intValue()) % 360;
}
result.putMetadata(ResultMetadataType.ORIENTATION, new Integer(orientation));
return result;
} else { } else {
throw re; throw re;
} }
@ -108,6 +118,8 @@ public abstract class AbstractOneDReader implements OneDReader {
if (barcodesToSkip > 0) { // See if we should skip and keep looking if (barcodesToSkip > 0) { // See if we should skip and keep looking
barcodesToSkip--; barcodesToSkip--;
} else { } else {
// Found it, but upside-down:
result.putMetadata(ResultMetadataType.ORIENTATION, new Integer(180));
return result; return result;
} }
} catch (ReaderException re2) { } catch (ReaderException re2) {