diff --git a/core/src/com/google/zxing/client/result/ResultParser.java b/core/src/com/google/zxing/client/result/ResultParser.java index a00c0ef72..d741de666 100644 --- a/core/src/com/google/zxing/client/result/ResultParser.java +++ b/core/src/com/google/zxing/client/result/ResultParser.java @@ -232,11 +232,8 @@ public abstract class ResultParser { String value = uri.substring(separator + 1, paramEnd); value = urlDecode(value); result.put(key, value); - } else { - // key, no value - String key = uri.substring(paramStart, paramEnd); - result.put(key, null); } + // Can't put key, null into a hashtable } static String[] matchPrefixedField(String prefix, String rawText, char endChar, boolean trim) { diff --git a/core/src/com/google/zxing/client/result/SMSMMSResultParser.java b/core/src/com/google/zxing/client/result/SMSMMSResultParser.java index 8e3883544..dfa5da084 100644 --- a/core/src/com/google/zxing/client/result/SMSMMSResultParser.java +++ b/core/src/com/google/zxing/client/result/SMSMMSResultParser.java @@ -51,10 +51,23 @@ final class SMSMMSResultParser extends ResultParser { } else { return null; } + + // Check up front if this is a URI syntax string with query arguments + Hashtable nameValuePairs = parseNameValuePairs(rawText); + String subject = null; + String body = null; + boolean querySyntax = false; + if (nameValuePairs != null && nameValuePairs.size() > 0) { + subject = (String) nameValuePairs.get("subject"); + body = (String) nameValuePairs.get("body"); + querySyntax = true; + } + // Drop sms, query portion int queryStart = rawText.indexOf('?', prefixLength); String smsURIWithoutQuery; - if (queryStart < 0) { + // If it's not query syntax, the question mark is part of the subject or message + if (queryStart < 0 || !querySyntax) { smsURIWithoutQuery = rawText.substring(prefixLength); } else { smsURIWithoutQuery = rawText.substring(prefixLength, queryStart); @@ -74,13 +87,7 @@ final class SMSMMSResultParser extends ResultParser { via = null; } } - Hashtable nameValuePairs = parseNameValuePairs(rawText); - String subject = null; - String body = null; - if (nameValuePairs != null) { - subject = (String) nameValuePairs.get("subject"); - body = (String) nameValuePairs.get("body"); - } + // Thanks to dominik.wild for suggesting this enhancement to support // smsto:number:body URIs if (body == null) { diff --git a/core/test/src/com/google/zxing/client/result/ParsedReaderResultTestCase.java b/core/test/src/com/google/zxing/client/result/ParsedReaderResultTestCase.java index 9474f9855..b2419ab9e 100644 --- a/core/test/src/com/google/zxing/client/result/ParsedReaderResultTestCase.java +++ b/core/test/src/com/google/zxing/client/result/ParsedReaderResultTestCase.java @@ -121,6 +121,7 @@ public final class ParsedReaderResultTestCase extends TestCase { doTestResult("TEL:+15551212", ParsedResultType.TEL); doTestResult("tel:212 555 1212", ParsedResultType.TEL); doTestResult("tel:2125551212", ParsedResultType.TEL); + doTestResult("tel:212-555-1212", ParsedResultType.TEL); doTestResult("telephone", ParsedResultType.TEXT); } @@ -167,6 +168,9 @@ public final class ParsedReaderResultTestCase extends TestCase { doTestResult("sms:+15551212;via=999333", ParsedResultType.SMS); doTestResult("sms:+15551212?subject=foo&body=bar", ParsedResultType.SMS); doTestResult("sms:+15551212:subject", ParsedResultType.SMS); + // Need to handle question mark in the subject + doTestResult("sms:+15551212:What's up?", ParsedResultType.SMS); + doTestResult("sms:212-555-1212:Here's a longer message. Should be fine.", ParsedResultType.SMS); } public void testMMS() { @@ -177,6 +181,8 @@ public final class ParsedReaderResultTestCase extends TestCase { doTestResult("mms:+15551212;via=999333", ParsedResultType.SMS); doTestResult("mms:+15551212?subject=foo&body=bar", ParsedResultType.SMS); doTestResult("mms:+15551212:subject", ParsedResultType.SMS); + doTestResult("mms:+15551212:What's up?", ParsedResultType.SMS); + doTestResult("mms:212-555-1212:Here's a longer message. Should be fine.", ParsedResultType.SMS); } /* diff --git a/javase/src/com/google/zxing/client/j2se/CommandLineRunner.java b/javase/src/com/google/zxing/client/j2se/CommandLineRunner.java index 08e17f4f7..78f762657 100644 --- a/javase/src/com/google/zxing/client/j2se/CommandLineRunner.java +++ b/javase/src/com/google/zxing/client/j2se/CommandLineRunner.java @@ -21,6 +21,8 @@ import com.google.zxing.MonochromeBitmapSource; import com.google.zxing.MultiFormatReader; import com.google.zxing.ReaderException; import com.google.zxing.Result; +import com.google.zxing.client.result.ParsedResult; +import com.google.zxing.client.result.ResultParser; import javax.imageio.ImageIO; import java.awt.image.BufferedImage; @@ -31,8 +33,8 @@ import java.io.IOException; import java.io.OutputStreamWriter; import java.io.Writer; import java.net.URI; -import java.util.Hashtable; import java.nio.charset.Charset; +import java.util.Hashtable; /** *
This simple command line utility decodes files, directories of files, or URIs which are passed @@ -138,8 +140,10 @@ public final class CommandLineRunner { try { MonochromeBitmapSource source = new BufferedImageMonochromeBitmapSource(image); Result result = new MultiFormatReader().decode(source, hints); - System.out.println(uri.toString() + " (format: " + result.getBarcodeFormat() + "):\n" + - result.getText()); + ParsedResult parsedResult = ResultParser.parseResult(result); + System.out.println(uri.toString() + " (format: " + result.getBarcodeFormat() + + ", type: " + parsedResult.getType() + "):\nRaw result:\n" + result.getText() + + "\nParsed result:\n" + parsedResult.getDisplayResult()); return result; } catch (ReaderException e) { System.out.println(uri.toString() + ": No barcode found");