Showing posts with label j2me rms. Show all posts
Showing posts with label j2me rms. Show all posts

Storing Image into RMS

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);
}
}
}

Basic RMS Example

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

public class RMSDemo extends MIDlet implements CommandListener {

private Display display;
private RecordStore rs=null;
private Command exit;
private RecordEnumeration re;
private int recordNO;
Form frm;
int index=0;
public RMSDemo() {
display = Display.getDisplay(this);

//Create a RMS
try {
rs= RecordStore.openRecordStore("myRecord",false);
rs.closeRecordStore();
} catch(Exception e) {
System.out.println(e);
}

}

public void startApp() {

frm=new Form("RMSDemo");

exit= new Command("Exit",Command.EXIT,1);
frm.addCommand(exit);

add= new Command("Add",Command.SCREN,1);
frm.addCommand(add);

delete= new Command("Delete",Command.SCREEN,2);
frm.addCommand(delete);

show= new Command("SHOW",Command.SCREEN ,3);
frm.addCommand(show);

frm.setCommandListener(this);
frm.append("#####");
display.setCurrent(frm);
}

public void pauseApp() {

}

public void destroyApp(boolean un) {
}

// Handling commands
public void commandAction(Command cmd,Displayable d) {
if(cmd==add) {
addRecord();
} else
if(cmd==delete) {
removeRecord();
} else
if(cmd==show) {
try {
byte b[]= rs.getRecord(recordNO);
String s= new String(b);
frm.append(s);
} catch(Exception e) {}
}
}


void addRecord() {
try {
rs= RecordStore.openRecordStore("myRecord",false);
index++;
byte b[]=("Record NO "+index).getBytes();
//Adding record to record store
rs.addRecord(b,0,b.length);
rs.closeRecordStore() ;
} catch(Exception e) {
System.out.println(e);
}

}

// Deleting a record
void removeRecord(int recordID) {
try {
rs= RecordStore.openRecordStore("myRecord",false);
rs.deleteRecord(recordID);
index--;
rs.closeRecordStore();
} catch(Exception e) {
System.out.println(e);
}
}


}
 

Design by Blogger Buster | Distributed by Blogging Tips