对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了.