>following is my code to find dimension of gif or jpeg.
>For gif it is quite simple but jpeg it is a bit difficult.
>Any way I hope it works for you.
>
>Gargi
Thanks alot Gargi, your code works a treat and saved
me alot of time.
Enclosed the updated test java with your code included (for
anyone else who is trying to solve this problem).
Tsch�ss,
Andreas.
-trace----------------------------------------------------------------
> java testURL ~/xfer/pics/a.gif
url.toString =file://localhost/home/xfer/pics/a.gif
url.toExternalForm =file://localhost/home/xfer/pics/a.gif
content.toString =sun.awt.image.URLImageSource@1dacdb77
content.getClass =class sun.awt.image.URLImageSource
connecting
con.getContentLength =5969
con.getContentType =image/gif
con.getContentEncoding =null
content is image
content is GIF
decoding /home/xfer/pics/a.gif
File: /home/xfer/pics/a.gif, width: 357, height=58
...
-testURL.java---------------------------------------------------------
import java.net.*;
import java.io.*;
import java.util.*;
import sun.awt.image.*;
public class testURL
{
/*
** test
*/
private static void decode_jpeg( String fname )
{
System.out.println("decoding "+fname);
try
{
FileInputStream fis;
fis = new FileInputStream(fname); // for a jpeg
fis.skip(4); // after ff d8 ff eo is a length info of 2 bytes
//look for the marker ffc0
// ffd9 is the end of the image
boolean myEOF = false;
while (!myEOF)
{
// read the length info of a marker; length is 2 bytes
short xx[] = new short[2];
for(int i = 0;i<2;i++)
{ xx[i] = (short)fis.read();
}
// make the proper value of the out of the byte info
int x0 = (int)xx[0];
int x1 = (int)xx[1];
long markLength = ((x0 << 8) | (x1));
markLength= markLength-2;
fis.skip(markLength); // to get to the next marker
short markerName[] = new short[2]; /*marker name*/
for(int i = 0;i<2;i++)
{ markerName[i] = (short)fis.read();
}
int theHighByte = (int)markerName[0];
int theLowByte = (int)markerName[1];
/*Start Of Frame (0xFFCx where x = 0,1-3,5-7,9-11,13-15)*/
if ((theHighByte == 255) && (
(theLowByte == 192) /*c0*/
|| (theLowByte == 193) /*c1*/
|| (theLowByte == 194) /*c2*/
|| (theLowByte == 195) /*c3*/
|| (theLowByte == 197) /*c5*/
|| (theLowByte == 198) /*c6*/
|| (theLowByte == 199) /*c7*/
|| (theLowByte == 201) /*c9*/
|| (theLowByte == 202) /*c10*/
|| (theLowByte == 203) /*c11*/
|| (theLowByte == 205) /*c13*/
|| (theLowByte == 206) /*c14*/
|| (theLowByte == 207) /*c15*/
)
) /* ffc0 or ffc1 or ffc2..... marker contains the width and
height info*/
{
myEOF =true; /*no more scanning of the file ,we came to the
desired marker*/
fis.skip(3);//now that we are at the desired marker we can
//skip the length info+ 1 byte shit info
/*read in the 4 bytes of height and width information*/
short dim[] = new short[4];
for(int i = 0;i<4;i++)
{ dim[i] = (short)fis.read();
}
/*calculate the height from the byte information */
int h0 = dim[0];
int h1 = dim[1];
int height = (h0 << 8) | (h1);
/*calculate the width from the byte information */
int w0 = dim[2];
int w1 = dim[3];
int width = (w0 << 8) | (w1);
System.out.println("File: "+fname+", width: "+width+",
height="+height );
}
else
{
myEOF =false; /*go on reading the file to search for the dimension*/
}
}
fis.close();
}
catch (Exception e)
{
System.out.println(e.getMessage());
e.printStackTrace();
}
}
private static void decode_gif( String fname )
{
System.out.println("decoding "+fname);
try
{
FileInputStream fis;
fis = new FileInputStream(fname); //for gif file
fis.skip(6); // the width info starts at the 7th byte and it is 2
byte long
// the height info starts at the 9th byte and it is 2
byte long
short xx[] = new short[4]; /*read in width and height of the GIF file*/
for(int i = 0;i<4;i++)
{ xx[i] = (short)fis.read();
}
fis.close();
int y0 = (int)xx[1];
y0 = (y0 << 8) & (0xff00);
int y1 = (int)xx[0];
int yx = y0 | y1;
int y2 = (int)xx[3];
y2 = (y2 << 8) & (0xff00);
int y3 = (int)xx[2];
int yy = y2 | y3;
int width = yx;
int height = yy;
System.out.println("File: "+fname+", width: "+width+", height="+height );
}
catch (Exception e)
{
System.out.println(e.getMessage());
e.printStackTrace();
}
}
public static void main( String[] args )
{
if (args.length < 1)
{
System.err.println("usage: java testURL <url> [debug]");
System.exit(0);
}
String paramString = args[0];
boolean Debug = (args.length > 1) ? true : false;
URL url = null;
Object content = null;
try
{
// url = new URL( paramString );
url = new URL( "file", "localhost", paramString );
System.out.println("url.toString ="+ url.toString() );
System.out.println("url.toExternalForm ="+ url.toExternalForm() );
content = url.getContent();
System.out.println("content.toString ="+ content.toString() );
System.out.println("content.getClass ="+ content.getClass() );
URLConnection con = url.openConnection();
System.out.println("connecting");
con.connect();
System.out.println("con.getContentLength ="+con.getContentLength() );
System.out.println("con.getContentType ="+con.getContentType() );
System.out.println("con.getContentEncoding ="+con.getContentEncoding() );
if (con.getContentType().startsWith("image/") )
{
System.out.println("content is image");
if (con.getContentType().endsWith("/jpeg") )
{
System.out.println("content is JPEG");
//decode
decode_jpeg(paramString);
} else if (con.getContentType().endsWith("/gif") )
{
System.out.println("content is GIF");
//decode
decode_gif(paramString);
}
else System.out.println("IMAGE TYPE NOT SUPPORTED");
}
for (int i=0; i<10; i++)
{
System.out.println(i+". con.getHeaderFieldKey
="+con.getHeaderFieldKey(i) );
System.out.println(i+". con.getHeaderField ="+con.getHeaderField(i) );
}
}
catch (Exception e)
{
System.out.println(e.getMessage());
e.printStackTrace();
}
}
}
-eof------------------------------------------------------------------
___________________________________________________________________________
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff SERVLET-INTEREST".
Archives: http://archives.java.sun.com/archives/servlet-interest.html
Resources: http://java.sun.com/products/servlet/external-resources.html
LISTSERV Help: http://www.lsoft.com/manuals/user/user.html