Live Traffic

Our Story

Ceremony

Search

Just Married

Storing Image into RMS

by - May 12, 2010

import javax.microedition.lcdui.*;
import javax.microedition.midlet.MIDlet;
import javax.microedition.rms.*;
import java.io.*;

public class ImageStore extends MIDlet implements CommandListener {

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

public ImageStore() {

rStore = null;

display = Display.getDisplay(this);

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

form = new Form(”Image Show”);

}

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

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

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

public void pauseApp() {
}

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

public void destroyApp(boolean flag) {
Close();
}

public Image load(int width,int height) {

byte[] b = null;
String imagename = null;
Image image = null;

try {

int i = rStore.getNumRecords();

for(int j = 1; j < i + 1; j++) { if(rStore.getRecord(j) != null) { b = rStore.getRecord(j); ByteArrayInputStream bin = new ByteArrayInputStream( b ); DataInputStream din = new DataInputStream( bin ); imagename = din.readUTF(); int remaining = (b.length-imagename.getBytes().length-2)/4; int[] rawdata = new int[remaining]; for(int k =0 ;k < rawdata.length ;k++) { rawdata[k] = din.readInt(); } image = Image.createRGBImage(rawdata, width, height, false); bin.reset(); din.close(); din =null; } } } catch (IOException e) { e.printStackTrace(); } catch(RecordStoreException recordstoreexception) { recordstoreexception.printStackTrace(); } return image; } public boolean save(Image img, int width, int height, String imgName) { if (img == null || width < 0 || height < 0 || imgName == null) { throw new IllegalArgumentException(”Check arguments”); } int[] imgRgbData = new int[width * height]; try { img.getRGB(imgRgbData, 0, width, 0, 0, width, height); } catch (Exception e) { // Problem getting image RGB data return false; } try { // Write image data to output stream (in order to get // the record bytes in needed form) ByteArrayOutputStream baos = new ByteArrayOutputStream(); DataOutputStream dos = new DataOutputStream(baos); dos.writeUTF(imgName); for (int i = 0; i < imgRgbData.length; i++) { dos.writeInt(imgRgbData[i]); } // Open record store, create if it doesn’t exist rStore.addRecord(baos.toByteArray(), 0, baos.toByteArray().length); // Add record } catch (RecordStoreNotFoundException rsnfe) { // Record storage not found return false; } catch (RecordStoreException rse) { // Other record storage problem return false; } catch (IOException ioe) { // Problem writing data return false; } return true; // We’ve successfuly done } public void commandAction(Command command, Displayable displayable) { if(command == CmdExit) { destroyApp(true); notifyDestroyed(); } else if(command == CmdOpen) { Form showform = new Form(”Image from DB”); Image i = load(image.getWidth(),image.getHeight()); if(i !=null ) { Image img = Image.createImage(i); showform.append(img); } showform.addCommand(CmdBack); showform.setCommandListener(this); display.setCurrent(showform); } else if(command == CmdBack) { display.setCurrent(form); } else if(command == CmdSave) { byte[] b = null; Alert a =new Alert(”Image saved”); try { if(save(image,image.getWidth(),image.getHeight(),”leaf”)) a.setString(”Success”); else a.setString(”Failed”); a.setTimeout(1000); } catch (Exception e) { e.printStackTrace(); } display.setCurrent(a); } } } http://j2me-codes.inhandlearning.com/?p=34

You May Also Like

0 comments