- package lab.sodino.img;
- import java.io.IOException;
- import java.io.InputStream;
- import javax.microedition.io.Connector;
- import javax.microedition.io.file.FileConnection;
- import javax.microedition.midlet.MIDlet;
- import javax.microedition.midlet.MIDletStateChangeException;
- /** @author sodino */
- public class ImgType extends MIDlet {
- public ImgType() {
- }
- protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
- }
- protected void pauseApp() {
- }
- protected void startApp() throws MIDletStateChangeException {
- String prefix = "file:///root1/";
- // testFile(prefix + "logo_cn.gif");
- // testFile(prefix + "04.jpg");
- testFile(prefix + "img.png");
- }
- public void testFile(String url) {
- try {
- int length = 10;
- FileConnection fc = (FileConnection) Connector.open(url);
- InputStream is = fc.openInputStream();
- byte[] data = new byte[length];
- is.read(data);
- String type = getType(data);
- System.out.println(url + " is " + type);
- is.close();
- fc.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- public String getType(byte[] data) {
- String type = null;
- // Png test:
- if (data[1] == 'P' && data[2] == 'N' && data[3] == 'G') {
- type = "PNG";
- return type;
- }
- // Gif test:
- if (data[0] == 'G' && data[1] == 'I' && data[2] == 'F') {
- type = "GIF";
- return type;
- }
- // JPG test:
- if (data[6] == 'J' && data[7] == 'F' && data[8] == 'I'
- && data[9] == 'F') {
- type = "JPG";
- return type;
- }
- return type;
- }
- }
通过文件二进制信息判断图片类型(png,jpg,gif)
最新推荐文章于 2023-10-23 10:16:50 发布