Easily scale image in Java

提供一個簡單的類別來更改圖片大小。

/**
 * Image utility. 
 * 
 * @author Joseph Kuo
 * @since
 */
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.awt.image.ColorModel;
import java.awt.image.WritableRaster;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

public class ImageUtils {
  public static final String JPEG_SUFFIX_1 = "jpg";

  public static final String JPEG_SUFFIX_2 = "jpeg";
  
  public static final String PNG_SUFFIX = "png";

  public static final String GIF_SUFFIX = "gif";

  public static final String[] IMAGE_SUFFIXES = {
    PNG_SUFFIX, GIF_SUFFIX, JPEG_SUFFIX_1, JPEG_SUFFIX_2
  };

  // Check if the name of the given file has image format suffix. 
  public static boolean isImage(File file) {
    return file != null && isImageFilename(file.getName());
  }

  // Check if the given name has image format suffix. 
  public static boolean isImageFilename(String filename) {
    if (filename == null || filename.length() < 1) {
      return false;
    }
    // Check if it has ''.''
    int index = filename.lastIndexOf(".");
    if (-1 == index) {
      return false;
    }

    // Check the extension. 
    String filenameExt = filename.substring(index + 1);
    for (String ext : IMAGE_SUFFIXES) {
      if (ext.equalsIgnoreCase(filenameExt)) {
        return true;
      }
    }
    return false;
  }
  
  // width: target width, height: target height.
  public static boolean scaleImage(File source, File target, int width, int height) {
    // Check if source is an image and readable.
    if (!isImage(source)) {
      return false;
    }
    BufferedImage bImage = null;
    try {
      bImage = ImageIO.read(source);
    } catch (IOException ex) {
    }
    if (null == bImage) {
      return false;
    }

    // Set right width and height.
    int sWidth = bImage.getWidth();
    int sHeight = bImage.getHeight();
    if (width <= 0 && height <= 0) {
      width = sWidth;
      height = sHeight;
    } else if (width <= 0) {
      width = (int) ((double) sWidth * height / sHeight); 
    } else if (height <= 0) {
      height = (int) ((double) sHeight * width / sWidth); 
    }

    // Prepare the target image environment.
    BufferedImage bImage2 = null;
    if (bImage.getType() != BufferedImage.TYPE_CUSTOM) {
      bImage2 = new BufferedImage(width, height, bImage.getType());
      Graphics2D g2d = (Graphics2D) bImage2.getGraphics();
      g2d.scale((double) width / sWidth, (double) height / sHeight);
      g2d.drawImage(bImage, 0, 0, null);
      g2d.dispose();
    } else {
      ColorModel cm = bImage.getColorModel();
      WritableRaster raster = cm.createCompatibleWritableRaster(width, height);
      boolean alphaPremultiplied = cm.isAlphaPremultiplied();
      bImage2 = new BufferedImage(cm, raster, alphaPremultiplied, null);
    }

    try {
      // Get image format. 
      String formatName = source.getName().substring(source.getName().indexOf("."));
      // We can not save a file as ''gif'', so we save it as ''png''.
      if (GIF_SUFFIX.equalsIgnoreCase(formatName)) {
        formatName = PNG_SUFFIX;
      }
      // And we also choose the right JPEG format ''jpeg'' instand of ''jpg''
      if (!PNG_SUFFIX.equalsIgnoreCase(formatName)) {
        formatName = JPEG_SUFFIX_2;
      }
      ImageIO.write(bImage2, formatName, target);
    } catch (IOException ex) {
      return false;
    }
    return true;
  }
}

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *

16 + 17 =

返回頂端