Compress and save an image to the disk : Image IO « 2D Graphics GUI « Java

Home
Java
1.2D Graphics GUI
2.2D Graphics GUI1
3.3D
4.Advanced Graphics
5.Ant
6.Apache Common
7.Chart
8.Class
9.Collections Data Structure
10.Data Type
11.Database SQL JDBC
12.Design Pattern
13.Development Class
14.EJB3
15.Email
16.Event
17.File Input Output
18.Game
19.Generics
20.GWT
21.Hibernate
22.I18N
23.J2EE
24.J2ME
25.JDK 6
26.JNDI LDAP
27.JPA
28.JSP
29.JSTL
30.Language Basics
31.Network Protocol
32.PDF RTF
33.Reflection
34.Regular Expressions
35.Scripting
36.Security
37.Servlets
38.Spring
39.Swing Components
40.Swing JFC
41.SWT JFace Eclipse
42.Threads
43.Tiny Application
44.Velocity
45.Web Services SOA
46.XML
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
SCJP
Java » 2D Graphics GUI » Image IOScreenshots 
Compress and save an image to the disk
    

/*
 * Copyright (c) JForum Team
 * All rights reserved.

 * Redistribution and use in source and binary forms, 
 * with or without modification, are permitted provided 
 * that the following conditions are met:

 * 1) Redistributions of source code must retain the above 
 * copyright notice, this list of conditions and the 
 * following  disclaimer.
 * 2)  Redistributions in binary form must reproduce the 
 * above copyright notice, this list of conditions and 
 * the following disclaimer in the documentation and/or 
 * other materials provided with the distribution.
 * 3) Neither the name of "Rafael Steil" nor 
 * the names of its contributors may be used to endorse 
 * or promote products derived from this software without 
 * specific prior written permission.
 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT 
 * HOLDERS AND CONTRIBUTORS "AS IS" AND ANY 
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, 
 * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR 
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL 
 * THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE 
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES 
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, 
 * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 
 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER 
 * IN CONTRACT, STRICT LIABILITY, OR TORT 
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 
 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE
 
 * This file creation date: 21/04/2004 - 19:54:16
 * The JForum Project
 * http://www.jforum.net
 */

import java.awt.Dimension;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.awt.image.PixelGrabber;
import java.io.File;
import java.io.IOException;
import java.util.Iterator;
import java.util.Locale;

import javax.imageio.IIOImage;
import javax.imageio.ImageIO;
import javax.imageio.ImageWriteParam;
import javax.imageio.ImageWriter;
import javax.imageio.plugins.jpeg.JPEGImageWriteParam;
import javax.imageio.stream.ImageOutputStream;


/**
 * Utilities methods for image manipulation. It does not support writting of GIF images, but it can
 * read from. GIF images will be saved as PNG.
 
 @author Rafael Steil
 @version $Id: ImageUtils.java,v 1.23 2007/09/09 01:05:22 rafaelsteil Exp $
 */
public class ImageUtils
{
  public static final int IMAGE_UNKNOWN = -1;
  public static final int IMAGE_JPEG = 0;
  public static final int IMAGE_PNG = 1;
  public static final int IMAGE_GIF = 2;

  /**
   * Resizes an image
   
   @param imgName The image name to resize. Must be the complet path to the file
   @param type int
   @param maxWidth The image's max width
   @param maxHeight The image's max height
   @return A resized <code>BufferedImage</code>
   */
  public static BufferedImage resizeImage(String imgName, int type, int maxWidth, int maxHeight)
  {
    try {
      return resizeImage(ImageIO.read(new File(imgName)), type, maxWidth, maxHeight);
    }
    catch (IOException e) {
      e.printStackTrace();
    }
  }

  /**
   * Resizes an image.
   
   @param image
   *            The image to resize
   @param maxWidth
   *            The image's max width
   @param maxHeight
   *            The image's max height
   @return A resized <code>BufferedImage</code>
   @param type
   *            int
   */
  public static BufferedImage resizeImage(BufferedImage image, int type, int maxWidth, int maxHeight)
  {
    Dimension largestDimension = new Dimension(maxWidth, maxHeight);

    // Original size
    int imageWidth = image.getWidth(null);
    int imageHeight = image.getHeight(null);

    float aspectRatio = (floatimageWidth / imageHeight;

    if (imageWidth > maxWidth || imageHeight > maxHeight) {
      if ((floatlargestDimension.width / largestDimension.height > aspectRatio) {
        largestDimension.width = (intMath.ceil(largestDimension.height * aspectRatio);
      }
      else {
        largestDimension.height = (intMath.ceil(largestDimension.width / aspectRatio);
      }

      imageWidth = largestDimension.width;
      imageHeight = largestDimension.height;
    }

    return createHeadlessSmoothBufferedImage(image, type, imageWidth, imageHeight);
  }

  /**
   * Saves an image to the disk.
   
   @param image  The image to save
   @param toFileName The filename to use
   @param type The image type. Use <code>ImageUtils.IMAGE_JPEG</code> to save as JPEG images,
   *  or <code>ImageUtils.IMAGE_PNG</code> to save as PNG.
   @return <code>false</code> if no appropriate writer is found
   */
  public static boolean saveImage(BufferedImage image, String toFileName, int type)
  {
    try {
      return ImageIO.write(image, type == IMAGE_JPEG ? "jpg" "png"new File(toFileName));
    }
    catch (IOException e) {
      e.printStackTrace();
    }
  }

  /**
   * Compress and save an image to the disk. Currently this method only supports JPEG images.
   
   @param image The image to save
   @param toFileName The filename to use
   @param type The image type. Use <code>ImageUtils.IMAGE_JPEG</code> to save as JPEG images,
   * or <code>ImageUtils.IMAGE_PNG</code> to save as PNG.
   */
  public static void saveCompressedImage(BufferedImage image, String toFileName, int type)
  {
    try {
      if (type == IMAGE_PNG) {
        throw new UnsupportedOperationException("PNG compression not implemented");
      }

      Iterator iter = ImageIO.getImageWritersByFormatName("jpg");
      ImageWriter writer;
      writer = (ImageWriteriter.next();

      ImageOutputStream ios = ImageIO.createImageOutputStream(new File(toFileName));
      writer.setOutput(ios);

      ImageWriteParam iwparam = new JPEGImageWriteParam(Locale.getDefault());

      iwparam.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
      iwparam.setCompressionQuality(0.7F);

      writer.write(null, new IIOImage(image, null, null), iwparam);

      ios.flush();
      writer.dispose();
      ios.close();
    }
    catch (IOException e) {
      e.printStackTrace();
    }
  }

  /**
   * Creates a <code>BufferedImage</code> from an <code>Image</code>. This method can
   * function on a completely headless system. This especially includes Linux and Unix systems
   * that do not have the X11 libraries installed, which are required for the AWT subsystem to
   * operate. This method uses nearest neighbor approximation, so it's quite fast. Unfortunately,
   * the result is nowhere near as nice looking as the createHeadlessSmoothBufferedImage method.
   
   @param image  The image to convert
   @param w The desired image width
   @param h The desired image height
   @return The converted image
   @param type int
   */
  public static BufferedImage createHeadlessBufferedImage(BufferedImage image, int type, int width, int height)
  {
    if (type == ImageUtils.IMAGE_PNG && hasAlpha(image)) {
      type = BufferedImage.TYPE_INT_ARGB;
    }
    else {
      type = BufferedImage.TYPE_INT_RGB;
    }

    BufferedImage bi = new BufferedImage(width, height, type);

    for (int y = 0; y < height; y++) {
      for (int x = 0; x < width; x++) {
        bi.setRGB(x, y, image.getRGB(x * image.getWidth() / width, y * image.getHeight() / height));
      }
    }

    return bi;
  }

  /**
   * Creates a <code>BufferedImage</code> from an <code>Image</code>. This method can
   * function on a completely headless system. This especially includes Linux and Unix systems
   * that do not have the X11 libraries installed, which are required for the AWT subsystem to
   * operate. The resulting image will be smoothly scaled using bilinear filtering.
   
   @param source The image to convert
   @param w The desired image width
   @param h The desired image height
   @return The converted image
   @param type  int
   */
  public static BufferedImage createHeadlessSmoothBufferedImage(BufferedImage source, int type, int width, int height)
  {
    if (type == ImageUtils.IMAGE_PNG && hasAlpha(source)) {
      type = BufferedImage.TYPE_INT_ARGB;
    }
    else {
      type = BufferedImage.TYPE_INT_RGB;
    }

    BufferedImage dest = new BufferedImage(width, height, type);

    int sourcex;
    int sourcey;

    double scalex = (doublewidth / source.getWidth();
    double scaley = (doubleheight / source.getHeight();

    int x1;
    int y1;

    double xdiff;
    double ydiff;

    int rgb;
    int rgb1;
    int rgb2;

    for (int y = 0; y < height; y++) {
      sourcey = y * source.getHeight() / dest.getHeight();
      ydiff = scale(y, scaley- sourcey;

      for (int x = 0; x < width; x++) {
        sourcex = x * source.getWidth() / dest.getWidth();
        xdiff = scale(x, scalex- sourcex;

        x1 = Math.min(source.getWidth() 1, sourcex + 1);
        y1 = Math.min(source.getHeight() 1, sourcey + 1);

        rgb1 = getRGBInterpolation(source.getRGB(sourcex, sourcey), source.getRGB(x1, sourcey), xdiff);
        rgb2 = getRGBInterpolation(source.getRGB(sourcex, y1), source.getRGB(x1, y1), xdiff);

        rgb = getRGBInterpolation(rgb1, rgb2, ydiff);

        dest.setRGB(x, y, rgb);
      }
    }

    return dest;
  }

  private static double scale(int point, double scale)
  {
    return point / scale;
  }

  private static int getRGBInterpolation(int value1, int value2, double distance)
  {
    int alpha1 = (value1 & 0xFF000000>>> 24;
    int red1 = (value1 & 0x00FF0000>> 16;
    int green1 = (value1 & 0x0000FF00>> 8;
    int blue1 = (value1 & 0x000000FF);

    int alpha2 = (value2 & 0xFF000000>>> 24;
    int red2 = (value2 & 0x00FF0000>> 16;
    int green2 = (value2 & 0x0000FF00>> 8;
    int blue2 = (value2 & 0x000000FF);

    int rgb = ((int) (alpha1 * (1.0 - distance+ alpha2 * distance<< 24)
      ((int) (red1 * (1.0 - distance+ red2 * distance<< 16)
      ((int) (green1 * (1.0 - distance+ green2 * distance<< 8)
      (int) (blue1 * (1.0 - distance+ blue2 * distance);

    return rgb;
  }

  /**
   * Determines if the image has transparent pixels.
   
   @param image The image to check for transparent pixel.s
   @return <code>true</code> of <code>false</code>, according to the result
   */
  public static boolean hasAlpha(Image image)
  {
    try {
      PixelGrabber pg = new PixelGrabber(image, 0011false);
      pg.grabPixels();

      return pg.getColorModel().hasAlpha();
    }
    catch (InterruptedException e) {
      return false;
    }
  }
}

   
    
    
    
  
Related examples in the same category
1.Display image supported by ImageIO
2.Print an Image to print directly
3.List All reader and writer formats supported by ImageIO
4.Display available ImageReaders and ImageWriters by image format and MIME typeDisplay available ImageReaders and ImageWriters by image format and MIME type
5.Write an image of a given format
6.Read an image
7.Simple, functional ImageReaderSpi used to understand how information
8.Simple, functional ImageWriterSpi used to understand how information
9.Example showing how to reset the ordering of ImageReaderSpis in Image I/OExample showing how to reset the ordering of ImageReaderSpis in Image I/O
10.Using mediatracker to pre-load images
11.Get list of unique supported read formats
12.Get list of unique supported write formats
13.Get list of unique MIME types that can be read
14.Get list of unique MIME types that can be written
15.Returns true if the specified format name can be read
16.Returns true if the specified format name can be written
17.Returns true if the specified file extension can be read
18.Returns true if the specified file extension can be written
19.Returns true if the specified mime type can be read
20.Returns true if the specified mime type can be written
21.Show ImageIO Info
22.Write Image with different types
23.Load the image file from a folder or a jar file: use javax.imageio.ImageIO class to read the image file
24.Creates a new raster copy
25.Resizes an image
26.Creates an image compatible with the current display
27.Produces a copy of the supplied image
28.Produces a resized image that is of the given dimensions
29.Loads an image in a format compatible with the current display
30.Thumbnail Generator
31.Saves an image to the disk
32.Helper class for debugging stuff in Image I/O.
33.Reading a gif image with ImageIO.read and display it on screenReading a gif image with ImageIO.read and display it on screen
34.Read and write image files in the formats that the JDK supports. Multi-file images are supported.
35.A program for viewing imagesA program for viewing images
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.