package com.img.manager;
import java.io.*;
import java.awt.*;
import java.awt.image.*;
import java.awt.Graphics;
import javax.imageio.ImageIO;
public class ImageCut {
/** */
/** */
/** */
/**
* 图像切割
*
* @param srcImageFile
* 源图像地址
* @param x
* 目标切片起点x坐标
* @param y
* 目标切片起点y坐标
* @param destWidth
* 目标切片宽度
* @param destHeight
* 目标切片高度
*/
public static String abscut(String srcImageFile, int x, int y,
int destWidth, int destHeight) {
String _src = "";
try {
Image img;
String s = srcImageFile;
ImageFilter cropFilter;
// 读取源图像
BufferedImage bi = ImageIO.read(new File(srcImageFile));
int srcWidth = bi.getWidth(); // 源图宽度
int srcHeight = bi.getHeight(); // 源图高度
if (srcWidth >= destWidth && srcHeight >= destHeight) {
Image image = bi.getScaledInstance(srcWidth, srcHeight,
Image.SCALE_DEFAULT);
// 四个参数分别为图像起点坐标和宽高
// 即: CropImageFilter(int x,int y,int width,int height)
cropFilter = new CropImageFilter(x, y, destWidth, destHeight);
img = Toolkit.getDefaultToolkit().createImage(
new FilteredImageSource(image.getSource(), cropFilter));
BufferedImage tag = new BufferedImage(destWidth, destHeight,
BufferedImage.TYPE_INT_RGB);
Graphics g1 = tag.getGraphics();
Graphics2D g=(Graphics2D)g1;
g.setRenderingHint(RenderingHints.KEY_INTERPOLATION ,RenderingHints.VALUE_INTERPOLATION_BILINEAR);
g.drawImage(img, 0, 0, null); // 绘制剪切后的图
g.dispose();
// 输出为文件
String[] srcs = null;
try {
srcs = s.split("\\\\");
} catch (RuntimeException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
for (int i = 0; i < srcs.length; i++) {//连接拆分后的URL 重新得到一个URL
if (i == srcs.length - 1) {
srcs[i] = srcs[i].split("\\.")[0] + "cut."
+ srcs[i].split("\\.")[1];
_src = _src + srcs[i];
} else
_src = _src + srcs[i] + "\\";
}
ImageIO.write(tag, "png", new File(_src));
}
} catch (Exception e) {
e.printStackTrace();
}
return _src;
}
/** */
/** */
/** */
/**
* @param args
*/
//
}
这是我的代码 有没有高手帮我看下怎么改改 ,谢谢···