📄 imagealbum.java
字号:
/**
*
* @author Sam Huang
*
*/
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import javax.microedition.io.Connector;
import javax.microedition.io.HttpConnection;
import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.List;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;
public class ImageAlbum extends MIDlet implements CommandListener {
private static Display display = null;
public final static String SUB_URL = "http://10.0.0.172/ImageAlbum/";
public final static String CATALOG_URL = "http://10.0.0.172/ImageAlbum/catalog.htm";
public final static String NOTICE_URL = "http://10.0.0.172/ImageAlbum/notice.htm";
private Command submitCommand = new Command("选择", Command.OK, 2);
private Command exitCommand = new Command("退出", Command.EXIT, 3);
private static List list = new List("请选择", List.IMPLICIT,
new String[] { "汤加丽写真", "更新图库目录", "从手机存储中读取", "连接方式", "清空手机存储",
"系统公告", "关于", "帮助" }, null);
private static Image icon;
private static String currentUrl;
private static Displayable currentForm;
private static Displayable previousForm;
private static String connectionMethod;
private boolean refreshIndex = false;
// 公共静态变量 显示图像的Canvas
public static ScrollCanvas scrollCanvas;
public String[] catalogFromServer;
public ImageAlbum() {
connectionMethod = FormConf.loadConnectionMethod();
display = Display.getDisplay(this);
// System.out.println(connectionMethod);
list.addCommand(submitCommand);
list.addCommand(exitCommand);
list.setCommandListener(this);
this.getIcon();
display.setCurrent(list);
}
private Image getIcon() {
try {
icon = Image.createImage("/icon.png");
} catch (Exception e) {
// 忽略
}
return icon;
}
public void startApp() throws MIDletStateChangeException {
}
public void pauseApp() {
}
public void destroyApp(boolean unconditional) {
}
private void actionBySelectedIndex(int i) {
String catalog = list.getString(i);
if (catalog.equals("汤加丽写真")) {
currentUrl = SUB_URL + "qrjlejfljlkwdejglrwe2" + "/tj01.jpg";
getImageFromServer();
} else if (catalog.equals("更新图库目录")) {
if (!this.refreshIndex) {
new GetCatalog(CATALOG_URL, true).start();
} else {
showAlert("已经更新过图库列表");
}
} else if (catalog.equals("从手机存储中读取")) {
// 从RMS里读取一个图像,在模拟器里非常慢,但在真机上好像还可以
Image img = ImagePersistence.load(1);
if (img != null) {
// 设置currentUrl,作为标志位,当length==1时,说明是从RMS里读的
currentUrl = "" + 1;
scrollCanvas = new ScrollCanvas(img);
display.setCurrent(scrollCanvas);
} else {
showAlert("你还没有保存图像,不能加载");
}
} else if (catalog.equals("连接方式")) {
FormConf config = new FormConf();
config.setCommandListener(config);
setCurrentForm(config);
setPreviousForm(list);
display.setCurrent(config);
} else if (catalog.equals("清空手机存储")) {
if (!ImagePersistence.clearRms("imagefile")) {
showAlert("清空手机存储出错,可能你没有保存过图片");
} else {
showAlert("清空手机存储成功");
}
} else if (catalog.equals("系统公告")) {
new GetCatalog(NOTICE_URL, false).start();
} else if (catalog.equals("关于")) {
showAlert("此程序的创意来自于the3gwireless(http://blog.csdn.net/the3gwireless),第一版的源代码由the3gwireless利用从互联网上获得的资料整理修改完成."
+ "目的是帮助J2ME的爱好者学习和掌握基本的图像处理知识,也希望其它深谙此道的朋友对代码进行进一步完善和增强."
+ "此代码主要作为学习使用,时间仓促,程序结构和内容肯定会有许多问题,因此不适合在产品中直接使用."
+ "大家可以任意修改和使用此代码,如果做过修改并要在互联网上公开传播,请一定在本文后注明自己的名字,修改或者增强的功能以及新的版本号.并将新的文件发给sam4it@gmail.com以便在下载服务器上更新.\n"
+ "版本:1.0.0,作者:the3gwireless,功能:状态条显示从服务器上读取图像,图像的90度/180度翻转,水平镜像,垂直镜像,缩小,增大,淡出渐进,保存和加载(RMS),生成拇指图标,横向纵向滚动图片,调整图像及背景颜色等.\n"
+ "版本:1.0.1,作者:the3gwireless,功能:添加了灰度显示图像(两种方法),调节图像明暗,反转显示图像"
+ "版本:1.0.2,作者:the3gwireless,功能:支持CMWAP和CMNET两种连接方式,动态更新图库列表,清空手机存储图片功能,系统公告");
} else if (catalog.equals("帮助")) {
showAlert("1)上下左右键以及2,4,6,8键控制图片的移动,当图片较大时,可以通过移动图片来看到隐藏的部分,按住不放则表示连续移动\n"
+ "2)按键3,5,7(调整R,G,B)可以调整背景色,按住不放则表示连续调整\n"
+ "3)按键0,7,9(调整R,G,B)可以调整图像颜色\n"
+ "3)其它的功能直接看菜单项\n"
+ "在开发中遇到任何问题,请登陆http://www.j2medev.com,中国最好的J2ME开发论坛.");
} else {
int len = this.catalogFromServer.length;
currentUrl = SUB_URL + this.catalogFromServer[len - i * 3 + 1]
+ "/" + this.catalogFromServer[len - 1 - i * 3 + 3];
// System.out.println(currentUrl);
getImageFromServer();
}
}
private void getImageFromServer() {
HttpWaitForm waitForm = new HttpWaitForm(currentUrl);
// 这里设置当前窗口和原来窗口的目的是为了导航
// 只是一个很简单的实现,如果大家感兴趣,可以去www.j2medev.com的论坛,里边有很多好文章
// 介绍了如何很好地进行屏幕导航,由于如何屏幕导航并不是本例的重点,加上时间的原因,因此这里没有体现,有兴趣的朋友可以优化一下
setCurrentForm(waitForm);
setPreviousForm(list);
display.setCurrent(waitForm);
}
public void refreshList(String[] newCatalog) {
this.refreshIndex = true;
for (int i = 0; i < newCatalog.length; i++) {
if (i % 3 == 0) {
list.insert(1, newCatalog[i], null);
}
}
display.setCurrent(list);
}
public void commandAction(Command c, Displayable d) {
int i = list.getSelectedIndex();
if (c == List.SELECT_COMMAND) {
actionBySelectedIndex(i);
}
if (c == exitCommand) {
destroyApp(true);
notifyDestroyed();
} else if (c == submitCommand) {
actionBySelectedIndex(i);
}
}
public static Display getDisplay() {
return display;
}
public static List getList() {
return list;
}
public static void showAlert(String err) {
Alert a = new Alert("");
a.setString(err);
a.setImage(icon);
a.setTimeout(Alert.FOREVER);
ImageAlbum.getDisplay().setCurrent(a);
}
public static String getCurrentUrl() {
return currentUrl;
}
public static void setCurrentUrl(String currentUrl) {
ImageAlbum.currentUrl = currentUrl;
}
public static Displayable getCurrentForm() {
return currentForm;
}
public static void setCurrentForm(Displayable currentForm) {
ImageAlbum.currentForm = currentForm;
}
public static Displayable getPreviousForm() {
return previousForm;
}
public static void setPreviousForm(Displayable previousForm) {
ImageAlbum.previousForm = previousForm;
}
public static String getConnectionMethod() {
return connectionMethod;
}
public static void setConnectionMethod(String connectionMethod) {
ImageAlbum.connectionMethod = connectionMethod;
}
// public boolean isRefreshIndex() {
// return refreshIndex;
// }
//
// public void setRefreshIndex(boolean refreshIndex) {
// this.refreshIndex = refreshIndex;
// }
private class GetCatalog extends Thread {
private String url;
private boolean isGetCatalog;
GetCatalog(String urlAddress, boolean isGetCatalog) {
this.url = urlAddress;
this.isGetCatalog = isGetCatalog;
}
public void run() {
String result = getInfFromServer(this.url);
if (isGetCatalog) {
if (result == null) {
showAlert("当前没有新的图库目录");
} else {
// 从服务器上下载catalog.txt的内容
// 返回结果是以","号分开的字符串,结构如下:
// 图库名字,目录,起始文件名,图库名字,目录,起始文件名。。。。
// 例如:
// 艺术摄影,nhkhihiouo,th_001.jpg,人体彩绘,frlwejtowreot,ch01.jpg
// 从结构上讲,应该把catalog.txt的内容放在一个xml描述文件里,这样会比较有逻辑性
// 比如:
// <CATALOG>
// <NAME>艺术摄影</NAME>
// <FOLDER>nhkhihiouo</FOLDER>
// <FILENAME>th_001.jpg</FILENAME>
// </CATALOG>
// 但是解析xml文件需要其它包如kxml,为了节约空间和简化编程,在此不使用这种方法。
// 具体问题具体对待
catalogFromServer = StringUtil.split(result, ",");
refreshList(catalogFromServer);
}
} else {
showAlert(result);
}
}
private String getInfFromServer(String url) {
HttpConnection httpconnection = null;
InputStream inputstream = null;
OutputStream outputstream = null;
String result = null;
try {
if (ImageAlbum.getConnectionMethod().equals("cmwap")) {
httpconnection = (HttpConnection) Connector.open(url);
httpconnection.setRequestProperty("X-Online-Host",
"www.the3gwireless.com");
} else {
// 不使用代理
StringBuffer sb = new StringBuffer(url.substring(0, url
.indexOf("10.0.0.172")));
sb.append("www.the3gwireless.com");
sb.append(url.substring(sb.length()
- "www.the3gwireless.com".length()
+ "10.0.0.172".length(), url.length()));
String url1 = sb.toString();
httpconnection = (HttpConnection) Connector.open(url1);
}
httpconnection.setRequestMethod("GET");
outputstream = httpconnection.openOutputStream();
if (httpconnection.getResponseCode() == 200) {
inputstream = httpconnection.openInputStream();
int k;
byte abyte0[] = null;
if ((k = (int) httpconnection.getLength()) > 0) {
int l = 0;
int i1 = 0;
abyte0 = new byte[k];
for (; i1 != k && l != -1; i1 += l)
l = inputstream.read(abyte0, i1, k - i1);
} else if (k == -1) {
ByteArrayOutputStream bStrm = new ByteArrayOutputStream();
int ch;
while ((ch = inputstream.read()) != -1) {
bStrm.write(ch);
}
abyte0 = bStrm.toByteArray();
bStrm.close();
}
//System.out.println(abyte0.length);
//catalog.htm和notice.htm是utf8格式的文件,去掉头
result = (new String(abyte0,3,abyte0.length-4,"UTF-8")).trim();
if (result == null || result.length() == 0) {
result = null;
}
}
try {
if (outputstream != null)
outputstream.close();
if (inputstream != null)
inputstream.close();
if (httpconnection != null)
httpconnection.close();
} catch (Exception ex) {
// ignored
}
return result;
} catch (Exception e) {
return null;
} finally {
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -