NFC标签上的图片


问题内容

使用最新的NFC标签,最多可以存储8k数据。因此,我想知道如何将图片存储在标签上,例如NXP TagWriter应用程序。

我没有找到任何信息。谁能解释该怎么做?


问题答案:

您可以使用MIME类型记录将图像存储在NFC标签上。例如,如果您的图像是JPEG图像,则应使用MIME类型“ image /
jpeg”。您的NDEF记录可能如下所示:

+----------------------------------------+
+ 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)>            +
+----------------------------------------+

在Android上,您可以使用

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

或使用的构造函数NdefRecord

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

一旦有了TagNDEF标签的句柄(即通过接收和NFC发现意图),就可以将NDEF记录写入标签:

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