ImageUtil.java Source code

Java tutorial

Introduction

Here is the source code for ImageUtil.java

Source

/**
 * COPYRIGHT. Harry Wu 2010. ALL RIGHTS RESERVED.
 * Project: EasyPhoto
 * Author: Harry Wu <harrywu304@gmail.com>
 * Created On: Jun 28, 2008 5:12:21 PM
 *
 */
//package org.shaitu.easyphoto.util;

import java.awt.Image;
import java.awt.image.BufferedImage;
import java.awt.image.ColorModel;
import java.awt.image.IndexColorModel;
import java.awt.image.WritableRaster;
import java.io.File;
import java.util.Iterator;
import java.util.List;
import javax.imageio.IIOImage;
import javax.imageio.ImageIO;
import javax.imageio.ImageTypeSpecifier;
import javax.imageio.ImageWriteParam;
import javax.imageio.ImageWriter;
import javax.imageio.plugins.jpeg.JPEGImageWriteParam;
import javax.imageio.stream.ImageOutputStream;
import javax.swing.ImageIcon;

/**
 * userful methods about image
 * @author harry
 */
public class ImageUtil {
    public final static String JPEG = "jpeg";
    public final static String JPG = "jpg";
    public final static String BMP = "bmp";
    public final static String PNG = "png";
    public final static String GIF = "gif";
    public final static String TIFF = "tiff";
    public final static String TIF = "tif";

    /**
     * get fixing preview image rsize <br/>
     * Exif not apply: calculate by formula: iHeight/iWidth*rsize < (lbHeight-10)
     * Exif apply: calculate by formula: iHeight/iWidth*rsize < (lbHeight-80-10)
     * @param imageFile
     * @param lbWidth
     * @param lbHeight
     * @param applyExif
     * @return
     */
    public static int getFixedPreviewSize(File imageFile, int lbHeight, boolean applyExif) {
        int rsize = 0;
        ImageIcon image = new ImageIcon(imageFile.getPath());
        int iHeight = image.getIconHeight();
        int iWidth = image.getIconWidth();
        image = null;
        if (iHeight > iWidth) {
            if (!applyExif) {
                rsize = lbHeight - 10;
            } else {
                rsize = lbHeight - 90;
            }
        } else {
            if (!applyExif) {
                rsize = (lbHeight - 10) * iWidth / iHeight;
            } else {
                rsize = (lbHeight - 90) * iWidth / iHeight;
            }
        }
        return rsize;
    }
}