--- /dev/null
+/*
+ * 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 java.io.UnsupportedEncodingException;
+
+/**
+ * <p>Superclass for classes encapsulating results in the NDEF format.
+ * See <a href="http://www.nfc-forum.org/specs/">http://www.nfc-forum.org/specs/</a>.</p>
+ *
+ * <p>This code supports a limited subset of NDEF messages, ones that are plausibly
+ * useful in 2D barcode formats. This generally includes 1-record messages, no chunking,
+ * "short record" syntax, no ID field.</p>
+ *
+ * @author srowen@google.com (Sean Owen)
+ */
+abstract class AbstractNDEFParsedResult extends ParsedReaderResult {
+
+ /**
+ * MB = 1 (start of record)
+ * ME = 1 (also end of record)
+ * CF = 0 (not a chunk)
+ * SR = 1 (assume short record)
+ * ID = 0 (ID length field omitted)
+ * TNF = 0 (= 1, well-known type)
+ * 0
+ * 1
+ */
+ private static final int HEADER_VALUE = 0xD1;
+ private static final int MASK = 0xFF;
+
+ AbstractNDEFParsedResult(ParsedReaderResultType type) {
+ super(type);
+ }
+
+ static boolean isMaybeNDEF(byte[] bytes) {
+ return
+ bytes != null &&
+ bytes.length >= 4 &&
+ ((bytes[0] & MASK) == HEADER_VALUE) &&
+ ((bytes[1] & 0xFF) == 1);
+ }
+
+ static String bytesToString(byte[] bytes, int offset, int length, String encoding) {
+ try {
+ return new String(bytes, offset, length, encoding);
+ } catch (UnsupportedEncodingException uee) {
+ throw new RuntimeException("Platform does not support required encoding: " + uee);
+ }
+ }
+
+}
\ No newline at end of file
--- /dev/null
+/*
+ * 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;
+
+/**
+ * Recognizes an NDEF message that encodes text according to the
+ * "Text Record Type Definition" specification.
+ *
+ * @author srowen@google.com (Sean Owen)
+ */
+public final class NDEFTextParsedResult extends AbstractNDEFParsedResult {
+
+ private static final byte TEXT_WELL_KNOWN_TYPE = (byte) 0x54;
+
+ private final String language;
+ private final String text;
+
+ private NDEFTextParsedResult(String language, String text) {
+ super(ParsedReaderResultType.NDEF_TEXT);
+ this.language = language;
+ this.text = text;
+ }
+
+ public static NDEFTextParsedResult parse(Result result) {
+ byte[] bytes = result.getRawBytes();
+ if (!isMaybeNDEF(bytes)) {
+ return null;
+ }
+
+ int payloadLength = bytes[2] & 0xFF;
+
+ // Next 1 byte is type
+ if (bytes[3] != TEXT_WELL_KNOWN_TYPE) {
+ return null;
+ }
+
+ // Text record
+ byte statusByte = bytes[4];
+ boolean isUTF16 = (statusByte & 0x80) != 0;
+ int languageLength = statusByte & 0x1F;
+
+ // language is always ASCII-encoded:
+ String language = bytesToString(bytes, 5, languageLength, "US-ASCII");
+ String encoding = isUTF16 ? "UTF-16" : "UTF-8";
+ String text = bytesToString(bytes, 5 + languageLength, payloadLength - languageLength - 1, encoding);
+ return new NDEFTextParsedResult(language, text);
+ }
+
+ public String getLanguage() {
+ return language;
+ }
+
+ public String getText() {
+ return text;
+ }
+
+ public String getDisplayResult() {
+ return text;
+ }
+
+}
\ No newline at end of file
--- /dev/null
+/*
+ * 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;
+
+/**
+ * Recognizes an NDEF message that encodes a URI according to the
+ * "URI Record Type Definition" specification.
+ *
+ * @author srowen@google.com (Sean Owen)
+ */
+public final class NDEFURIParsedResult extends AbstractNDEFParsedResult {
+
+ private static final byte URI_WELL_KNOWN_TYPE = (byte) 0x55;
+
+ private static final String[] URI_PREFIXES = new String[] {
+ null,
+ "http://www.",
+ "https://www.",
+ "http://",
+ "https://",
+ "tel:",
+ "mailto:",
+ "ftp://anonymous:anonymous@",
+ "ftp://ftp.",
+ "ftps://",
+ "sftp://",
+ "smb://",
+ "nfs://",
+ "ftp://",
+ "dav://",
+ "news:",
+ "telnet://",
+ "imap:",
+ "rtsp://",
+ "urn:",
+ "pop:",
+ "sip:",
+ "sips:",
+ "tftp:",
+ "btspp://",
+ "btl2cap://",
+ "btgoep://",
+ "tcpobex://",
+ "irdaobex://",
+ "file://",
+ "urn:epc:id:",
+ "urn:epc:tag:",
+ "urn:epc:pat:",
+ "urn:epc:raw:",
+ "urn:epc:",
+ "urn:nfc:",
+ };
+
+ private final String uri;
+
+ private NDEFURIParsedResult(String uri) {
+ super(ParsedReaderResultType.NDEF_TEXT);
+ this.uri = uri;
+ }
+
+ public static NDEFURIParsedResult parse(Result result) {
+ byte[] bytes = result.getRawBytes();
+ if (!isMaybeNDEF(bytes)) {
+ return null;
+ }
+
+ int payloadLength = bytes[2] & 0xFF;
+
+ // Next 1 byte is type
+ if (bytes[3] != URI_WELL_KNOWN_TYPE) {
+ return null;
+ }
+
+ int identifierCode = bytes[4] & 0xFF;
+ String prefix = null;
+ if (identifierCode < URI_PREFIXES.length) {
+ prefix = URI_PREFIXES[identifierCode];
+ }
+
+ String restOfURI = bytesToString(bytes, 5, payloadLength - 1, "UTF-8");
+ String fullURI = prefix == null ? restOfURI : prefix + restOfURI;
+ return new NDEFURIParsedResult(fullURI);
+ }
+
+ public String getURI() {
+ return uri;
+ }
+
+ public String getDisplayResult() {
+ return uri;
+ }
+
+}
\ No newline at end of file
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");
+ // TODO later, add the NDEF types to those actually processed by the clients
+ public static final ParsedReaderResultType NDEF_TEXT = new ParsedReaderResultType("NDEF_TEXT");
+ public static final ParsedReaderResultType NDEF_URI = new ParsedReaderResultType("NDEF_URI");
private final String name;