Question

I am trying to write an image on a Ndef Tag, currently, i am able to write it, but when I try to read it with any market application, they treat it like a text message. here is my piece of code writing the image :

        Bitmap mBitmap = Bitmap.createScaledBitmap(mPhoto, 100, 100, true);
        ByteArrayOutputStream stream = new ByteArrayOutputStream(); 
        mBitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
        byte[] byteArray = stream.toByteArray();
        NdefRecord picRecord = new NdefRecord(NdefRecord.TNF_MIME_MEDIA, "image/png".getBytes(), null, byteArray);
        String informations = "name: "+name + "\ntitle: " + title + "\naddress: "+ address + "\ncity: "+ city + "\nphone: "+ phone + "\nmail: "+mail;
        NdefRecord textRecord = createTextRecord(informations);
        NdefMessage message = new NdefMessage(new NdefRecord[]{picRecord, textRecord});

i also tryed writting the image this way :

        NdefMessage msg = new NdefMessage(new NdefRecord[] {createMimeRecord("image/png", byteArray), textRecord});

with the method createMimeRecord :

        public NdefRecord createMimeRecord(String mimeType, byte[]payload) {
        byte[] mimeBytes = mimeType.getBytes(Charset.forName("USASCII"));
        NdefRecord mimeRecord = new
        NdefRecord(NdefRecord.TNF_MIME_MEDIA,
        mimeBytes, new byte[0], payload);
        return mimeRecord;
        }

Here is the result i get when trying to read my image with applications like "TagInfo" :enter image description here

The text message is well writen and can be read normally. I've tried to use the "createMime(String mime type, byte[] data) but this method seems "undefined".I've tried to compress the bitmap image in Jpeg format with the same result. Also I've tried to find examples of sending images via NdefMessages, but didn't find any. Any suggestion ?

Was it helpful?

Solution

Finally after searching for an application writting and reading Business card on NFC tag and finding nothing. I decided to create my own kind of business card and read it myself. here is the code i used to write the card using Ndef Message :

        Bitmap mBitmap = mPhoto;
        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        mBitmap.compress(Bitmap.CompressFormat.JPEG, 80, stream);
        byte[] byteArray = stream.toByteArray();
        NdefRecord picRecord = new NdefRecord(NdefRecord.TNF_MIME_MEDIA,  "image/jpeg".getBytes(), null, byteArray);
        String informations = "name: "+name + "\ntitle: " + title + "\naddress: "+ address + "\ncity: "+ city + "\nphone: "+ phone + "\nmail: "+mail + "\n";
        NdefRecord textRecord = createTextRecord(informations);
        NdefMessage message = new NdefMessage(new NdefRecord[]{picRecord, textRecord});

and here is the code for the reading part :

        NdefRecord picRecord = records[0];
        NdefRecord infoRecord = records[1];
        byte[] picload = picRecord.getPayload();
        byte[] infoload = infoRecord.getPayload();
        Bitmap photo = BitmapFactory.decodeByteArray(picload, 0, picload.length);
        String textEncoding = ((infoload[0] & 0200) == 0) ? "UTF-8" : "UTF-16";
        int languageCodeLength = infoload[0] & 0077;
        String text = null;
        try{
            String languageCode = new String(infoload, 1, languageCodeLength, "US-ASCII");
            text = new String(infoload, languageCodeLength + 1,infoload.length - languageCodeLength - 1, textEncoding);
        }catch(Exception e){
            Alert("String decoding", e.toString());
            return;
        }

The Jpeg encryption helps a lot to compress the image without loosing too much quality. The transfer on the tag take 2-3 seconds but the solution works well.

OTHER TIPS

If your use case is storing business card on NFC tag, I would suggest that you do not store the image data but rather a link to the image. Otherwise you will have hard time storing the business card on a normal tags (usual size of the tags is 1K or 4K) and also time for transferring the data will be too long. According to the vCard specification you can do both: storing binary image data in base64 format and also url link(which I strongly recommend).

For more information about the format of the vCard look at here:

http://en.wikipedia.org/wiki/VCard

or in more details here:

https://www.rfc-editor.org/rfc/rfc2426#section-3.1.4

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top