Wednesday, December 12, 2012
Menyimpan/mengambil gambar ke/dari record store pada JAVA (J2ME)
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import java.io.IOException;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import javax.microedition.rms.InvalidRecordIDException;
import javax.microedition.rms.RecordStore;
import javax.microedition.rms.RecordStoreException;
import javax.microedition.rms.RecordStoreNotFoundException;
import javax.microedition.rms.RecordStoreNotOpenException;
public class SimpanAmbilGambar extends MIDlet
implements CommandListener {
private Display display;
private Form formUtama;
private RecordStore rs = null;
private Image image = null;
private final Command cmdKeluar =
new Command("Keluar", Command.EXIT, 1);
private final Command cmdKembali =
new Command("Kembali", Command.BACK, 1);
private final Command cmdSimpan =
new Command("Simpan", Command.SCREEN, 1);
private final Command cmdBuka =
new Command("Buka", Command.SCREEN, 1);
public SimpanAmbilGambar() {
display = Display.getDisplay(this);
try {
rs= RecordStore.openRecordStore("DBGambar", true);
} catch (RecordStoreException rse) {
rse.printStackTrace();
}
formUtama = new Form("Menyimpan Gambar");
try {
image = Image.createImage("Java.png");
} catch (IOException ioe) {
ioe.printStackTrace();
}
formUtama.append(image);
formUtama.addCommand(cmdKeluar);
formUtama.addCommand(cmdSimpan);
formUtama.addCommand(cmdBuka);
formUtama.setCommandListener(this);
}
public void startApp() {
display.setCurrent(formUtama);
}
public void pauseApp() {
}
public void destroyApp(boolean unconditional) {
}
public void commandAction(Command c, Displayable s) {
if (c == cmdKeluar) {
destroyApp(false);
notifyDestroyed();
} else if (c == cmdKembali) {
display.setCurrent(formUtama);
} else if (c == cmdSimpan) {
byte[] b = null;
Alert alert = new Alert("Simpan Gambar");
alert.setTimeout(Alert.FOREVER);
try {
if (simpanGambar(image,
image.getWidth(), image.getHeight(),
"Java")) {
alert.setString("Gambar telah tersimpan " +
"di dalam record store");
} else {
alert.setString("Gambar gagal disimpan " +
"ke dalam record store");
}
} catch (Exception e) {
e.printStackTrace();
}
alert.addCommand(cmdKembali);
alert.setCommandListener(this);
display.setCurrent(alert, formUtama);
} else if (c == cmdBuka) {
Form formGambar = new Form("Gambar dari Record Store");
Image img =
ambilGambar(image.getWidth(), image.getHeight());
if (img != null) {
Image newImage = Image.createImage(img);
formGambar.append(newImage);
}
formGambar.addCommand(cmdKembali);
formGambar.setCommandListener(this);
display.setCurrent(formGambar);
}
}
public boolean simpanGambar(Image img, int lebar,
int tinggi, String nama) {
if (img == null || lebar < 0 ||
tinggi < 0 || nama == null) {
throw new IllegalArgumentException("Terjadi kesalahan " +
"pengisian parameter");
}
int[] dataRGB = new int[lebar * tinggi];
try {
img.getRGB(dataRGB, 0, lebar, 0, 0, lebar, tinggi);
} catch (Exception e) {
return false;
}
try {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
DataOutputStream dos = new DataOutputStream(baos);
dos.writeUTF(nama);
for (int i=0; i<dataRGB.length; i++) {
dos.writeInt(dataRGB[i]);
}
byte[] dataRecord = baos.toByteArray();
rs.addRecord(dataRecord, 0, dataRecord.length);
} catch (RecordStoreNotFoundException rsnfe) {
return false;
} catch (RecordStoreException rse) {
return false;
} catch (IOException ioe) {
return false;
}
return true;
}
public Image ambilGambar(int lebar, int tinggi) {
byte[] data = null;
String nama = null;
Image img = null;
try {
int jumlahRecord = rs.getNumRecords();
for (int i=1; i < jumlahRecord + 1; i++) {
if (rs.getRecord(i) != null) {
data = rs.getRecord(i);
ByteArrayInputStream bais =
new ByteArrayInputStream(data);
DataInputStream dis = new DataInputStream(bais);
nama = dis.readUTF();
int sisa = (data.length-nama.getBytes().length-2)/4;
int[] dataRGB = new int[sisa];
for (int j=0; j<dataRGB.length; j++) {
dataRGB[j] = dis.readInt();
}
img = Image.createRGBImage(dataRGB, lebar,
tinggi, false);
bais.reset();
dis.close();
dis = null;
}
}
} catch (IOException ioe) {
ioe.printStackTrace();
} catch(InvalidRecordIDException irie) {
irie.printStackTrace();
} catch (RecordStoreNotOpenException rsnoe) {
rsnoe.printStackTrace();
} catch (RecordStoreException rse) {
rse.printStackTrace();
}
return img;
}
}
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment