/**
* 裁剪图片的方法
*
* @param request
* @param response
* @return
* @author julong 2013-8-26 下午06:01:45
*/
public ModelAndView cutImg(HttpServletRequest request,HttpServletResponse response){
//获取smid
String smid = request.getParameter("smid");
//获得x裁剪的位置
String x = request.getParameter("x");
//获得y裁剪的位置
String y = request.getParameter("y");
//获得width选中的大小
String w = request.getParameter("w");
//获得height选中的大小
String h = request.getParameter("h");
//获取原图片对象
PhotoTemp2 photoTemp2 = this.photoTemp2Service.getZp(smid);
//获取图片对象
byte[] imgBtye = photoTemp2.getZp();
try {
//获取输出流
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(imgBtye);
//将byte数组转换为image对象
BufferedImage bufferedImage=ImageIO.read(byteArrayInputStream);
//获得当前的图片的高度和宽度
int thisHeight = bufferedImage.getHeight();
int thisWidth = bufferedImage.getWidth();
//获得裁剪的图片的高度和宽度 x y为裁剪的其实位置 w h 为裁剪的图片大小
BufferedImage subImage = bufferedImage.getSubimage(Integer.valueOf(x), Integer.valueOf(y), Integer.valueOf(w), Integer.valueOf(h));
//判断是否等于原图的大
if(subImage.getWidth()>=0 || subImage.getHeight()>=0){
//设置绘制的图片的大小
BufferedImage tempImg = new BufferedImage(Integer.valueOf(w),Integer.valueOf(h),BufferedImage.TYPE_INT_RGB);
//绘制图片
tempImg.getGraphics().drawImage(subImage.getScaledInstance(Integer.valueOf(w), Integer.valueOf(h), Image.SCALE_DEFAULT), 0, 0,null); //创建字节输入流
ByteArrayOutputStream byteArrayOutputStream =new ByteArrayOutputStream();
ImageIO.write(tempImg, "JPEG", byteArrayOutputStream);
//获取新图片的数组
byte[] newImg = byteArrayOutputStream.toByteArray();
photoTemp2.setZp(newImg);
//执行更新操作
ExcuteResult r = this.photoTemp2Service.updateDrvPhotoTempZP(photoTemp2);
System.out.println(r);
}
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
request.setAttribute("smid",smid);
return new ModelAndView("dagl/lsda_drv_cutImg");
}
jsp裁剪页面 <%@ page language="java"  pageEncoding="gbk"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>My JSP 'lsda_drv_cutImg.jsp' starting page</title>

<meta http-equiv="pragma" content="no-cache"/>
<meta http-equiv="cache-control" content="no-cache"/>
<meta http-equiv="expires" content="0"/>
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3"/>
<meta http-equiv="description" content="This is my page"/>
<link rel="stylesheet" href="jquery/jcrop/css/jquery.Jcrop.css" type="text/css" />
<script type="text/javascript" src="jquery/jquery-1.7.2.min.js"></script>
<script type="text/javascript" src="jquery/jcrop/js/jquery.Jcrop.min.js"></script>
<script type="text/javascript" src="jquery/jcrop/js/jquery.Jcrop.js"></script>

<script language="Javascript"> // Remember to invoke within jQuery(window).load(...)
// If you don't, Jcrop may not initialize properly
jQuery(document).ready(function(){ jQuery('#cropbox').Jcrop({
onChange: showCoords,
onSelect: showCoords
});

}); // Our simple event handler, called from onChange and onSelect
// event handlers, as per the Jcrop invocation above
function showCoords(c)
{
jQuery('#x').val(c.x);
jQuery('#y').val(c.y);
jQuery('#x2').val(c.x2);
jQuery('#y2').val(c.y2);
jQuery('#w').val(c.w);
jQuery('#h').val(c.h);
};
//单击裁剪的事件
function cutButton(){
$("#cutImgForm").submit();
}

function getEventType(event) {
//opener 属性非常有用,创建的窗口可以引用创建它的窗口所定义的属性和函数
window.opener.cutImgPageClosed();
window.opener = null;
}

</script>
<style type="text/css">
div,form,label{
padding:0px;margin:0px;
}
</style>
</head> <body οnunlοad="getEventType(event)">
<div style="padding:0px;text-align:center; " >
<form id="cutImgForm" action="drvphototemp.dagl?method=cutImg&smid=${smid }" method="post" >
<label>X1 <input type="text" size="4" id="x" name="x" /></label>
<label>Y1 <input type="text" size="4" id="y" name="y" /></label>
<label>X2 <input type="text" size="4" id="x2" name="x2" /></label>
<label>Y2 <input type="text" size="4" id="y2" name="y2" /></label>
<label>W <input type="text" size="4" id="w" name="w" /></label>
<label>H <input type="text" size="4" id="h" name="h" /></label>
<input type="button" name="" value="提交" οnclick="cutButton()"></input>
</form>
<img src="drvphototemp.dagl?method=getZpList&smid=${smid}" id="cropbox" />
</div>

</body>
</html>
我用的jcrop插件

这个你在网上搜索一下就能出来很方便的东西 这是我做的项目中的代码 数据库中存储的是blob格式的16进制文件 亲测裁剪成功了 分享给大家 希望对你有所帮助