JBuilder設計獲取圖片屬性程序(2 / 3)

* Copyright: Copyright (c) 2003 by HEKAI

* @Version: 1.0

* Modify History

* Author Date Description

* hekai 2003-11-18 Create the readpic.java

*****************************************************************/

//請用你的相應包名進行替換

package bmptest;

import java.io.*;

public class readpic

{

public static void main(String[] args)

{

PicsModel model = getFileAttributes("E:/hekai/hk0001.jpg");

// "E:\\hekai\\hk0001.jpg" 也是可以的

System.out.println("picextendname is:"+model.getpicsExtendName());

System.out.println("picwidth is:"+model.getpicsWidth());

System.out.println("picheight is:"+model.getpicsHeight());

System.out.println("piccolor:"+model.getpicsColor());

System.out.println("picsize:"+model.getpicsSize());

}

private static PicsModel getFileAttributes(String picpath)

{

PicsModel model = null;

String picextendname;

picextendname = null;

byte[] content = null;

try

{

content = readFromFile(picpath);

int k;

k = content.length;

//不想處理的話,請直接獲取其字節數

Integer kk;

kk = null;

String picsize;

picsize = null;

if (k >= 1024)

{

//bigger than fact pic file sizes

k = k / 1024 + 1;

kk = new Integer(k);

picsize = kk.toString() + "K";

}

else if (k > 0)

{

kk = new Integer(k);

picsize = kk.toString();

}

model = new PicsModel();

model.setpicsSize(picsize);

}

catch( IOException e )

{

content = new byte[0];

e.printStackTrace();

}

picextendname = getFileExtendName(content);

int picwidth,picheight,color;

String piccolor;

picwidth = 0;

picheight = 0;

color = 0;

piccolor = null;

if (picextendname.equals("GIF"))

{

//picwidth position

picwidth = getFileAttribute(content,7,2,picextendname);

//picheight position

picheight = getFileAttribute(content,9,2,picextendname);

//piccolor position

color = getFileAttribute(content,10,1,picextendname);

color = color % 8 + 1;

piccolor = getPicColor(color);

}

if (picextendname.equals("JPG"))

{

//考慮了兩種情況

picwidth = getFileAttribute(content,166,2,picextendname);

picheight = getFileAttribute(content,164,2,picextendname);

color = getFileAttribute(content,167,1,picextendname);

color = color * 8;

if ((picwidth == 0 )||(picheight == 0)||(Color > 3))

{

picwidth = getFileAttribute(content,197,2,picextendname);

picheight = getFileAttribute(content,195,2,picextendname);

color = getFileAttribute(content,198,1,picextendname);

color = color * 8;

}

piccolor = getPicColor(color);

}

if (picextendname.equals("BMP"))

{

picwidth = getFileAttribute(content,19,2,picextendname);

picheight = getFileAttribute(content,23,2,picextendname);

color = getFileAttribute(content,28,1,picextendname);

piccolor = getPicColor(color);

}

if (picextendname.equals("PNG"))

{

picwidth = getFileAttribute(content,19,2,picextendname);

picheight = getFileAttribute(content,23,2,picextendname);

// usually is "16M"??

piccolor = "16M";

}

model.setpicsExtendName(picextendname);

model.setpicsWidth(picwidth);

model.setpicsHeight(picheight);

model.setpicsColor(piccolor);