Picture on NFC tags

旧城冷巷雨未停 提交于 2019-11-30 08:52:58

问题


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 information about it. Can anyone explain how to do that?


回答1:


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                             +
+----------------------------------------+
+ <Your image data (N bytes)>            +
+----------------------------------------+

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();
    }
}


来源:https://stackoverflow.com/questions/23590420/picture-on-nfc-tags

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!