primeface+jsf 截取图片

来源:互联网 发布:机械公差配合软件 编辑:程序博客网 时间:2024/05/16 10:08

1、引入相应的js和相应的css

jquery.Jcrop.js和jquery.Jcrop.css

2、处理逻辑
    
1) 将本地图片上传至图片服务器

     2)读取图片服务器上图片

     3)截取服务器上图片

     4 )保存截取图片

     5)将服务器上图片再次上传到图片服务器

      6)删除原来未处理的图片

3、需要自己手动设置的js

$(function() {$('#cropbox').Jcrop({setSelect : [ 0, 0, 150, 100 ],//默认选中区域aspectRatio : 100 / 100,//宽度和高度比minSize : [ 100, 100 ],//最小值maxSize : [ 200, 200 ],//最大值bgOpacity : 0.9,//透明度onSelect : updateCoords//选择时调用方法});});function updateCoords(c) {$('#x').val(c.x);$('#y').val(c.y);$('#w').val(c.w);$('#h').val(c.h);};</script>

4、页面实现

<p:fileUpload multiple="true" cancelLabel="取消" uploadLabel="上传"label="选择" update="formCat" auto="true" fileUploadListener="#{uploadBean.saveMediaData}" /></div><img src="#{uploadBean.md.url}" id="cropbox" style="" /><input type="hidden" size="4" id="x" name="x" /><input type="hidden" size="4" id="y" name="y" /><input type="hidden" size="4" id="w" name="w" /><input type="hidden" size="4" id="h" name="h" /><p:commandButton value="保存" action="#{uploadBean.crop}"styleClass="btn btn-warning" />

45代码实现

String x = RequestUtil.getRequestParam("x");String y = RequestUtil.getRequestParam("y");String w = RequestUtil.getRequestParam("w");String h = RequestUtil.getRequestParam("h");String type = StringKit.substringFileType(mediaUrl);//图片类型String fileName = "/" + System.currentTimeMillis()+ StringKit.substringPash(mediaUrl);//目标图片路径,截取后图片的据另ImageKit.abscut(mediaUrl, fileName, Integer.parseInt(x),Integer.parseInt(y), Integer.parseInt(w), Integer.parseInt(h),type);//截取图片

5.截取图片方法

/** * 图像切割(改) * *  * @param srcImageFile *            源图像地址 * @param dirImageFile *            新图像地址 * @param x *            目标切片起点x坐标 * @param y *            目标切片起点y坐标 * @param destWidth *            目标切片宽度 * @param destHeight *            目标切片高度 */public static void abscut(String srcImageFile, String dirImageFile, int x,int y, int destWidth, int destHeight, String fileType) {try {Image img;ImageFilter cropFilter;// 读取源图像BufferedImage bi = ImageIO.read(new File(srcImageFile));int srcWidth = bi.getWidth(); // 源图宽度int srcHeight = bi.getHeight(); // 源图高度if (srcWidth >= destWidth && srcHeight >= destHeight) {Image image = bi.getScaledInstance(srcWidth, srcHeight,Image.SCALE_DEFAULT);// 改进的想法:是否可用多线程加快切割速度// 四个参数分别为图像起点坐标和宽高// 即: CropImageFilter(int x,int y,int width,int height)cropFilter = new CropImageFilter(x, y, destWidth, destHeight);img = Toolkit.getDefaultToolkit().createImage(new FilteredImageSource(image.getSource(), cropFilter));BufferedImage tag = new BufferedImage(destWidth, destHeight,BufferedImage.TYPE_INT_RGB);Graphics g = tag.getGraphics();g.drawImage(img, 0, 0, null); // 绘制缩小后的图g.dispose();// 输出为文件ImageIO.write(tag, fileType, new File(dirImageFile));}} catch (Exception e) {e.printStackTrace();}}



0 0
原创粉丝点击