WAP之家:为您提供最全最新的WAP技术,CP.SP.3G等行业资讯。 WAP之家交流论坛全新开放 点击进入>>
WAP资讯 | 3G动态 | SP动态 | 运营商动态 | 内容商动态 | 制造商动态 | 论坛讨论>> 每次自动访问
WAP技术 | WAP源码 | 手机编程 | 手机源码 | 无线技术 | J2ME技术 | 手机软件 添加到收藏夹
IVR技术 | SP资料 | SMS MMS技术 | 商业方案 | IVR下载 | 书籍教程 | 工具软件 语言:繁體中文

WAP之家技术文章J2ME技术进阶教程图片资源节省的做法

图片资源节省的做法
作者:daojianxiao  来源:转载  发布时间:2005-10-15 1:35:38

对Png格式再进行研究之后,我找到这么一条,将PNG文件中非关键数据块去掉之后能够将文件变小,但效果不变,因此我写了以下的算法,为了方便使用,直接做成了一个类,如下

import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.rms.*;
import java.util.*;
import java.io.*;
public class zipImage extends MIDlet
{
byte[] newpix;
public zipImage()
{
}
public void startApp()
{
zip();
}
public void pauseApp(){

}
public void destroyApp(boolean boo){

}
RecordStore rs;
public void zip()
{
try
{
rs=RecordStore.openRecordStore("zipImage",true);
rs.closeRecordStore();
RecordStore.deleteRecordStore("zipImage");
rs=RecordStore.openRecordStore("zipImage",true);//保证数据库里永远只有一条记录
newpix=cutChunk("/1.png");
rs.addRecord(newpix,0,newpix.length);
}
catch(Exception e)
{
}
}
/*将图片中的非关键数据块去掉*/
public byte[] cutChunk(String s)
{
try
{
byte[] content;
InputStream is=getClass().getResourceAsStream(s);
int m=0;
while(is.read()!=-1)
m++;
content=new byte[m];
is=null;
is=getClass().getResourceAsStream(s);
is.read(content);
byte[] newChunk;
int lengPLTE=0;
int lengIDAT=0;//相应数据块的长度
int posPLTE=0;
int posIDAT=0;//相应数据块的位置
int pos=33;
for(int i=33;i {
if(content[i]==0x50&&content[i+1]==0x4c
&&content[i+2]==0x54
&&content[i+3]==0x45)
{
lengPLTE=(content[i-4]<<24&0xff000000)+(content[i-3]<<16&0x00ff0000)+(content[i-2]<<8&0x0000ff00)+(content[i-1]&0xff)
+12;
posPLTE=i-4;
}
if(content[i]==0x49&&content[i+1]==0x44
&&content[i+2]==0x41
&&content[i+3]==0x54)
{
lengIDAT=(content[i-4]<<24&0xff000000)+(content[i-3]<<16&0x00ff0000)+(content[i-2]<<8&0x0000ff00)+(content[i-1]&0xff)
+12;
posIDAT=i-4;
}
}
System.out.println(lengPLTE);
System.out.println(lengIDAT);
newChunk=new byte[pos+lengPLTE+lengIDAT+12];
System.out.println(newChunk.length);
for(int i=0;i {
if(i<33)
newChunk[i]=content[i];
else
if(i<33+lengPLTE)
newChunk[i]=content[i+(posPLTE-33)];
else
if(i<33+lengPLTE+lengIDAT)
newChunk[i]=content[i+(posIDAT-33-lengPLTE)];
else
newChunk[i]=content[i+(content.length-45-lengPLTE-lengIDAT)];
}
System.out.println("out");
return newChunk;
}
catch(Exception e)
{
return null;
}
}
}

在使用的时候,如果是在WTK下,请将需要压缩的源图放在RES目录下,并取名为1.png,然后运行程序,运行之后,请在WTK的appdb目录下相应的模拟器目录里寻找一个名叫run_by_class_storage_zip#Image.db的文件,用UE打开此文件,将文件的前四行删掉(不知道怎么删的请问一下我),最后保存为你要的文件名就OK了.

[] [返回上一页] [打 印]
文章评论

用户名: 查看更多评论

分 值:100分 85分 70分 55分 40分 25分 10分 0分

内 容:

         (注“”为必填内容。) 验证码: 验证码,看不清楚?请点击刷新验证码