Picture on NFC tags

前端 未结 1 1970
無奈伤痛
無奈伤痛 2020-12-18 15:31

With the latest NFC tags, it is possible to store up to 8k of data. So I would like to know how to store a picture on a tag, like the NXP TagWriter app.

I found no i

1条回答
  •  隐瞒了意图╮
    2020-12-18 16:00

    You can use MIME type records to store images on NFC tags. If, for instance, your image is a JPEG image, you would use the MIME type "image/jpeg". You NDEF record could then look like this:

    +----------------------------------------+
    + MB=1, ME=1, CF=0, SR=0, IL=0, TNF=MIME +
    +----------------------------------------+
    + Type Length = 10                       +
    +----------------------------------------+
    + Payload Length = N                     +
    +----------------------------------------+
    + image/jpeg                             +
    +----------------------------------------+
    +             +
    +----------------------------------------+
    

    On Android, you could create such a record using

    byte[] myImage = ...;
    NdefRecord myImageRecord = NdefRecord.createMime("image/jpeg", myImage);
    

    Or using the constructor of NdefRecord:

    byte[] myImage = ...;
    NdefRecord myImageRecord = new NdefRecord(
            NdefRecord.TNF_MIME_MEDIA,
            "image/jpeg".getBytes("US-ASCII"),
            null,
            myImage
    );
    

    Once you have a Tag handle of an NDEF tag (i.e. through receiving and NFC discovery intent), you could then write the NDEF record to the tag:

    NdefMessage ndefMsg = new NdefMessage(new NdefRecord[] { myImageRecord });
    
    Tag tag = ...;
    Ndef ndefTag = Ndef.get(tag);
    if (ndefTag != null) {
        ndefTag.connect();
        ndefTag.writeNdefMessage(ndefMsg);
        ndefTag.close();
    } else {
        NdefFormatable ndefFormatable = NdefFormatable.get(tag);
        if (ndefFormatable != null) {
            ndefFormatable.connect();
            ndefFormatable.format(ndefMsg);
            ndefFormatable.close();
        }
    }
    

    0 讨论(0)
提交回复
热议问题