2 * Copyright 2008 ZXing authors
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.optional;
19 import com.google.zxing.Result;
22 * <p>Recognizes an NDEF message that encodes information according to the
23 * "Smart Poster Record Type Definition" specification.</p>
25 * <p>This actually only supports some parts of the Smart Poster format: title,
26 * URI, and action records. Icon records are not supported because the size
27 * of these records are infeasibly large for barcodes. Size and type records
28 * are not supported. Multiple titles are not supported.</p>
32 final class NDEFSmartPosterResultParser extends AbstractNDEFResultParser {
34 public static NDEFSmartPosterParsedResult parse(Result result) {
35 byte[] bytes = result.getRawBytes();
39 NDEFRecord headerRecord = NDEFRecord.readRecord(bytes, 0);
40 // Yes, header record starts and ends a message
41 if (headerRecord == null || !headerRecord.isMessageBegin() || !headerRecord.isMessageEnd()) {
44 if (!headerRecord.getType().equals(NDEFRecord.SMART_POSTER_WELL_KNOWN_TYPE)) {
50 NDEFRecord ndefRecord = null;
51 byte[] payload = headerRecord.getPayload();
52 int action = NDEFSmartPosterParsedResult.ACTION_UNSPECIFIED;
56 while (offset < payload.length && (ndefRecord = NDEFRecord.readRecord(payload, offset)) != null) {
57 if (recordNumber == 0 && !ndefRecord.isMessageBegin()) {
61 String type = ndefRecord.getType();
62 if (NDEFRecord.TEXT_WELL_KNOWN_TYPE.equals(type)) {
63 String[] languageText = NDEFTextResultParser.decodeTextPayload(ndefRecord.getPayload());
64 title = languageText[1];
65 } else if (NDEFRecord.URI_WELL_KNOWN_TYPE.equals(type)) {
66 uri = NDEFURIResultParser.decodeURIPayload(ndefRecord.getPayload());
67 } else if (NDEFRecord.ACTION_WELL_KNOWN_TYPE.equals(type)) {
68 action = ndefRecord.getPayload()[0];
71 offset += ndefRecord.getTotalRecordLength();
74 if (recordNumber == 0 || (ndefRecord != null && !ndefRecord.isMessageEnd())) {
78 return new NDEFSmartPosterParsedResult(action, uri, title);