Jcrop+strut2+jsp实现图片剪切
在网上找,发现都是不全的,要么没获取图片路径,要么没后台等等,今天就来个全的
一:总体步骤
=》页面上传图片
=》获取上传图片剪切的四个值x,y,w,h
=》后天进行剪切
接下来就开始飞吧
页面上传图片,这个大家都懂的
<form action="bookpicture.action" enctype="multipart/form-data" method="post">
<input type="file" name="picture" value="选择图片" />
<input type="submit" value="点击上传" />
</form>
先看看配置文件,这个应该也没问题的
<action name="bookpicture" class="com.ggxytxh.action.PictureAction" method="uploadPicture">
<!-- 动态设置Action的属性值 -->
<param name="path">/img/book_picture</param>
<!-- 配置Struts 2默认的视图页面 -->
<result name="bookpicture">/picture.jsp</result>
<result name="error">/fial.jsp</result>
</action>
上传后,后面的action处理,这些
/******************* 上传图片 *******************/
public String uploadPicture() {
File file = new File(getPath(), pictureFileName);
request.setAttribute("path", path.substring(1) + "/" + pictureFileName);// 页面读取,不知为啥,路径请前多多了“/”
act.getSession().put("bookName", pictureFileName);//等下剪切还要用到名
try {
FileUtils.copyFile(picture, file);
if (!file.getParentFile().exists()) {
file.getParentFile().mkdirs();
}
FileUtils.copyFile(picture, file);
} catch (IOException e) { return ERROR;
}
return "bookpicture";
}
接下来就是大招,开始剪切咯
导入jcrop,这个东西自己找下吧,有心的都会找到
<script src="./js/jquery-1.7.2.min.js" type="text/javascript"></script>
<script src="./js/jquery.Jcrop.js" type="text/javascript"></script> <link rel="stylesheet" href="./css/jquery.Jcrop.css" type="text/css" />
接着就是剪切了,这里也有两个地方可能会有问题的,
<img onerror="this.style.display='none'"id="edit" src="${path}"/>这个路径,网上很多人都没说怎么获取,其实也很简单
就把它放到request中
request.setAttribute("path", path.substring(1) + "/" + pictureFileName);// 页面读取,不知为啥,路径请前多多了“/”
<div class="one">
<img onerror="this.style.display='none'"
id="edit" src="${path}"/>
</div> <div class="two" style="width: 150px;height: 200px;">
<img onerror="this.style.display='none'"
id="preview" src="${path}"/>
</div> <script type="text/javascript">
jQuery(function($) {
// Create variables (in this scope) to hold the API and image size
var jcrop_api, boundx, boundy;
$('#edit').Jcrop({
onChange : updatePreview,//调用各个事件
onSelect : updatePreview,
aspectRatio : 3/4//规定选择区域比例
}, function() {
var bounds = this.getBounds();
boundx = bounds[0];
boundy = bounds[1];
jcrop_api = this;
});
function updatePreview(c) {
if (parseInt(c.w) > 0) {
//计算预览区域图片缩放的比例,通过计算显示区域的宽度(与高度)与剪裁的宽度(与高度)之比得到
var rx = 150 / c.w;//rx=$('#preview').value/ c.w
var ry = 200 / c.h;
$('#preview').css({
width : Math.round(rx * boundx) + 'px',
height : Math.round(ry * boundy) + 'px',
marginLeft : '-' + Math.round(rx * c.x) + 'px',
marginTop : '-' + Math.round(ry * c.y) + 'px',
});
$('#width').val(c.w); //c.w 裁剪区域的宽
$('#height').val(c.h); //c.h 裁剪区域的高
$('#x').val(c.x); //c.x 裁剪区域左上角顶点相对于图片左上角顶点的x坐标
$('#y').val(c.y); //c.y 裁剪区域顶点的y坐标
}
};
});
</script>
<div style="position:absolute;right:5px;bottom:5px;">
<form action="savePicture.action" method="post">
点击
<%-- <input type="hidden" name="pictureFileName" value="${pictureFileName}"> --%>
<input type="hidden" name="x" id="x"/>
<input type="hidden" name="y" id="y"/>
<input type="hidden" name="width" id="width"/>
<input type="hidden" name="height" id="height"/>
<input type="submit" value="确定" />
,设置完成。
</form>
</div>
再看看配置,也没什么的
<action name="savePicture" class="com.ggxytxh.action.PictureAction" method="cutPicture">
<result name="cutpicture">/addbook.jsp</result>
<result name="error">/fial.jsp</result>
</action>
到这里为止应该大家都会的
很多人就因为接下来的被卡住了
我也不例外
public class PictureAction extends ActionSupport {
private File picture;
private String pictureFileName;
private String path;// 上传路径
private float x;//用了int的
private float y;
private float width;
private float height;
之前一直用int给四个值,一直出错,

问了很多人,都说路径错误,但是路径的确没错,他们没有给正确答案的我的,后来就把
<input type="hidden" name="x" id="x"/> 改了<input type="text" name="y" id="y"/>,发现数据是小数,突然醒悟
改为float就解决问题了
最后就是后台进行剪切啦
/******************* 剪切图片 *******************/
public String cutPicture() {
String savePath;// 最终保存路径
String picName = null;
Image img;
ImageFilter cropFilter;
//获取上传的文件名
pictureFileName=(String) act.getSession().get("bookName");
path = "D:\\Wed\\Tomcat 8.0\\webapps\\Library\\img\\book_picture\\"+ pictureFileName;
File picFile=new File(path);
try {
// 读取源图像
BufferedImage bi = (BufferedImage)ImageIO.read(picFile);
int srcWidth = bi.getWidth(); // 源图宽度
int srcHeight = bi.getHeight(); // 源图高度
//剪切
Image image = bi.getScaledInstance(srcWidth, srcHeight,Image.SCALE_DEFAULT);
cropFilter = new CropImageFilter((int)x, (int)y, (int)width, (int)height);
img = Toolkit.getDefaultToolkit().createImage(new FilteredImageSource(image.getSource(), cropFilter));
BufferedImage tag = new BufferedImage((int)width, (int)height,BufferedImage.TYPE_INT_RGB);
Graphics g = tag.getGraphics();
g.drawImage(img, 0, 0, null); // 绘制缩小后的图
g.dispose();
// 产生图片前的文件夹
SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");
//设置保存保存路径
savePath = "D:\\img\\book_picture\\"
+ formatter.format(new Date()).toString();
File file = new File(savePath);
if (!file.exists()) {
file.mkdirs();// 多级目录,不能用mkdir()
}
//查找该路径下的最大文件名,i+1用于生产下一个文件名
if(file.list().length>0){
Arrays.sort(file.list());
picName=file.list()[file.list().length-1];
picName=String.valueOf((Integer.parseInt(picName.substring(0, picName.length()-4))+1));
System.out.println(picName);
}else {
picName=1+"";
}
// 整个路径包含文件名放在request中
request.setAttribute("bookpath", savePath+"\\"+picName+".jpg");
// 用于保存的图片,生成
ImageIO.write(tag, "JPEG", new File(savePath+"\\"+picName+".jpg"));
//删除原来文件
picFile.delete();
//清除session
act.getSession().remove("bookName");
} catch (IOException e) {
e.printStackTrace();
return ERROR;
}/* */
return "cutpicture";
本人菜鸟,大家共同学习学习,有不好地方求大神指点指点!
2015-05-09
Jcrop+strut2+jsp实现图片剪切的更多相关文章
- jQuery Jcrop API参数说明(中文版)(转)(图片剪切)
Jcrop是一个jQuery图片裁剪插件,它能为你的WEB应用程序快速简单地提供图片裁剪的功能.特点如下: 对所有图片均unobtrusively(无侵入的,保持DOM简洁) 支持宽高比例锁定 支持 ...
- 图片上传,图片剪切jquery.imgareaselect
---恢复内容开始--- <%@ page language="java" contentType="text/html; charset=UTF-8" ...
- iOS开发UI篇—Quartz2D使用(图片剪切)
iOS开发UI篇—Quartz2D使用(图片剪切) 一.使用Quartz2D完成图片剪切 1.把图片显示在自定义的view中 先把图片绘制到view上.按照原始大小,把图片绘制到一个点上. 代码: - ...
- 【iOS】Quartz2D图片剪切
一.使用Quartz2D完成图片剪切1.把图片显示在自定义的view中 先把图片绘制到view上.按照原始大小,把图片绘制到一个点上. 代码: - (void)drawRect:(CGRect)rec ...
- android——拍照,相册图片剪切其实就这么简单
接触android这么久了.还没有真正的浩浩看看android拍照,相册图片剪切到底是怎么回事,每次都是从别人的代码一扣,就过来了.其实,谷歌提供的API已经很强大.只需要用的好,就那么几句就可以搞定 ...
- 图片剪切之Croppic插件
前几天做图片上传时需要进行图片的剪切和缩放,上网查找时找到了这个插件.样式很好看,功能也很OK.但是网上都是php进行后台处理图片的例子,然后只好慢慢琢磨C#的处理.插件地址是:http://www. ...
- 用JavaScript实现图片剪切效果
学会如何获取鼠标的坐标位置以及监听鼠标的按下.拖动.松开等动作事件,从而实现拖动鼠标来改变图片大小. 还可以学习css中的clip属性. 一.CSS实现图片不透明及裁剪效果. 图片剪切三层结构 1.第 ...
- php imagemagick 处理 图片剪切、压缩、合并、插入文本、背景色透明
php有一款插件叫做imagemagick,功能很强大,提供了图片的很多操作,图片剪切.压缩.合并.插入文本.背景色透明等.并且有api方法调用和命令行操作两种方式,如果只是简单处理的话建议api方法 ...
- opencv 图片剪切
import cv2 as cv import numpy as np # 图片剪切 img = cv.imread('../images/moon.jpg', flags=1) # flags=1读 ...
随机推荐
- 解决:Eclipse调试的时候报错'Launching XXX' has encountered a problem. Cannot connect to VM.
问题如下图: 原因:开了代理(我使用的是Proxifier)如图 解决方案:关闭Proxifier即可
- 大规模视觉识别挑战赛ILSVRC2015各团队结果和方法 Large Scale Visual Recognition Challenge 2015
Large Scale Visual Recognition Challenge 2015 (ILSVRC2015) Legend: Yellow background = winner in thi ...
- jquery Deferred demo
<!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- Docker —— 用于统一开发和部署的轻量级 Linux 容器【转】
转自:http://www.oschina.net/translate/docker-lightweight-linux-containers-consistent-development-and-d ...
- urllib.request ProxyHandler
import urllib.request proxy_support = urllib.request.ProxyHandler({}) opener = urllib.request.build_ ...
- gulp使用外部配置文件
这很有好处,因为它使得任务很干净,并且 config.json 可以被其他的任务运行器(例如grunt)重复利用. config.json { "desktop" : { &quo ...
- HeadFirst Jsp 07 (使用 jsp)
Jsp 变成 Servlet, 容器会查看你的JSP, 把它转换成java源代码, 再编译成完整的Java servlet类. Jsp 不需要你的编译, 容器会自动替换成servlet. 在 jsp中 ...
- document.body.innerHTML用jquery如何表示
$("body").html('XXXX'); //这个是赋值 $("body").html(); //这个是获取HTML的内容
- 【笨嘴拙舌WINDOWS】实践检验之剪切板查看器【Delphi】
该程序能够监视Windows剪切板的内容(文字和图片) 其思路是 先调用SetClipBoardViewer(Self.Handle),让Windows剪切板内容发生改变之后,通知本程序: 然后截获W ...
- UVa 1395 Slim Span【最小生成树】
题意:给出n个节点的图,求最大边减最小边尽量小的值的生成树 首先将边排序,然后枚举边的区间,判定在该区间内是否n个点连通,如果已经连通了,则构成一颗生成树, 则此时的苗条度是这个区间内最小的(和kru ...