一、引入js和css

二、实现

1、jsp页面

<%--
Created by IntelliJ IDEA.
User: a
Date: 2019/8/19
Time: 9:36
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
<link rel="stylesheet" type="text/css" href="js/jquery.Jcrop.min.css">
<script type="text/javascript" src="js/jquery-1.8.3.min.js"></script>
<script type="text/javascript" src="js/Jcrop_upload.js"></script>
<script type="text/javascript" src="js/jquery.Jcrop.min.js"></script> </head>
<body> <form id="upload_form" enctype="multipart/form-data" method="post" action="${pageContext.request.contextPath}/user/upLoadImage.do" >
<!-- hidden crop params -->
<input type="hidden" id="x1" name="x1" />
<input type="hidden" id="y1" name="y1" />
<input type="hidden" id="x2" name="x2" />
<input type="hidden" id="y2" name="y2" /> <p>第一步:请选择图像文件</p>
<div><input type="file" name="image_file" id="image_file" onchange="fileSelectHandler()" /></div> <div class="error"></div> <div class="step2">
<p>第二步:请选择需要截图的部位,然后按上传</p>
<img id="preview" >
<br> <input type="submit" value="上传" />
</div>
</form> </body>
</html>

2、工具类

package com.zy.utils;

import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.awt.image.CropImageFilter;
import java.awt.image.FilteredImageSource;
import java.awt.image.ImageFilter;
import java.io.File;
import java.io.IOException; import javax.imageio.ImageIO; public class ImageCut { /**
* 图片切割
* @param imagePath 原图地址
* @param x 目标切片坐标 X轴起点
* @param y 目标切片坐标 Y轴起点
* @param w 目标切片 宽度
* @param h 目标切片 高度
*/
public static void cutImage(String imagePath, int x ,int y ,int w,int h){
try {
Image img;
ImageFilter cropFilter;
// 读取源图像
BufferedImage bi = ImageIO.read(new File(imagePath));
int srcWidth = bi.getWidth(); // 源图宽度
int srcHeight = bi.getHeight(); // 源图高度 //若原图大小大于切片大小,则进行切割
if (srcWidth >= w && srcHeight >= h) {
Image image = bi.getScaledInstance(srcWidth, srcHeight,Image.SCALE_DEFAULT); //如果jsp页面上展示的是原图的大小,那么此处就不计算起始坐标和宽高了,直接裁剪
int x1 = x;
int y1 = y;
int w1 = w;
int h1 = h; cropFilter = new CropImageFilter(x1, y1, w1, h1);
img = Toolkit.getDefaultToolkit().createImage(new FilteredImageSource(image.getSource(), cropFilter));
BufferedImage tag = new BufferedImage(w1, h1,BufferedImage.TYPE_INT_RGB);
Graphics g = tag.getGraphics();
g.drawImage(img, 0, 0, null); // 绘制缩小后的图
g.dispose();
// 输出为文件
ImageIO.write(tag, "JPEG", new File(imagePath));
}
} catch (IOException e) {
e.printStackTrace();
}
}
}

3、controller

package com.zy.controller;

import com.zy.utils.ImageCut;
import org.apache.commons.io.IOUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView; import javax.servlet.http.HttpServletRequest;
import java.io.*;
import java.util.UUID; @Controller
@RequestMapping("user")
public class ImageController { @RequestMapping("/upLoadImage")
public void upimage(Double x1,Double x2,Double y1,Double y2,HttpServletRequest request,MultipartFile image_file ){//包装类 Boolean isCut=false;//不裁剪
if (x2!=null&&x2!=0){//如果x2有值,说明横坐标终点发生变化,用户使用了裁剪
//截取的宽度
isCut=true;
} //图片上传
String path="/img";
String realPath = myGetRealPath(path, request);
String newFileName = newFileName(image_file); //上传----把本地文件按流的方式copy到服务器上 //输入流
InputStream is = null;
try {
is = image_file.getInputStream();
} catch (IOException e) {
e.printStackTrace();
}
//输出流
FileOutputStream os = null;
try {
os = new FileOutputStream(realPath+"/"+newFileName);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
//copy
try {
IOUtils.copy(is, os);
} catch (IOException e) {
e.printStackTrace();
} try {
os.close();
is.close();
} catch (IOException e) {
e.printStackTrace();
}
int i1=x1.intValue();
int i2=x2.intValue();
int i3=y1.intValue();
int i4=y2.intValue(); //3图片裁剪
if(isCut){
ImageCut.cutImage(realPath+"/"+newFileName,i1,i3,i2-i1,i4-i3);
} } //辅助方法
//1根据逻辑路径得到真实路径
//过期的
//@SuppressWarnings(“deprecation”)表示不检测过期的方法
@SuppressWarnings("deprecation")
public String myGetRealPath(String path, HttpServletRequest request){
String realPath = request.getRealPath(path);
System.out.println("真实路径:"+realPath);
File file = new File(realPath);
if(!file.exists()){
file.mkdirs();
} return realPath;
} //2更改文件名
public String newFileName(MultipartFile file){
String originalFilename = file.getOriginalFilename();
//abc.jpg
//截取后缀,拼接新的文件名
//后缀
String substring = originalFilename.substring(originalFilename.lastIndexOf("."));
//新文件名要求:上传中防止文件名重复,发生覆盖
String uu = UUID.randomUUID().toString(); String newName=uu+substring;
return newName; } }

Jcrop图片裁剪的更多相关文章

  1. 上传、裁剪图片-----Jcrop图片裁剪插件

    Jcrop文档:http://code.ciaoca.com/jquery/jcrop/C#裁剪:http://www.cnblogs.com/xyang/archive/2013/02/25/293 ...

  2. struts2+jsp+jquery+Jcrop实现图片裁剪并上传

    <1> 使用html标签上传需要裁剪的大图. <2> 在页面呈现大图,使用Jcrop(Jquery)对大图进行裁剪,并且可以进行预览. <3> 选择好截取部分之后发 ...

  3. 使用JCrop进行图片裁剪,裁剪js说明,裁剪预览,裁剪上传,裁剪设计的图片处理的工具类和代码

     1.要想制作图片裁剪功能,可以使用网上的裁剪工具JCrop,网址是:https://github.com/tapmodo/Jcrop/ 案例效果如下: 2.引入JCrop的js代码,具体要引入那 ...

  4. jQuery 图片裁剪插件 Jcrop

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

  5. bootstrap-wysiwyg 结合 base64 解码 .net bbs 图片操作类 (二) 图片裁剪

    图片裁剪参见: http://deepliquid.com/projects/Jcrop/demos.php?demo=thumbnail        一个js插件 http://www.mikes ...

  6. jQuery Jcrop 图像裁剪

    jQuery Jcrop 图像裁剪 http://code.ciaoca.com/jquery/jcrop/ cropper.js 实现HTML5 裁剪图片并上传(裁剪上传头像.) https://b ...

  7. java图片裁剪和java生成缩略图

    一.缩略图 在浏览相冊的时候.可能须要生成相应的缩略图. 直接上代码: public class ImageUtil { private Logger log = LoggerFactory.getL ...

  8. Java实现图片裁剪预览功能

    在项目中.我们须要做些类似头像上传,图片裁剪的功能,ok看以下文章! 须要插件:jQuery Jcrop 后端代码: package org.csg.upload; import java.awt.R ...

  9. JavaScript图片裁剪

    1.jquery 图片裁剪库选择 Jcrop:http://deepliquid.com/content/Jcrop.html imgareaselect:http://odyniec.net/pro ...

随机推荐

  1. OOP、封装、继承、多态,真的懂了吗?

    平时只要一提起来面向对象编程OOP的好处,随口就能说出来,不就是封装.继承.多态么,可他们的含义是什么呢,怎么体现,又有什么非用不可的好处啊.可能平时工作中天天在用OOP,仅仅是在用OOP语言,就是一 ...

  2. 【Azure Developer】使用Postman获取Azure AD中注册应用程序的授权Token,及为Azure REST API设置Authorization

    Azure Active Directory (Azure AD) is Microsoft's cloud-based identity and access management service, ...

  3. 数组的方法some和includes

    some() 方法用于检测数组中的元素是否满足指定条件(函数提供). some() 方法会依次执行数组的每个元素: 如果有一个元素满足条件,则表达式返回true , 剩余的元素不会再执行检测. 如果没 ...

  4. 【Qt】实现程序重启的两种方法

    Qt5/PyQt5 实现程序重启的两种方法 前言 最近在写一个开源项目,需要实现一个程序自动重启的功能.尝试了好几种方式,效果均不太理想. 一开始的实现思路是,记为思路一吧.大概就是写一些 shell ...

  5. 【Linux】1、命令行及命令参数

    命令行及命令参数 文章目录 命令行及命令参数 1.命令行提示符 2.命令和命令参数 简单的命令 date ls 命令参数 短参数(一个字母) 长参数(多个字母) 参数的值 其它参数 3.小结 4.参考 ...

  6. zabbix_agent items not supported状态

    不记得自己究竟更改了什么东西,然后突然发现所有的有关mysql的监控items都变成了not supported,怎么做不行,最后在web主页把主机删除,又重新添加一下,重新添加了一下模版就好了.这究 ...

  7. freopen函数总结

    函数原型: freopen(const char * __restrict__ _Filename,const char * __restrict__ _Mode,FILE * __restrict_ ...

  8. python7、8章

    目录 第七章 用户输入和while循环 7.1 函数input()的工作原理 7.1.1 编写清晰的程序 7.1.2 使用int()来获取数值输入 分析: 结果: 7.1.3 求模运算符 7.1.4 ...

  9. 如何创建一个Java项目

    目录 新建项目 项目信息配置 创建Java类 编译和运行 新建项目 首先双击eclipse进入到eclipse页面. 菜单"File"下的"New"里" ...

  10. __init__ raises an exception, then __del__ will still be called

    issue 808164: socket.close() doesn't play well with __del__ - Python tracker https://bugs.python.org ...