??xml version="1.0" encoding="utf-8" standalone="yes"?>国产欧美亚洲精品a,午夜视频久久久,桃乃木かなav在线播放http://www.aygfsteel.com/lukewange-hit1983/category/33793.htmlpresentzh-cnFri, 12 Sep 2008 17:13:14 GMTFri, 12 Sep 2008 17:13:14 GMT60Serializing an Image http://www.aygfsteel.com/lukewange-hit1983/archive/2008/09/11/228407.htmlLukeWLukeWThu, 11 Sep 2008 09:09:00 GMThttp://www.aygfsteel.com/lukewange-hit1983/archive/2008/09/11/228407.htmlhttp://www.aygfsteel.com/lukewange-hit1983/comments/228407.htmlhttp://www.aygfsteel.com/lukewange-hit1983/archive/2008/09/11/228407.html#Feedback0http://www.aygfsteel.com/lukewange-hit1983/comments/commentRss/228407.htmlhttp://www.aygfsteel.com/lukewange-hit1983/services/trackbacks/228407.htmlSerializing an Image

Creating an image from an array of data is an easy task, but to create a byte-array of data from an image is a little more complicated. But it's required if you want to send a modified image to a server.

To create a byte-array of data from an image, we can use the getRGB(..) method in the image class in MIDP 2.0. From the getRGB method we get an int-array of data containing all the ARGB values of each pixel in the image. Integers in java are four bytes, so we need to split each int value into four byte values.

To mask out each of the bytes in the int we can use the 'AND &' operator and the 'shift-right >>' operator. Here's an example:

int ARGB = 0xFFFFFFFF;  // AARRGGBB 
int a = (ARGB & 0xFF000000);
int r = (ARGB & 0x00FF0000);
int g = (ARGB & 0x0000FF00);
int b = (ARGB & 0x000000FF);
// Here we move each bit to the right.
a = (a >> 24); // a = 0x000000FF
r = (r >> 16); // r = 0x000000FF
g = (g >> 8); // g = 0x000000FF
b = b;        // b = 0x000000FF

When we convert the integer to a byte, there are some problems since there are only signed bytes in Java. A byte may contain values between –128 and 127 and from our integer we'll have a value between 0 and 255.

Here are some conversion examples:

Integer val 127 = byte val 127. 
Integer val 128 = byte val –128.
Integer val 255 = byte val –1.
byte ba = (byte)(a);  // if a=0x000000FF (255), then ba = -1
byte br = (byte)(r);
byte bg = (byte)(g);
byte bb = (byte)(b);

We have to loop though each pixel in the image and get each pixel value to our byte-array. When that's done, we can send the image to a server where we can convert the byte-array back to a integer-array and then show our picture.

So, when we convert the byte back to an integer we have to check if the byte value is less than zero.

Int a, r, g, b;
If(ba<0)
     a = 256 – a;

Now our new integer value will be between 0 and 255 and we just have to use the 'shift-left <<' operator and add the values together to get our new int-array.

a = (a << 24);
r = (r << 16);
g = (g << 8);
b = (b);
  0xFF000000 (a)
+ 0x00FF0000 (r)
+ 0x0000FF00 (g)
+ 0x000000FF (b)
= 0xFFFFFFFF (argb)
int ARGB = a+r+g+b;

After the integer-array is recreated we can use the createRGBImage(..) method in the Image class to create our image.

Be aware that the byte-array is quite large as it contains all of the ARGB values for each pixel. If an image is 100*60 px, where each pixel is four bytes, the byte array will be 24kb.

ImageBytearryConvert.rar



LukeW 2008-09-11 17:09 发表评论
]]>
J2me的Listȝhttp://www.aygfsteel.com/lukewange-hit1983/archive/2008/08/15/222261.htmlLukeWLukeWFri, 15 Aug 2008 07:20:00 GMThttp://www.aygfsteel.com/lukewange-hit1983/archive/2008/08/15/222261.htmlhttp://www.aygfsteel.com/lukewange-hit1983/comments/222261.htmlhttp://www.aygfsteel.com/lukewange-hit1983/archive/2008/08/15/222261.html#Feedback0http://www.aygfsteel.com/lukewange-hit1983/comments/commentRss/222261.htmlhttp://www.aygfsteel.com/lukewange-hit1983/services/trackbacks/222261.html 那就是getSelectedIndex()函数Q一般情况下它都能正常工作。唯有在List处于复选模式(MULTIPLEQ时Q用该函数无法获得当前高亮条选中索引Q而Lq回-1?br />
参考List控g的源代码Q可以发玎ͼListcdCChoice接口Qƈ且包含一个ChoiceGroup成员。它才是实现List大部分功能的大功臣?br /> ListcȝgetSelectedIndex()ҎQ实际上是ChoiceGroup的getSelectedIndex()Ҏ。大家可以参考Doc中关于ChoiceGroup的getSelectedIndex()Ҏ的如下部分?br /> “For ChoiceGroup objects of type MULTIPLE, this always returns -1 because no single value can in general represent the state of such a ChoiceGroup.”

是_List在多选模式(MULTIPLEQ下Q我们是无法获取当前高亮条所在项的烦引值的?br /> 当然Q你可以自己数数?br />
也许你觉得这个问题不严重,也许吧。但我觉得对于程序员来说Q最严重的问题就是理解发生偏差?br /> 比如我,在发现这个问题之前。凭借多q的~程l验Q我很确定很定的认为,q个getSelectedIndex()永远能够获得当前光标所指项的烦引倹{?br /> ……当错误来临时Q我百思不得其解。最后,׃好些旉去调试才发现Q那个值ȝ?1。操Q花了太多时间去猜测原本正确的代码?br /> 我只惛_诉大Ӟ真正耗费旉最多,让h最恶心的错误,往往是q样的问题。再操!

那么有什么简单的解决办法么?
我可以很负责ȝ告诉你,自己写一个多选的List控g吧,C不要让getSelectedIndex()总返?1。即使是多选,有时候也是需要这个值的?br />
也许你想l承List,然后重蝲getSelectedIndex()Ҏ。但你无法重载List的keyPressedҎQ其实List{Screen控g都用Canvas写的Q?br /> ……或许q能惛_别的什么好办法。不q我的选择是自己写一个List控g代替它(如果对它感兴,请回复,改天我在弄出来吧Q?br />
至于List的其它功能,没什么重复的必要的,看看文档吧?

LukeW 2008-08-15 15:20 发表评论
]]>
Vector ȝhttp://www.aygfsteel.com/lukewange-hit1983/archive/2008/08/15/222260.htmlLukeWLukeWFri, 15 Aug 2008 07:19:00 GMThttp://www.aygfsteel.com/lukewange-hit1983/archive/2008/08/15/222260.htmlhttp://www.aygfsteel.com/lukewange-hit1983/comments/222260.htmlhttp://www.aygfsteel.com/lukewange-hit1983/archive/2008/08/15/222260.html#Feedback0http://www.aygfsteel.com/lukewange-hit1983/comments/commentRss/222260.htmlhttp://www.aygfsteel.com/lukewange-hit1983/services/trackbacks/222260.html 在仔l阅L代码后发玎ͼVectorq没有Q何链表的性质。它是一个纯_的数组。当内存不够用时Q就重新初始化一个容量较大新数组Q然后用System.arraycopy()函数原有的数组copy到新的数l当中?br />
System.arraycopy()是一个由pȝq_来实现的函数Q这Lpȝ调用性能是比较高的?br /> 即如此Q我们在写程序时Q注意initialCapacityQ初始容量)和capacityIncrementQ增量)的设|,会有效的减重新定义数lƈ且拷贝数l的ơ数?br />
例如Q?br />
Vector v = new Vector(1010);//初始定w?0Q增?0
Integer[] ints = new Integer11;
for (int i = 0; i < ints.length; i++) {
ints 
= new Integer(i);
v.addElement(ints);
}

在这里,当vdW?1个Integer的时候,v׃自动创徏一个长度ؓ20的数l(当前定w+增量Q,以后每次装满数组Q都会重新按增量q加数组长度?br /> 所以,讄一个合适的初始定w和增量,会提高Vector的效率。千万别像我一P把它当做链表。因为链表在增长I间时是不会影响C前用的I间和数据的?br />
我想指出一点,因ؓVector中存储的Q实际上都是Object变量Q大家可以把它理解ؓ指针。重新初始化Vector内置的数lƈ且拷贝,相当于对一个指针数l进行操作,q不{同于对输入cd的重新分配内存?br /> 是说Vector的重新分配,不论q行多少ơ都不涉及到Integer对象的创建,拯{工作。它只是重新创徏q拷?#8220;指针”数组Q也是Object数组?br /> 此外Qjava中有一个容易被忽视的基本概c当某个对象再没有指向该对象的引用时Q垃圑֛收器才会自动其自动释放。不心使用Q很Ҏl程序造成内存问题?br /> 例如Q?br />

Vector v = new Vector(1010);//初始定w?0Q增?0
Integer[] ints = new Integer11;
for (int i = 0; i < ints.length; i++) {
ints 
= new Integer(i);
v.addElement(ints);
}
ints 
= null;//对它的释攑ְ造成数组元素的释放。但是由于v中还保存有元素的引用Q所以这些Integer对象q不会被回收?/span>
我强调这个问题,是因为曾l写q一D代码:
Image img = Image.createImage(”/xxx.png”);
Image img2 = img;
q张囄非常大,在用完img之后Q我释放掉它Q然后重新申请另一个图片。于是,内存爆了。因为img2仍然持有xxx.png的引用,所以无法释放?br /> Vectro中存储的也是引用Q所以在使用时应该更加注意编E规范,以免发生cM的问题。其实应该尽量避免用多个引用?br />
上面说了q些题外话,正是惌告各位程序员QVector是一个可变的Object数组Q一个引用数l。所以请大家使用时要心Q别惛_然的以ؓ它是一个容器,它里面存储的可不是对象,而是引用?br /> 而且Q在释放曄加入到vector的对象时Q对象本wƈ不会被真正释放,得到回收。只是原有的引用无法再用Ş了?br />
最后还有一Ҏ巧?br /> 应该量使用索引获取对象Q避免用IndexOf()Ҏ。把它当做堆栈来使用时更应该注意Q要避免使用insertElementAt()Ҏ?br /> 此外Qj2me中的StackcLZVector实现的,使用时也要留心?/em>

LukeW 2008-08-15 15:19 发表评论
]]>
汉字转拼?/title><link>http://www.aygfsteel.com/lukewange-hit1983/archive/2008/08/15/222256.html</link><dc:creator>LukeW</dc:creator><author>LukeW</author><pubDate>Fri, 15 Aug 2008 07:15:00 GMT</pubDate><guid>http://www.aygfsteel.com/lukewange-hit1983/archive/2008/08/15/222256.html</guid><wfw:comment>http://www.aygfsteel.com/lukewange-hit1983/comments/222256.html</wfw:comment><comments>http://www.aygfsteel.com/lukewange-hit1983/archive/2008/08/15/222256.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.aygfsteel.com/lukewange-hit1983/comments/commentRss/222256.html</wfw:commentRss><trackback:ping>http://www.aygfsteel.com/lukewange-hit1983/services/trackbacks/222256.html</trackback:ping><description><![CDATA[     摘要: 在网上参考了一些汉字{换到拼音的资料。思\应该只有以下两种?1Q查表法。这样做需要一个庞大的映射表,在j2me环境下不大合适。不q效果好Q有些还支持多音字?2Q用GB字库的映关pR因为GB2312及其扩展GBK的汉字编码都ҎZ于拼韛_在映关pR?实际上网l上的大部分文章都是ҎW二U方法来实现的? 我也是采用这U方法,因ؓ它基本上可以利用GB2312字库Q直接映成拼音?..  <a href='http://www.aygfsteel.com/lukewange-hit1983/archive/2008/08/15/222256.html'>阅读全文</a><img src ="http://www.aygfsteel.com/lukewange-hit1983/aggbug/222256.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.aygfsteel.com/lukewange-hit1983/" target="_blank">LukeW</a> 2008-08-15 15:15 <a href="http://www.aygfsteel.com/lukewange-hit1983/archive/2008/08/15/222256.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>GB2312转换Unicodehttp://www.aygfsteel.com/lukewange-hit1983/archive/2008/08/15/222250.htmlLukeWLukeWFri, 15 Aug 2008 06:58:00 GMThttp://www.aygfsteel.com/lukewange-hit1983/archive/2008/08/15/222250.htmlhttp://www.aygfsteel.com/lukewange-hit1983/comments/222250.htmlhttp://www.aygfsteel.com/lukewange-hit1983/archive/2008/08/15/222250.html#Feedback0http://www.aygfsteel.com/lukewange-hit1983/comments/commentRss/222250.htmlhttp://www.aygfsteel.com/lukewange-hit1983/services/trackbacks/222250.html 后来l箋对编码及charcdq行学习Q发C些有的问题?br /> 首先java环境下的charcd变量Q实际上是以unicode方式存储的?br /> 所以以下方法有效:

输入unicode~码的byte数组Q即可两两拼接成一个char?br /> 而Stringcd实际上就是在char数组的基上衍生出来的。大家可以参考cldc的源代码?br />
public static String read_Uni(byte[] word_unicode) {
        StringBuffer stringbuffer 
= new StringBuffer("");
        
for (int j = 0; j < word_unicode.length;) {
            
int l = word_unicode[j++];
            
int h = word_unicode[j++];
            
char c = (char) ((l & 0xff| ((h << 8& 0xff00));
            stringbuffer.append(c);
        }
        
return stringbuffer.toString();
    }


j2me环境下也是如此?br /> 所以在W一ơ给出的转换cMQ提供的gb2312到utf-8直接转换的快速方法。现在看来是画蛇添了?br />
Ҏ以上l验Q更新{换类如下Q?br />
public class HGB2312 {
 
        
private byte[] map = new byte[15228];
 
        
public HGB2312() throws Exception {
            InputStream is 
= getClass().getResourceAsStream("/gb2u.dat");
            is.read(map);
            is.close();
        }
 
        
public String gb2utf8(byte[] gb) {
            StringBuffer sb 
= new StringBuffer();
            
int c, h, l, ind;
            
for (int i = 0; i < gb.length;) {
                
if (gb[i] >= 0) {
                    sb.append((
char) gb[i++]);
                } 
else {
                    h 
= 256 + gb[i++];
                    l 
= 256 + gb[i++];
                    h 
= h - 0xA0 - 1;
                    l 
= l - 0xA0 - 1;
                    
if (h < 9) {
                        ind 
= (h * 94 + l) << 1;
                        c 
= (byte2Int(map[ind]) << 8 | byte2Int(map[ind + 1]));
                        sb.append((
char) c);
                    } 
else if (h >= 9 && h <= 14) {
                        sb.append((
char0);
                    } 
else if (h > 14) {
                        h 
-= 6;
                        ind 
= (h * 94 + l) << 1;
                        c 
= (byte2Int(map[ind]) << 8 | byte2Int(map[ind + 1]));
                        sb.append((
char) c);
 
                    } 
else {
                        sb.append((
char0);
                    }
                }
            }
            
return sb.toString();
        }
 
        
private int byte2Int(byte b) {
            
if (b < 0) {
                
return 256 + b;
            } 
else {
                
return b;
            }
        }
    }


q个Ҏ明显要比W一ơ快很多了,直接查表Q然后拼接成StringQ不需要{换成utf-8~码?br />
数据文g请在http://download.csdn.net/source/263609获取

MQjava中的charcd实际上是存储了unicode~码。目前在nokia 5300上测试通过?br /> 我觉得其它机器也应该是这栗如果哪位大侠知道这斚w的资料,误教?

LukeW 2008-08-15 14:58 发表评论
]]>
MIDP2.0及MIDP数字{֐http://www.aygfsteel.com/lukewange-hit1983/archive/2008/08/15/222247.htmlLukeWLukeWFri, 15 Aug 2008 06:53:00 GMThttp://www.aygfsteel.com/lukewange-hit1983/archive/2008/08/15/222247.htmlhttp://www.aygfsteel.com/lukewange-hit1983/comments/222247.htmlhttp://www.aygfsteel.com/lukewange-hit1983/archive/2008/08/15/222247.html#Feedback0http://www.aygfsteel.com/lukewange-hit1983/comments/commentRss/222247.htmlhttp://www.aygfsteel.com/lukewange-hit1983/services/trackbacks/222247.html
一、概q?

MIDP2.0 采用了全新的安全机制Q这对于需要调用一个敏感的(重要?函数?API ?MIDlet 开发者来讲是必须了解的,如:|络q接 API 、消?API 和推 (Push) 函数{,q有一些可选的 MIDP 包也有许多受限制?API ?

虽然购买代码{֐证书需要费 用,但签?MIDlet 对开发者来讲是收益非浅的,因ؓ许多受保护的 API 都是需要签名的Q以保护开发者和用户的利益。当Ӟ有些应用是不需要签名的Q如有些不需要联|的仅用C些图?API 的小游戏软g。但一些重要的应用Q如Q连接网l、发送短消息 ( 短信和彩?) 或访问移动终?( 手机?PDA {,以下UCؓ手机 ) 上的 PIM( 个h信息理 ) 数据{等都需要签名?

数字{֐ MIDlet 的好处包括:

(1) Z MIDlet 的安全策略,某些功能是必ȝ名才能用的Q而有些功能虽然不{֐也可以用,但必要求用户在使用时确认和修改其安全策略,如:写用h据缺省是不允许没有签名的 MIDlet 操作的;

(2) Z手机的系l安全和Ud|络的安全考虑Q某些手机制造商、移动运营商{可能拒l没有签名的 MIDlet 在手Z安装和运行;

(3) 大大改善用户体验Q让用户使用方便Q得用户不会遭遇调用受保护 API 时的安全警告的烦|

(4) Z安全考虑Q安装没有签名的 MIDlet 是会有安全警告的Q而相反,安装已经{֐?MIDlet 则不会出现烦人的警告Q手Z自动验证{֐而顺利地安装成功Q?

(5) 已经{֐?MIDlet 得用戯改善其低安全{略讄Q提高手机的安全性;

(6) 保已经{֐?MIDlet 不会被非法篡改和非法盗用?

二?MIDP 2.0 安全机制

MIDP 是一个开攄q_Q得Q何h都可以ؓ支持 MIDP 的设备开发各U应用YӞ一般都是移动终端设备?MIDlet 套g可以以匿名方式通过|络下蝲Q非常方便,但这也会带来许多安全问题和隐U信息保护问题,用户会问Q?MIDlet 能把用户的个Z息发l不知道的服务器吗?会自动生没有授权的呼叫或短消息而给用户带来费用吗?恶意软g会破坏手机?{等?

除了 Java 语言的安全特性外Q?MIDP q增加了许多安全考虑?MIDP 2.0 ?MIDP 1.0 增强了安全策略,?API 分ؓ普?API 和敏?API Q如Q通过 HTTP 协议讉KUd|络Q由于会l用户生费用, 所以被列ؓ 敏感 API ?MIDlet 2.0 推出了可信Q MIDlet(trusted) 和不可信?MIDlet(untrusted) 的概念,一个不可信?MIDlet 只能讉K有限?API Q同时还需要用h动确认ƈ修改其安全策略;而可信Q MIDlet 则自动承系l中的安全策略而获得访问许可?

许可 (Permissions) 用于需要n份认证的 敏感 API ?MIDP 2.0 要求调用 敏感 API 之前必须获得必要的许可,q些许可包的命名?J2SE 许可Q如Q?HTTP q接许可同样UCؓQ? javax.microedition.io.Connector.http ?有关许可的文档同意归cd受保?API 中?

2.1 Protection Domains( 保护?)

保护域是 MIDP 2.0 中一个非帔R要的安全概念Q一个保护域是一个许可集和一U交互模式,q些许可既可以是自己l承的,也可能是用户讄的,前者称为允? (allowed) Q而后者称为用户允?(user permission) 。当一?MIDlet 被安装后Q它被分配到一个指定的保护域而获得它的许可和交互模式?

而用户允许则需要用戯己决定是否同意,用户既拒l一个许可,也可以同意。用户允许有 3 U交互模式: blanket( 普遍适用 ) ? session( 短期适用 ) ?oneshot( 本次适用 ) Q?普遍适用 模式是 MIDlet 安装时获得的许可一直有效,除非用户取消q些许可Q?短期适用 模式则是指第一ơ调?API 旉要用户允许,有效期到?MIDlet 套gq行l束Q?本次适用 模式则在每次调用 API 旉要求用户允许。保护域为用戯可定义了~省的交互模式?

一?MIDlet 套g使用 MIDlet-Permissions ?MIDlet-Permissions-Opt 属性来明确地定义其许可Q可以是?JAD 文g中定义,也可以在 manifest 文g中定义。其中: MIDlet-Permissions 定义?MIDlet 套g中必d有的许可Q?MIDlet-Permissions-Opt 则定义希望具有的许可。如Q一个应用Y件的基本要求是要?http q接才能正常工作Q同Ӟ也可以?https q接 ( 服务器部|了 SSL 证书 ) 来增强安全性,但不是必ȝQ这Pq个应用软g的应用描q可以是q样Q?

MIDlet-Permissions: javax.microedition.io.Connector.http

MIDlet-Permissions-Opt: javax.microedition.io.Connector.https

h意:一?MIDlet 所要求的许可必L安装时分配的保护域所h的许可的子集。如Q?Nokia S60 MIDP Emulator Prototype 2.0 (SDK) 有一个叫?#8220; minimum ”的域Q此域没有Q何许可。所以,如果一个含有许多许可的已经{֐? MIDlet 如果被安装到此域Q则会安装失败,因ؓ此域不支持这些许可。同P如果一个许可的名称有拼写错误,则一样会D安装p|Q因为域中没有此拼写错误的许可?

MIDP 2.0 ?GSM/UTMS 讑֤定义?4 U保护域Q?manufacturer( 讑֤刉商 ) , operator( Udq营?) , trusted third party( 可信ȝW三?) , and untrusted( 不受信Q?) Q?除了 untrusted 域外Q每个保护域都对应一l根证书Q用于签?MIDlet 的签名证书的根证书必d含在q些根证书中Q用不同的{֐证书{֐?MIDlet 被自动归类予根证书所属的保护域,根证书与保护域的关系是:一个保护域可以有许多个根证书,而一个根证书只能对应于一个保护域?

具体来讲Q?manufacturer 域属于设备制造商Q其根证书是讑֤刉商自己的根证书Q?operator 域运营商Q一般用其 SIM 卡中的根证书Q?trusted third party 域则预置了全球知名的数字证书颁发机构 (CA) 的根证书Q用于验证由 CA 颁发? MIDlet {֐证书Q?untrusted 域没有根证书Q将用于没有{֐?MIDlet ?MIDP 1.0 ?

Thawte ?VeriSign 的根证书已经预置?trusted third party ?中,?Java 代码{֐证书可以用于{֐ MIDlet 。当Ӟ用户也可以选择使用讑֤刉商和移动运营商颁发的证书,只要其根证书已经包含在手机的 4 个保护域中。据 WoTrust 了解Q大多数摩托|拉 (Motorola) 手机只支持设备制造商域,所以,只能?Motorola 甌{֐服务了?

h意:׃ MIDP 2.0 也在不断C改和增补Q所以,可能不用的移动网l运营商有不同的保护域和许可Q用户可能需要向Udq营商了解详l信息。而最单的Ҏ是检查目标用h使用的手机的根证书是否有计划购买?MIDlet {֐证书的根证书?

2.2 Untrusted MIDlet ( 不受信Q?MIDlet)

MIDP 2.0 定义了那?API ?untrusted 的,q些 Jar 文g的来源和完整性是不能被手机验证的。但qƈ不意味着q些 MIDlet 不能被安装和q行Q而是q行q些 MIDlet 需要用户h工确认允许。而所?MIDP 1.0 ?MIDlets 都被定义? untrusted ?

untrusted ?MIDlets 只能调用一个不需要许可保护的 API Q如Q?
java.util
java.lang
java.io
javax.microedition.rms
javax.microedition.midlet
javax.microedition.lcdui
javax.microedition.lcdui.game
javax.microedition.media
javax.microedition.media.control

如果 untrusted MIDlet 套g试图调用一个被保护?API 而且没有被h工允许,则会产生一?SecurityException 而被 MIDlet 按照安全{略处理。请注意Q?Nokia ?UI API 是不被保护的Q包括类Q?com.nokia.mid.sound ?com.nokia.mid.ui ?

2.3 Trusted MIDlets ( 可信ȝ MIDlets)

如果手机能验?MIDlet 的n份和完整?( 也就是已l数字签?) Q则会自动分配一个合适的保护 域这U?MIDlet 套gq为可信Q?MIDlet 。一个可信Q?MIDlet 套g所要求的许可将被准许,只要所属的保护域拥有这U许可,假如许可Q? javax.microedition.io.Connector.http 已经在所属保护域中是允许的,?MIDlet 在打开一?http q接时是不需要用L认的?

请不要؜淆了可信ȝ MIDlet 套g和可信Q的保护域的不同,每个可信ȝ MIDlet 套g依据安全{略被分配到一个特定的保护域?

您需要用一个手Z已经预置的根证书的证书颁发机构颁发的代码{֐证书来签?MIDlet Q否则将不能通过w䆾验证。成功签名后?JAD 文g中一定会包含有整个签名证书的证书链,属性名UCؓQ?MIDlet-Certificate-1-1 是您的{֐证书Q? MIDlet-Certificate-1-2 是 CA 的中U根证书Q?MIDlet-Certificate-1-3 是 CA 的顶U根证书。同时还会有一?MIDlet-Jar-RSA-SHA1 属性就?JAR 文g的摘要?

当一?MIDlet 被下载或被安装时Q?MIDlet 应用理器首先会?JAD 文g中是否包含了 MIDlet-Jar-RSA-SHA1 ?性,如果有,则启动如下验证过E:首先会读?MIDlet-Certificate-1-1 ?MIDlet-Certificate-1-2 ?MIDlet-Certificate-1-3 属性中的证书,q与已经预置的根证书相比较,如果证书链能被根证书验证Q则表明开发者n份已l被验证。接着׃使用用户证书来解? MIDlet-Jar-RSA-SHA1 ?性的摘要Q再计算出已l下载的 Jar 文g的摘要,比较两个摘要是否相等Q如果相{,则表? MIDlet 代码自签名后没有被修攏V这P既验证了w䆾又检查了完整性的 MIDlet 会被分配到所属根证书所对应的保护域中。但是,如果 MIDlet 中的许可属?( MIDlet-Permissions ) 中有一个或多个不属于所属的保护域,则仍然不允许安装。而如? MIDlet 中的可选许可属?( MIDlet-Permissions-Opt ) 中有一个或多个不属于所属的保护域,会允许安装。可见,正确讄许可属性和可选许可属性非帔R要?

2.4 Function Groups ( 功能分组 )

Z化用L理操作, MIDlet 把一些类似功能分l,q样Q用户只需对功能组讄许可卛_。如Q许?“Net Access”( |络讉K ) l来代替许可 javax.microedition.io.Connector.http Q这对于化手机的交互操作非常有用?

MIDP 2.0 ?JTWI 定义了如?7 个功能组Q?

(1) Net Access: 包括所有网l连接许可;

(2) Messaging: 包括所有与发送和接收短消?( 短信和彩?{?) 相关的许可;

(3) Auto Invocation : 包括与自动启?MIDlet 相关的许可,如: Push Registration

(4) Local Connectivity : 包括与本地连接相关的许可Q如Q?IrDA ?蓝牙Q?

(5) Multimedia Recording : 包括与允许录韟뀁照相、摄像等相关的许可;

(6) Read User Data : 包括d用户数据相关的许可,如:通讯录、日E表{;

(7) Write User Data : 包括写用h据相关的许可?

不同的手机支持不同的功能l,如: Multimedia Recording ׃会包含在没有摄录装置的手Z。当Ӟ也有可能来会增加更多的功能l?

功能l也同时定义了不同的域的不同交互方式Q如Q在不信dQ?“Net Access” ( |络讉K ) 被设|ؓ session( 短期适用 ) ?denied( 拒绝 ) Q而在可信d则可以设|ؓ oneshot ?blanket ?denied 的?

三、仿真器和手机的~省安全讄

让我们来看看具体的?Thawte ?VeriSign 代码{֐证书{֐后的 MIDlet ?trusted third party 域中的所有缺省许可,如下?1 所C,点击 NDS 3.0 ?#8220; Config Emulators ”可以看C真器?trusted third party 域的~省安全讄?#8220; Ask first time ”Q即W?1 ơ用是需要确认:


如下?2 所C,您可以下拉所有功能组的许可设|,?#8220; Network Access ”有 4 个选项可以修改Q?Ask first time ?Ask every time ?Always allowed ?Not allowed Q?


而如下图 3 所C,?#8220; Real Life ”模式Q也是实际手机的运行模式,可以看出Q定义的 7 个功能组都是“ Always allowed ” ( L允许 ) Q这显C出 MIDlet {֐对于开发商来讲是多么的重要Q将大大方便了用L使用Q再也不需要用h作烦人的pd认了?

LukeW 2008-08-15 14:53 发表评论
]]>
树Şl构http://www.aygfsteel.com/lukewange-hit1983/archive/2008/08/15/222244.htmlLukeWLukeWFri, 15 Aug 2008 06:51:00 GMThttp://www.aygfsteel.com/lukewange-hit1983/archive/2008/08/15/222244.htmlhttp://www.aygfsteel.com/lukewange-hit1983/comments/222244.htmlhttp://www.aygfsteel.com/lukewange-hit1983/archive/2008/08/15/222244.html#Feedback0http://www.aygfsteel.com/lukewange-hit1983/comments/commentRss/222244.htmlhttp://www.aygfsteel.com/lukewange-hit1983/services/trackbacks/222244.html 代码如下:
/**
 *
 * 
@author hunhun1981
 
*/
public class HTree {
 
 
private HNode root;
 
 
private HNode current;
 
 
private int currDepth;
 
 
private int maxDepth;
 
 
public HTree(Object rootValue) {
  root 
= new HNode(null, rootValue);
  current 
= root;
 }
 
 
public void goRoot() {
  current 
= root;
  currDepth 
= 0;
 }
 
 
public boolean goChild(int index) {
  
if (current.childList != null) {
   
if (current.childList.size() > 0
     
&& index < current.childList.size()) {
    current 
= (HNode) current.childList.elementAt(index);
    currDepth
++;
    
if (currDepth > maxDepth) {
     maxDepth 
= currDepth;
    }
    
return true;
   }
  }
  
return false;
 }
 
 
public void goBack() {
  
if (current.father != null) {
   current 
= current.father;
   currDepth–;
  }
 }
 
 
public Object getCurrent() {
  
return current.value;
 }
 
 
public int getCurrentDepth() {
  
return currDepth;
 }
 
 
public int getMaxDepth() {
  
return maxDepth;
 }
 
 
public Object[] getChilds() {
  
if (current.childList != null) {
   
if (current.childList.size() > 0) {
    Object[] ret 
= new Object[current.childList.size()];
    
for (int i = 0; i < ret.length; i++) {
     ret[i] 
= ((HNode) current.childList.elementAt(i)).value;
    }
    
return ret;
   }
  }
  
return null;
 }
 
 
public Object getChild(int index) {
  
if (current.childList != null) {
   
if (current.childList.size() > 0
     
&& index < current.childList.size()) {
    
return ((HNode) current.childList.elementAt(index)).value;
   }
  }
  
return null;
 }
 
 
public void addChild(Object obj) {
  
if (current.childList == null) {
   current.childList 
= new Vector();
  }
  current.childList.addElement(
new HNode(current, obj));
 }
 
 
public void addChilds(Object[] objs) {
  
if (current.childList == null) {
   current.childList 
= new Vector();
  }
  
for (int i = 0; i < objs.length; i++) {
   current.childList.addElement(
new HNode(current, objs[i]));
  }
 }
 
 
public int hasChild() {
  
if (current.childList == null || current.childList.size() <= 0) {
   
return 0;
  } 
else {
   
return current.childList.size();
  }
 }
 
 
private class HNode {
 
  
public Vector childList;
 
  
public HNode father;
 
  
public Object value;
 
  
public HNode(HNode father, Object value) {
   
this.value = value;
   
this.father = father;
   
this.childList = null;
  }
 }
}


q个cd现简单,没有包含复杂的功能,仅仅用来做树形数据的存储q是不错的。比如游戏中用来理场景Q管理资源;应用中用来作分类数据的表现等{。完全以胜仅R?br /> 使用Ҏ如下Q?br />
HTree tree = new HTree(”root”);//会自动创Z个默认的根节?/span>
tree.addChild(”天才”);//在根节点d新的节点
tree.addChild(”白痴”);
tree.goChild(
0);//q入到当前节点的W一个节点(天才Q?/span>
tree.addChild(”天才1?#8221;);//在当前节点(天才Q添加新的节?/span>
tree.addChild(”天才2?#8221;);
tree.goBack();
//q回当前节点Q天才)的父节点Q根Q?/span>
tree.goChild(1);//q入到当前节点的W二个节点(白痴Q?/span>
tree.addChild(”白痴1?#8221;);//在当前节点(白痴Q添加新的节?/span>
tree.addChild(”白痴2?#8221;);
tree.goRoot();
//完成创徏后将当前节点讄为根节点?/span>

上面的代码创Z一完整的树,当然Q您可以使用M对象代替q里存储的String对象?br /> q有一些方法,一看函数名大概都能明白Q就不再唠叨了?br /> 遍历的方法于上面创徏树的Ҏ怼QMQ要注意当前节点的位|,以免下次使用时处在错误的位置?br /> 有兴的朋友可以扩展一下遍历方法。不q我觉得没必要。因为J2ME环境下更需要的是树形结构,而不是强大的tree对象?br />
MQ我比较們֐于简单实玎ͼ希望它不太让得简陋就好。从实用出发Q它q是能够满大部分受限^台的需求的?

LukeW 2008-08-15 14:51 发表评论
]]>
URLEncodinghttp://www.aygfsteel.com/lukewange-hit1983/archive/2008/08/15/222242.htmlLukeWLukeWFri, 15 Aug 2008 06:49:00 GMThttp://www.aygfsteel.com/lukewange-hit1983/archive/2008/08/15/222242.htmlhttp://www.aygfsteel.com/lukewange-hit1983/comments/222242.htmlhttp://www.aygfsteel.com/lukewange-hit1983/archive/2008/08/15/222242.html#Feedback0http://www.aygfsteel.com/lukewange-hit1983/comments/commentRss/222242.htmlhttp://www.aygfsteel.com/lukewange-hit1983/services/trackbacks/222242.html
不过QJ2ME除外?br />
好在JAVA的源代码中带有这个类Q我们把它拷贝到J2ME环境下编译到我们的应用当中就可以了?br />
该文件位于JDK的目录下src.zip文g中,名叫URLEncoder.java?br />
但是Q这个文件还需要做很多修改才能使用在J2ME环境中?br />
先警告大Ӟ有几个真机(其中一个就是烦qQ好像是k500cQ,不管输入什么样的Encodeing都会出错Q甚x“UTF-8”。所以我一怒之? 去除了Encodeing参数。(q可是在实际应用中得出的l论Q不L的话可以在大部分情况下正怋用,但是Q现实L有点~陷Q?br />
修改后的代码如下Q大家请攑ֿ使用。如果有兴趣Q可以比较两个代码,看看我改动了什么地斏V?br />

import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
public class HURLEncoder {
 
 
private static boolean[] dontNeedEncoding;
 
 
static {
  dontNeedEncoding 
= new boolean[256];
 
  
for (int i = 0; i < 256; i++) {
   
boolean b = ((i >= ‘0′) && (i <= ‘9′))
     
|| ((i >= ‘A’) && (i <= ‘Z’)) || ((i >= ‘a’) && (i <= ‘z’));
 
   dontNeedEncoding[i] 
= b;
  }
 
  dontNeedEncoding[’ ‘] 
= true;
  dontNeedEncoding[’
-'] = true;
  dontNeedEncoding[’_'] = true;
  dontNeedEncoding[’.'] = true;
  dontNeedEncoding[’*'] = true;
 }
 
 
public static String encode(String s) {
 
  
boolean wroteUnencodedChar = false;
 
  StringBuffer writer 
= new StringBuffer();
 
  StringBuffer out 
= new StringBuffer(s.length());
 
  
for (int i = 0; i < s.length(); i++) {
   
char c = s.charAt(i);
 
   
if ((c < 256&& dontNeedEncoding[c]) {
    
if (c == ‘ ‘) {
     c 
= ‘+’;
    }
 
    out.append((
char) c);
    wroteUnencodedChar 
= true;
   } 
else {
    
try {
     
if (wroteUnencodedChar) {
      writer 
= new StringBuffer();
      wroteUnencodedChar 
= false;
     }
 
     writer.append(c);
 
     
if (c >= 0xD800 && c <= 0xDBFF) {
      
if ((i + 1< s.length()) {
       
int d = (int) (s.charAt(i + 1));
 
       
if (d >= 0xDC00 && d <= 0xDFFF) {
        writer.append(d);
        i
++;
       }
      }
     }
 
    } 
catch (Exception e) {
     writer 
= new StringBuffer();
     
continue;
    }
 
    String str 
= writer.toString();
 
    ByteArrayOutputStream baos 
= new ByteArrayOutputStream();
    DataOutputStream dos 
= new DataOutputStream(baos);
    
try {
     dos.writeUTF(str);
     dos.flush();
    } 
catch (Exception e) {
     e.printStackTrace();
    }
 
    
byte[] temp = baos.toByteArray();
    
byte[] ba = new byte[temp.length - 2];
    
for (int ix = 0; ix < ba.length; ix++) {
     ba[ix] 
= temp[ix + 2];
    }
 
    
for (int j = 0; j < ba.length; j++) {
     out.append(’
%');
 
     
char ch = forDigit((ba[j] >> 4& 0xF16);
     out.append(ch);
 
     ch 
= forDigit(ba[j] & 0xF16);
     out.append(ch);
    }
 
    writer 
= new StringBuffer();
    
try {
     dos.close();
     baos.close();
    } 
catch (Exception e) {
     e.printStackTrace();
    }
   }
  }
 
  
return out.toString();
 }
 
 
private static char forDigit(int digit, int radix) {
  
if ((digit >= radix) || (digit < 0)) {
   
return ‘0′;
  }
  
if (digit < 10) {
   
return (char) (’0′ + digit);
  }
  
return (char) (’A’ + digit - 10);
 }




LukeW 2008-08-15 14:49 发表评论
]]>
GB2312转换为UTF-8http://www.aygfsteel.com/lukewange-hit1983/archive/2008/08/15/222241.htmlLukeWLukeWFri, 15 Aug 2008 06:47:00 GMThttp://www.aygfsteel.com/lukewange-hit1983/archive/2008/08/15/222241.htmlhttp://www.aygfsteel.com/lukewange-hit1983/comments/222241.htmlhttp://www.aygfsteel.com/lukewange-hit1983/archive/2008/08/15/222241.html#Feedback0http://www.aygfsteel.com/lukewange-hit1983/comments/commentRss/222241.htmlhttp://www.aygfsteel.com/lukewange-hit1983/services/trackbacks/222241.html
关于gb2312,unicode,utf-8的一些资料,大家误行搜索。一下列丑և个比较好的资源网址?br /> http://baike.baidu.com/view/25492.htm
http://www.utf.com.cn/article/s45
http://www.utf.com.cn/article/s74
http://www.haiyan.com/steelk/navigator/ref/gb2312/gbindex.htm

要点Q?br /> 1,gb2312于unicode或者utf-8之间q不存在直接的映关pR所以我们只能通过查表法来q行转换?br /> 2,utf-8是unicode用于|络传输的一UŞ式,它与unicode之间是可以通过q算来进行{换的?br /> 3,j2me环境使用的都是utf-8~码Q但是请注意Qj2me中的utf-8~码比较ҎQ在整个~码前面对了两个字节Q用于存攑֭W串的长度?br />
q程Q?br /> 1Q制作映表gb2312-unicodeQ应为汉字的unicode比utf-8要小Q这样做出的表也会小一些,而且对于unicode的可扩展性也Z些?br /> 2Q先gb2312~码串通过查表Q{换ؓunicode?br /> 3Q然后通过q算Q将unicode转换为utf-8,以便在j2me环境下用?br />
我修改了Herong Yang大侠的一个映表生成函数Q原文请参?a >http://www.herongyang.com/gb2312/gb2312_unicode.html
它的作用是生成一个二q制的gb2312到unicode的查找表Q它按照gb2312的分区,分块Ҏ,其对应的unicode按顺序存入指定的位置?br /> q样我们只需要根据gb2312的编码,计算出烦引就可以获取~码对应的unicode了?br /> ׃是修改的代码Q没脸脓出来Q大家有需求可以直接参考Herong Yang的文章,然后Ҏ自己需求修改ƈ生成自己的映表?br />
q里我把自己q个转换表文件以及访问代码公开?br /> http://download.csdn.net/source/263609
转帖h明。这是个ȝ化的代码Q在java中给它gb2312的byte数组Q它q你构造出字符丌Ӏ?br /> 用在不支持gb2312的手Z非常方便。这个{换表的大是15228byteQ对j2me来说q是可以接受的?br />
如果有朋友需要沟通,可以发邮件到hunhun1981@hotmail.com

import java.io.InputStream;
 
public class HGB2312 {
 
    
private byte[] map = new byte[15228];
 
    
private byte[] buffer;
    
private int index;
 
    
public HGB2312() throws Exception {
        InputStream is 
= getClass().getResourceAsStream("/gb2u.dat");
        is.read(map);
        is.close();
    }
 
    
public String gb2utf8(byte[] gb) throws Exception {
        buffer 
= new byte[gb.length + gb.length / 2 + 3];
        index 
= 0;
        
int c, h, l, ind;
        
for (int i = 0; i < gb.length;) {
            
if (gb[i] >= 0) {
                fillBuffer(gb[i
++]);
            } 
else {
                h 
= 256 + gb[i++];
                l 
= 256 + gb[i++];
                h 
= h - 0xA0 - 1;
                l 
= l - 0xA0 - 1;
                
if (h < 9) {
                    ind 
= (h * 94 + l) << 1;
                    c 
= (byte2Int(map[ind]) << 8 | byte2Int(map[ind + 1]));
                    fillBuffer(c);
                } 
else if (h >= 9 && h <= 14) {
                    fillBuffer(
0);
                } 
else if (h > 14) {
                    h 
-= 6;
                    ind 
= (h * 94 + l) << 1;
                    c 
= (byte2Int(map[ind]) << 8 | byte2Int(map[ind + 1]));
                    fillBuffer(c);
                } 
else {
                    fillBuffer(
0);
                }
            }
        }
        
// ind = index - 2;
        
// h = (byte) ((ind >> 8) & 0x7F);
        
// l = (byte) (ind & 0xFF);
        
// buffer[0] = h;
        
// buffer[1] = l;
 
        
return new String(buffer, 0, index, "UTF-8");
    }
 
    
private void fillBuffer(int value) {
        
if (value <= 0x0000007F) {
            buffer[index
++= (byte) value;
        } 
else if (value >= 0x00000080 && value <= 0x000007FF) {
            
byte b1 = (byte) (0x60 | (value >> 6));
            
byte b2 = (byte) (0x80 | (value & 0x3F));
            buffer[index
++= b1;
            buffer[index
++= b2;
        } 
else if (value >= 0x00000800 && value <= 0x0000FFFF) {
            
byte b1 = (byte) (0xE0 | (value >> 12));
            
byte b2 = (byte) (0x80 | ((value >> 6& 0x3F));
            
byte b3 = (byte) (0x80 | (value & 0x3F));
            buffer[index
++= b1;
            buffer[index
++= b2;
            buffer[index
++= b3;
        } 
else if (value >= 0x00010000 && value <= 0x001FFFFF) {
            
byte b1 = (byte) (0x1E | (value >> 18));
            
byte b2 = (byte) (0x80 | ((value >> 12& 0x3F));
            
byte b3 = (byte) (0x80 | ((value >> 6& 0x3F));
            
byte b4 = (byte) (0x80 | (value & 0x3F));
            buffer[index
++= b1;
            buffer[index
++= b2;
            buffer[index
++= b3;
            buffer[index
++= b4;
        } 
else if (value >= 0x00200000 && value <= 0x03FFFFFF) {
            
byte b1 = (byte) (0x3E | (value >> 24));
            
byte b2 = (byte) (0x80 | ((value >> 18& 0x3F));
            
byte b3 = (byte) (0x80 | ((value >> 12& 0x3F));
            
byte b4 = (byte) (0x80 | ((value >> 6& 0x3F));
            
byte b5 = (byte) (0x80 | (value & 0x3F));
            buffer[index
++= b1;
            buffer[index
++= b2;
            buffer[index
++= b3;
            buffer[index
++= b4;
            buffer[index
++= b5;
        } 
else if (value >= 0x04000000 && value <= 0x7FFFFFFF) {
            
byte b1 = (byte) (0x7E | (value >> 30));
            
byte b2 = (byte) (0x80 | ((value >> 24& 0x3F));
            
byte b3 = (byte) (0x80 | ((value >> 18& 0x3F));
            
byte b4 = (byte) (0x80 | ((value >> 12& 0x3F));
            
byte b5 = (byte) (0x80 | ((value >> 6& 0x3F));
            
byte b6 = (byte) (0x80 | (value & 0x3F));
            buffer[index
++= b1;
            buffer[index
++= b2;
            buffer[index
++= b3;
            buffer[index
++= b4;
            buffer[index
++= b5;
            buffer[index
++= b6;
        }
    }
 
    
private int byte2Int(byte b) {
        
if (b < 0) {
            
return 256 + b;
        } 
else {
            
return b;
        }
    }
}



LukeW 2008-08-15 14:47 发表评论
]]>
修改png囄调色?/title><link>http://www.aygfsteel.com/lukewange-hit1983/archive/2008/08/15/222235.html</link><dc:creator>LukeW</dc:creator><author>LukeW</author><pubDate>Fri, 15 Aug 2008 06:34:00 GMT</pubDate><guid>http://www.aygfsteel.com/lukewange-hit1983/archive/2008/08/15/222235.html</guid><wfw:comment>http://www.aygfsteel.com/lukewange-hit1983/comments/222235.html</wfw:comment><comments>http://www.aygfsteel.com/lukewange-hit1983/archive/2008/08/15/222235.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.aygfsteel.com/lukewange-hit1983/comments/commentRss/222235.html</wfw:commentRss><trackback:ping>http://www.aygfsteel.com/lukewange-hit1983/services/trackbacks/222235.html</trackback:ping><description><![CDATA[今天在硬盘上挖出q个存放了几q的代码。又回忆?q前的那个j2me手机游戏E序?#8230;…<br /> <br /> q个法是参考一位高人的文章Q直接读取ƈ修改png格式囄的调色板Q然后生成新的调色板替代原来的?br /> q样可以实现游戏中常见的变色效果Q可以解x戏容量有限,不能存放太多_囄的问题?br /> <br /> 具体q程其实q不复杂Q大家可以先搜烦资料Q先看看png囄的格式定义。这个算法正是找到调色板区,Ҏ原有格式修改之后Q生成新的crc校验码,然后替换原来的调色板。这样就可以用一个png囄Q创建多个变色副本?br /> <div style="border: 1px solid #cccccc; padding: 4px 5px 4px 4px; background-color: #eeeeee; font-size: 13px; width: 98%;"><!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> --><span style="color: #0000ff;">public</span><span style="color: #000000;"> </span><span style="color: #0000ff;">class</span><span style="color: #000000;"> PalettedImage {<br />  <br />     </span><span style="color: #0000ff;">public</span><span style="color: #000000;"> Image getPalettedImage(</span><span style="color: #0000ff;">byte</span><span style="color: #000000;">[] data, </span><span style="color: #0000ff;">int</span><span style="color: #000000;">[] originalColors,<br />             </span><span style="color: #0000ff;">int</span><span style="color: #000000;">[] palettedColors) {<br />         </span><span style="color: #0000ff;">byte</span><span style="color: #000000;">[] tempData </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #0000ff;">new</span><span style="color: #000000;"> </span><span style="color: #0000ff;">byte</span><span style="color: #000000;">[data.length];<br />         System.arraycopy(data, </span><span style="color: #000000;">0</span><span style="color: #000000;">, tempData, </span><span style="color: #000000;">0</span><span style="color: #000000;">, data.length);<br />         Image img </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #0000ff;">null</span><span style="color: #000000;">;<br />         </span><span style="color: #0000ff;">int</span><span style="color: #000000;">[] parameter </span><span style="color: #000000;">=</span><span style="color: #000000;"> { </span><span style="color: #000000;">0</span><span style="color: #000000;">, </span><span style="color: #000000;">0</span><span style="color: #000000;">, </span><span style="color: #000000;">0</span><span style="color: #000000;"> };<br />         analyze(tempData, parameter);<br />         </span><span style="color: #0000ff;">for</span><span style="color: #000000;"> (</span><span style="color: #0000ff;">int</span><span style="color: #000000;"> i </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #000000;">0</span><span style="color: #000000;">; i </span><span style="color: #000000;"><</span><span style="color: #000000;"> originalColors.length; i</span><span style="color: #000000;">++</span><span style="color: #000000;">) {<br />             replaceColor(tempData, parameter, originalColors[i],<br />                     palettedColors[i]);<br />         }<br />         fillData(tempData, parameter);<br />         </span><span style="color: #0000ff;">try</span><span style="color: #000000;"> {<br />             img </span><span style="color: #000000;">=</span><span style="color: #000000;"> Image.createImage(tempData, </span><span style="color: #000000;">0</span><span style="color: #000000;">, data.length);<br />         } </span><span style="color: #0000ff;">catch</span><span style="color: #000000;"> (Exception e) {<br />             System.out.println(</span><span style="color: #000000;">"</span><span style="color: #000000;">getPalettedImage  &&  </span><span style="color: #000000;">"</span><span style="color: #000000;"> </span><span style="color: #000000;">+</span><span style="color: #000000;"> e.toString());<br />         }<br />         </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> img;<br />     }<br />  <br />     </span><span style="color: #0000ff;">private</span><span style="color: #000000;"> </span><span style="color: #0000ff;">void</span><span style="color: #000000;"> analyze(</span><span style="color: #0000ff;">byte</span><span style="color: #000000;">[] data, </span><span style="color: #0000ff;">int</span><span style="color: #000000;">[] para) {<br />         </span><span style="color: #0000ff;">int</span><span style="color: #000000;"> offset </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #000000;">8</span><span style="color: #000000;">;<br />         </span><span style="color: #0000ff;">int</span><span style="color: #000000;"> chunkLen </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #000000;">0</span><span style="color: #000000;">;<br />         </span><span style="color: #0000ff;">while</span><span style="color: #000000;"> (data[offset </span><span style="color: #000000;">+</span><span style="color: #000000;"> </span><span style="color: #000000;">4</span><span style="color: #000000;">] </span><span style="color: #000000;">!=</span><span style="color: #000000;"> </span><span style="color: #000000;">0x50</span><span style="color: #000000;"> </span><span style="color: #000000;">||</span><span style="color: #000000;"> data[offset </span><span style="color: #000000;">+</span><span style="color: #000000;"> </span><span style="color: #000000;">5</span><span style="color: #000000;">] </span><span style="color: #000000;">!=</span><span style="color: #000000;"> </span><span style="color: #000000;">0x4c</span><span style="color: #000000;"><br />                 </span><span style="color: #000000;">||</span><span style="color: #000000;"> data[offset </span><span style="color: #000000;">+</span><span style="color: #000000;"> </span><span style="color: #000000;">6</span><span style="color: #000000;">] </span><span style="color: #000000;">!=</span><span style="color: #000000;"> </span><span style="color: #000000;">0x54</span><span style="color: #000000;"> </span><span style="color: #000000;">||</span><span style="color: #000000;"> data[offset </span><span style="color: #000000;">+</span><span style="color: #000000;"> </span><span style="color: #000000;">7</span><span style="color: #000000;">] </span><span style="color: #000000;">!=</span><span style="color: #000000;"> </span><span style="color: #000000;">0x45</span><span style="color: #000000;">) {<br />             chunkLen </span><span style="color: #000000;">=</span><span style="color: #000000;"> readInt(data, offset);<br />             offset </span><span style="color: #000000;">+=</span><span style="color: #000000;"> (</span><span style="color: #000000;">4</span><span style="color: #000000;"> </span><span style="color: #000000;">+</span><span style="color: #000000;"> </span><span style="color: #000000;">4</span><span style="color: #000000;"> </span><span style="color: #000000;">+</span><span style="color: #000000;"> chunkLen </span><span style="color: #000000;">+</span><span style="color: #000000;"> </span><span style="color: #000000;">4</span><span style="color: #000000;">);<br />         }<br />         chunkLen </span><span style="color: #000000;">=</span><span style="color: #000000;"> readInt(data, offset);<br />         para[</span><span style="color: #000000;">2</span><span style="color: #000000;">] </span><span style="color: #000000;">=</span><span style="color: #000000;"> chunkLen </span><span style="color: #000000;">/</span><span style="color: #000000;"> </span><span style="color: #000000;">3</span><span style="color: #000000;">;<br />         para[</span><span style="color: #000000;">0</span><span style="color: #000000;">] </span><span style="color: #000000;">=</span><span style="color: #000000;"> offset </span><span style="color: #000000;">+</span><span style="color: #000000;"> </span><span style="color: #000000;">8</span><span style="color: #000000;">;<br />         para[</span><span style="color: #000000;">1</span><span style="color: #000000;">] </span><span style="color: #000000;">=</span><span style="color: #000000;"> offset </span><span style="color: #000000;">+</span><span style="color: #000000;"> </span><span style="color: #000000;">8</span><span style="color: #000000;"> </span><span style="color: #000000;">+</span><span style="color: #000000;"> chunkLen;<br />     }<br />  <br />     </span><span style="color: #0000ff;">private</span><span style="color: #000000;"> </span><span style="color: #0000ff;">int</span><span style="color: #000000;"> readInt(</span><span style="color: #0000ff;">byte</span><span style="color: #000000;">[] data, </span><span style="color: #0000ff;">int</span><span style="color: #000000;"> offset) {<br />         </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> ((data[offset] </span><span style="color: #000000;">&</span><span style="color: #000000;"> </span><span style="color: #000000;">0xFF</span><span style="color: #000000;">) </span><span style="color: #000000;"><<</span><span style="color: #000000;"> </span><span style="color: #000000;">24</span><span style="color: #000000;">)<br />                 </span><span style="color: #000000;">|</span><span style="color: #000000;"> ((data[offset </span><span style="color: #000000;">+</span><span style="color: #000000;"> </span><span style="color: #000000;">1</span><span style="color: #000000;">] </span><span style="color: #000000;">&</span><span style="color: #000000;"> </span><span style="color: #000000;">0xFF</span><span style="color: #000000;">) </span><span style="color: #000000;"><<</span><span style="color: #000000;"> </span><span style="color: #000000;">16</span><span style="color: #000000;">)<br />                 </span><span style="color: #000000;">|</span><span style="color: #000000;"> ((data[offset </span><span style="color: #000000;">+</span><span style="color: #000000;"> </span><span style="color: #000000;">2</span><span style="color: #000000;">] </span><span style="color: #000000;">&</span><span style="color: #000000;"> </span><span style="color: #000000;">0xFF</span><span style="color: #000000;">) </span><span style="color: #000000;"><<</span><span style="color: #000000;"> </span><span style="color: #000000;">8</span><span style="color: #000000;">) </span><span style="color: #000000;">|</span><span style="color: #000000;"> (data[offset </span><span style="color: #000000;">+</span><span style="color: #000000;"> </span><span style="color: #000000;">3</span><span style="color: #000000;">] </span><span style="color: #000000;">&</span><span style="color: #000000;"> </span><span style="color: #000000;">0xFF</span><span style="color: #000000;">);<br />     }<br />  <br />     </span><span style="color: #0000ff;">private</span><span style="color: #000000;"> </span><span style="color: #0000ff;">void</span><span style="color: #000000;"> replaceColor(</span><span style="color: #0000ff;">byte</span><span style="color: #000000;">[] data, </span><span style="color: #0000ff;">int</span><span style="color: #000000;">[] para, </span><span style="color: #0000ff;">int</span><span style="color: #000000;"> oldColor,<br />             </span><span style="color: #0000ff;">int</span><span style="color: #000000;"> newColor) {<br />         </span><span style="color: #0000ff;">byte</span><span style="color: #000000;"> rr </span><span style="color: #000000;">=</span><span style="color: #000000;"> (</span><span style="color: #0000ff;">byte</span><span style="color: #000000;">) ((oldColor </span><span style="color: #000000;">>></span><span style="color: #000000;"> </span><span style="color: #000000;">16</span><span style="color: #000000;">) </span><span style="color: #000000;">&</span><span style="color: #000000;"> </span><span style="color: #000000;">0xff</span><span style="color: #000000;">);<br />         </span><span style="color: #0000ff;">byte</span><span style="color: #000000;"> gg </span><span style="color: #000000;">=</span><span style="color: #000000;"> (</span><span style="color: #0000ff;">byte</span><span style="color: #000000;">) ((oldColor </span><span style="color: #000000;">>></span><span style="color: #000000;"> </span><span style="color: #000000;">8</span><span style="color: #000000;">) </span><span style="color: #000000;">&</span><span style="color: #000000;"> </span><span style="color: #000000;">0xff</span><span style="color: #000000;">);<br />         </span><span style="color: #0000ff;">byte</span><span style="color: #000000;"> bb </span><span style="color: #000000;">=</span><span style="color: #000000;"> (</span><span style="color: #0000ff;">byte</span><span style="color: #000000;">) (oldColor </span><span style="color: #000000;">&</span><span style="color: #000000;"> </span><span style="color: #000000;">0xff</span><span style="color: #000000;">);<br />         </span><span style="color: #0000ff;">for</span><span style="color: #000000;"> (</span><span style="color: #0000ff;">int</span><span style="color: #000000;"> i </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #000000;">0</span><span style="color: #000000;">, offset </span><span style="color: #000000;">=</span><span style="color: #000000;"> para[</span><span style="color: #000000;">0</span><span style="color: #000000;">], temp </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #000000;">0</span><span style="color: #000000;">; i </span><span style="color: #000000;"><</span><span style="color: #000000;"> para[</span><span style="color: #000000;">2</span><span style="color: #000000;">]; i</span><span style="color: #000000;">++</span><span style="color: #000000;">, offset </span><span style="color: #000000;">+=</span><span style="color: #000000;"> </span><span style="color: #000000;">3</span><span style="color: #000000;">) {<br />             </span><span style="color: #0000ff;">if</span><span style="color: #000000;"> (rr </span><span style="color: #000000;">==</span><span style="color: #000000;"> data[offset] </span><span style="color: #000000;">&&</span><span style="color: #000000;"> gg </span><span style="color: #000000;">==</span><span style="color: #000000;"> data[offset </span><span style="color: #000000;">+</span><span style="color: #000000;"> </span><span style="color: #000000;">1</span><span style="color: #000000;">]<br />                     </span><span style="color: #000000;">&&</span><span style="color: #000000;"> bb </span><span style="color: #000000;">==</span><span style="color: #000000;"> data[offset </span><span style="color: #000000;">+</span><span style="color: #000000;"> </span><span style="color: #000000;">2</span><span style="color: #000000;">]) {<br />                 data[offset] </span><span style="color: #000000;">=</span><span style="color: #000000;"> (</span><span style="color: #0000ff;">byte</span><span style="color: #000000;">) ((newColor </span><span style="color: #000000;">>></span><span style="color: #000000;"> </span><span style="color: #000000;">16</span><span style="color: #000000;">) </span><span style="color: #000000;">&</span><span style="color: #000000;"> </span><span style="color: #000000;">0xff</span><span style="color: #000000;">);<br />                 data[offset </span><span style="color: #000000;">+</span><span style="color: #000000;"> </span><span style="color: #000000;">1</span><span style="color: #000000;">] </span><span style="color: #000000;">=</span><span style="color: #000000;"> (</span><span style="color: #0000ff;">byte</span><span style="color: #000000;">) ((newColor </span><span style="color: #000000;">>></span><span style="color: #000000;"> </span><span style="color: #000000;">8</span><span style="color: #000000;">) </span><span style="color: #000000;">&</span><span style="color: #000000;"> </span><span style="color: #000000;">0xff</span><span style="color: #000000;">);<br />                 data[offset </span><span style="color: #000000;">+</span><span style="color: #000000;"> </span><span style="color: #000000;">2</span><span style="color: #000000;">] </span><span style="color: #000000;">=</span><span style="color: #000000;"> (</span><span style="color: #0000ff;">byte</span><span style="color: #000000;">) (newColor </span><span style="color: #000000;">&</span><span style="color: #000000;"> </span><span style="color: #000000;">0xff</span><span style="color: #000000;">);<br />                 </span><span style="color: #0000ff;">break</span><span style="color: #000000;">;<br />             }<br />         }<br />     }<br />  <br />     </span><span style="color: #0000ff;">private</span><span style="color: #000000;"> </span><span style="color: #0000ff;">void</span><span style="color: #000000;"> fillData(</span><span style="color: #0000ff;">byte</span><span style="color: #000000;">[] data, </span><span style="color: #0000ff;">int</span><span style="color: #000000;">[] para) {<br />         </span><span style="color: #0000ff;">int</span><span style="color: #000000;"> checksum </span><span style="color: #000000;">=</span><span style="color: #000000;"> update_crc(data, para[</span><span style="color: #000000;">0</span><span style="color: #000000;">] </span><span style="color: #000000;">-</span><span style="color: #000000;"> </span><span style="color: #000000;">4</span><span style="color: #000000;">, para[</span><span style="color: #000000;">2</span><span style="color: #000000;">] </span><span style="color: #000000;">*</span><span style="color: #000000;"> </span><span style="color: #000000;">3</span><span style="color: #000000;"> </span><span style="color: #000000;">+</span><span style="color: #000000;"> </span><span style="color: #000000;">4</span><span style="color: #000000;">);<br />         data[para[</span><span style="color: #000000;">1</span><span style="color: #000000;">]] </span><span style="color: #000000;">=</span><span style="color: #000000;"> (</span><span style="color: #0000ff;">byte</span><span style="color: #000000;">) ((checksum </span><span style="color: #000000;">>></span><span style="color: #000000;"> </span><span style="color: #000000;">24</span><span style="color: #000000;">) </span><span style="color: #000000;">&</span><span style="color: #000000;"> </span><span style="color: #000000;">0xff</span><span style="color: #000000;">);<br />         data[para[</span><span style="color: #000000;">1</span><span style="color: #000000;">] </span><span style="color: #000000;">+</span><span style="color: #000000;"> </span><span style="color: #000000;">1</span><span style="color: #000000;">] </span><span style="color: #000000;">=</span><span style="color: #000000;"> (</span><span style="color: #0000ff;">byte</span><span style="color: #000000;">) ((checksum </span><span style="color: #000000;">>></span><span style="color: #000000;"> </span><span style="color: #000000;">16</span><span style="color: #000000;">) </span><span style="color: #000000;">&</span><span style="color: #000000;"> </span><span style="color: #000000;">0xff</span><span style="color: #000000;">);<br />         data[para[</span><span style="color: #000000;">1</span><span style="color: #000000;">] </span><span style="color: #000000;">+</span><span style="color: #000000;"> </span><span style="color: #000000;">2</span><span style="color: #000000;">] </span><span style="color: #000000;">=</span><span style="color: #000000;"> (</span><span style="color: #0000ff;">byte</span><span style="color: #000000;">) ((checksum </span><span style="color: #000000;">>></span><span style="color: #000000;"> </span><span style="color: #000000;">8</span><span style="color: #000000;">) </span><span style="color: #000000;">&</span><span style="color: #000000;"> </span><span style="color: #000000;">0xff</span><span style="color: #000000;">);<br />         data[para[</span><span style="color: #000000;">1</span><span style="color: #000000;">] </span><span style="color: #000000;">+</span><span style="color: #000000;"> </span><span style="color: #000000;">3</span><span style="color: #000000;">] </span><span style="color: #000000;">=</span><span style="color: #000000;"> (</span><span style="color: #0000ff;">byte</span><span style="color: #000000;">) ((checksum) </span><span style="color: #000000;">&</span><span style="color: #000000;"> </span><span style="color: #000000;">0xff</span><span style="color: #000000;">);<br />     }<br />  <br />     </span><span style="color: #0000ff;">private</span><span style="color: #000000;"> </span><span style="color: #0000ff;">int</span><span style="color: #000000;"> update_crc(</span><span style="color: #0000ff;">byte</span><span style="color: #000000;">[] buf, </span><span style="color: #0000ff;">int</span><span style="color: #000000;"> off, </span><span style="color: #0000ff;">int</span><span style="color: #000000;"> len) {<br />         </span><span style="color: #0000ff;">int</span><span style="color: #000000;"> c </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #000000;">0xffffffff</span><span style="color: #000000;">;<br />         </span><span style="color: #0000ff;">int</span><span style="color: #000000;"> n, k;<br />         </span><span style="color: #0000ff;">int</span><span style="color: #000000;"> xx;<br />         </span><span style="color: #0000ff;">int</span><span style="color: #000000;">[] crc_table </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #0000ff;">new</span><span style="color: #000000;"> </span><span style="color: #0000ff;">int</span><span style="color: #000000;">[</span><span style="color: #000000;">256</span><span style="color: #000000;">];<br />         </span><span style="color: #0000ff;">for</span><span style="color: #000000;"> (n </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #000000;">0</span><span style="color: #000000;">; n </span><span style="color: #000000;"><</span><span style="color: #000000;"> </span><span style="color: #000000;">256</span><span style="color: #000000;">; n</span><span style="color: #000000;">++</span><span style="color: #000000;">) {<br />             xx </span><span style="color: #000000;">=</span><span style="color: #000000;"> n;<br />             </span><span style="color: #0000ff;">for</span><span style="color: #000000;"> (k </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #000000;">0</span><span style="color: #000000;">; k </span><span style="color: #000000;"><</span><span style="color: #000000;"> </span><span style="color: #000000;">8</span><span style="color: #000000;">; k</span><span style="color: #000000;">++</span><span style="color: #000000;">) {<br />                 </span><span style="color: #0000ff;">if</span><span style="color: #000000;"> ((xx </span><span style="color: #000000;">&</span><span style="color: #000000;"> </span><span style="color: #000000;">1</span><span style="color: #000000;">) </span><span style="color: #000000;">==</span><span style="color: #000000;"> </span><span style="color: #000000;">1</span><span style="color: #000000;">) {<br />                     xx </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #000000;">0xedb88320</span><span style="color: #000000;"> </span><span style="color: #000000;">^</span><span style="color: #000000;"> (xx </span><span style="color: #000000;">>>></span><span style="color: #000000;"> </span><span style="color: #000000;">1</span><span style="color: #000000;">);<br />                 } </span><span style="color: #0000ff;">else</span><span style="color: #000000;"> {<br />                     xx </span><span style="color: #000000;">=</span><span style="color: #000000;"> xx </span><span style="color: #000000;">>>></span><span style="color: #000000;"> </span><span style="color: #000000;">1</span><span style="color: #000000;">;<br />                 }<br />             }<br />             crc_table[n] </span><span style="color: #000000;">=</span><span style="color: #000000;"> xx;<br />         }<br />  <br />         </span><span style="color: #0000ff;">for</span><span style="color: #000000;"> (n </span><span style="color: #000000;">=</span><span style="color: #000000;"> off; n </span><span style="color: #000000;"><</span><span style="color: #000000;"> len </span><span style="color: #000000;">+</span><span style="color: #000000;"> off; n</span><span style="color: #000000;">++</span><span style="color: #000000;">) {<br />             c </span><span style="color: #000000;">=</span><span style="color: #000000;"> crc_table[(c </span><span style="color: #000000;">^</span><span style="color: #000000;"> buf[n]) </span><span style="color: #000000;">&</span><span style="color: #000000;"> </span><span style="color: #000000;">0xff</span><span style="color: #000000;">] </span><span style="color: #000000;">^</span><span style="color: #000000;"> (c </span><span style="color: #000000;">>>></span><span style="color: #000000;"> </span><span style="color: #000000;">8</span><span style="color: #000000;">);<br />         }<br />         </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> (c </span><span style="color: #000000;">^</span><span style="color: #000000;"> </span><span style="color: #000000;">0xffffffff</span><span style="color: #000000;">);<br />     }<br />  <br /> }<br /> <br /> </span></div> <br /> 接口是getPalettedImage()函数Q只需要输入原始图片的byte数组Q以及需要替换颜色的颜色D有目标颜色值就行了。因为可以同时替换多个颜Ԍ所以输入参数是代表颜色的整形的数组。MQ要保证原始颜色与目标颜色一一对应好了。方法简单实用?br /> <br /> Ƣ迎大家使用q留下宝늚意见。当Ӟ也可以修改一下这个函敎ͼ做一些特D的效果。这里就不多说了?br /> 不过q个代码用处已经不大Q因为现在的手机基本上都支持midp2.0所以可以用更方便的方法替换颜艌Ӏ?br /> <br /> MQ再ơ感谢这位已l被我忘掉名字的大侠Q关键代码是他写的,我只是修Ҏ理而已? <img src ="http://www.aygfsteel.com/lukewange-hit1983/aggbug/222235.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.aygfsteel.com/lukewange-hit1983/" target="_blank">LukeW</a> 2008-08-15 14:34 <a href="http://www.aygfsteel.com/lukewange-hit1983/archive/2008/08/15/222235.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Autostarting MIDlets in JP-7 phones using PushRegistry http://www.aygfsteel.com/lukewange-hit1983/archive/2008/08/15/222182.htmlLukeWLukeWFri, 15 Aug 2008 02:57:00 GMThttp://www.aygfsteel.com/lukewange-hit1983/archive/2008/08/15/222182.htmlhttp://www.aygfsteel.com/lukewange-hit1983/comments/222182.htmlhttp://www.aygfsteel.com/lukewange-hit1983/archive/2008/08/15/222182.html#Feedback0http://www.aygfsteel.com/lukewange-hit1983/comments/commentRss/222182.htmlhttp://www.aygfsteel.com/lukewange-hit1983/services/trackbacks/222182.html
Note: the auto start functionality is not supported by the first released K610 and K800 JP-7 phones. Please use the phone's upgrade service to ensure that you are using the latest firmware version.

The auto start functionality can be set static by including it in the .jad file or it can be set dynamically in the code. Two example MIDlets are included as an example of this.

Download the source code and examples here>>

To enable the auto start functionality in the .jad file include the line:
//MIDlet-Push-<n>: <ConnectionURL>, <MIDletClassName>, <AllowedSender>
MIDlet-Push-1: autostart://:, AutoStartStatic, *


To make the MIDlet auto start from the source code the following methods can be used to register and un-register the MIDlet.
//Registers the pushRegistry
public void Register(){
        
// List of registered push connections.
        String connections[];
        
// Check to see if the connection has been registered.
        
// This is a dynamic connection allocated on first
        
// time execution of this MIDlet.
        connections = PushRegistry.listConnections(false);
        
if (connections.length == 0) {
                
try {
                        
//Register so the MIDlet will wake up when phone is started.
                        PushRegistry.registerConnection("autostart://:""AutoStartDyn""*");
                        sDisplayString 
= "MIDlet is registered";
                } 
catch (Exception ex) {
                        System.out.println(
"Exception: " + ex);
                        sDisplayString 
= "Fail: " + ex;
                }
        } 
else {
                sDisplayString 
= "Already registered";
        }
        displayForm.deleteAll();
        displayForm.append(sDisplayString);
}
 
//Unregisters the pushRegistry
public void Unregister(){
        
if (PushRegistry.unregisterConnection("autostart://:")){
                System.out.println(
"The pushRegistry is unregistered");
                sDisplayString 
= "MIDlet is unregistered.";
        }
else{
                System.out.println(
"There is no pushRegistry to unregister");
                sDisplayString 
= "No MIDlet to unregister or failed to unregister";
        }
        displayForm.deleteAll();
        displayForm.append(sDisplayString);
}



To find out if the MIDlet is started via pushRegistry or manually you can inspect the connections registered by pushRegistry. Below is a small example of how to handle pushRegistry autostart or manual startup.
    private void handlePushActivation() {
        String[] connections 
= PushRegistry.listConnections(true);
        
if (connections != null && connections.length > 0) {
            
for (int i = 0; i < connections.length; i++) {
                
if (connections[i].startsWith("autostart")) {
                    alert(
"Application autostarted");
                }
            }
        } 
else {
            alert(
"User started the application");
        }
    }



LukeW 2008-08-15 10:57 发表评论
]]>
Using simultaneous soundshttp://www.aygfsteel.com/lukewange-hit1983/archive/2008/08/15/222173.htmlLukeWLukeWFri, 15 Aug 2008 02:34:00 GMThttp://www.aygfsteel.com/lukewange-hit1983/archive/2008/08/15/222173.htmlhttp://www.aygfsteel.com/lukewange-hit1983/comments/222173.htmlhttp://www.aygfsteel.com/lukewange-hit1983/archive/2008/08/15/222173.html#Feedback0http://www.aygfsteel.com/lukewange-hit1983/comments/commentRss/222173.htmlhttp://www.aygfsteel.com/lukewange-hit1983/services/trackbacks/222173.html
Only one wav file can be played simultaniously but several midi files might be played at the same time. The number of existing players is only limited by available memory, and you have the possibility to play more than one player at the same time. The example below shows how to do this using one midi file and one wav file.

The code is straight-forward - just load the resource, create the player and start playing the file.

Here's the sample:
InputStream is = getClass().getResourceAsStream(file);
InputStream is1 
= getClass().getResourceAsStream(file1);

player 
= Manager.createPlayer(is, " audio/midi");
player.setLoopCount(
-1);
player.prefetch();
player.realize();

player1 
= Manager.createPlayer(is1, "audio/x-wav");
player1.setLoopCount(
1);
player1.prefetch();
player1.realize();

player.start();
player1.start();

example code


LukeW 2008-08-15 10:34 发表评论
]]>
Serializing an Image http://www.aygfsteel.com/lukewange-hit1983/archive/2008/08/15/222171.htmlLukeWLukeWFri, 15 Aug 2008 02:29:00 GMThttp://www.aygfsteel.com/lukewange-hit1983/archive/2008/08/15/222171.htmlhttp://www.aygfsteel.com/lukewange-hit1983/comments/222171.htmlhttp://www.aygfsteel.com/lukewange-hit1983/archive/2008/08/15/222171.html#Feedback0http://www.aygfsteel.com/lukewange-hit1983/comments/commentRss/222171.htmlhttp://www.aygfsteel.com/lukewange-hit1983/services/trackbacks/222171.html
To create a byte-array of data from an image, we can use the getRGB(..) method in the image class in MIDP 2.0. From the getRGB method we get an int-array of data containing all the ARGB values of each pixel in the image. Integers in java are four bytes, so we need to split each int value into four byte values.

To mask out each of the bytes in the int we can use the 'AND &' operator and the 'shift-right >>' operator. Here's an example:
int ARGB = 0xFFFFFFFF;  // AARRGGBB 
int a = (ARGB & 0xFF000000); 
int r = (ARGB & 0x00FF0000); 
int g = (ARGB & 0x0000FF00); 
int b = (ARGB & 0x000000FF);
 
// Here we move each bit to the right.
= (a >> 24); // a = 0x000000FF 
= (r >> 16); // r = 0x000000FF 
= (g >> 8); // g = 0x000000FF
= b;        // b = 0x000000FF


When we convert the integer to a byte, there are some problems since there are only signed bytes in Java. A byte may contain values between -128 and 127 and from our integer we'll have a value between 0 and 255.

Here are some conversion examples:

Integer val 
127 = byte val 127.
Integer val 
128 = byte val -128.
Integer val 
255 = byte val -1.

byte ba = (byte)(a);  // if a=0x000000FF (255), then ba = -1
byte br = (byte)(r); 
byte bg = (byte)(g); 
byte bb = (byte)(b);


We have to loop though each pixel in the image and get each pixel value to our byte-array. When that's done, we can send the image to a server where we can convert the byte-array back to a integer-array and then show our picture.

So, when we convert the byte back to an integer we have to check if the byte value is less than zero.
int a, r, g, b;
if(ba<0)
     a 
= 256 - a;


Now our new integer value will be between 0 and 255 and we just have to use the 'shift-left <<' operator and add the values together to get our new int-array.
= (a << 24);
= (r << 16);
= (g << 8);
= (b);
 
  
0xFF000000 (a)
+ 0x00FF0000 (r)
+ 0x0000FF00 (g)
+ 0x000000FF (b)
= 0xFFFFFFFF (argb)
 
int ARGB = a+r+g+b;

After the integer-array is recreated we can use the createRGBImage(..) method in the Image class to create our image.

Be aware that the byte-array is quite large as it contains all of the ARGB values for each pixel. If an image is 100*60 px, where each pixel is four bytes, the byte array will be 24kb.
example code


LukeW 2008-08-15 10:29 发表评论
]]>
Fade in and out images in MIDP 2.0http://www.aygfsteel.com/lukewange-hit1983/archive/2008/08/15/222167.htmlLukeWLukeWFri, 15 Aug 2008 02:18:00 GMThttp://www.aygfsteel.com/lukewange-hit1983/archive/2008/08/15/222167.htmlhttp://www.aygfsteel.com/lukewange-hit1983/comments/222167.htmlhttp://www.aygfsteel.com/lukewange-hit1983/archive/2008/08/15/222167.html#Feedback0http://www.aygfsteel.com/lukewange-hit1983/comments/commentRss/222167.htmlhttp://www.aygfsteel.com/lukewange-hit1983/services/trackbacks/222167.html
In MIDP 2.0 there's a new method in the Image class, getRGB(...) that will get all Alpha, Red, Green, Blue (ARGB) values from the image to an int array. We can use this method, and the resulting array, to change the alpha value of the image.

Integers in J2ME are four bytes, each pixel in the image is described by the ARGB values where each color is one byte 0 to 255. If the alpha value is 0, the corresponding pixel will be transparent, if the alpha is 255, the pixel will be opaque.

To get a specific color from the int array, it's possible to use the AND '&' operator and to accomplish the blending effect we need to get the colors, without the alpha value, from the int array.
FF = 11111111 = 255
0xFFFFFFFF - Alpha = 255, Red =255 Green = 255, Blue = 255
(
0xFFFFFFFF & 0x00FFFFFF= 0x00FFFFFF


By doing this we'll only get the RGB colors from the int array, alpha equals zero.

Now when we just have the RGB colors and alpha equals zero, we can just add our new alpha value.

If we have the alpha value 255 and want to add this to our color, we need to use the shift left operator.

To change:
(00000000 00000000 00000000 11111111) to 
(
11111111 00000000 00000000 00000000)
use the shift left 
'<<' operator.
(
0xFF << 24= 0xFF000000.


With this knowledge we now can change the alpha value or do masking for specific colors.

  • Use the getRGB(...) method in the image class to get all the ARGB values from the image to a int array.
  • Use the blend function below to change the alpha value for each value in the array.
  • Use the createRGBImage(...) method in the image class to create a new image from the edited int array.

There are examples on how to use the getRGB and createRGBImage methods in the MIDlet below.
public static void blend(int[] raw, int alphaValue){
    
int len = raw.length;
    
// Start loop through all the pixels in the image.
    for(int i=0; i<len; i++){
        
int a = 0;
        
int color = (raw[i] & 0x00FFFFFF); // get the color of the pixel.
        a = alphaValue;     // set the alpha value we want to use 0-255.
 
        a 
= (a<<24);    // left shift the alpha value 24 bits.
        
// if color = 00000000 11111111 11111111 00000000 (0xFFFF00 = Yellow)
        
// and alpha= 01111111 00000000 00000000 00000000
        
// then c+a = 01111111 11111111 11111111 00000000
        
// and the pixel will be blended.
        color += a;
        raw[i] 
= color;
    }
}

example code


LukeW 2008-08-15 10:18 发表评论
]]>
Fast stream reading in Javahttp://www.aygfsteel.com/lukewange-hit1983/archive/2008/08/15/222163.htmlLukeWLukeWFri, 15 Aug 2008 02:11:00 GMThttp://www.aygfsteel.com/lukewange-hit1983/archive/2008/08/15/222163.htmlhttp://www.aygfsteel.com/lukewange-hit1983/comments/222163.htmlhttp://www.aygfsteel.com/lukewange-hit1983/archive/2008/08/15/222163.html#Feedback0http://www.aygfsteel.com/lukewange-hit1983/comments/commentRss/222163.htmlhttp://www.aygfsteel.com/lukewange-hit1983/services/trackbacks/222163.html
If you know the maximum content length, the most optimal read would be a single line, like this:
len = instream.read( buf, 0, MAX_BUFFER_SIZE );

In this case we assume that the buffer has already been allocated with a size set to MAX_BUFFER_SIZE.

If the content is of varying size, we have to make a tradeoff between performance and memory usage. If you keep your buffer size just above the average content length, then the number of reallocations of the data buffer and the number of reads will be kept at a minimum. There is no defined size of what a large buffer is but a good rule of thumb might be to keep the buffer size at most about 0,5 to 1 MB.


LukeW 2008-08-15 10:11 发表评论
]]>
վ֩ģ壺 | | | ɽ| ڻ| ԭƽ| | ֹ| | | | | | | ƺ| | | | ͤ| | | | | ɽ| | ʯ| | ¯| | | | | Ӫɽ| | | | ʡ| | | | ն|