手机自由裁剪任意形状 js图片裁剪原理( 二 )


//剪裁图片坐标String x = request.getParameter(\\”x\\”);String y = request.getParameter(\\”y\\”);String w = request.getParameter(\\”w\\”);String h = request.getParameter(\\”h\\”);
//原始图片坐标String boundx = request.getParameter(\\”boundx\\”);String boundy = request.getParameter(\\”boundy\\”);
//切图参数int imgeX = (int) Double.parseDouble(x);int imgeY = (int) Double.parseDouble(y);int imegW = (int) Double.parseDouble(w);int imgeH = (int) Double.parseDouble(h);int srcX = (int) Double.parseDouble(boundx);int srcY = (int) Double.parseDouble(boundy);
//文件保存文件夹String path = request.getSession().getServletContext().getRealPath(\\”/\\”)+\\”fileUpload\\”+File.separator;//文件重命名Date day = new Date();SimpleDateFormat sdf = new SimpleDateFormat(\\”yyyyMMdd\\”);String newName = sdf.format(day)+System.currentTimeMillis()+\\”.jpg\\”;
try{//处理头像附件if(imgFile !=null){//判断是否为图片文件if(FileUploadCheck.allowUpload(imgFile.getContentType())){boolean cut = CutImgeUtil.cutImge(imgFile.getInputStream(), imgeX, imgeY, imegW, imgeH, srcX, srcY, path+newName);if(cut){//当头像剪切成功进行用户信息数据库存储System.out.println(userName+\\” \\”+artName+\\” \\”+newName);}
}}
} catch (Exception e){e.printStackTrace();logger.error(\\”上传失败\\”);}
}}
工具类
package com.lovo.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.ByteArrayOutputStream;import java.io.File;import java.io.InputStream;
import javax.imageio.ImageIO;
import org.apache.log4j.Logger;
public class CutImgeUtil {
private static Logger logger = Logger.getLogger(CutImgeUtil.class);
/*** 图片剪切工具类* @param input 图片输入流* @param x 截取时的x坐标* @param y 截取时的y坐标* @param desWidth 截取的宽度* @param desHeight 截取的高度* @param srcWidth 页面图片的宽度* @param srcHeight 页面图片的高度* @param newFilePath 保存路径+文件名* @return*/public static boolean cutImge(InputStream input, int x, int y, int desWidth, int desHeight, int srcWidth,int srcHeight,String newFilePath){
boolean cutFlag = true;
try{//图片类Image imge ;ImageFilter cropFilter;
//读取图片BufferedImage bi = ImageIO.read(input);
//当剪裁大小小于原始图片大小才执行if(srcWidth >= desWidth && srcHeight >= desHeight){//获取原始图Image image = bi.getScaledInstance(srcWidth, srcHeight, Image.SCALE_DEFAULT);//获取新图cropFilter = new CropImageFilter(x, y, desWidth, desHeight);imge = Toolkit.getDefaultToolkit().createImage(new FilteredImageSource(image.getSource(), cropFilter));BufferedImage tag = new BufferedImage(desWidth, desHeight, BufferedImage.TYPE_INT_RGB);Graphics g = tag.getGraphics();g.drawImage(imge, 0, 0, null);g.dispose();
File out = new File(newFilePath);// 输出文件ImageIO.write(tag, \\”JPEG\\”, out);}
} catch (Exception e){cutFlag = false;e.printStackTrace();logger.error(\\”剪切失败\\”);}return cutFlag;
}}
package com.lovo.utils;
import java.util.Arrays;import java.util.List;
public class FileUploadCheck {
//支持的文件类型public static final List ALLOW_TYPES = Arrays.asList(\\”image/jpg\\”,\\”image/jpeg\\”,\\”image/png\\”,\\”image/gif\\”);
//校验文件类型是否是被允许的public static boolean allowUpload(String postfix){return ALLOW_TYPES.contains(postfix);}}