📄 imageutil.java
字号:
package cn.js.fan.util;
import java.awt.image.*;
import java.io.*;
import javax.imageio.*;
/**
* Image utilities class.
*
* @author T55555
* @version 1.0 2003-01-29
*/
public class ImageUtil {
/**
* Return scaled image.
* Pre-conditions: (source != null) && (width > 0) && (height > 0)
*
* @param source the image source
* @param width the new image's width
* @param height the new image's height
* @return the new image scaled
*/
public static BufferedImage getScaleImage(BufferedImage source,
int width, int height) {
//assert(source != null && width > 0 && height > 0);
BufferedImage image = new BufferedImage(width, height, source.getType());
image.createGraphics().drawImage(source, 0, 0, width, height, null);
return image;
}
/**
* Return scaled image.
* Pre-conditions: (source != null) && (xscale > 0) && (yscale > 0)
*
* @param source the image source
* @param xscale the percentage of the source image's width
* @param yscale the percentage of the source image's height
* @return the new image scaled
*/
public static BufferedImage getScaleImage(BufferedImage source,
double xscale, double yscale) {
//assert(source != null && width > 0 && height > 0);
return getScaleImage(source,
(int)(source.getWidth() * xscale), (int)(source.getHeight() * yscale));
}
/**
* Read the input image file, scaled then write the output image file.
*
* @param input the input image file
* @param output the output image file
* @param width the output image's width
* @param height the output image's height
* @return true for sucessful,
* false if no appropriate reader or writer is found.
*/
public static boolean scaleImage(File input, File output,
int width, int height) throws IOException {
BufferedImage image = ImageIO.read(input);
if (image == null) { return false; }
image = getScaleImage(image, width, height);
String name = output.getName();
String format = name.substring(name.lastIndexOf('.')+1).toLowerCase();
return ImageIO.write(image, format, output);
}
/**
* Read the input image file, scaled then write the output image file.
*
* @param input the input image file
* @param output the output image file
* @param xscale the percentage of the input image's width for output image's width
* @param yscale the percentage of the input image's height for output image's height
* @return true for sucessful,
* false if no appropriate reader or writer is found.
*/
public static boolean scaleImage(File input, File output,
double xscale, double yscale) throws IOException {
BufferedImage image = ImageIO.read(input);
if (image == null) { return false; }
image = getScaleImage(image, xscale, yscale);
String name = output.getName();
String format = name.substring(name.lastIndexOf('.')+1).toLowerCase();
return ImageIO.write(image, format, output);
}
/**
* 固定图的宽度,高度按比例变化,如果图的宽度小于指定的宽度,则用其原来的宽度
* @param input File
* @param output File
* @param width int
* @param height int
* @return boolean
* @throws IOException
*/
public static boolean Image2Thumb(File input, File output,
int thumbWidth) throws IOException {
BufferedImage image = ImageIO.read(input);
if (image == null) {
return false;
}
int w = image.getWidth();
int h = image.getHeight();
double dHeight = ((double)thumbWidth)/w * h;
int height = (int)dHeight;
if (w>thumbWidth)
image = getScaleImage(image, thumbWidth, height);
else
image = getScaleImage(image, w, h);
String name = output.getName();
String format = name.substring(name.lastIndexOf('.')+1).toLowerCase();
return ImageIO.write(image, format, output);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -