Para projeto em JSP/Servlets preciso saber o tamanho de imagens que
estou inserindo no Banco de Dados. Crie uma classe para fazer isso, T�
funcionando �timo com GIF mas naum consigo deixar funcionando com JPEG.
Os resultados s�o incorretas.
Ser� que alguem pode me ajudar?
<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;
  }
}
</code>

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

    --------------------------- LISTA SOUJAVA ---------------------------
    http://www.soujava.org.br  -  Sociedade de Usu�rios Java da Sucesu-SP
    [d�vidas mais comuns: http://www.soujava.org.br/faq.htm]
    [para sair da lista: http://www.soujava.org.br/forum/cadastrados.htm]
    [regras da lista: http://www.soujava.org.br/regras.htm]
    ---------------------------------------------------------------------

Responder a