This might help you with image resizing. It was built for a very specific purpose: take an input image, downsize it to fit a specified maximum width and output it as a JPEG. It could easily be extended to work with a maximum height as well (typical thumbnail bounding box approach). The useful thing in this code is that it demonstrates how to get good quality resized JPEG's (something people seem to have a lot of trouble with using awt imaging). This is the guts of the "service" with all the image processing code.

import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.Iterator;

import javax.imageio.IIOImage;
import javax.imageio.ImageIO;
import javax.imageio.ImageWriteParam;
import javax.imageio.ImageWriter;
import javax.imageio.stream.ImageOutputStream;

import org.apache.tapestry.ApplicationRuntimeException;

public class ImageService {
public static final float JPEG_COMPRESSION_RATIO = 0.90f;

public static void saveConstrainedJpeg(InputStream input, int maxWidth, File outFile) { // Read source file into a BufferedImage.
       BufferedImage inputImage;
       try {
           inputImage = ImageIO.read(input);
       } catch (IOException ex) {
throw new ApplicationRuntimeException("Cannot read image file.", ex);
       }
// Get input image width and height.
       int imgWidth = inputImage.getWidth();
       int imgHeight = inputImage.getHeight();
// Write the original image if it is within the given constraints.
       if (imgWidth <= maxWidth) {
           writeJpeg(inputImage, outFile);
           return;
       }
// Calculate new width and height;
       double factor = (double) maxWidth / imgWidth;
       int newWidth = (int) Math.round(factor * imgWidth);
       int newHeight = (int) Math.round(factor * imgHeight);
// Create and save target image. BufferedImage target = new BufferedImage(newWidth, newHeight, BufferedImage.TYPE_INT_RGB);
       writeJpeg(resize(inputImage, target), outFile);
} private static BufferedImage resize(BufferedImage source, BufferedImage target) {
       Graphics2D g2 = target.createGraphics();
g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
       double scalex = (double) target.getWidth() / source.getWidth();
       double scaley = (double) target.getHeight() / source.getHeight();
AffineTransform at = AffineTransform.getScaleInstance(scalex, scaley);
       g2.drawRenderedImage(source, at);
       g2.dispose();
       return target;
   }

   private static void writeJpeg(BufferedImage image, File outFile) {
// Find a jpeg writer.
       ImageWriter writer = null;
       Iterator iter = ImageIO.getImageWritersByMIMEType("image/jpeg");
       while (iter.hasNext()) {
           writer = (ImageWriter) iter.next();
       }
// Prepare the output file.
       ImageOutputStream ios = null;
       try {
       ios = ImageIO.createImageOutputStream(outFile);
       } catch (IOException ex) {
throw new ApplicationRuntimeException("Could not open file for output.", ex);
       }
       writer.setOutput(ios);
// Set the compression quality.
       ImageWriteParam param = writer.getDefaultWriteParam();
       param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
       param.setCompressionQuality(JPEG_COMPRESSION_RATIO);
// Write the image.
       try {
           writer.write(null, new IIOImage(image, null, null), param);
           ios.flush();
           writer.dispose();
           ios.close();
       } catch (IOException ex) {
throw new ApplicationRuntimeException("Could not write image file.", ex); } }
}


-Ryan

Henri Dupre wrote:
I seem to recall that quite a while ago someone was working on an image
service.
I would need a service that allows to resize pictures (hopefully cache the
images). Has anyone developped such service?
Thanks,

Henri.


---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to