Improve URL detection rule to exclude stuff like "Hi."

git-svn-id: https://zxing.googlecode.com/svn/trunk@913 59b500cc-1b3d-0410-9834-0bbf25fbcc57
This commit is contained in:
srowen 2009-04-18 08:02:08 +00:00
parent 7f2685af2f
commit e74c041e46

View file

@ -30,13 +30,13 @@ final class URIResultParser extends ResultParser {
public static URIParsedResult parse(Result result) { public static URIParsedResult parse(Result result) {
String rawText = result.getText(); String rawText = result.getText();
// We specifically handle the odd "URL" scheme here for simplicity
if (rawText != null && rawText.startsWith("URL:")) {
rawText = rawText.substring(4);
}
if (!isBasicallyValidURI(rawText)) { if (!isBasicallyValidURI(rawText)) {
return null; return null;
} }
// We specifically handle the odd "URL" scheme here for simplicity
if (rawText.startsWith("URL:")) {
rawText = rawText.substring(4);
}
return new URIParsedResult(rawText, null); return new URIParsedResult(rawText, null);
} }
@ -46,8 +46,16 @@ final class URIResultParser extends ResultParser {
* need to know when a string is obviously not a URI. * need to know when a string is obviously not a URI.
*/ */
static boolean isBasicallyValidURI(String uri) { static boolean isBasicallyValidURI(String uri) {
return uri != null && uri.indexOf(' ') < 0 && uri.indexOf('\n') < 0 &&
(uri.indexOf(':') >= 0 || uri.indexOf('.') >= 0); if (uri == null || uri.indexOf(' ') >= 0 || uri.indexOf('\n') >= 0) {
return false;
}
int period = uri.indexOf('.');
// Look for period in a domain but followed by at least a two-char TLD
if (period >= uri.length() - 2) {
return false;
}
return period >= 0 || uri.indexOf(':') >= 0;
} }
} }