问题
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