You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@cocoon.apache.org by Bert Van Kets <be...@vankets.com> on 2002/07/08 16:27:32 UTC

ImageReader

Hi Cocoon Guru's,
I'm planning on using Stefano's ImageReader reader to generate 
thumbnails.  It took me only a few minutes to implement it, but then I saw 
it only supports JPG images.  Is there any possibility to support GIF 
images or other formats as well?  I found that JDK 1.4 jai implements a GIF 
reader 
(http://java.sun.com/products/java-media/jai/forDevelopers/jaifaq.html#fileformat). 
Using this I could convert GIF images to JPG for the thumbnail 
presentation.  My Java knowledge is a bit small to do this myself, so hints 
are welcome.

There's another thing I could use and that's an image type reader.  It 
takes an image source and converts it to a specified image type.  This way 
the users can upload a whole scale of image types that would automagically 
be transformed to another type (JPG, GIF, PNG, ...).  How would I implement 
this in Cocoon?
Gathering knowledge from the thumbnail generator/reader I should be able to 
generate this component easily (I think).

Bert


---------------------------------------------------------------------------------------------
Please notify me if you did not receive this mail.


---------------------------------------------------------------------
To unsubscribe, e-mail: cocoon-dev-unsubscribe@xml.apache.org
For additional commands, email: cocoon-dev-help@xml.apache.org


RE: ImageReader

Posted by Per Kreipke <pe...@onclave.com>.
Not sure if this helps but here's another conversion program in Java which:

- uses AWT but not the toolkit part
- supports GIFs
- is relatively smart about keep the x/y ratio
- would need a little conversion to work in memory without writing to/from
disk

Per Kreipke

P.s. Can't remember where I found portions of this code snippet so I can't
give credit where credit is due, my apologies to the original author.



package org.onclave.util;

import java.awt.Image;
import java.awt.Graphics2D;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.OutputStream;
import java.io.FileOutputStream;
import javax.swing.ImageIcon;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;

public class Thumbnail
{
    public static void main(String[] args)
    {
        System.out.print("starting...\n");
        createThumbnail(args[0], args[1], Integer.parseInt(args[2]));
        System.out.print("finished.\n");
        System.exit(0);
    }

    /**
     * Reads an image in a file and creates
     * a thumbnail in another file.
     * @param orig The name of image file.
     * @param thumb The name of thumbnail file.
     * Will be created if necessary.
     * @param maxDim The width and height of
     * the thumbnail must
     * be maxDim pixels or less.
     */
    public static void createThumbnail(String orig, String thumb, int
maxDim)
    {
        try
        {
            // Get the image from a file.
            Image inImage = new ImageIcon(orig).getImage();

            // Determine the scale.
            double scale = (double)maxDim / (double)
inImage.getHeight(null);
            if (inImage.getWidth(null) > inImage.getHeight(null))
            {
                scale = (double)maxDim / (double) inImage.getWidth(null);
            }

            // Determine size of new image.
            // One of them should equal maxDim.
            int scaledW = (int)(scale * inImage.getWidth(null));
            int scaledH = (int)(scale * inImage.getHeight(null));

            // Create an image buffer in which to paint on.
            BufferedImage outImage = new BufferedImage(scaledW, scaledH,
                BufferedImage.TYPE_INT_RGB);

            // Set the scale.
            AffineTransform tx = new AffineTransform();

            // If the image is smaller than the desired image size,
            // don't bother scaling.
            if (scale < 1.0d)
            {
                tx.scale(scale, scale);
            }

            // Paint image.
            Graphics2D g2d = outImage.createGraphics();
            g2d.drawImage(inImage, tx, null);
            g2d.dispose();

            // JPEG-encode the image and write to file.
            OutputStream os = new FileOutputStream(thumb);
            JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(os);
            encoder.encode(outImage);
            os.close();
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
    }
}


---------------------------------------------------------------------
To unsubscribe, e-mail: cocoon-dev-unsubscribe@xml.apache.org
For additional commands, email: cocoon-dev-help@xml.apache.org


RE: ImageReader

Posted by Bert Van Kets <be...@vankets.com>.
Thanks Jens and Berin,
My Java experience is limited to a bit of JSP and taglibs.  Trying to get 
this running will be a good experience and jump to other stuff I'd like 
Cocoon to do.
ImageMagic seems to have circumvented the GIF license stuff because AFAICT 
it can write GIFs too.  The image convertion utility will be a useful 
addition to my Cocoon based CMS.  I almost have a complete browser based 
page editor (IE5.5+) running incl. image support.  I'll check out Xopus as 
soon as it's OSS, but from what I read on xopus.org the OSS version will 
not implement table support. :-(  I guess there must be a reason them to 
sell the commercial version.
Bert


At 11:17 8/07/2002 -0400, you wrote:
> > From: Jens Lorenz [mailto:jens.lorenz@interface-projects.de]
>
> > Oh. Didn't think of this. But writing is another thing. AFAIK
> > it's not possible with AWT to write images. So you might be
> > stuck with jpg for writing - unless you use JAI.
>
>
>ImageMagick (http://www.imagemagick.org/) is also a possibility.
>I have used its C++ API, and I must say that it rocks.  It is the
>way C++ was meant to be.
>
>It does have a Java API, does not require X windows, and if the
>Java API is as good as its C++ API, then it might be a definite
>win.  (BTW, it is under the GPL).
>
>
>---------------------------------------------------------------------
>To unsubscribe, e-mail: cocoon-dev-unsubscribe@xml.apache.org
>For additional commands, email: cocoon-dev-help@xml.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: cocoon-dev-unsubscribe@xml.apache.org
For additional commands, email: cocoon-dev-help@xml.apache.org


RE: ImageReader

Posted by Berin Loritsch <bl...@apache.org>.
> From: Jens Lorenz [mailto:jens.lorenz@interface-projects.de] 

> Oh. Didn't think of this. But writing is another thing. AFAIK 
> it's not possible with AWT to write images. So you might be 
> stuck with jpg for writing - unless you use JAI.


ImageMagick (http://www.imagemagick.org/) is also a possibility.
I have used its C++ API, and I must say that it rocks.  It is the
way C++ was meant to be.

It does have a Java API, does not require X windows, and if the
Java API is as good as its C++ API, then it might be a definite
win.  (BTW, it is under the GPL).


---------------------------------------------------------------------
To unsubscribe, e-mail: cocoon-dev-unsubscribe@xml.apache.org
For additional commands, email: cocoon-dev-help@xml.apache.org


Re: ImageReader

Posted by Jens Lorenz <je...@interface-projects.de>.
----- Original Message -----
From: "Bert Van Kets" <be...@vankets.com>
To: <co...@xml.apache.org>
Sent: Monday, July 08, 2002 4:27 PM
Subject: ImageReader


> Hi Cocoon Guru's,

Sorry, no guru here, but some guy with image loading experience.

> I'm planning on using Stefano's ImageReader reader to generate
> thumbnails.  It took me only a few minutes to implement it, but then I
saw
> it only supports JPG images.  Is there any possibility to support GIF
> images or other formats as well?  I found that JDK 1.4 jai implements a
GIF
> reader
>
(http://java.sun.com/products/java-media/jai/forDevelopers/jaifaq.html#fil
eformat).
> Using this I could convert GIF images to JPG for the thumbnail
> presentation.  My Java knowledge is a bit small to do this myself, so
hints
> are welcome.
>

Here you go. I used this snippet to get the RGB values of an Image.
This does not use JAI, but the AWT Toolkit.
The issue with AWT Toolkit (and probably the reason why Stefano
chose to use the com.sun. class), is that you'll run into problems
with headless unix servers, just like the batik stuff.
The advantage of this is that you don't have to worry what format
the image is in, as long as it is supported. (currently at least
gif, jpg, png and maybe bmp on win32)
Also this solution will most likely eat even more memory than
the original ImageReader. (opening up an X connection, drawing,
getting the drawing result back into memory)

<snip/>

import java.awt.Image;
import java.awt.Toolkit;
import java.awt.MediaTracker;
import java.awt.image.BufferedImage;
import java.awt.Graphics;
import java.awt.image.Raster;


Image source = Toolkit.getDefaultToolkit().getImage( picfile );
MediaTracker tracker = new MediaTracker( new Container() );
tracker.addImage( source, 0 );
try
{
  tracker.waitForID( 0 );
}
catch ( InterruptedException e )
{
  e.printStackTrace();
  return null;
}
width = source.getWidth( null );
height = source.getHeight( null );

BufferedImage image = new BufferedImage( width, height,
BufferedImage.TYPE_INT_RGB );
image.getGraphics().drawImage( source, 0, 0, null );

Raster raster = image.getData();

<snip/>

> There's another thing I could use and that's an image type reader.  It
> takes an image source and converts it to a specified image type.  This
way
> the users can upload a whole scale of image types that would
automagically
> be transformed to another type (JPG, GIF, PNG, ...).  How would I
implement
> this in Cocoon?

Oh. Didn't think of this. But writing is another thing. AFAIK it's not
possible with AWT to write images. So you might be stuck with jpg for
writing - unless you use JAI.

> Gathering knowledge from the thumbnail generator/reader I should be able
to
> generate this component easily (I think).
>
> Bert
>

Regards,


Jens

--

jens.lorenz at interface-projects dot de

interface:projects GmbH                             \\|//
Tolkewitzer Strasse 49                              (o o)
01277 Dresden                               ~~~~oOOo~(_)~oOOo~~~~
Germany


---------------------------------------------------------------------
To unsubscribe, e-mail: cocoon-dev-unsubscribe@xml.apache.org
For additional commands, email: cocoon-dev-help@xml.apache.org