在网上找,发现都是不全的,要么没获取图片路径,要么没后台等等,今天就来个全的

一:总体步骤

=》页面上传图片

=》获取上传图片剪切的四个值x,y,w,h

=》后天进行剪切

接下来就开始飞吧

页面上传图片,这个大家都懂的

  1. <form action="bookpicture.action" enctype="multipart/form-data" method="post">&nbsp;&nbsp;
  2. <input type="file" name="picture" value="选择图片" />
  3. <input type="submit" value="点击上传" />
  4. </form>

先看看配置文件,这个应该也没问题的

  1. <action name="bookpicture" class="com.ggxytxh.action.PictureAction" method="uploadPicture">
  2. <!-- 动态设置Action的属性值 -->
  3. <param name="path">/img/book_picture</param>
  4. <!-- 配置Struts 2默认的视图页面 -->
  5. <result name="bookpicture">/picture.jsp</result>
  6. <result name="error">/fial.jsp</result>
  7. </action>

上传后,后面的action处理,这些

  1. /******************* 上传图片 *******************/
  2. public String uploadPicture() {
  3. File file = new File(getPath(), pictureFileName);
  4. request.setAttribute("path", path.substring(1) + "/" + pictureFileName);// 页面读取,不知为啥,路径请前多多了“/”
  5. act.getSession().put("bookName", pictureFileName);//等下剪切还要用到名
  6. try {
  7. FileUtils.copyFile(picture, file);
  8. if (!file.getParentFile().exists()) {
  9. file.getParentFile().mkdirs();
  10. }
  11. FileUtils.copyFile(picture, file);
  12. } catch (IOException e) {
  13.  
  14. return ERROR;
  15. }
  16. return "bookpicture";
  17. }

接下来就是大招,开始剪切咯

导入jcrop,这个东西自己找下吧,有心的都会找到

  1. <script src="./js/jquery-1.7.2.min.js" type="text/javascript"></script>
  2. <script src="./js/jquery.Jcrop.js" type="text/javascript"></script>
  3.  
  4. <link rel="stylesheet" href="./css/jquery.Jcrop.css" type="text/css" />

接着就是剪切了,这里也有两个地方可能会有问题的,

  1. <img onerror="this.style.display='none'"id="edit" src="${path}"/>这个路径,网上很多人都没说怎么获取,其实也很简单
    就把它放到request中
  1. request.setAttribute("path", path.substring(1) + "/" + pictureFileName);// 页面读取,不知为啥,路径请前多多了“/”
  1. <div class="one">
  2. <img onerror="this.style.display='none'"
  3. id="edit" src="${path}"/>
  4. </div>
  5.  
  6. <div class="two" style="width: 150px;height: 200px;">
  7. <img onerror="this.style.display='none'"
  8. id="preview" src="${path}"/>
  9. </div>
  10.  
  11. <script type="text/javascript">
  12. jQuery(function($) {
  13. // Create variables (in this scope) to hold the API and image size
  14. var jcrop_api, boundx, boundy;
  15. $('#edit').Jcrop({
  16. onChange : updatePreview,//调用各个事件
  17. onSelect : updatePreview,
  18. aspectRatio : 3/4//规定选择区域比例
  19. }, function() {
  20. var bounds = this.getBounds();
  21. boundx = bounds[0];
  22. boundy = bounds[1];
  23. jcrop_api = this;
  24. });
  25. function updatePreview(c) {
  26. if (parseInt(c.w) > 0) {
  27. //计算预览区域图片缩放的比例,通过计算显示区域的宽度(与高度)与剪裁的宽度(与高度)之比得到
  28. var rx = 150 / c.w;//rx=$('#preview').value/ c.w
  29. var ry = 200 / c.h;
  30. $('#preview').css({
  31. width : Math.round(rx * boundx) + 'px',
  32. height : Math.round(ry * boundy) + 'px',
  33. marginLeft : '-' + Math.round(rx * c.x) + 'px',
  34. marginTop : '-' + Math.round(ry * c.y) + 'px',
  35. });
  36. $('#width').val(c.w); //c.w 裁剪区域的宽
  37. $('#height').val(c.h); //c.h 裁剪区域的高
  38. $('#x').val(c.x); //c.x 裁剪区域左上角顶点相对于图片左上角顶点的x坐标
  39. $('#y').val(c.y); //c.y 裁剪区域顶点的y坐标
  40. }
  41. };
  42. });
  43. </script>
  44.  

<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>

再看看配置,也没什么的

  1. <action name="savePicture" class="com.ggxytxh.action.PictureAction" method="cutPicture">
  2.  
  3. <result name="cutpicture">/addbook.jsp</result>
  4. <result name="error">/fial.jsp</result>
  5. </action>

到这里为止应该大家都会的

很多人就因为接下来的被卡住了

我也不例外

  1. public class PictureAction extends ActionSupport {
  2. private File picture;
  3. private String pictureFileName;
  4. private String path;// 上传路径
  5. private float x;//用了int的
  6. private float y;
  7. private float width;
  8. private float height;

之前一直用int给四个值,一直出错,

问了很多人,都说路径错误,但是路径的确没错,他们没有给正确答案的我的,后来就把

<input type="hidden" name="x" id="x"/> 改了<input type="text" name="y" id="y"/>,发现数据是小数,突然醒悟

改为float就解决问题了

 

最后就是后台进行剪切啦

  1. /******************* 剪切图片 *******************/
  2.  
  3. public String cutPicture() {
  4. String savePath;// 最终保存路径
  5. String picName = null;
  6. Image img;
  7. ImageFilter cropFilter;
  8. //获取上传的文件名
  9. pictureFileName=(String) act.getSession().get("bookName");
  10. path = "D:\\Wed\\Tomcat 8.0\\webapps\\Library\\img\\book_picture\\"+ pictureFileName;
  11. File picFile=new File(path);
  12. try {
  13. // 读取源图像
  14. BufferedImage bi = (BufferedImage)ImageIO.read(picFile);
  15. int srcWidth = bi.getWidth(); // 源图宽度
  16. int srcHeight = bi.getHeight(); // 源图高度
  17. //剪切
  18. Image image = bi.getScaledInstance(srcWidth, srcHeight,Image.SCALE_DEFAULT);
  19. cropFilter = new CropImageFilter((int)x, (int)y, (int)width, (int)height);
  20. img = Toolkit.getDefaultToolkit().createImage(new FilteredImageSource(image.getSource(), cropFilter));
  21. BufferedImage tag = new BufferedImage((int)width, (int)height,BufferedImage.TYPE_INT_RGB);
  22. Graphics g = tag.getGraphics();
  23. g.drawImage(img, 0, 0, null); // 绘制缩小后的图
  24. g.dispose();
  25. // 产生图片前的文件夹
  26. SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");
  27. //设置保存保存路径
  28. savePath = "D:\\img\\book_picture\\"
  29. + formatter.format(new Date()).toString();
  30. File file = new File(savePath);
  31. if (!file.exists()) {
  32. file.mkdirs();// 多级目录,不能用mkdir()
  33. }
  34. //查找该路径下的最大文件名,i+1用于生产下一个文件名
  35. if(file.list().length>0){
  36. Arrays.sort(file.list());
  37. picName=file.list()[file.list().length-1];
  38. picName=String.valueOf((Integer.parseInt(picName.substring(0, picName.length()-4))+1));
  39. System.out.println(picName);
  40. }else {
  41. picName=1+"";
  42. }
  43. // 整个路径包含文件名放在request中
  44. request.setAttribute("bookpath", savePath+"\\"+picName+".jpg");
  45. // 用于保存的图片,生成
  46. ImageIO.write(tag, "JPEG", new File(savePath+"\\"+picName+".jpg"));
  47. //删除原来文件
  48. picFile.delete();
  49. //清除session
  50. act.getSession().remove("bookName");
  51. } catch (IOException e) {
  52. e.printStackTrace();
  53. return ERROR;
  54. }/* */
  55. return "cutpicture";

本人菜鸟,大家共同学习学习,有不好地方求大神指点指点!

2015-05-09

Jcrop+strut2+jsp实现图片剪切的更多相关文章

  1. jQuery Jcrop API参数说明(中文版)(转)(图片剪切)

    Jcrop是一个jQuery图片裁剪插件,它能为你的WEB应用程序快速简单地提供图片裁剪的功能.特点如下: 对所有图片均unobtrusively(无侵入的,保持DOM简洁) 支持宽高比例锁定 支持 ...

  2. 图片上传,图片剪切jquery.imgareaselect

    ---恢复内容开始--- <%@ page language="java" contentType="text/html; charset=UTF-8" ...

  3. iOS开发UI篇—Quartz2D使用(图片剪切)

    iOS开发UI篇—Quartz2D使用(图片剪切) 一.使用Quartz2D完成图片剪切 1.把图片显示在自定义的view中 先把图片绘制到view上.按照原始大小,把图片绘制到一个点上. 代码: - ...

  4. 【iOS】Quartz2D图片剪切

    一.使用Quartz2D完成图片剪切1.把图片显示在自定义的view中 先把图片绘制到view上.按照原始大小,把图片绘制到一个点上. 代码: - (void)drawRect:(CGRect)rec ...

  5. android——拍照,相册图片剪切其实就这么简单

    接触android这么久了.还没有真正的浩浩看看android拍照,相册图片剪切到底是怎么回事,每次都是从别人的代码一扣,就过来了.其实,谷歌提供的API已经很强大.只需要用的好,就那么几句就可以搞定 ...

  6. 图片剪切之Croppic插件

    前几天做图片上传时需要进行图片的剪切和缩放,上网查找时找到了这个插件.样式很好看,功能也很OK.但是网上都是php进行后台处理图片的例子,然后只好慢慢琢磨C#的处理.插件地址是:http://www. ...

  7. 用JavaScript实现图片剪切效果

    学会如何获取鼠标的坐标位置以及监听鼠标的按下.拖动.松开等动作事件,从而实现拖动鼠标来改变图片大小. 还可以学习css中的clip属性. 一.CSS实现图片不透明及裁剪效果. 图片剪切三层结构 1.第 ...

  8. php imagemagick 处理 图片剪切、压缩、合并、插入文本、背景色透明

    php有一款插件叫做imagemagick,功能很强大,提供了图片的很多操作,图片剪切.压缩.合并.插入文本.背景色透明等.并且有api方法调用和命令行操作两种方式,如果只是简单处理的话建议api方法 ...

  9. opencv 图片剪切

    import cv2 as cv import numpy as np # 图片剪切 img = cv.imread('../images/moon.jpg', flags=1) # flags=1读 ...

随机推荐

  1. Linux下安装Python-3.3.2【转】

    # 下载最新版本 cd /usr/local/src/ sudo wget http://www.python.org/ftp/python/3.3.2/Python-3.3.2.tar.bz2 su ...

  2. YTU 2605: 熟悉题型——自由设计(比较大小-类模板)

    2605: 熟悉题型--自由设计(比较大小-类模板) 时间限制: 1 Sec  内存限制: 128 MB 提交: 125  解决: 107 题目描述 声明一个类模板,利用它分别实现两个整数.浮点数和字 ...

  3. 《c程序设计语言》读书笔记--字符串复制

    #include <stdio.h> #define MAXLINE 10 int getLine(char s[], int lim); void copy(char to[], cha ...

  4. MySQL工具:管理员必备的10款MySQL工具

    MySQL是一个复杂的的系统,需要许多工具来修复,诊断和优化它.幸运的是,对于管理员,MySQL已经吸引了很多软件开发商推出高品质的开源工具来解决MySQL的系统的复杂性,性能和稳定性,其中大部分是免 ...

  5. 运行所有sdk目录下的示例,查看它们的功能,方便以后查寻

    运行所有sdk目录下的示例,查看它们的功能,方便以后查寻

  6. MongoDB 学习笔记(五)索引

    http://www.cnblogs.com/stephen-liu74/archive/2012/08/01/2561557.html

  7. ADB安装应用报错 Segmentation fault pm install /data...

    路径一定不能有中文…… 路径一定不能有中文…… 路径一定不能有中文…… 路径一定不能有中文…… 路径一定不能有中文……

  8. JS 原型链学习总结

    废话篇: 在js的学习过程中有一大难点就是原型链.学习的时候一直对这一内容不是十分的明白.纠结的我简直难受.,幸好总算给他弄通了,哇咔咔,总算可以不用在睡梦中还想着他了. 正文篇: 要了解原型链我们首 ...

  9. eclipse有生成不带参数的构造方法的快捷键吗

    你打上类名的2个字母,然后”alt“ +“/” 基本上选第一个就行了

  10. Core Text

    Core Text 本文所涉及的代码你可以在这里下载到 https://github.com/kejinlu/CTTest,包含两个项目,一个Mac的NSTextView的测试项目,一个iOS的Cor ...