Add geo: URL support (oh and removed an old moved file)

git-svn-id: https://zxing.googlecode.com/svn/trunk@284 59b500cc-1b3d-0410-9834-0bbf25fbcc57
This commit is contained in:
srowen 2008-03-17 14:57:56 +00:00
parent bf29a16253
commit fab1e27971
5 changed files with 113 additions and 59 deletions

View file

@ -0,0 +1,103 @@
/*
* 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.client.result;
import com.google.zxing.Result;
/**
* Represents a "geo:" URI result. See
* <a href="http://tools.ietf.org/html/draft-mayrhofer-geo-uri-00">
* http://tools.ietf.org/html/draft-mayrhofer-geo-uri-00</a>.
*
* @author srowen@google.com (Sean Owen)
*/
public final class GeoParsedResult extends ParsedReaderResult {
private final float latitude;
private final float longitude;
private final float altitude;
private GeoParsedResult(float latitude, float longitude, float altitude) {
super(ParsedReaderResultType.GEO);
this.latitude = latitude;
this.longitude = longitude;
this.altitude = altitude;
}
public static GeoParsedResult parse(Result result) {
String rawText = result.getText();
if (!rawText.startsWith("geo:")) {
return null;
}
// Drop geo, query portion
int queryStart = rawText.indexOf('?', 4);
if (queryStart < 0) {
rawText = rawText.substring(4);
} else {
rawText = rawText.substring(4, queryStart);
}
int latitudeEnd = rawText.indexOf(',');
if (latitudeEnd < 0) {
return null;
}
float latitude = Float.parseFloat(rawText.substring(0, latitudeEnd));
int longitudeEnd = rawText.indexOf(',', latitudeEnd + 1);
float longitude;
float altitude;
if (longitudeEnd < 0) {
longitude = Float.parseFloat(rawText.substring(latitudeEnd + 1));
altitude = 0.0f;
} else {
longitude = Float.parseFloat(rawText.substring(latitudeEnd + 1, longitudeEnd));
altitude = Float.parseFloat(rawText.substring(longitudeEnd + 1));
}
return new GeoParsedResult(latitude, longitude, altitude);
}
/**
* @return latitude in degrees
*/
public float getLatitude() {
return latitude;
}
/**
* @return longitude in degrees
*/
public float getLongitude() {
return longitude;
}
/**
* @return altitude in meters. If not specified, in the geo URI, returns 0.0
*/
public float getAltitude() {
return altitude;
}
public String getDisplayResult() {
StringBuffer result = new StringBuffer(50);
result.append(latitude);
result.append("deg N, ");
result.append(longitude);
result.append("deg E, ");
result.append(altitude);
result.append('m');
return result.toString();
}
}

View file

@ -58,6 +58,8 @@ public abstract class ParsedReaderResult {
return result;
} else if ((result = AddressBookAUResult.parse(theResult)) != null) {
return result;
} else if ((result = GeoParsedResult.parse(theResult)) != null) {
return result;
} else if ((result = URLTOResult.parse(theResult)) != null) {
return result;
} else if ((result = URIParsedResult.parse(theResult)) != null) {

View file

@ -34,6 +34,7 @@ public final class ParsedReaderResultType {
public static final ParsedReaderResultType URI = new ParsedReaderResultType("URI");
public static final ParsedReaderResultType TEXT = new ParsedReaderResultType("TEXT");
public static final ParsedReaderResultType ANDROID_INTENT = new ParsedReaderResultType("ANDROID_INTENT");
public static final ParsedReaderResultType GEO = new ParsedReaderResultType("GEO");
private final String name;

View file

@ -91,6 +91,12 @@ public final class ParsedReaderResultTestCase extends TestCase {
doTestResult("google.com:443/foobar", ParsedReaderResultType.URI);
}
public void testGeo() {
doTestResult("geo:1,2", ParsedReaderResultType.GEO);
doTestResult("geo:1,2,3", ParsedReaderResultType.GEO);
doTestResult("geo:100.33,-32.3344,3.35", ParsedReaderResultType.GEO);
}
private static void doTestResult(String text, ParsedReaderResultType type) {
Result fakeResult = new Result(text, null, null, null);
ParsedReaderResult result = ParsedReaderResult.parseReaderResult(fakeResult);

View file

@ -1,58 +0,0 @@
/*
* Copyright 2007 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.qrcode.detector;
import junit.framework.TestCase;
/**
* @author srowen@google.com (Sean Owen)
*/
public final class PerspectiveTransformTestCase extends TestCase {
private static final float EPSILON = 0.0001f;
public void testSquareToQuadrilateral() {
PerspectiveTransform pt = PerspectiveTransform.squareToQuadrilateral(
2.0f, 3.0f, 10.0f, 4.0f, 16.0f, 15.0f, 4.0f, 9.0f);
assertPointEquals(2.0f, 3.0f, 0.0f, 0.0f, pt);
assertPointEquals(10.0f, 4.0f, 1.0f, 0.0f, pt);
assertPointEquals(4.0f, 9.0f, 0.0f, 1.0f, pt);
assertPointEquals(16.0f, 15.0f, 1.0f, 1.0f, pt);
assertPointEquals(6.535211f, 6.8873234f, 0.5f, 0.5f, pt);
assertPointEquals(48.0f, 42.42857f, 1.5f, 1.5f, pt);
}
public void testQuadrilateralToQuadrilateral() {
PerspectiveTransform pt = PerspectiveTransform.quadrilateralToQuadrilateral(
2.0f, 3.0f, 10.0f, 4.0f, 16.0f, 15.0f, 4.0f, 9.0f,
103.0f, 110.0f, 300.0f, 120.0f, 290.0f, 270.0f, 150.0f, 280.0f);
assertPointEquals(103.0f, 110.0f, 2.0f, 3.0f, pt);
assertPointEquals(300.0f, 120.0f, 10.0f, 4.0f, pt);
assertPointEquals(290.0f, 270.0f, 16.0f, 15.0f, pt);
assertPointEquals(150.0f, 280.0f, 4.0f, 9.0f, pt);
assertPointEquals(7.1516876f, -64.60185f, 0.5f, 0.5f, pt);
assertPointEquals(328.09116f, 334.16385f, 50.0f, 50.0f, pt);
}
private static void assertPointEquals(float expectedX, float expectedY, float sourceX, float sourceY, PerspectiveTransform pt) {
float[] points = new float[]{sourceX, sourceY};
pt.transformPoints(points);
assertEquals(expectedX, points[0], EPSILON);
assertEquals(expectedY, points[1], EPSILON);
}
}