decided to redo my old thumbnail code and found a solution that generates high quality thumbnails by "stepping" down large images in half before getting to the final size. makes high-quality thumbs.

enjoy,
./james


import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.Transparency;
import java.awt.image.BufferedImage;
import java.awt.image.IndexColorModel;
//import java.io.ByteArrayOutputStream;
//import java.net.URL;

/*
 * original thumbnail code from:
 * 
http://bits2share.netsons.org/2008/05/20/create-high-quality-thumbnails-in-java/
 *
 *
        // send file as BufferedImage
        file = new URL( pathToFile );
        
        // convert file to buffered image
        // byte[] fileByte = fileContents.bytes();
// BufferedImage image = ImageIO.read ( new ByteArrayInputStream ( fileByte ) );
        
        // or from URL() path above
        BufferedImage image = ImageIO.read( file );
BufferedImage buffThumb = ThumbnailMaker.createThumbnail(image, maxWidth, maxHeight);

 * [EMAIL PROTECTED]
 *
 *
 */

public class ThumbnailMaker {


public static BufferedImage createThumbnail(BufferedImage image, int thWidth, int thHeight) throws Exception {
        
    int w = image.getWidth();
    int h = image.getHeight();

BufferedImage thumbnail = new BufferedImage(thWidth, thHeight, BufferedImage.TYPE_INT_RGB);

    float scalex = ((float)w) / ((float) thWidth);
    float scaley = ((float)h) / ((float) thHeight);

    float scale = Math.min(scalex, scaley);
    if (scale < 1) scale = 1;

    BufferedImage scaled = createResizedImage(image,
        Math.round(w/scale), Math.round(h/scale));

    Graphics2D g = thumbnail.createGraphics();
    g.setColor(Color.white);
    g.fillRect(0, 0, thWidth, thHeight);
g .setRenderingHint (RenderingHints .KEY_TEXT_ANTIALIASING,RenderingHints.VALUE_TEXT_ANTIALIAS_ON); g .setRenderingHint (RenderingHints.KEY_RENDERING,RenderingHints.VALUE_RENDER_QUALITY); g .setRenderingHint (RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON); g.drawImage(scaled, (thWidth - scaled.getWidth()) / 2, (thHeight - scaled.getHeight()) / 2, null);
    g.dispose();

    return thumbnail;

  }

private static BufferedImage createResizedImage(BufferedImage img, int targetWidth, int targetHeight) {

if (img.getWidth() == targetHeight && img.getHeight() == targetHeight) return img;

    boolean higherQuality = true;
boolean isIndexColorModel = img.getColorModel() instanceof IndexColorModel;
    int type;

    if (!isIndexColorModel){
      type = (img.getTransparency() == Transparency.OPAQUE) ?
          BufferedImage.TYPE_INT_RGB :
            BufferedImage.TYPE_INT_ARGB;
    }
    else {type = img.getType();}

    BufferedImage ret = (BufferedImage) img;

    int w, h;
    if (higherQuality) {
      w = img.getWidth();
      h = img.getHeight();
    } else {
      w = targetWidth;
      h = targetHeight;
    }

    do {
      if (higherQuality && w > targetWidth) {
        w /= 2;
        if (w < targetWidth) {
          w = targetWidth;
        }
      }
      if (higherQuality && h > targetHeight) {
        h /= 2;
        if (h < targetHeight) {
          h = targetHeight;
        }
      }

      BufferedImage tmp = isIndexColorModel ?
new BufferedImage(w, h, type, (IndexColorModel)img.getColorModel()) :
            new BufferedImage(w, h, type);

      Graphics2D g2 = tmp.createGraphics();
g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
      g2.drawImage(ret, 0, 0, w, h, null);
      g2.dispose();

      ret = tmp;
    } while (w != targetWidth || h != targetHeight);


    return ret;
  }

}
 _______________________________________________
Do not post admin requests to the list. They will be ignored.
Webobjects-dev mailing list      ([email protected])
Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/webobjects-dev/archive%40mail-archive.com

This email sent to [EMAIL PROTECTED]

Reply via email to