Re: reading gif/jpeg image dimensions
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;i4;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;i2;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;i2;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;i4;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;
Re: reading gif/jpeg image dimensions
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;i2;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;i2;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;i4;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;i4;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) {