Hi Andreas,
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
===================
try
{
fis = new FileInputStream(source); //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;
d.width = yx;
d.height = yy;
}
==============================================
fis = new FileInputStream(source); // 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 h = (h0 << 8) | (h1);
/*calculate the width from the byte information */
int w0 = dim[2];
int w1 = dim[3];
int w = (w0 << 8) | (w1);
d.height = h;
d.width = w;
}
else
{
myEOF =false; /*go on reading the file to search for the dimension*/
}
}
fis.close();
==============================================
Andreas Gutzwiller wrote:
> Hi all,
>
> I need to obtain image dimensions (for gif and jpeg)
> from within the servlet.
> eg. "a.jpeg" is 456 x 234 pixels
>
> Reading the image headers via URLConnection
> returns only ContentLength and ContentType.
> See output trace and test code below.
>
> What do I need to call to get the image dimensions
> from the header?
> Any help greatly appreciated.
>
> Regards,
> Andreas.
>
> -trace----------------------------------------------------------------
> ---------------
>
> > java testURL ./a.jpg
> url.toString =file://localhost./a.jpg
> url.toExternalForm =file://localhost./a.jpg
> content.toString =sun.awt.image.URLImageSource@1dacdb85
> content.getClass =class sun.awt.image.URLImageSource
> connecting
> con.getContentLength =23328
> con.getContentType =image/jpeg
> con.getContentEncoding =null
> 0. con.getHeaderFieldKey =content-type
> 0. con.getHeaderField =image/jpeg
> 1. con.getHeaderFieldKey =Content-length
> 1. con.getHeaderField =23328
> 2. con.getHeaderFieldKey =null
> 2. con.getHeaderField =null
> ...[cut for brevity]
> 9. con.getHeaderFieldKey =null
> 9. con.getHeaderField =null
> content is image
>
> -testURL.java---------------------------------------------------------
> ----------
>
> import java.net.*;
> import java.io.*;
> import java.util.*;
>
> import sun.awt.image.*;
>
> public class testURL
> {
>
> /*
> ** test
> */
>
> 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() );
>
> 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) );
> }
>
> if ( content.toString().startsWith("sun.awt.image.URLImageSource") )
> {
> System.out.println("content is image");
>
> URLImageSource image = new URLImageSource(con);
>
> }
>
> }
> 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
___________________________________________________________________________
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