狠狠撸

狠狠撸Share a Scribd company logo
2次元と仲良くしよう
                と思いました
               NFCLab 8月定例会   下川 敬弘




12年8月25日土曜日
自己绍介

              株式会社ソニックムーブ(神楽坂)

              フロントエンド エンジニア

              NFCLab, JAG, JAG   城支部

              @androhi



12年8月25日土曜日
アプリ作ってみてます




12年8月25日土曜日
概要


              NDEFメッセージ? ?QRコード

              Library : ZXing 2.0




12年8月25日土曜日
想定する利用シーン

              A「このポスターにタッチすると、スマートフォンで何か見られるらしいぞ。」


              B「どれどれ…。?。何も起きないけど。」


              C「あっ!おサイフケータイには、順次対応予定ですって書いてある!」


              D「私のも、おサイフケータイだからダメだわ。」


              あなた「まかせろっ!(」?ω?)」うー!(/?ω?)/にゃー!」


              ABCD「キャー!ステキー!」




12年8月25日土曜日
勉強になったコード




12年8月25日土曜日
狈顿贰贵メッセージ読み込み
      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日土曜日
TNF = Type Name Format

              TNF_ABSOLUTE_URI

                URIをベースとしたタイプフィールド


              TNF_WELL_KNOWN

                設定したRTDに応じて、振る舞いが変わる



12年8月25日土曜日
狈顿贰贵レコード読み込み
     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日土曜日
RTD = Record Type Definition

              RTD_URI

                URIをベースとしたPayload




12年8月25日土曜日
搁别肠辞谤诲蝉マッピング
          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日土曜日
URI identifier code
     Decimal   Hex             Protocol            Decimal        Hex         Protocol

        0      0x00              N/A                  18         0x12         rtsp://

        1      0x01          http://www.              19         0x13           urn:

        2      0x02          https://www.             20         0x14           pop:

        3      0x03            http://                21         0x15           sip:

        4      0x04            https://               22         0x16          sips:

        5      0x05              tel:                 23         0x17          tftp:

        6      0x06            mailto:                24         0x18         btspp://

        7      0x07   ftp://anonymous:anonymous@      25         0x19        btl2cap://

        8      0x08           ftp://ftp.              26         0x1A        btgoep://

        9      0x09            ftps://                27         0x1B        tcpobex://

        10     0x0A            sftp://                28         0x1C       irdaobex://

        11     0x0B             smb://                29         0x1D         file://

        12     0x0C             nfs://                30         0x1E       urn:epc:id:

        13     0x0D             ftp://                31         0x1F       urn:epc:tag:

        14     0x0E             dav://                32         0x20       urn:epc:pat:

        15     0x0F             news:                 33         0x21       urn:epc:raw:

        16     0x10           telnet://               34         0x22         urn:epc:

        17     0x11             imap:                 35         0x23         urn:nfc:

                                                   36...255   0x24...0xFF       RFU


12年8月25日土曜日
private String getUrl(NdefMessage msg) {

              // NDEFレコードからTNFを取り出す
              NdefRecord[] records = msg.getRecords();
              short tnf = records[0].getTnf();

              if (tnf == NdefRecord.TNF_WELL_KNOWN) {
                  // URLを取り出す
                  return parseWellKnown(records[0]);
              } else if (tnf == NdefRecord.TNF_ABSOLUTE_URI) {
                  // URLを取り出す
                  return parseAbsolute(records[0]);
              }

              throw new IllegalArgumentException("Unknown TNF " + tnf);
      }




12年8月25日土曜日
蚕搁コード生成は、以下を参考にしました。


               ブログ:明日の


               ZXingを使ってQRコードを表示する


               http://d.hatena.ne.jp/tomorrowkey/
               20091114/1258206013




12年8月25日土曜日
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日土曜日
蚕搁コード生成のイメージ
              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日土曜日
まとめ1
              NDEFメッセージ

               仕様を理解する

               TNF = Type Name Format

               RTD = Record Type Definition




12年8月25日土曜日
まとめ2
              QRコード

               ZXing超便利(遅っ)




12年8月25日土曜日
ご清聴
              ありがとうございました



12年8月25日土曜日

More Related Content

Nfc lab8月定例会

  • 1. 2次元と仲良くしよう と思いました NFCLab 8月定例会 下川 敬弘 12年8月25日土曜日
  • 2. 自己绍介 株式会社ソニックムーブ(神楽坂) フロントエンド エンジニア NFCLab, JAG, JAG 城支部 @androhi 12年8月25日土曜日
  • 4. 概要 NDEFメッセージ? ?QRコード Library : ZXing 2.0 12年8月25日土曜日
  • 5. 想定する利用シーン A「このポスターにタッチすると、スマートフォンで何か見られるらしいぞ。」 B「どれどれ…。?。何も起きないけど。」 C「あっ!おサイフケータイには、順次対応予定ですって書いてある!」 D「私のも、おサイフケータイだからダメだわ。」 あなた「まかせろっ!(」?ω?)」うー!(/?ω?)/にゃー!」 ABCD「キャー!ステキー!」 12年8月25日土曜日
  • 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日土曜日
  • 12. URI identifier code Decimal Hex Protocol Decimal Hex Protocol 0 0x00 N/A 18 0x12 rtsp:// 1 0x01 http://www. 19 0x13 urn: 2 0x02 https://www. 20 0x14 pop: 3 0x03 http:// 21 0x15 sip: 4 0x04 https:// 22 0x16 sips: 5 0x05 tel: 23 0x17 tftp: 6 0x06 mailto: 24 0x18 btspp:// 7 0x07 ftp://anonymous:anonymous@ 25 0x19 btl2cap:// 8 0x08 ftp://ftp. 26 0x1A btgoep:// 9 0x09 ftps:// 27 0x1B tcpobex:// 10 0x0A sftp:// 28 0x1C irdaobex:// 11 0x0B smb:// 29 0x1D file:// 12 0x0C nfs:// 30 0x1E urn:epc:id: 13 0x0D ftp:// 31 0x1F urn:epc:tag: 14 0x0E dav:// 32 0x20 urn:epc:pat: 15 0x0F news: 33 0x21 urn:epc:raw: 16 0x10 telnet:// 34 0x22 urn:epc: 17 0x11 imap: 35 0x23 urn:nfc: 36...255 0x24...0xFF RFU 12年8月25日土曜日
  • 13. private String getUrl(NdefMessage msg) { // NDEFレコードからTNFを取り出す NdefRecord[] records = msg.getRecords(); short tnf = records[0].getTnf(); if (tnf == NdefRecord.TNF_WELL_KNOWN) { // URLを取り出す return parseWellKnown(records[0]); } else if (tnf == NdefRecord.TNF_ABSOLUTE_URI) { // URLを取り出す return parseAbsolute(records[0]); } throw new IllegalArgumentException("Unknown TNF " + tnf); } 12年8月25日土曜日
  • 14. 蚕搁コード生成は、以下を参考にしました。 ブログ:明日の ZXingを使ってQRコードを表示する http://d.hatena.ne.jp/tomorrowkey/ 20091114/1258206013 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日土曜日
  • 18. まとめ2 QRコード ZXing超便利(遅っ) 12年8月25日土曜日
  • 19. ご清聴 ありがとうございました 12年8月25日土曜日