您的位置:首页 > 其它

J2ME 判断图片的格式

2009-03-10 18:08 232 查看
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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: