Renamed UPC result type to Product, and introduced an idea of 'product ID' and 'normalized product ID' to account for UPC-E, where the actual visible ID is different from what we may want to search for as a key. Updated clients to use this too.

git-svn-id: https://zxing.googlecode.com/svn/trunk@668 59b500cc-1b3d-0410-9834-0bbf25fbcc57
This commit is contained in:
srowen 2008-11-04 12:08:19 +00:00
parent ef7c421a66
commit e6e3472471
9 changed files with 59 additions and 33 deletions

View file

@ -31,7 +31,7 @@ public class ResultHandlerFactory {
return new AddressBookResultHandler(activity, result);
} else if (type.equals(ParsedResultType.EMAIL_ADDRESS)) {
return new EmailAddressResultHandler(activity, result);
} else if (type.equals(ParsedResultType.UPC)) {
} else if (type.equals(ParsedResultType.PRODUCT)) {
return new UPCResultHandler(activity, result);
} else if (type.equals(ParsedResultType.URI)) {
return new URIResultHandler(activity, result);

View file

@ -19,7 +19,7 @@ package com.google.zxing.client.android.result;
import android.app.Activity;
import com.google.zxing.client.android.R;
import com.google.zxing.client.result.ParsedResult;
import com.google.zxing.client.result.UPCParsedResult;
import com.google.zxing.client.result.ProductParsedResult;
public class UPCResultHandler extends ResultHandler {
@ -41,13 +41,13 @@ public class UPCResultHandler extends ResultHandler {
}
public void handleButtonPress(int index) {
UPCParsedResult upcResult = (UPCParsedResult) mResult;
ProductParsedResult upcResult = (ProductParsedResult) mResult;
switch (index) {
case 0:
openProductSearch(upcResult.getUPC());
openProductSearch(upcResult.getNormalizedProductID());
break;
case 1:
webSearch(upcResult.getUPC());
webSearch(upcResult.getNormalizedProductID());
break;
}
}

View file

@ -26,7 +26,7 @@ public final class ParsedResultType {
public static final ParsedResultType ADDRESSBOOK = new ParsedResultType("ADDRESSBOOK");
public static final ParsedResultType EMAIL_ADDRESS = new ParsedResultType("EMAIL_ADDRESS");
public static final ParsedResultType UPC = new ParsedResultType("UPC");
public static final ParsedResultType PRODUCT = new ParsedResultType("PRODUCT");
public static final ParsedResultType URI = new ParsedResultType("URI");
public static final ParsedResultType TEXT = new ParsedResultType("TEXT");
public static final ParsedResultType ANDROID_INTENT = new ParsedResultType("ANDROID_INTENT");

View file

@ -19,21 +19,31 @@ package com.google.zxing.client.result;
/**
* @author dswitkin@google.com (Daniel Switkin)
*/
public final class UPCParsedResult extends ParsedResult {
public final class ProductParsedResult extends ParsedResult {
private final String upc;
private final String productID;
private final String normalizedProductID;
UPCParsedResult(String upc) {
super(ParsedResultType.UPC);
this.upc = upc;
ProductParsedResult(String productID) {
this(productID, productID);
}
public String getUPC() {
return upc;
ProductParsedResult(String productID, String normalizedProductID) {
super(ParsedResultType.PRODUCT);
this.productID = productID;
this.normalizedProductID = normalizedProductID;
}
public String getProductID() {
return productID;
}
public String getNormalizedProductID() {
return normalizedProductID;
}
public String getDisplayResult() {
return upc;
return productID;
}
}

View file

@ -18,40 +18,49 @@ package com.google.zxing.client.result;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.Result;
import com.google.zxing.oned.UPCEReader;
/**
* Parses strings of digits that repesent a UPC code.
*
* @author dswitkin@google.com (Daniel Switkin)
*/
final class UPCResultParser extends ResultParser {
final class ProductResultParser extends ResultParser {
private UPCResultParser() {
private ProductResultParser() {
}
// Treat all UPC and EAN variants as UPCs, in the sense that they are all product barcodes.
public static UPCParsedResult parse(Result result) {
public static ProductParsedResult parse(Result result) {
BarcodeFormat format = result.getBarcodeFormat();
if (!BarcodeFormat.UPC_A.equals(format) && !BarcodeFormat.UPC_E.equals(format) &&
!BarcodeFormat.EAN_8.equals(format) && !BarcodeFormat.EAN_13.equals(format)) {
if (!(BarcodeFormat.UPC_A.equals(format) || BarcodeFormat.UPC_E.equals(format) ||
BarcodeFormat.EAN_8.equals(format) || BarcodeFormat.EAN_13.equals(format))) {
return null;
}
// Really neither of these should happen:
String rawText = result.getText();
if (rawText == null) {
return null;
}
int length = rawText.length();
if (length != 12 && length != 13) {
return null;
}
for (int x = 0; x < length; x++) {
char c = rawText.charAt(x);
if (c < '0' || c > '9') {
return null;
}
}
// Not actually checking the checksum again here
return new UPCParsedResult(rawText);
// Not actually checking the checksum again here
String normalizedProductID;
// Expand UPC-E for purposes of searching
if (BarcodeFormat.UPC_E.equals(format)) {
normalizedProductID = UPCEReader.convertUPCEtoUPCA(rawText);
} else {
normalizedProductID = rawText;
}
return new ProductParsedResult(rawText, normalizedProductID);
}
}

View file

@ -68,7 +68,7 @@ public abstract class ResultParser {
} else if ((result = ISBNResultParser.parse(theResult)) != null) {
// We depend on ISBN parsing coming before UPC, as it is a subset.
return result;
} else if ((result = UPCResultParser.parse(theResult)) != null) {
} else if ((result = ProductResultParser.parse(theResult)) != null) {
return result;
}
return new TextParsedResult(theResult.getText(), null);

View file

@ -112,7 +112,7 @@ public final class UPCEReader extends AbstractUPCEANReader {
* @param upce UPC-E code as string of digits
* @return equivalent UPC-A code as string of digits
*/
private static String convertUPCEtoUPCA(String upce) {
public static String convertUPCEtoUPCA(String upce) {
char[] upceChars = new char[6];
upce.getChars(1, 7, upceChars, 0);
StringBuffer result = new StringBuffer(12);

View file

@ -77,11 +77,18 @@ public final class ParsedReaderResultTestCase extends TestCase {
}
public void testUPC() {
doTestResult("123456789012", ParsedResultType.UPC, BarcodeFormat.UPC_A);
doTestResult("1234567890123", ParsedResultType.UPC, BarcodeFormat.UPC_A);
doTestResult("123456789012", ParsedResultType.PRODUCT, BarcodeFormat.UPC_A);
doTestResult("1234567890123", ParsedResultType.PRODUCT, BarcodeFormat.UPC_A);
doTestResult("12345678901", ParsedResultType.TEXT);
}
public void testEAN() {
doTestResult("00393157", ParsedResultType.PRODUCT, BarcodeFormat.EAN_8);
doTestResult("00393158", ParsedResultType.TEXT);
doTestResult("5051140178499", ParsedResultType.PRODUCT, BarcodeFormat.EAN_13);
doTestResult("5051140178490", ParsedResultType.TEXT);
}
public void testISBN() {
doTestResult("9784567890123", ParsedResultType.ISBN, BarcodeFormat.EAN_13);
doTestResult("9794567890123", ParsedResultType.ISBN, BarcodeFormat.EAN_13);

View file

@ -23,7 +23,7 @@ import com.google.zxing.client.result.ParsedResultType;
import com.google.zxing.client.result.ResultParser;
import com.google.zxing.client.result.SMSParsedResult;
import com.google.zxing.client.result.TelParsedResult;
import com.google.zxing.client.result.UPCParsedResult;
import com.google.zxing.client.result.ProductParsedResult;
import com.google.zxing.client.result.URIParsedResult;
import javax.microedition.io.ConnectionNotFoundException;
@ -205,10 +205,10 @@ public final class ZXingMIDlet extends MIDlet {
} else if (type.equals(ParsedResultType.SMS)) {
SMSParsedResult smsResult = (SMSParsedResult) result;
showOpenURL("Compose SMS?", smsResult.getNumber(), smsResult.getSMSURI());
} else if (type.equals(ParsedResultType.UPC)) {
String upc = ((UPCParsedResult) result).getUPC();
String uri = "http://www.upcdatabase.com/item.asp?upc=" + upc;
showOpenURL("Look Up Barcode Online?", upc, uri);
} else if (type.equals(ParsedResultType.PRODUCT)) {
ProductParsedResult productResult = (ProductParsedResult) result;
String uri = "http://www.upcdatabase.com/item.asp?upc=" + productResult.getNormalizedProductID();
showOpenURL("Look Up Barcode Online?", productResult.getProductID(), uri);
} else if (type.equals(ParsedResultType.TEL)) {
TelParsedResult telResult = (TelParsedResult) result;
showOpenURL("Dial Number?", telResult.getNumber(), telResult.getTelURI());