anderson wrote:
>
> My database contains a field that has the name and path of an image (Gif or JPG ), I
> need to show the image inside the window, and this window must have the same width
> and height of the image. To do it I need to get the image size with the JSP.
>
That's exactly what I need. I'm trying to write some code that checks
for the image width and height. It works perfectly for GIF type images
but I can't get it to work for JPEG. any ideas ?
<code>:

import java.io.*;
import br.vip.util.LogWriter;

public class ImageSizer extends Object {
  private int _height;
  private int _width;
  private int _type; // 1 denotes gif, 2 denotes jpeg
  private String _name;
  private File _f;
  private LogWriter _log;

  public ImageSizer(File f) throws IOException, FileNotFoundException,
Exception{
    _name = f.getName();
    _f = f;
    _log = log;
    setType();
    setSize();
  }

  private void setType() throws IOException, FileNotFoundException{
    String ext = _name.substring(_name.length() - 3, _name.length());
    if (ext.equals("gif")){
      _type = 1;
    } else if (ext.equals("jpg")){
      _log.log("set Type JPG", LogWriter.DEBUG);
      _type = 2;
    } else {
      throw new IOException("Invalid Image Format!");
    }
  }

  private void setSize() throws IOException, FileNotFoundException,
Exception{
    switch(_type){
      case 1:{
        sizeGIF();
        break;
      }
      case 2:{
        _log.log("setSize(2)", LogWriter.ERROR);
        sizeJPEG();
        break;
      }
    }
  }

  private void sizeGIF() throws IOException, FileNotFoundException{
    byte a[] = new byte[2];
    FileInputStream img = new FileInputStream(_f);
    img.skip(6L);
    img.read(a);
    _width = ((short)a[0] | ((short)a[1])<<8);
    img.read(a);
    _height = ((short)a[0] | ((short)a[1])<<8);
  }


  private void sizeJPEG() throws IOException, FileNotFoundException,
Exception{
    byte a[] = new byte[2];
    int marker = 0;
    FileInputStream img = new FileInputStream(_f);
    if (img.read() != 0xFF){
      throw new Exception("Not a JPEG Header!");
    }
    if (img.read() != 0xD8){
      throw new Exception("Not a JPEG Header!");
    }

    for (;;){
      marker = getNextMarker(img);
      switch(marker){
        case 0xC0:
        case 0xC1:
        case 0xC2:
        case 0xC3:
        case 0xC5:
        case 0xC6:
        case 0xC7:
        case 0xC9:
        case 0xCA:
        case 0xCB:
        case 0xCD:
        case 0xCE:
        case 0xCF:{
          _log.log("Executing case: " + Integer.toHexString(marker) ,
LogWriter.ERROR);
          img.skip(3);
          img.read(a);
          _height = (((short)a[0]) << 8) + ((short) a[1]);
          img.read(a);
          _width = (((short)a[0]) << 8) + (short)a[1];
          return;
        }

      }

    }
  }


  private int getNextMarker(FileInputStream img) throws IOException{
    int c;
    c = img.read();
    while (c != 0xff) {
      if ((c = img.read()) == -1){
        return 0xD9;
      }
    }
    do {
      if ((c = img.read()) == -1)
        return 0xD9;
    } while (c == 0xff);
    return c;
  }

  public int getHeight(){
    return _height;
  }

  public int getWidth(){
    return _width;
  }
}

--
======================================================================================
Sven van 't Veer                                              http://www.cachoeiro.net
Java Developer                                                      [EMAIL PROTECTED]
                                        _/_
The answer                             /   \
    to the ultimate question        nnn|.
.|nnn                                     42
=========================================U============================================

===========================================================================
To unsubscribe: mailto [EMAIL PROTECTED] with body: "signoff JSP-INTEREST".
Some relevant FAQs on JSP/Servlets can be found at:

 http://java.sun.com/products/jsp/faq.html
 http://www.esperanto.org.nz/jsp/jspfaq.html
 http://www.jguru.com/jguru/faq/faqpage.jsp?name=JSP
 http://www.jguru.com/jguru/faq/faqpage.jsp?name=Servlets

Reply via email to