📄 imagepersistence.java
字号:
/**
*
* @author Sam Huang
* 这个类是我临时的实现,结构非常不好,因为不是本例的重点,所以先这样了,有兴趣的朋友请改进
* 在产品代码里,大家应该参考其它好的例子进行RMS操作
*/
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import javax.microedition.lcdui.Image;
import javax.microedition.rms.RecordStore;
import javax.microedition.rms.RecordStoreException;
import javax.microedition.rms.RecordStoreNotFoundException;
import javax.microedition.rms.RecordStoreNotOpenException;
public class ImagePersistence {
/*
*
* 将RMS编号为index的图片从RMS里读出来
* 因为没有加删除图片的方法,因此号码是连续的,
* 如果添加了删除图片的方法,这个得修改
*/
public static Image load(int index) {
int width = 0;
int height = 0;
RecordStore rStore = null;
byte[] b = null;
String imagename = null;
Image image = null;
int[] rawdata = null;
ByteArrayInputStream bin = null;
DataInputStream din = null;
try {
rStore = RecordStore.openRecordStore("imagefile", true);
int i = rStore.getNumRecords();
if (i < index)
return null;
if (rStore.getRecord(index) != null) {
b = rStore.getRecord(index);
bin = new ByteArrayInputStream(b);
din = new DataInputStream(bin);
imagename = din.readUTF();
width = din.readInt();
height = din.readInt();
int remaining = (b.length
- imagename.getBytes("ISO8859_1").length - 2 - 8) / 4;
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();
}
} catch (IOException e) {
//e.printStackTrace();
} catch (RecordStoreException recordstoreexception) {
//recordstoreexception.printStackTrace();
}
finally {
try {
rStore.closeRecordStore();
rStore = null;
b = null;
rawdata = null;
din = null;
bin = null;
} catch (RecordStoreNotOpenException recordstorenotopenexception) {
recordstorenotopenexception.printStackTrace();
} catch (RecordStoreException recordstoreexception) {
recordstoreexception.printStackTrace();
}
}
return image;
}
/*
*
* 从RMS里加载名字为imgName的图片,速度真的很慢呀
*/
public static Image load(String imgName) {
int width = 0;
int height = 0;
RecordStore rStore = null;
byte[] b = null;
String imagename = null;
Image image = null;
int[] rawdata = null;
ByteArrayInputStream bin = null;
DataInputStream din = null;
try {
rStore = RecordStore.openRecordStore("imagefile", true);
int i = rStore.getNumRecords();
for (int j = 1; j < i + 1; j++) {
if (rStore.getRecord(j) != null) {
b = rStore.getRecord(j);
bin = new ByteArrayInputStream(b);
din = new DataInputStream(bin);
imagename = din.readUTF();
if (!imagename.equals(imgName)) {
bin.reset();
bin = null;
din = null;
b = null;
continue;
}
width = din.readInt();
height = din.readInt();
int remaining = (b.length
- imagename.getBytes("ISO8859_1").length - 2 - 8) / 4;
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();
}
}
} catch (IOException e) {
e.printStackTrace();
} catch (RecordStoreException recordstoreexception) {
recordstoreexception.printStackTrace();
} finally {
try {
rStore.closeRecordStore();
rStore = null;
b = null;
rawdata = null;
din = null;
bin = null;
} catch (RecordStoreNotOpenException recordstorenotopenexception) {
recordstorenotopenexception.printStackTrace();
} catch (RecordStoreException recordstoreexception) {
recordstoreexception.printStackTrace();
}
}
return image;
}
/*
*
* 将名字为imgName的图片保存到RMS里
*/
public static boolean save(Image img, String imgName) {
RecordStore rStore = null;
ByteArrayOutputStream baos = null;
DataOutputStream dos = null;
int width = img.getWidth();
int height = img.getHeight();
if (img == null || imgName == null) {
throw new IllegalArgumentException("参数错误");
}
int[] imgRgbData = null;
try {
imgRgbData = new int[width * height];
img.getRGB(imgRgbData, 0, width, 0, 0, width, height);
baos = new ByteArrayOutputStream();
dos = new DataOutputStream(baos);
dos.writeUTF(imgName);
dos.writeInt(width);
dos.writeInt(height);
for (int i = 0; i < imgRgbData.length; i++) {
dos.writeInt(imgRgbData[i]);
}
// 打开RecordStore,如果不存在,则创建一个
rStore = RecordStore.openRecordStore("imagefile", true);
rStore.addRecord(baos.toByteArray(), 0, baos.toByteArray().length); // Add
} catch (Error oe) {
// oe.printStackTrace();
return false;
} catch (Exception e) {
// e.printStackTrace();
return false;
} finally {
try {
if (rStore != null) {
rStore.closeRecordStore();
rStore = null;
}
imgRgbData = null;
baos = null;
dos = null;
} catch (RecordStoreNotOpenException recordstorenotopenexception) {
recordstorenotopenexception.printStackTrace();
} catch (RecordStoreException recordstoreexception) {
recordstoreexception.printStackTrace();
}
}
return true;
}
public static boolean clearRms(String recordStoreName) {
try {
RecordStore.deleteRecordStore(recordStoreName);
} catch (RecordStoreNotFoundException e) {
//e.printStackTrace();
return false;
} catch (RecordStoreException e) {
//e.printStackTrace();
return false;
}
return true;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -