2 * Copyright 2008 Google Inc.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package com.google.zxing.client.result;
19 import com.google.zxing.Result;
22 * Recognizes an NDEF message that encodes text according to the
23 * "Text Record Type Definition" specification.
25 * @author srowen@google.com (Sean Owen)
27 public final class NDEFTextParsedResult extends AbstractNDEFParsedResult {
29 private static final byte TEXT_WELL_KNOWN_TYPE = (byte) 0x54;
31 private final String language;
32 private final String text;
34 private NDEFTextParsedResult(String language, String text) {
35 super(ParsedReaderResultType.NDEF_TEXT);
36 this.language = language;
40 public static NDEFTextParsedResult parse(Result result) {
41 byte[] bytes = result.getRawBytes();
42 if (!isMaybeNDEF(bytes)) {
46 int payloadLength = bytes[2] & 0xFF;
48 // Next 1 byte is type
49 if (bytes[3] != TEXT_WELL_KNOWN_TYPE) {
54 byte statusByte = bytes[4];
55 boolean isUTF16 = (statusByte & 0x80) != 0;
56 int languageLength = statusByte & 0x1F;
58 // language is always ASCII-encoded:
59 String language = bytesToString(bytes, 5, languageLength, "US-ASCII");
60 String encoding = isUTF16 ? "UTF-16" : "UTF-8";
61 String text = bytesToString(bytes, 5 + languageLength, payloadLength - languageLength - 1, encoding);
62 return new NDEFTextParsedResult(language, text);
65 public String getLanguage() {
69 public String getText() {
73 public String getDisplayResult() {