Storing Image into RMS


<strong>import javax.microedition.lcdui.*;</strong>
<strong> import javax.microedition.midlet.MIDlet;</strong>
<strong> import javax.microedition.rms.*;</strong>
<strong> import java.io.*;</strong>

<strong>public class ImageStore extends MIDlet implements CommandListener {</strong>

<strong>private Command CmdExit;</strong>
<strong> private Command CmdOpen;</strong>
<strong> private Command CmdBack;</strong>
<strong> private Command CmdSave;</strong>
<strong> private Display display;</strong>
<strong> RecordStore rStore;</strong>
<strong> Form form = null;</strong>
<strong> Image image = null;</strong>
<strong> InputStream is =null;</strong>

<strong>public ImageStore() {</strong>

<strong>rStore = null;</strong>

<strong>display = Display.getDisplay(this);</strong>

<strong>CmdExit = new Command(”Exit”, 1, 2);</strong>
<strong> CmdOpen = new Command(”Show”, 1, 3);</strong>
<strong> CmdBack = new Command(”Back”, 1, 3);</strong>
<strong> CmdSave = new Command(”Save”, 1, 3);</strong>

<strong>form = new Form(”Image Show”);</strong>

<strong>}</strong>

<strong>public void startApp() {</strong>
<strong> try {</strong>
<strong> rStore = RecordStore.openRecordStore(”imagefile”, true);</strong>
<strong> } catch(RecordStoreException recordstoreexception) {</strong>
<strong> recordstoreexception.printStackTrace();</strong>
<strong> }</strong>
<strong> try {</strong>

<strong>is = getClass().getResourceAsStream(”/leaf.jpg”);</strong>
<strong> image = Image.createImage(is);</strong>
<strong> form.append(image);</strong>

<strong>} catch(IOException ioexception) { }</strong>
<strong> form.addCommand(CmdExit);</strong>
<strong> form.addCommand(CmdSave);</strong>
<strong> form.addCommand(CmdOpen);</strong>
<strong> form.setCommandListener(this);</strong>
<strong> display.setCurrent(form);</strong>
<strong> }</strong>

<strong>public void pauseApp() {</strong>
<strong> }</strong>

<strong>public void Close() {</strong>
<strong> try {</strong>
<strong> rStore.closeRecordStore();</strong>
<strong> } catch(RecordStoreNotOpenException recordstorenotopenexception) {</strong>
<strong> recordstorenotopenexception.printStackTrace();</strong>
<strong> } catch(RecordStoreException recordstoreexception) {</strong>
<strong> recordstoreexception.printStackTrace();</strong>
<strong> }</strong>
<strong> }</strong>

<strong>public void destroyApp(boolean flag) {</strong>
<strong> Close();</strong>
<strong> }</strong>

<strong>public Image load(int width,int height) {</strong>

<strong>byte[] b = null;</strong>
<strong> String imagename = null;</strong>
<strong> Image image = null;</strong>

<strong>try {</strong>

<strong>int i = rStore.getNumRecords();</strong>

<strong>for(int j = 1; j &lt; i + 1; j++) {</strong>

<strong>if(rStore.getRecord(j) != null) {</strong>

<strong>b = rStore.getRecord(j);</strong>
<strong> ByteArrayInputStream bin =</strong>
<strong> new ByteArrayInputStream( b );</strong>

<strong>DataInputStream din = new DataInputStream( bin );</strong>

<strong>imagename = din.readUTF();</strong>
<strong> int remaining =</strong>
<strong> (b.length-imagename.getBytes().length-2)/4;</strong>

<strong>int[] rawdata = new int[remaining];</strong>

<strong>for(int k =0 ;k &lt; rawdata.length ;k++) {</strong>
<strong> rawdata[k] = din.readInt();</strong>
<strong> }</strong>

<strong>image = Image.createRGBImage(rawdata,</strong>
<strong> width, height, false);</strong>

<strong>bin.reset();</strong>
<strong> din.close();</strong>
<strong> din =null;</strong>
<strong> }</strong>
<strong> }</strong>
<strong> } catch (IOException e) {</strong>

<strong>e.printStackTrace();</strong>

<strong>} catch(RecordStoreException recordstoreexception) {</strong>

<strong>recordstoreexception.printStackTrace();</strong>

<strong>}</strong>

<strong>return image;</strong>
<strong> }</strong>

<strong>public boolean save(Image img, int width,</strong>
<strong> int height, String imgName) {</strong>

<strong>if (img == null || width &lt; 0 || height &lt; 0 || imgName == null) {</strong>

<strong>throw new IllegalArgumentException(”Check arguments”);</strong>

<strong>}</strong>

<strong>int[] imgRgbData = new int[width * height];</strong>

<strong>try {</strong>

<strong>img.getRGB(imgRgbData, 0, width, 0, 0, width, height);</strong>

<strong>} catch (Exception e) {</strong>
<strong> // Problem getting image RGB data</strong>
<strong> return false;</strong>
<strong> }</strong>
<strong> try {</strong>
<strong> // Write image data to output stream (in order to get</strong>
<strong> // the record bytes in needed form)</strong>
<strong> ByteArrayOutputStream baos = new ByteArrayOutputStream();</strong>
<strong> DataOutputStream dos = new DataOutputStream(baos);</strong>
<strong> dos.writeUTF(imgName);</strong>

<strong>for (int i = 0; i &lt; imgRgbData.length; i++) {</strong>
<strong> dos.writeInt(imgRgbData[i]);</strong>
<strong> }</strong>

<strong>// Open record store, create if it doesn’t exist</strong>
<strong> rStore.addRecord(baos.toByteArray(), 0,</strong>
<strong> baos.toByteArray().length); // Add record</strong>

<strong>} catch (RecordStoreNotFoundException rsnfe) {</strong>
<strong> // Record storage not found</strong>
<strong> return false;</strong>
<strong> } catch (RecordStoreException rse) {</strong>
<strong> // Other record storage problem</strong>
<strong> return false;</strong>
<strong> } catch (IOException ioe) {</strong>
<strong> // Problem writing data</strong>
<strong> return false;</strong>
<strong> }</strong>

<strong>return true; // We’ve successfuly done</strong>
<strong> }</strong>

<strong>public void commandAction(Command command, Displayable displayable) {</strong>

<strong>if(command == CmdExit) {</strong>

<strong>destroyApp(true);</strong>
<strong> notifyDestroyed();</strong>

<strong>}</strong>
<strong> else if(command == CmdOpen) {</strong>

<strong>Form showform = new Form(”Image from DB”);</strong>
<strong> Image i = load(image.getWidth(),image.getHeight());</strong>

<strong>if(i !=null ) {</strong>

<strong>Image img = Image.createImage(i);</strong>
<strong> showform.append(img);</strong>

<strong>}</strong>

<strong>showform.addCommand(CmdBack);</strong>
<strong> showform.setCommandListener(this);</strong>
<strong> display.setCurrent(showform);</strong>

<strong>} else if(command == CmdBack) {</strong>

<strong>display.setCurrent(form);</strong>

<strong>} else if(command == CmdSave) {</strong>

<strong>byte[] b = null;</strong>
<strong> Alert a =new Alert(”Image saved”);</strong>

<strong>try {</strong>
<strong> if(save(image,image.getWidth(),image.getHeight(),”leaf”))</strong>
<strong> a.setString(”Success”);</strong>
<strong> else</strong>
<strong> a.setString(”Failed”);</strong>
<strong> a.setTimeout(1000);</strong>
<strong> } catch (Exception e) {</strong>
<strong> e.printStackTrace();</strong>
<strong> }</strong>
<strong> display.setCurrent(a);</strong>
<strong> }</strong>
<strong> }</strong>
<strong> }</strong>

Tinggalkan Balasan

Isikan data di bawah atau klik salah satu ikon untuk log in:

Logo WordPress.com

You are commenting using your WordPress.com account. Logout / Ubah )

Gambar Twitter

You are commenting using your Twitter account. Logout / Ubah )

Foto Facebook

You are commenting using your Facebook account. Logout / Ubah )

Foto Google+

You are commenting using your Google+ account. Logout / Ubah )

Connecting to %s

Zoemalang's community

ujung malang adalah Sebuah desa yang hilang terganti dengan ujung harapan

YoYo Games Blog Feed

Ujung malang adalah Sebuah desa yang hilang terganti dengan ujung harapan

Dealer Termurah

Menjual Segala Jenis Motor Baru Di Indonesia Cash Maupun Kredit

%d blogger menyukai ini: