之前做一个关于截图的东东,搞了好久终于弄好了,其主要关键是把前端截图的数据(x坐标,y坐标,宽度,高度和旋转角度)传到后台,然后在后台对图片做相关处理,记录一下方便以后查看。

后台配置为ssm。

Java代码:

     /**
* headers="content-type=multipart/*"(必填),avatar_file(name),avatar_data(截图数据)
*/
@RequestMapping(params = "method=picture",headers="content-type=multipart/*",method=RequestMethod.POST)

 6 public @ResponseBody Map picture(@RequestParam("avatar_file")MultipartFile file ,
        String avatar_data,HttpServletRequest request){
Map map=new HashMap<>();
Map resultMap=new HashMap<>();
try { String nodepath = this.getClass().getClassLoader().getResource("/").getPath();//获取项目的绝对路径
String[] str1 = nodepath.split("wtpwebapps");
int empid=(int)request.getSession().getAttribute("empid");
String filename=file.getOriginalFilename();
String prefix = filename.substring((filename.lastIndexOf(".")+1));//获取图片后缀
System.err.println("prefix=="+prefix);
String[] str=avatar_data.split(",");
int x = (int)Math.floor(Double.parseDouble(str[0].split(":")[1]));//获取截取的x坐标
int y = (int)Math.floor(Double.parseDouble(str[1].split(":")[1]));//获取截取的y坐标
int h = (int)Math.floor(Double.parseDouble(str[2].split(":")[1])); //获取截取的高度
int w = (int)Math.floor(Double.parseDouble(str[3].split(":")[1])); //获取截取的宽度
int r = Integer.parseInt(str[4].split(":")[1].replaceAll("}", ""));//获取旋转的角度
BufferedImage cutImage = PersonalCenterController.cutImage(file,x,y,w,h,prefix,str1[0]);
BufferedImage rotateImage = PersonalCenterController.rotateImage(cutImage, r);
ByteArrayOutputStream out = new ByteArrayOutputStream();
boolean flag = ImageIO.write(rotateImage, prefix, out);
byte[] b = out.toByteArray();//转换后存入数据库
map.put("in", b);
map.put("empid", empid);
int i = pcs.Pictureupload(map);
if(i>0){
resultMap.put("result", "success");
}else{
resultMap.put("result", "defeat");
}
} catch (Exception e) {
resultMap.put("result", "error");
}
return resultMap;
} /***
* 剪裁图片
* @param file 图片 * @param dest 路径
* @param x 起点横坐标
* @param y 纵坐标
* @param w 长
* @param h 高
* @throws IOException
* @date
*/
public static BufferedImage cutImage(MultipartFile file,int x,int y,int w,int h,
      String prefix,String dest) { Iterator iterator = ImageIO.getImageReadersByFormatName(prefix);
dest=dest+"/wtpwebapps/pic";//剪切图片需要依赖一个本地路径(空文件夹即可)
System.err.println(dest);
try {
ImageReader reader = (ImageReader)iterator.next();
InputStream in = file.getInputStream();//转换成输入流
ImageInputStream iis = ImageIO.createImageInputStream(in);
reader.setInput(iis, true);
ImageReadParam param = reader.getDefaultReadParam();
Rectangle rect = new Rectangle(x, y, w,h);
param.setSourceRegion(rect);
BufferedImage bi = reader.read(0,param);
ImageIO.write(bi, prefix, new File(dest));
return bi;
} catch (Exception e) {
}
return null;
}
/***
* 图片旋转指定角度
* @param bufferedimage 图像
* @param degree 角度
* @return
* @date
*/
public static BufferedImage rotateImage(BufferedImage bufferedimage, int degree) {
int w = bufferedimage.getWidth();
int h = bufferedimage.getHeight();
int type = bufferedimage.getColorModel().getTransparency();
BufferedImage img;
Graphics2D graphics2d;
(graphics2d = (img = new BufferedImage(w, h, type))
.createGraphics()).setRenderingHint(
RenderingHints.KEY_INTERPOLATION,
RenderingHints.VALUE_INTERPOLATION_BILINEAR);
graphics2d.setPaint(Color.WHITE);
graphics2d.fillRect(0, 0, w, h);
graphics2d.rotate(Math.toRadians(degree), w / 2, h / 2);
graphics2d.drawImage(bufferedimage, 0, 0,Color.WHITE, null);
graphics2d.dispose();
return img;
}

html代码:

在前端页面采用form表单提交的方式,当表单中存在文件时,注意添加 enctype='multipart/form-data'  这么个玩意,主要是用来设置表单的MIME编码。默认情况,这个编码格式是application/x-www-form-urlencoded,不能用于文件上传;只有使用了multipart/form-data,才能完整的传递文件数据。

 1  <div id="crop-avatar">
2 <div class="avatar-view" id="imgshow">
3 <img id="image" src="" /> <!-- 显示图片的路径,我保存在数据库中的,所有填写数据库的查询即可 -->
4
5 </div>
6 <!-- Cropping modal -->
7 <div class="modal fade" id="avatar-modal" aria-hidden="true"
8 aria-labelledby="avatar-modal-label" role="dialog"
9 tabindex="-1">
10 <div class="modal-dialog modal-lg">
11 <div class="modal-content">
12 <form class="avatar-form" action='' enctype='multipart/form-data' method='post' name="form">
    <!-- action填写后台路径,不用说都知道。关键是enctype='multipart/form-data'必须写。 -->
13 <div class="modal-header">
14 <button class="close" data-dismiss="modal" type="button">&times;</button>
15 <h4 class="modal-title" id="avatar-modal-label">更换头像</h4>
16 </div>
17 <div class="modal-body">
18 <div class="avatar-body">
19 <!-- Upload image and data -->
20 <div class="avatar-upload">
21 <input class="avatar-src" name="avatar_src" type="hidden" />
22 <input class="avatar-data" name="avatar_data" type="hidden" />
23 <label for="avatarInput">头像上传</label>
24 <input class="avatar-input" id="avatarInput" name="avatar_file" type="file" />
25
26
27 </div>
28 <!-- Crop and preview -->
29 <div class="row">
30 <div class="col-md-9">
31 <div class="avatar-wrapper" id="avatar-wrapper"></div>
32 </div>
33 <div class="col-md-3">
34 <div class="avatar-preview preview-lg"></div>
35 <div class="avatar-preview preview-md"></div>
36 <div class="avatar-preview preview-sm"></div>
37 </div>
38 </div>
39
40 <div class="row avatar-btns">
41 <div class="col-md-9">
42 <div class="btn-group">
43 <button class="btn btn-primary btn-sm"
44 data-method="rotate" data-option="-90" type="button"
45 title="Rotate -90 degrees">旋转-90度</button>
46 <button class="btn btn-primary btn-sm"
47 data-method="rotate" data-option="-45" type="button">旋转-45度</button>
48 </div>
49 <div class="btn-group">
50 <button class="btn btn-primary btn-sm"
51 data-method="rotate" data-option="90" type="button"
52 title="Rotate 90 degrees">旋转90度</button>
53 <button class="btn btn-primary btn-sm"
54 data-method="rotate" data-option="45" type="button">旋转45度</button>
55 </div>
56 </div>
57 <div class="col-md-3">
58 <button class="btn btn-primary btn-block avatar-save"
59 type="submit" id="btn">完成</button>
60 </div>
61 </div>
62
63 <div class="row avatar-btns">
64 <div class="col-md-12"> </div>
65
66
67 </div>
68 </div>
69 </div>
70 </form>
71 </div>
72 </div>
73 </div>
74 <!-- /.modal -->
75
76 <!-- Loading state -->
77 <div class="loading" aria-label="Loading" role="img"
78   tabindex="-1"></div>
79 </div>

关于bootstrap中cropper的截图上传问题的更多相关文章

  1. 在Asp.Net Core中配置使用MarkDown富文本编辑器实现图片上传和截图上传(开源代码.net core3.0)

    我们的富文本编辑器不能没有图片上传尤其是截图上传,下面我来教大家怎么实现MarkDown富文本编辑器截图上传和图片上传. 1.配置编辑器到html页 <div id="test-edi ...

  2. JS打开摄像头并截图上传

    直入正题,JS打开摄像头并截图上传至后端的一个完整步骤 1. 打开摄像头主要用到getUserMedia方法,然后将获取到的媒体流置入video标签 2. 截取图片主要用到canvas绘图,使用dra ...

  3. 头像截图上传三种方式之一(一个简单易用的flash插件)(asp.net版本)

    flash中有版权声明,不适合商业开发.这是官网地址:http://www.hdfu.net/ 本文参考了http://blog.csdn.net/yafei450225664/article/det ...

  4. Laravel中的日志与上传

    PHP中的框架众多,我自己就接触了好几个.大学那会啥也不懂啥也不会,拿了一个ThinkPHP学了.也许有好多人吐槽TP,但是个人感觉不能说哪个框架好,哪个框架不好,再不好的框架你能把源码读上一遍,框架 ...

  5. Spring中MultipartHttpServletRequest实现文件上传

    Spring中MultipartHttpServletRequest实现文件上传 转贴自:http://my.oschina.net/nyniuch/blog/185266 实现图片上传  用户必须能 ...

  6. ASP.NET中扩展FileUpload的上传文件的容量

    ASP.NET中扩展FileUpload只能上传小的文件,大小在4MB以内的.如果是上传大一点的图片类的可以在web.config里面扩展一下大小,代码如下 <system.web> &l ...

  7. Aps.net中基于bootstrapt图片上传插件的应用

    Aps.net中基于bootstrapt图片上传插件的应用 在最近的项目中需要使用一个图片上传的功能,而且是多张图片同时上传到服务器的文件夹中,将图片路径存放在数据库中.为了外观好看使用了bootst ...

  8. 在MVC应用程序中,怎样删除上传的文件

    在ASP.NET MVC应用程序中,怎样删除上传的文件. 由于上传时,真正文件是存储在应用程序某一目录,在数据库表中,只是存储其基本信息.在删除时,需要注意一下,由于没有事务可操作.Insus.NET ...

  9. windows中修改catalina.sh上传到linux执行报错This file is needed to run this program解决

    windows中修改catalina.sh上传到linux执行报错This file is needed to run this program解决 一.发现问题 由于tomcat内存溢出,在wind ...

随机推荐

  1. Oracle自动备份.bat 最新更新(支持Win10了)

    20170418更新: 很多年没有用了,最近两天打开来用,发现在Win10下面At命令已经被淘汰不能使用了,还有不少命令工作方式和原来也不一样了 所以就更新了一下下,使用 schtasks 命令代替了 ...

  2. RSA加密算法 C++实现

    上信息安全课,老师布置了几个大作业,其中一个为RSA加密算法的实现,不能用Java写.出于兴趣,决定尝试.完成之后,为了便于查找,于是写下这篇文章,以备后续查看.也供大家一起学习,一起进步. 1.预备 ...

  3. libev事件库使用笔记

    源码下载地址:http://dist.schmorp.de/libev/ libev是一个高性能的事件循环库,比libevent库的性能要好. 安装: tar -zxf libev-4.15.tar. ...

  4. Spring Cloud搭建微服务架构----前言

    前言 微服务并不神秘,只是在互联网技术发展过程中的一个产物,整个架构系统随着客户端的多样性,服务越来越多,devops的发展而产生的架构变种. 许多公司,通过采用微处理结构模式解决单体应用的问题,分解 ...

  5. Hibernate启动非常慢问题分析

    项目中使用hibernate3,在启动项目过程中,发现加载显示数据很慢,要多几分钟才显示出数据,没有报其他异常.今天特别慢,过了好久都不加载显示数据. 排查思路有以下几个方面: 1.数据库是否开启.检 ...

  6. 《Vue2.0 实践揭秘》终于出版啦!

    不知不觉间在园子开博都两年多了,最近一些园友问最近去哪了为何都没有新的文章了.最近确实发生了很多的事,一是忙工作二就是忙着写书.这还得多些园子的小编,自两年前发表的"架构师修炼"系 ...

  7. Python全栈之路-Day33

    1 time模块 #!/usr/bin/env python # __Author__: "wanyongzhen" # Date: 2017/4/7 import time # ...

  8. PHP 分支与循环

    一.概述: 上面一章我们讲解了PHP当中的运算符和表达式,通过上面的知识点我们就可以完成一些基本的运算操作了.但是涉及到一些比较复杂的逻辑,分支与循环就必不可少了.通过分支和循环的结合使用可以使业务更 ...

  9. 接口加密《二》: API权限设计总结

    来源:http://meiyitianabc.blog.163.com/blog/static/105022127201310562811897/ API权限设计总结: 最近在做API的权限设计这一块 ...

  10. Vue 项目实战系列 (二)

    上一章节我们已经把项目的初始化工作完成了,接下来我们再来进行具体的代码编写.这一节我们将完成如下的页面. 我们在src/目录下新建一个views文件夹,存放我们的主要页面文件.目录结构如下: cine ...