ImageUtility.java :  » Image » jchannel » org » jchannel » server » utility » Java Open Source

Java Open Source » Image » jchannel 
jchannel » org » jchannel » server » utility » ImageUtility.java
package org.jchannel.server.utility;

import java.awt.Graphics2D;
import java.awt.GraphicsConfiguration;
import java.awt.GraphicsEnvironment;
import java.awt.RenderingHints;
import java.awt.Transparency;
import java.awt.image.BufferedImage;
import java.awt.image.BufferedImageOp;
import java.awt.image.ConvolveOp;
import java.awt.image.Kernel;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

import java.util.HashMap;
import java.util.Map;

import javax.imageio.ImageIO;

public final class ImageUtility {

  private static final float BLUR_FACTOR = 1f / 4f;
  private static final float[] BLUR_KERNEL = {
    BLUR_FACTOR, BLUR_FACTOR, BLUR_FACTOR, BLUR_FACTOR
  };
  private static final Map<RenderingHints.Key, Object> RENDERING_HINTS = new HashMap<RenderingHints.Key, Object>();
  static {
    RENDERING_HINTS.put(RenderingHints.KEY_INTERPOLATION,
      RenderingHints.VALUE_INTERPOLATION_BILINEAR);
    RENDERING_HINTS.put(RenderingHints.KEY_RENDERING,
      RenderingHints.VALUE_RENDER_QUALITY);
    RENDERING_HINTS.put(RenderingHints.KEY_ANTIALIASING,
      RenderingHints.VALUE_ANTIALIAS_ON);
  }
  private static final BufferedImageOp BLUR_OP = new ConvolveOp(new Kernel(2,
    2, BLUR_KERNEL), ConvolveOp.EDGE_NO_OP,
    new RenderingHints(RENDERING_HINTS));

  private ImageUtility() {
  }

  public static BufferedImage read(final String filename)
    throws IOException {
    return ImageIO.read(new BufferedInputStream(new FileInputStream(new File(filename))));
  }

  public static BufferedImage read(final InputStream inputStream)
    throws IOException {
    return ImageIO.read(new BufferedInputStream(inputStream));
  }

  public static BufferedImage scale(final BufferedImage image, final int size) {
    int imageWidth = image.getWidth();
    int imageHeight = image.getHeight();

    if ((imageWidth <= size) && (imageHeight <= size)) {
      return image;
    }

    int thumbnailWidth;
    int thumbnailHeight;

    if (imageWidth > imageHeight) {
      thumbnailWidth = size;
      thumbnailHeight = (int) (((double) imageHeight / (double) imageWidth) * size);
    } else {
      thumbnailWidth = (int) (((double) imageWidth / (double) imageHeight) * size);
      thumbnailHeight = size;
    }

    BufferedImage thumbnail = new BufferedImage(thumbnailWidth,
      thumbnailHeight, BufferedImage.TYPE_INT_RGB);
    Graphics2D g = thumbnail.createGraphics();
    g.setRenderingHints(RENDERING_HINTS);
    g.drawImage(blur(compatible(image)), 0, 0, thumbnailWidth, thumbnailHeight, null);
    g.dispose();

    return thumbnail;
  }

  public static byte[] toByteArray(final BufferedImage image,
    final String format) throws IOException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    BufferedOutputStream bos = new BufferedOutputStream(baos);
    ImageIO.write(image, format, bos);

    try {
      return baos.toByteArray();
    } finally {
      baos.close();
    }
  }

  private static BufferedImage blur(final BufferedImage image) {
    return BLUR_OP.filter(image, null);
  }

  private static BufferedImage compatible(final BufferedImage image) {
    GraphicsConfiguration gc = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDefaultConfiguration();
    BufferedImage result = gc.createCompatibleImage(image.getWidth(),
      image.getHeight(), Transparency.TRANSLUCENT);
    Graphics2D g2 = result.createGraphics();
    g2.drawRenderedImage(image, null);
    g2.dispose();

    return result;
  }
}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.