J2ME 判断图片的格式

public String GetFormat(String path)
{
String PicFormat = "UNKNOWN";

try{
InputStream is = this.getClass().getResourceAsStream(path);
byte[] data = new byte[128];
ByteArrayOutputStream os = new ByteArrayOutputStream();
while(is.read(data)!=-1)
{
os.write(data);
}
byte [] buffer = os.toByteArray();

StringBuffer sb = new StringBuffer();
int tmp = 0;
for(int i = 0; i < 16;i++)
{
tmp = buffer[i];
if(tmp >= 32 && tmp <= 127)
{
sb.append((char)tmp);
}
}
String head = sb.toString();
System.out.println(head);
if(head.toUpperCase().startsWith("GIF"))
{
PicFormat = "GIF";
}
else if(head.toUpperCase().startsWith("JFIF"))
{
PicFormat = "JPG";
}
else if(head.toUpperCase().startsWith("PNG"))
{
PicFormat = "PNG";
}
else if(head.toUpperCase().startsWith("BM"))
{
PicFormat = "BMP";
}
}
catch(IOException ioe){ioe.printStackTrace();}

return PicFormat;
}

你可能感兴趣的:(j2me)