You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by "Alexey V. Meledin" <av...@webclub.ru> on 2000/07/14 17:58:56 UTC

image procesing

Hi,
 tomcat-users!

 Excuse me for a small offtopic, but I can't find decision needed in my
 project with tomcat.
 I've spend a lot of time trying to find how to load gif,jpegp,jpeg
 files with Java and make some operation with them (determine width, height and
 so on).
 Currently this task occur in file upload operations.
 If someone has any library to process image formats specified above,
 it would very fine.

 great thanks.


Alexey V. Meledin <av...@webclub.ru>
> InterForge Developers Group,  St-Petersburg, Russia
New: http://www.crossroad.ru; http://www.garoway.com
> > > > > > "InterForge to Forge Ahead" > > > > > > >



Re: image procesing

Posted by ke...@core.tb.cz.
Also, take a look at JIMI
http://java.sun.com/products/jimi/

It's not as heavyweighted as Java Advanced Imaging, doesn't require native
libs to install (so it should work under - say - Linux) and is very useful
for at least saving images to the disk (it's almost impossible to do it
with 'standard' Java)... I'm using it in my 'image resizer' (currently
working with JPEGs only), although the resizing is done with AWT function
image.getScaledInstance(...).


Regards
Roman Kratochvil


On Fri, 14 Jul 2000, Sven van 't Veer wrote:

> 
> 
> "Alexey V. Meledin" wrote:
> > 
> > Hi,
> >  tomcat-users!
> > 
> >  Excuse me for a small offtopic, but I can't find decision needed in my
> >  project with tomcat.
> >  I've spend a lot of time trying to find how to load gif,jpegp,jpeg
> >  files with Java and make some operation with them (determine width, height and
> >  so on).
> >  Currently this task occur in file upload operations.
> >  If someone has any library to process image formats specified above,
> >  it would very fine.
> > 
> Alexey,
> 
> For my uploads U use com.oreilly.servlet.MultipartRequest. I too needed
> to know the isze of the Image, which seemed to be a difficult task,
> standard java needs an Image object to do this. I developed the
> following code, which works for GIF, I have not been able to do the same
> thing with JPEG.
> <code>
> /*
>  * ImageSizer.java
>  *
>  * Created on 4 de Julho de 2000, 14:20
>  *
>  * known bug: returns wrong size for animated gifs
>  */
>  
> import java.io.*;
> import br.vip.util.LogWriter;
> import com.sun.image.codec.jpeg.*;
> 
> /** 
>  *
>  * @author  Sven
>  * @version 
>  */
> 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;
> 
>   /** Creates new ImageSizer */
>   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")){
>       _type = 2;
>     } else {
>       throw new IOException("Invalid Image Format!");
>     }
>   }
>   
>   private void setSize() throws IOException, FileNotFoundException,
> Exception{
>     switch(_type){
>       case 1:{
>         sizeGIF();
>         break;
>       }
>       case 2:{
>         sizeJPEG();
>         //sizeJPEGCodec();
>         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 sizeJPEGCodec(){
>     //EGCodec jpeg = new JPEGCodec();
>     try{
>       JPEGDecodeParam param = null;// = new
> JPEGDecodeParam();//dec.getDefaultJPEGDecodeParam();
> 
>       //JPEGImageDecoder dec = JPEGCodec.createJPEGDecoder(new
> FileInputStream(_f));
>       JPEGImageDecoder dec = JPEGCodec.createJPEGDecoder(new
> FileInputStream(_f), param);
> 
>       //JPEGDecodeParam param = dec.getDefaultJPEGDecodeParam();
> 
>       _height = param.getHeight();
>       _width = param.getWidth();
>     } catch (ImageFormatException ex){
> 
>     } catch (Exception ex){
> 
>     }
>   }
>   
>   
>   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:{
>           img.skip(2);
>           int bits = img.read();
>           img.read(a);
>           _height = (a[0] << 8) + a[1];
>           _height = read2bytes(img);
>           _width = read2bytes(img);
>           img.read(a);
>           _height = -1;
>           _width = -1;
>           return ;
>           if (marker == 0xD9) return;
>         }
> 
>       }
>       
>     }
>   }
>   
>   
>   private int getNextMarker(FileInputStream img) throws IOException{
>     int c;
>     c = img.read();
>     while (c != 0xff) { 
>       if ((c = img.read()) == -1){
>         return 0xD9; /* we hit EOF */
>       }
>     }
>     do {
>       if ((c = img.read()) == -1)
> 	return 0xD9; /* we hit EOF */
>     } while (c == 0xff);
>     return c;
>   }
>   
>   public int getHeight(){
>     return _height;
>   }
>   
>   public int getWidth(){
>     return _width;
>   }
> }
> 
> </code>
> 
> As I said, it works for GIF however neither method works for JPEG.
> Animated GIF returns the wromg size.
> 
> Sven
> 
> -- 
> ======================================================================================
> Sven van 't Veer					      http://www.cachoeiro.net
> Java Developer	         					    sven@cachoeiro.net	
>                                         _/_
> The answer                             /   \  					    
>     to the ultimate question        nnn|.
> .|nnn                                     42
> =========================================U============================================
> 


Re: image procesing

Posted by Sven van 't Veer <sv...@vip.br>.

"Alexey V. Meledin" wrote:
> 
> Hi,
>  tomcat-users!
> 
>  Excuse me for a small offtopic, but I can't find decision needed in my
>  project with tomcat.
>  I've spend a lot of time trying to find how to load gif,jpegp,jpeg
>  files with Java and make some operation with them (determine width, height and
>  so on).
>  Currently this task occur in file upload operations.
>  If someone has any library to process image formats specified above,
>  it would very fine.
> 
Alexey,

For my uploads U use com.oreilly.servlet.MultipartRequest. I too needed
to know the isze of the Image, which seemed to be a difficult task,
standard java needs an Image object to do this. I developed the
following code, which works for GIF, I have not been able to do the same
thing with JPEG.
<code>
/*
 * ImageSizer.java
 *
 * Created on 4 de Julho de 2000, 14:20
 *
 * known bug: returns wrong size for animated gifs
 */
 
import java.io.*;
import br.vip.util.LogWriter;
import com.sun.image.codec.jpeg.*;

/** 
 *
 * @author  Sven
 * @version 
 */
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;

  /** Creates new ImageSizer */
  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")){
      _type = 2;
    } else {
      throw new IOException("Invalid Image Format!");
    }
  }
  
  private void setSize() throws IOException, FileNotFoundException,
Exception{
    switch(_type){
      case 1:{
        sizeGIF();
        break;
      }
      case 2:{
        sizeJPEG();
        //sizeJPEGCodec();
        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 sizeJPEGCodec(){
    //EGCodec jpeg = new JPEGCodec();
    try{
      JPEGDecodeParam param = null;// = new
JPEGDecodeParam();//dec.getDefaultJPEGDecodeParam();

      //JPEGImageDecoder dec = JPEGCodec.createJPEGDecoder(new
FileInputStream(_f));
      JPEGImageDecoder dec = JPEGCodec.createJPEGDecoder(new
FileInputStream(_f), param);

      //JPEGDecodeParam param = dec.getDefaultJPEGDecodeParam();

      _height = param.getHeight();
      _width = param.getWidth();
    } catch (ImageFormatException ex){

    } catch (Exception ex){

    }
  }
  
  
  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:{
          img.skip(2);
          int bits = img.read();
          img.read(a);
          _height = (a[0] << 8) + a[1];
          _height = read2bytes(img);
          _width = read2bytes(img);
          img.read(a);
          _height = -1;
          _width = -1;
          return ;
          if (marker == 0xD9) return;
        }

      }
      
    }
  }
  
  
  private int getNextMarker(FileInputStream img) throws IOException{
    int c;
    c = img.read();
    while (c != 0xff) { 
      if ((c = img.read()) == -1){
        return 0xD9; /* we hit EOF */
      }
    }
    do {
      if ((c = img.read()) == -1)
	return 0xD9; /* we hit EOF */
    } while (c == 0xff);
    return c;
  }
  
  public int getHeight(){
    return _height;
  }
  
  public int getWidth(){
    return _width;
  }
}

</code>

As I said, it works for GIF however neither method works for JPEG.
Animated GIF returns the wromg size.

Sven

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

Re: image procesing

Posted by "Panther (as in the Pinkboard Panther)" <pa...@pinkboard.com.au>.
>  Excuse me for a small offtopic, but I can't find decision needed in my
>  project with tomcat.
>  I've spend a lot of time trying to find how to load gif,jpegp,jpeg
>  files with Java and make some operation with them (determine width, 
> height and
>  so on).
>  Currently this task occur in file upload operations.
>  If someone has any library to process image formats specified above,
>  it would very fine.

Another one to look at
         http://www.eteks.com



love Panther

---
Panther (as in Pinkboard Panther) <pa...@pinkboard.com.au>
Larry Singer <la...@pinkboard.com.au>
Pinkboard is on http://www.pinkboard.com.au
I am on http://www.pinkboard.com.au/~panther
Pinkboard is Australia's most popular site for lesbians and gay men.
Make sure you always have Safe Sex.
Love your sister and brother no matter what colour.

Say NO to Internet Censorship: http://www.efa.org.au


Re: image procesing

Posted by ke...@core.tb.cz.
Hi,
have a look at Java Advanced Imaging
http://java.sun.com/products/java-media/jai/index.html

But be prepared - it will eat a lot of memory...

Roman Kratochvil


On Fri, 14 Jul 2000, Alexey V. Meledin wrote:

> Hi,
>  tomcat-users!
> 
>  Excuse me for a small offtopic, but I can't find decision needed in my
>  project with tomcat.
>  I've spend a lot of time trying to find how to load gif,jpegp,jpeg
>  files with Java and make some operation with them (determine width, height and
>  so on).
>  Currently this task occur in file upload operations.
>  If someone has any library to process image formats specified above,
>  it would very fine.
> 
>  great thanks.
> 
> 
> Alexey V. Meledin <av...@webclub.ru>
> > InterForge Developers Group,  St-Petersburg, Russia
> New: http://www.crossroad.ru; http://www.garoway.com
> > > > > > > "InterForge to Forge Ahead" > > > > > > >
> 
>