下面列出了怎么用java.awt.image.CropImageFilter的API类实例代码及写法,或者点击链接到github查看源代码。
/**
* 图像切割(指定切片的宽度和高度)
* @param bi 原图像
* @param x 裁剪原图像起点坐标X
* @param y 裁剪原图像起点坐标Y
* @param width 目标切片宽度
* @param height 目标切片高度
* @return
*/
public static BufferedImage cut(BufferedImage bi,int x, int y, int width, int height) {
BufferedImage tag = new BufferedImage(width, height,
BufferedImage.TYPE_INT_RGB);
Graphics2D g2 = tag.createGraphics();
tag = g2.getDeviceConfiguration().createCompatibleImage(width, height,
Transparency.TRANSLUCENT);
g2.dispose();
g2 = tag.createGraphics();
int srcWidth = bi.getHeight(); // 源图宽度
int srcHeight = bi.getWidth(); // 源图高度
if (srcWidth > 0 && srcHeight > 0) {
ImageFilter cropFilter = new CropImageFilter(x, y, width, height);
Image img = Toolkit.getDefaultToolkit().createImage(
new FilteredImageSource(bi.getSource(),cropFilter));
g2.drawImage(img, 0, 0, width, height, null); // 绘制切割后的图
g2.dispose();
}
return tag;
}
/**
* 图像切割(按指定起点坐标和宽高切割)
* @param srcImg 源图像地址
* @param outImg 切片后的图像地址
* @param x 目标切片起点坐标X
* @param y 目标切片起点坐标Y
* @param width 目标切片宽度
* @param height 目标切片高度
* @param imageType 图片类型
*/
public static void cut(File srcImg, File outImg, int x, int y, int width, int height,String imageType) throws Exception{
//读取源图像
BufferedImage bi = ImageIO.read(srcImg);
int srcWidth = bi.getHeight();//源图宽度
int srcHeight = bi.getWidth();//源图高度
if (srcWidth > 0 && srcHeight > 0) {
Image image = bi.getScaledInstance(srcWidth, srcHeight,Image.SCALE_DEFAULT);
//四个参数分别为图像起点坐标和宽高
//即:CropImageFilter(int x,int y,int width,int height)
ImageFilter cropFilter = new CropImageFilter(x, y, width,height);
Image img = Toolkit.getDefaultToolkit().createImage(new FilteredImageSource(image.getSource(), cropFilter));
BufferedImage tag = new BufferedImage(width, height,BufferedImage.TYPE_INT_RGB);
Graphics g = tag.getGraphics();
g.drawImage(img, 0, 0, width, height, null);//绘制切割后的图
g.dispose();
//输出为文件
ImageIO.write(tag, imageType, outImg);
}
}
/**
* 图像切割(按指定起点坐标和宽高切割)
* @param srcImageFile 源图像地址
* @param result 切片后的图像地址
* @param x 目标切片起点坐标X
* @param y 目标切片起点坐标Y
* @param width 目标切片宽度
* @param height 目标切片高度
*/
public final static void cut(String srcImageFile, String result,
int x, int y, int width, int height) {
try {
// 读取源图像
BufferedImage bi = ImageIO.read(new File(srcImageFile));
int srcWidth = bi.getHeight(); // 源图宽度
int srcHeight = bi.getWidth(); // 源图高度
if (srcWidth > 0 && srcHeight > 0) {
Image image = bi.getScaledInstance(srcWidth, srcHeight,
Image.SCALE_DEFAULT);
// 四个参数分别为图像起点坐标和宽高
// 即: CropImageFilter(int x,int y,int width,int height)
ImageFilter cropFilter = new CropImageFilter(x, y, width, height);
Image img = Toolkit.getDefaultToolkit().createImage(
new FilteredImageSource(image.getSource(),
cropFilter));
BufferedImage tag = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics g = tag.getGraphics();
g.drawImage(img, 0, 0, width, height, null); // 绘制切割后的图
g.dispose();
// 输出为文件
ImageIO.write(tag, "JPEG", new File(result));
}
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main (String[] args) {
new CropImageFilter(20, 20, 40, 50).clone();
}