From e9f900e72dade7a52ee57dcafd070d860ed09e50 Mon Sep 17 00:00:00 2001 From: srowen Date: Mon, 17 Mar 2008 14:57:56 +0000 Subject: [PATCH 1/1] Add geo: URL support (oh and removed an old moved file) git-svn-id: http://zxing.googlecode.com/svn/trunk@284 59b500cc-1b3d-0410-9834-0bbf25fbcc57 --- .../zxing/client/result/GeoParsedResult.java | 103 ++++++++++++++++++ .../client/result/ParsedReaderResult.java | 2 + .../client/result/ParsedReaderResultType.java | 3 +- .../result/ParsedReaderResultTestCase.java | 6 + .../PerspectiveTransformTestCase.java | 58 ---------- 5 files changed, 113 insertions(+), 59 deletions(-) create mode 100644 core/src/com/google/zxing/client/result/GeoParsedResult.java delete mode 100644 core/test/src/com/google/zxing/qrcode/detector/PerspectiveTransformTestCase.java diff --git a/core/src/com/google/zxing/client/result/GeoParsedResult.java b/core/src/com/google/zxing/client/result/GeoParsedResult.java new file mode 100644 index 00000000..5162b810 --- /dev/null +++ b/core/src/com/google/zxing/client/result/GeoParsedResult.java @@ -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 + * + * http://tools.ietf.org/html/draft-mayrhofer-geo-uri-00. + * + * @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(); + } + +} \ No newline at end of file diff --git a/core/src/com/google/zxing/client/result/ParsedReaderResult.java b/core/src/com/google/zxing/client/result/ParsedReaderResult.java index d4b4f00d..cff844d0 100644 --- a/core/src/com/google/zxing/client/result/ParsedReaderResult.java +++ b/core/src/com/google/zxing/client/result/ParsedReaderResult.java @@ -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) { diff --git a/core/src/com/google/zxing/client/result/ParsedReaderResultType.java b/core/src/com/google/zxing/client/result/ParsedReaderResultType.java index 3e403138..e4af7d7b 100644 --- a/core/src/com/google/zxing/client/result/ParsedReaderResultType.java +++ b/core/src/com/google/zxing/client/result/ParsedReaderResultType.java @@ -33,7 +33,8 @@ public final class ParsedReaderResultType { public static final ParsedReaderResultType UPC = new ParsedReaderResultType("UPC"); 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 ANDROID_INTENT = new ParsedReaderResultType("ANDROID_INTENT"); + public static final ParsedReaderResultType GEO = new ParsedReaderResultType("GEO"); private final String name; 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 33fdfc29..c796825c 100644 --- a/core/test/src/com/google/zxing/client/result/ParsedReaderResultTestCase.java +++ b/core/test/src/com/google/zxing/client/result/ParsedReaderResultTestCase.java @@ -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); diff --git a/core/test/src/com/google/zxing/qrcode/detector/PerspectiveTransformTestCase.java b/core/test/src/com/google/zxing/qrcode/detector/PerspectiveTransformTestCase.java deleted file mode 100644 index 2459cd9a..00000000 --- a/core/test/src/com/google/zxing/qrcode/detector/PerspectiveTransformTestCase.java +++ /dev/null @@ -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); - } - -} \ No newline at end of file -- 2.20.1