Currently showing src/net/jtank/image/ImageUtil.java
package net.jtank.image;
import java.awt.Image;
import java.awt.Component;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
import java.io.File;
import java.io.IOException;
import java.awt.MediaTracker;
import java.awt.geom.*;
import java.awt.Dimension;
import java.awt.Point;
import java.awt.Graphics2D;
/**
* Image manipulation methods.
*
* @author William Denniss
* @version 2.4, 22th October 2003
*/
public class ImageUtil {
/**
* Returns a cropped instance of the image. If null is passed as either of the corners
* then nothing is cropped.
*
*@param in The source image
*@param top The top left hand corner of the crop rectangle
*@param bottom The bottom right hand corner of the crop rectangle
*@return the cropped instance of the image
*/
public static BufferedImage cropImage (BufferedImage in, Point top, Point bottom) {
if (top == null || bottom == null)
return in;
Dimension size = new Dimension (bottom.x - top.x , bottom.y - top.y);
BufferedImage cropped = new BufferedImage(size.width, size.height, BufferedImage.TYPE_INT_RGB);
cropped.createGraphics().drawImage(in,0,0,size.width, size.height, top.x, top.y, bottom.x, bottom.y, null);
return cropped;
}
/**
* Reads an image from the disk as a BufferedImage - can be case as an Image
*
*@param filename the image to be read
*@return the image object
*/
public static BufferedImage readImage (String filename) throws IOException {
File imagein = new File (filename);
BufferedImage bi = ImageIO.read(imagein);
return bi;
}
/**
* Loades the passed image into memory.
*
*
*@param toLoad the image that will be loaded into memory
*@param c the Component to be notified
*@param timout the number of milliseconds before loading will be aborted
*@return boolen representing success of operation
*/
public static boolean loadImageNow (Image toLoad, Component c, int timeout) {
MediaTracker tracker = new MediaTracker(c);
tracker.addImage(toLoad, 0);
try {
tracker.waitForAll(timeout);
return true;
} catch (InterruptedException e) {
return false;
}
}
/**
* Scales an Image.
*
* Useful ref: http://saloon.javaranch.com/cgi-bin/ubb/ultimatebb.cgi?ubb=get_topic&f=34&t=002153
*/
public static BufferedImage scaleImage (BufferedImage imageIn, int width, int height) throws IOException {
return scaleImage(imageIn, width, height, Image.SCALE_SMOOTH, false);
}
public static BufferedImage scaleImage (BufferedImage imageIn, int width, int height, int scaleQuality) throws IOException {
return scaleImage(imageIn, width, height, scaleQuality, false);
}
public static BufferedImage scaleImage (BufferedImage imageIn, int width, int height, int scaleQuality, boolean maintainAspect) throws IOException {
if (maintainAspect && width != -1 && height != -1) {
int im_width = imageIn.getWidth(null);
int im_height = imageIn.getHeight(null);
int tmp_w;
int tmp_h;
tmp_h = height;
tmp_w = (int) (( (double) height / (double) im_height) * im_width);
if (tmp_w > width) {
tmp_w = width;
tmp_h = (int) (( (double) width / (double) im_width) * im_height);
}
width = tmp_w;
height = tmp_h;
}
Image imageIn2 = imageIn.getScaledInstance(width, height, scaleQuality);
BufferedImage scaled = new BufferedImage(imageIn2.getWidth(null), imageIn2.getHeight(null), BufferedImage.TYPE_INT_RGB);
scaled.createGraphics().drawImage(imageIn2,0,0,null);
return scaled;
}
/**
* Writes a buffered Image to disk
*
*/
public static void writeImage (BufferedImage toWrite, String fileout) throws IOException {
File f = new File(fileout);
String ext = fileout.substring(fileout.length()-3,fileout.length());
String filetype = "jpeg";
if (ext.equalsIgnoreCase("jpg")) {
filetype = "jpeg";
} else if (ext.equalsIgnoreCase("gif")) {
filetype = "gif";
} else if (ext.equalsIgnoreCase("png")) {
filetype = "png";
}
ImageIO.write( toWrite, filetype, f);
}
public static BufferedImage rotateImage (BufferedImage bi, int rotations) {
rotations = rotations % 4;
int newWidth = bi.getWidth();
int newHeight = bi.getHeight();
int moveX = 0;
int moveY = 0;
if (rotations % 2 != 0) {
newHeight = bi.getWidth();
newWidth = bi.getHeight();
}
if (rotations > 1)
moveY = newHeight;
if (rotations > 0 && rotations < 3)
moveX = newWidth;
BufferedImage rbi = new BufferedImage (newWidth, newHeight, BufferedImage.TYPE_INT_RGB);
Graphics2D g2d = rbi.createGraphics();
AffineTransform af = new AffineTransform ();
af.concatenate(AffineTransform.getTranslateInstance(moveX,moveY));
af.concatenate(AffineTransform.getRotateInstance((rotations*0.5) * Math.PI));
g2d.drawImage((Image) bi, af, null);
return rbi;
}
}
Total 234 Lines of Code.
|
Source code formatted using showsrc by William Denniss
|