This page was last modified 18:36, 20 May 2008.
Writing image to NDEF tag
From Forum Nokia Wiki
Simple example MIDlet that can write image to RFID tag and read it from it. Writing is done using TargetListener and NDEFTagConnection. Reading using NDEFRecordListener. Image ised in this example: Media:smiley.png
import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; import javax.microedition.contactless.ContactlessException; import javax.microedition.contactless.DiscoveryManager; import javax.microedition.contactless.TargetListener; import javax.microedition.contactless.TargetProperties; import javax.microedition.contactless.TargetType; import javax.microedition.contactless.ndef.NDEFMessage; import javax.microedition.contactless.ndef.NDEFRecord; import javax.microedition.contactless.ndef.NDEFRecordListener; import javax.microedition.contactless.ndef.NDEFRecordType; import javax.microedition.contactless.ndef.NDEFTagConnection; import javax.microedition.io.Connector; import javax.microedition.lcdui.Command; import javax.microedition.lcdui.CommandListener; import javax.microedition.lcdui.Display; import javax.microedition.lcdui.Displayable; import javax.microedition.lcdui.Form; import javax.microedition.lcdui.Image; import javax.microedition.midlet.MIDlet; import javax.microedition.midlet.MIDletStateChangeException; /* * Simple example that can write image to RFID tag and read it from it. * Writing is done using TargetListener and NDEFTagConnection. * Reading using NDEFRecordListener. */ public class ImageWrite extends MIDlet implements TargetListener, NDEFRecordListener, CommandListener { private DiscoveryManager dm; private Form form; // Read and write commands to set mode of the application private Command readCommand; private Command writeCommand; // Boolean telling in which mode the application is private boolean read = true; protected void startApp() throws MIDletStateChangeException { dm = DiscoveryManager.getInstance(); try { // Register NDEF tag to target listener.. dm.addTargetListener(this, TargetType.NDEF_TAG); // ..and png image mime type to NDEF record listener. dm.addNDEFRecordListener(this, new NDEFRecordType( NDEFRecordType.MIME, "image/png")); } catch (IllegalStateException e) { form.append(e.toString()); } catch (ContactlessException e) { form.append(e.toString()); } form = new Form("Form"); readCommand = new Command("Read", Command.OK, 1); writeCommand = new Command("Write", Command.OK, 1); form.addCommand(writeCommand); form.setCommandListener(this); Display.getDisplay(this).setCurrent(form); } protected void pauseApp() { } protected void destroyApp(boolean arg0) throws MIDletStateChangeException { } // NDEF tag detected public void targetDetected(TargetProperties[] properties) { // If application is not in read mode - write image to tag if (!read) { NDEFTagConnection conn = null; try { // Open connection to NDEF tag conn = (NDEFTagConnection) Connector.open(properties[0] .getUrl()); // Read image from phone to ByteArrayOutputStream InputStream is = getClass().getResourceAsStream("smiley.png"); ByteArrayOutputStream baos = new ByteArrayOutputStream(); int c; while ((c = is.read()) != -1) { baos.write(c); } // Create NDEF Record to be added to NDEF Message NDEFRecord recordToWrite = new NDEFRecord(new NDEFRecordType( NDEFRecordType.MIME, "image/png"), null, baos .toByteArray()); // Create NDEF Message and add created record in it. NDEFMessage write_container = new NDEFMessage(); write_container.appendRecord(recordToWrite); // Write NDEF Message to tag conn.writeNDEF(write_container); form.append("Image witten"); } catch (IOException e) { form.append(e.toString()); } catch (Exception e) { form.append(e.toString()); } finally { try { if (conn != null) { conn.close(); } } catch (IOException e) { form.append(e.toString()); } } } } // proper NDEF message detected public void recordDetected(NDEFMessage mes) { // If application is in read mode - show image from tag if (read) { // Get png image record from NDEF message NDEFRecord rec[] = mes.getRecord(new NDEFRecordType( NDEFRecordType.MIME, "image/png")); // Create Image from payload data from record - which is the image // as byte array. Image img = Image.createImage(rec[0].getPayload(), 0, (int) rec[0] .getPayloadLength()); form.append(img); } } // Change application mode from reading to writing and vice versa public void commandAction(Command c, Displayable d) { if (read) { read = false; form.removeCommand(writeCommand); form.addCommand(readCommand); } else { read = true; form.removeCommand(readCommand); form.addCommand(writeCommand); } } }
| Related Discussions | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| JSR 257 Read Mifare Ultra Light | Alex_Polt | Near Field Communication | 9 | 2007-08-17 12:09 |
| 6131, Tag reading and Permissions | lionel.jive | Near Field Communication | 3 | 2007-04-27 13:17 |
| Karaoke midlet (midp 1.0) | budfoxone | Mobile Java General | 1 | 2003-09-20 04:42 |
| Re-reading the NDEF message | npr.novo | Near Field Communication | 1 | 2007-09-26 09:12 |
| Image localization through gps info in exif | dresnu | Location Based Services and Navigation | 6 | 2008-03-18 01:09 |
