7. 狈顿贰贵メッセージ読み込み
private String getUrl(NdefMessage msg) {
// NDEFレコードからTNFを取り出す
NdefRecord[] records = msg.getRecords();
short tnf = records[0].getTnf();
if (tnf == NdefRecord.TNF_WELL_KNOWN) {
return parseWellKnown(records[0]);
} else if (tnf == NdefRecord.TNF_ABSOLUTE_URI) {
return parseAbsolute(records[0]);
}
throw new IllegalArgumentException("Unknown TNF " + tnf);
}
12年8月25日土曜日
8. TNF = Type Name Format
TNF_ABSOLUTE_URI
URIをベースとしたタイプフィールド
TNF_WELL_KNOWN
設定したRTDに応じて、振る舞いが変わる
12年8月25日土曜日
9. 狈顿贰贵レコード読み込み
private String parseAbsolute(NdefRecord record) {
byte[] payload = record.getPayload();
return new String(payload, Charset.forName("UTF-8"));
}
private String parseWellKnown(NdefRecord record) {
// RTD Uriかチェック
if (Arrays.equals(record.getType(), NdefRecord.RTD_URI)) {
byte[] payload = record.getPayload();
String prefix = prefixMap.get(payload[0]);
byte[] uriData = Arrays.copyOfRange(payload, 1, payload.length);
String uri = new String(uriData, Charset.forName("UTF-8"));
return prefix.concat(uri);
}
throw new IllegalArgumentException("Not Support RTD");
}
12年8月25日土曜日
10. RTD = Record Type Definition
RTD_URI
URIをベースとしたPayload
12年8月25日土曜日
11. 搁别肠辞谤诲蝉マッピング
Name Offset Size Value Description
URI The URI identifier
Identifier
0 1 byte identifier code, as specified in
code
code Table 3.
The rest of the URI,
URI or the entire URI (if
1 N UTF-8 string
field identifier code is
0x00).
引用:NFC URI RTD Technical Specification
http://www.nfc-forum.org/specs/spec_list/#rtds
12年8月25日土曜日
15. public Bitmap createQRCodeByZxing(String contents, int size) throws WriterException {
QRCodeWriter qrWriter = new QRCodeWriter();
// 異なる型の値を入れるためgenericは使えない
@SuppressWarnings("rawtypes")
Hashtable encodeHint = new Hashtable();
encodeHint.put(EncodeHintType.CHARACTER_SET, "shiftjis");
// エラー修復レベルを指定
encodeHint.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L);
BitMatrix qrCodeData = qrWriter.encode(contents, BarcodeFormat.QR_CODE, size, size,
encodeHint);
// QRコードのbitmap画像を生成
Bitmap bitmap = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
bitmap.eraseColor(Color.argb(255, 255, 255, 255));
for(int x = 0; x < qrCodeData.getWidth(); x++) {
for(int y = 0; y < qrCodeData.getHeight(); y++) {
if(qrCodeData.get(x, y)) {
// trueはBlack
bitmap.setPixel(x, y, Color.BLACK);
} else {
// falseはWhite
bitmap.setPixel(x, y, Color.WHITE);
}
}
}
return bitmap;
}
12年8月25日土曜日
16. 蚕搁コード生成のイメージ
T F T T F T T F F
F F F T T T F T F
T T F T F T T F T
T F T T F T F F F
F F T F F F T T T
F T T F F F T T F
F F T T F T F F F
T T F F T T F T F
F T T T F F T F T
12年8月25日土曜日
17. まとめ1
NDEFメッセージ
仕様を理解する
TNF = Type Name Format
RTD = Record Type Definition
12年8月25日土曜日