function cutImage(){
var list = new Array();
list[0] = $("#x1").val();
list[1] = $("#x2").val();
list[2] = $("#y1").val();
list[3] = $("#y2").val();
list[4] = $("#w").val();
list[5] = $("#h").val();
var currentPath = document.getElementById("currentPath").value;
// 这里是利用的dwr框架直接调用后台方法,以及使用后台传回的值
// 这个方法就是利用坐标宽高进行切图,事实上这时候的原图已经在服务器了,所以我们只需要知道他的相对路径,即currentPath
ReleaseService.cutImage(list, currentPath, function(value){
document.getElementById("currentPath").value = value;
}
);
var bgObj=document.getElementById("bgDiv");
var msgObj=document.getElementById("cutImageDiv");
bgObj.style.display = msgObj.style.display = "none";
piso.cancelSelection();
haveImage = 1;
$('#msHaveImage').show();
//$(".imgareaselect-outer").hide();
//$(".imgareaselect-selection").parent().hide();
}ReleaseService的cutImage方法:
public String cutImage(int[] size, String path) {
int x1 = size[0];
int x2 = size[1];
int y1 = size[2];
int y2 = size[3];
int w = size[4];
int h = size[5];
if(w <= 0)
w = 480;
if(h<=0)
h = 520;
if(x1<0)
x1 = 0;
if(y1<0)
y1 = 0;
//File file = new File(request.getSession().getServletContext().getRealPath(path));
path = path.substring(2);
WebContext ctx = WebContextFactory.get();
User user = (User)ctx.getSession().getAttribute("user");
SimpleDateFormat df = new SimpleDateFormat(BusinessConstants.DATE_FORMAT);
String file_ext = path.substring(path.lastIndexOf(BusinessConstants.DOT) + 1);
File file = new File(ctx.getSession().getServletContext().getRealPath(path));
String imageName = user.getId() + BusinessConstants.UNDERLINE +
df.format(new Date()) + BusinessConstants.DOT + file_ext;
String newFile = ctx.getSession().getServletContext().getRealPath(BusinessConstants.TEMP_PICTURE_PATH + imageName);
//切图操作
ImageCut.abscut(ctx.getSession().getServletContext().getRealPath(path), newFile, x1, y1, w, h);
return BusinessConstants.TEMP_RELATIVE_PICTURE_PATH + imageName;
}来看一下截图方法:主要是Graphics2D的drawImage方法。
public static void abscut(String srcImageFile,String desImageFile, int x, int y,int width, int height) {
try {
Image img;
ImageFilter cropFilter;
File srcFile = new File(srcImageFile);
//String fileName = srcFile.getName();
String ext = getExtension(srcImageFile);
if(ext==null)ext="jpg";
// 读取源图像
BufferedImage bi = ImageIO.read(srcFile);
int srcWidth = bi.getWidth(); // 源图宽度
int srcHeight = bi.getHeight(); // 源图高度
if (srcWidth >= width && srcHeight >= height) {
BufferedImage tag;
Image image = bi.getScaledInstance(srcWidth, srcHeight,Image.SCALE_DEFAULT);
// 四个参数分别为图像起点坐标和宽高
// 即: CropImageFilter(int x,int y,int width,int height)
cropFilter = new CropImageFilter(x, y, width, height);
img = Toolkit.getDefaultToolkit().createImage(new FilteredImageSource(image.getSource(), cropFilter));
int type = BufferedImage.TYPE_INT_RGB;
if("gif".equalsIgnoreCase(ext)||"png".equalsIgnoreCase(ext)){
type = BufferedImage.TYPE_INT_ARGB;
}
tag = new BufferedImage(width, height,type);
Graphics2D g = (Graphics2D)tag.getGraphics();
g.setRenderingHint(RenderingHints.KEY_INTERPOLATION,RenderingHints.VALUE_INTERPOLATION_BILINEAR);
g.drawImage(img, 0, 0, null); // 绘制剪切后的图
g.dispose();
ImageIO.write(tag,ext, new File(desImageFile));
srcFile.delete();//删除原图
}
} catch (Exception e) {
e.printStackTrace();
}
}
public static String getExtension(String srcImageFile) {
String ext = null;
if(srcImageFile!=null && srcImageFile.lastIndexOf(".")>-1){
ext = srcImageFile.substring(srcImageFile.lastIndexOf(".")+1);
}
return ext;
}