uploadifyJcrop

来源:互联网 发布:再生聚酯切片进口数据 编辑:程序博客网 时间:2024/06/04 19:52

jsp:

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>Insert title here</title><link rel="stylesheet" href="${pageContext.request.contextPath}/js/uploadify/uploadify.css"><link href="${pageContext.request.contextPath}/js/jcrop/jquery.Jcrop.css" rel="stylesheet" type="text/css" /><script type="text/javascript" src="${pageContext.request.contextPath}/js/commonJs/jquery.js"></script><script type="text/javascript" src="${pageContext.request.contextPath}/js/jcrop/jquery.Jcrop.js"></script><script type="text/javascript" src="${pageContext.request.contextPath}/js/uploadify/jquery.uploadify.min.js"></script><script type="text/javascript" src="${pageContext.request.contextPath}/js/uploadifyJcrop.js"></script><style type="text/css">.jcrop-holder #preview-pane {display: block;position: absolute;z-index: 2000;top: 10px;right: -280px;padding: 6px;border: 1px rgba(0, 0, 0, .4) solid;background-color: white;-webkit-border-radius: 6px;-moz-border-radius: 6px;border-radius: 6px;-webkit-box-shadow: 1px 1px 5px 2px rgba(0, 0, 0, 0.2);-moz-box-shadow: 1px 1px 5px 2px rgba(0, 0, 0, 0.2);box-shadow: 1px 1px 5px 2px rgba(0, 0, 0, 0.2);}#preview-pane .preview-container {width: 250px;height: 170px;overflow: hidden;}</style></head><body><img id="aaa" alt="" src="${imgsrc }"><div id="preview-pane" style="display: none;"><div class="preview-container"><img id="ccc" src="" class="jcrop-preview" alt="Preview" /></div></div><input id="zzz" type="file"><form action="<%=request.getContextPath()%>/cutImg" method="post"><input type="hidden" id="path" name="path"> <input type="hidden" name="view" value="uploadifyJcrop"> <input type="hidden" id="x" name="x" />  <input type="hidden" id="y" name="y" />   <input type="hidden" id="w" name="w" />    <input type="hidden" id="h" name="h" />    <input type="submit" id="confirmPic" value="确认选择" style="display: none;"></form></body></html>
java:

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 javax.imageio.ImageIO;


public class ImageCut
{
/**
* 截取图片

* @param srcImageFile
*            原图片地址
* @param x
*            截取时的x坐标
* @param y
*            截取时的y坐标
* @param desWidth
*            截取的宽度
* @param desHeight
*            截取的高度
*/
public static void imgCut(String srcImageFile, int x, int y, int desWidth, int desHeight)
{
try
{
Image img;
ImageFilter cropFilter;
BufferedImage bi = ImageIO.read(new File(srcImageFile));
int srcWidth = bi.getWidth();
int srcHeight = bi.getHeight();
if(srcWidth >= desWidth && srcHeight >= desHeight)
{
Image image = bi.getScaledInstance(srcWidth, srcHeight, Image.SCALE_DEFAULT);
cropFilter = new CropImageFilter(x, y, desWidth, desHeight);
img = Toolkit.getDefaultToolkit().createImage(new FilteredImageSource(image.getSource(), cropFilter));
BufferedImage tag = new BufferedImage(desWidth, desHeight, BufferedImage.TYPE_INT_RGB);
Graphics g = tag.getGraphics();
g.drawImage(img, 0, 0, null);
g.dispose();
//输出文件
String[] split = srcImageFile.split("[.]");
ImageIO.write(tag, "JPEG", new File(split[0] + "_cut.jpg"));
File file = new File(srcImageFile);
file.delete();
split = null;
}
}
catch (Exception e)
{
e.printStackTrace();
}
}
}


import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.InputStream;
import java.io.OutputStream;
import javax.imageio.ImageIO;


public class ImageUtil {


/**
     * 
     * @param infile 输入文件
     * @param outfile 输出文件
     * @param srcFormat 源格式
     * @param destFormat 输出格式
     * @return
     * @throws Exception
     */
    public static boolean convertFormat(InputStream infile,
            OutputStream outfile, String srcFormat, String destFormat, int width ,int height) throws Exception {
        boolean flag = false;
        BufferedImage src = ImageIO.read(infile);
        if(height > 0  && width > 0) {// compress the origin image if width and height are non-zero
            height = src.getHeight() > height ? height: src.getHeight();
            width = src.getWidth() > width ? width : src.getWidth();
            Image image = src.getScaledInstance(width, height, Image.SCALE_DEFAULT);//这个是用来进行图片大小调整的
     
            BufferedImage tag = new BufferedImage(width, height,
                    BufferedImage.TYPE_INT_RGB);
     
            Graphics g = tag.getGraphics();
            //可在下面对图片进行绘制和更改
            g.drawImage(image, 0, 0, null); // 绘制缩小后的图
     
            g.dispose();
            tag.flush();
            flag = ImageIO.write(tag, destFormat, outfile);// 输出到经过缩放的文件流
        } else {
            flag = ImageIO.write(src, destFormat, outfile);//输出原分辨率的图片
        }
        return flag;
    }
}


import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;
import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.multipart.MultipartFile;

@Controller
@RequestMapping("/")
public class FileController
{
// 处理上传
@RequestMapping(value =
{ "uploadImg" }, method = RequestMethod.POST)
public void upload(HttpServletRequest request, HttpServletResponse response, MultipartFile mf) throws IOException
{
String responseStr = "";
String configPath = "/" + "image" + "/";
String ctxPath = "d:/img";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
String year = sdf.format(new Date());
configPath += year + "/";
sdf = new SimpleDateFormat("MM");
String month = sdf.format(new Date());
configPath += month + "/";
ctxPath += configPath;
// 创建文件夹
File file = new File(ctxPath);
if(!file.exists())
{
file.mkdirs();
}
String fileName = null;
String newFileName = null;
fileName = mf.getOriginalFilename();
String fileExt = fileName.substring(fileName.lastIndexOf(".") + 1).toLowerCase();
SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
newFileName = df.format(new Date()) + "_" + new Random().nextInt(1000) + "." + fileExt;
responseStr = configPath + newFileName;
File uploadFile = new File(ctxPath + newFileName);
try
{
FileCopyUtils.copy(mf.getBytes(), uploadFile);
}
catch (IOException e)
{
responseStr = "上传失败";
e.printStackTrace();
}
BufferedImage bi = ImageIO.read(mf.getInputStream());
StringBuffer s = new StringBuffer(responseStr + "," + bi.getWidth() + "," + bi.getHeight());
response.setHeader("Content-type", "text/html;charset=UTF-8");
response.setCharacterEncoding("UTF-8");
response.getWriter().write(s.toString());
response.getWriter().flush();
}


@RequestMapping(value = "cutImg", method = RequestMethod.POST)
public String cutImage(String path, String view, HttpServletRequest request, Double x, Double y, Double w, Double h)
{
Integer x1 = x.intValue();
Integer y1 = y.intValue();
Integer w1 = w.intValue();
Integer h1 = h.intValue();
ImageCut.imgCut("d:/"+path, x1, y1, w1, h1);
String[] split = path.split("[.]");
request.setAttribute("imgsrc", split[0] + "_cut.jpg");
split = null;
return view;
}
}


0 0
原创粉丝点击