mirror of
https://github.com/zxing/zxing.git
synced 2025-03-05 20:48:51 -08:00
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:
parent
ef7c421a66
commit
e6e3472471
|
@ -31,7 +31,7 @@ public class ResultHandlerFactory {
|
||||||
return new AddressBookResultHandler(activity, result);
|
return new AddressBookResultHandler(activity, result);
|
||||||
} else if (type.equals(ParsedResultType.EMAIL_ADDRESS)) {
|
} else if (type.equals(ParsedResultType.EMAIL_ADDRESS)) {
|
||||||
return new EmailAddressResultHandler(activity, result);
|
return new EmailAddressResultHandler(activity, result);
|
||||||
} else if (type.equals(ParsedResultType.UPC)) {
|
} else if (type.equals(ParsedResultType.PRODUCT)) {
|
||||||
return new UPCResultHandler(activity, result);
|
return new UPCResultHandler(activity, result);
|
||||||
} else if (type.equals(ParsedResultType.URI)) {
|
} else if (type.equals(ParsedResultType.URI)) {
|
||||||
return new URIResultHandler(activity, result);
|
return new URIResultHandler(activity, result);
|
||||||
|
|
|
@ -19,7 +19,7 @@ package com.google.zxing.client.android.result;
|
||||||
import android.app.Activity;
|
import android.app.Activity;
|
||||||
import com.google.zxing.client.android.R;
|
import com.google.zxing.client.android.R;
|
||||||
import com.google.zxing.client.result.ParsedResult;
|
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 {
|
public class UPCResultHandler extends ResultHandler {
|
||||||
|
|
||||||
|
@ -41,13 +41,13 @@ public class UPCResultHandler extends ResultHandler {
|
||||||
}
|
}
|
||||||
|
|
||||||
public void handleButtonPress(int index) {
|
public void handleButtonPress(int index) {
|
||||||
UPCParsedResult upcResult = (UPCParsedResult) mResult;
|
ProductParsedResult upcResult = (ProductParsedResult) mResult;
|
||||||
switch (index) {
|
switch (index) {
|
||||||
case 0:
|
case 0:
|
||||||
openProductSearch(upcResult.getUPC());
|
openProductSearch(upcResult.getNormalizedProductID());
|
||||||
break;
|
break;
|
||||||
case 1:
|
case 1:
|
||||||
webSearch(upcResult.getUPC());
|
webSearch(upcResult.getNormalizedProductID());
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -26,7 +26,7 @@ public final class ParsedResultType {
|
||||||
|
|
||||||
public static final ParsedResultType ADDRESSBOOK = new ParsedResultType("ADDRESSBOOK");
|
public static final ParsedResultType ADDRESSBOOK = new ParsedResultType("ADDRESSBOOK");
|
||||||
public static final ParsedResultType EMAIL_ADDRESS = new ParsedResultType("EMAIL_ADDRESS");
|
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 URI = new ParsedResultType("URI");
|
||||||
public static final ParsedResultType TEXT = new ParsedResultType("TEXT");
|
public static final ParsedResultType TEXT = new ParsedResultType("TEXT");
|
||||||
public static final ParsedResultType ANDROID_INTENT = new ParsedResultType("ANDROID_INTENT");
|
public static final ParsedResultType ANDROID_INTENT = new ParsedResultType("ANDROID_INTENT");
|
||||||
|
|
|
@ -19,21 +19,31 @@ package com.google.zxing.client.result;
|
||||||
/**
|
/**
|
||||||
* @author dswitkin@google.com (Daniel Switkin)
|
* @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) {
|
ProductParsedResult(String productID) {
|
||||||
super(ParsedResultType.UPC);
|
this(productID, productID);
|
||||||
this.upc = upc;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getUPC() {
|
ProductParsedResult(String productID, String normalizedProductID) {
|
||||||
return upc;
|
super(ParsedResultType.PRODUCT);
|
||||||
|
this.productID = productID;
|
||||||
|
this.normalizedProductID = normalizedProductID;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getProductID() {
|
||||||
|
return productID;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getNormalizedProductID() {
|
||||||
|
return normalizedProductID;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getDisplayResult() {
|
public String getDisplayResult() {
|
||||||
return upc;
|
return productID;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
|
@ -18,40 +18,49 @@ package com.google.zxing.client.result;
|
||||||
|
|
||||||
import com.google.zxing.BarcodeFormat;
|
import com.google.zxing.BarcodeFormat;
|
||||||
import com.google.zxing.Result;
|
import com.google.zxing.Result;
|
||||||
|
import com.google.zxing.oned.UPCEReader;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Parses strings of digits that repesent a UPC code.
|
* Parses strings of digits that repesent a UPC code.
|
||||||
*
|
*
|
||||||
* @author dswitkin@google.com (Daniel Switkin)
|
* @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.
|
// 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();
|
BarcodeFormat format = result.getBarcodeFormat();
|
||||||
if (!BarcodeFormat.UPC_A.equals(format) && !BarcodeFormat.UPC_E.equals(format) &&
|
if (!(BarcodeFormat.UPC_A.equals(format) || BarcodeFormat.UPC_E.equals(format) ||
|
||||||
!BarcodeFormat.EAN_8.equals(format) && !BarcodeFormat.EAN_13.equals(format)) {
|
BarcodeFormat.EAN_8.equals(format) || BarcodeFormat.EAN_13.equals(format))) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
// Really neither of these should happen:
|
||||||
String rawText = result.getText();
|
String rawText = result.getText();
|
||||||
if (rawText == null) {
|
if (rawText == null) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
int length = rawText.length();
|
int length = rawText.length();
|
||||||
if (length != 12 && length != 13) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
for (int x = 0; x < length; x++) {
|
for (int x = 0; x < length; x++) {
|
||||||
char c = rawText.charAt(x);
|
char c = rawText.charAt(x);
|
||||||
if (c < '0' || c > '9') {
|
if (c < '0' || c > '9') {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// Not actually checking the checksum again here
|
// Not actually checking the checksum again here
|
||||||
return new UPCParsedResult(rawText);
|
|
||||||
|
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);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
|
@ -68,7 +68,7 @@ public abstract class ResultParser {
|
||||||
} else if ((result = ISBNResultParser.parse(theResult)) != null) {
|
} else if ((result = ISBNResultParser.parse(theResult)) != null) {
|
||||||
// We depend on ISBN parsing coming before UPC, as it is a subset.
|
// We depend on ISBN parsing coming before UPC, as it is a subset.
|
||||||
return result;
|
return result;
|
||||||
} else if ((result = UPCResultParser.parse(theResult)) != null) {
|
} else if ((result = ProductResultParser.parse(theResult)) != null) {
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
return new TextParsedResult(theResult.getText(), null);
|
return new TextParsedResult(theResult.getText(), null);
|
||||||
|
|
|
@ -112,7 +112,7 @@ public final class UPCEReader extends AbstractUPCEANReader {
|
||||||
* @param upce UPC-E code as string of digits
|
* @param upce UPC-E code as string of digits
|
||||||
* @return equivalent UPC-A 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];
|
char[] upceChars = new char[6];
|
||||||
upce.getChars(1, 7, upceChars, 0);
|
upce.getChars(1, 7, upceChars, 0);
|
||||||
StringBuffer result = new StringBuffer(12);
|
StringBuffer result = new StringBuffer(12);
|
||||||
|
|
|
@ -77,11 +77,18 @@ public final class ParsedReaderResultTestCase extends TestCase {
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testUPC() {
|
public void testUPC() {
|
||||||
doTestResult("123456789012", ParsedResultType.UPC, BarcodeFormat.UPC_A);
|
doTestResult("123456789012", ParsedResultType.PRODUCT, BarcodeFormat.UPC_A);
|
||||||
doTestResult("1234567890123", ParsedResultType.UPC, BarcodeFormat.UPC_A);
|
doTestResult("1234567890123", ParsedResultType.PRODUCT, BarcodeFormat.UPC_A);
|
||||||
doTestResult("12345678901", ParsedResultType.TEXT);
|
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() {
|
public void testISBN() {
|
||||||
doTestResult("9784567890123", ParsedResultType.ISBN, BarcodeFormat.EAN_13);
|
doTestResult("9784567890123", ParsedResultType.ISBN, BarcodeFormat.EAN_13);
|
||||||
doTestResult("9794567890123", ParsedResultType.ISBN, BarcodeFormat.EAN_13);
|
doTestResult("9794567890123", ParsedResultType.ISBN, BarcodeFormat.EAN_13);
|
||||||
|
|
|
@ -23,7 +23,7 @@ import com.google.zxing.client.result.ParsedResultType;
|
||||||
import com.google.zxing.client.result.ResultParser;
|
import com.google.zxing.client.result.ResultParser;
|
||||||
import com.google.zxing.client.result.SMSParsedResult;
|
import com.google.zxing.client.result.SMSParsedResult;
|
||||||
import com.google.zxing.client.result.TelParsedResult;
|
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 com.google.zxing.client.result.URIParsedResult;
|
||||||
|
|
||||||
import javax.microedition.io.ConnectionNotFoundException;
|
import javax.microedition.io.ConnectionNotFoundException;
|
||||||
|
@ -205,10 +205,10 @@ public final class ZXingMIDlet extends MIDlet {
|
||||||
} else if (type.equals(ParsedResultType.SMS)) {
|
} else if (type.equals(ParsedResultType.SMS)) {
|
||||||
SMSParsedResult smsResult = (SMSParsedResult) result;
|
SMSParsedResult smsResult = (SMSParsedResult) result;
|
||||||
showOpenURL("Compose SMS?", smsResult.getNumber(), smsResult.getSMSURI());
|
showOpenURL("Compose SMS?", smsResult.getNumber(), smsResult.getSMSURI());
|
||||||
} else if (type.equals(ParsedResultType.UPC)) {
|
} else if (type.equals(ParsedResultType.PRODUCT)) {
|
||||||
String upc = ((UPCParsedResult) result).getUPC();
|
ProductParsedResult productResult = (ProductParsedResult) result;
|
||||||
String uri = "http://www.upcdatabase.com/item.asp?upc=" + upc;
|
String uri = "http://www.upcdatabase.com/item.asp?upc=" + productResult.getNormalizedProductID();
|
||||||
showOpenURL("Look Up Barcode Online?", upc, uri);
|
showOpenURL("Look Up Barcode Online?", productResult.getProductID(), uri);
|
||||||
} else if (type.equals(ParsedResultType.TEL)) {
|
} else if (type.equals(ParsedResultType.TEL)) {
|
||||||
TelParsedResult telResult = (TelParsedResult) result;
|
TelParsedResult telResult = (TelParsedResult) result;
|
||||||
showOpenURL("Dial Number?", telResult.getNumber(), telResult.getTelURI());
|
showOpenURL("Dial Number?", telResult.getNumber(), telResult.getTelURI());
|
||||||
|
|
Loading…
Reference in a new issue