图片裁剪并支持预览,然后可以上传至服务器

下面是java部分代码,可以直接预览上传,可以进行裁剪后预览上传,可以支持二次裁剪预览上传

@RequestMapping(value = “/personalCenter/uploadImgSave”, method = RequestMethod.POST)
public @ResponseBody Message createSave(HttpServletRequest request,RequestParameters parameters,LoginInfo loginInfo) throws Exception {
String uploadHidden = parameters.getStringValue(“uploadHidden”);
MultipartFile file = parameters.getMultipartFile(“file”);
String fileName = null;
int width = parameters.getIntValue(“width”);
int height = parameters.getIntValue(“height”);
InputStream iStream = null;
String type = null;
//截取
int x = parameters.getIntValue(“x”);
int y = parameters.getIntValue(“y”);
System.out.println(file == null && (width== 0 && height == 0));
//确定是修改无操作
if(file == null && (width== 0 && height == 0)){
return Message.SUCCESS();
//修改本图片并进行裁剪
}else if(file == null &&(width!= 0 && height != 0)){
fileName = uploadHidden;
type = uploadHidden.substring(uploadHidden.lastIndexOf(".")+1);
//下载图片
String tt = request.getRequestURL().toString();
String ip = tt.substring(tt.indexOf(“http://”)+7,tt.lastIndexOf("😊);
URL httpUrl = new URL(“http://”+ip+":"+request.getLocalPort()+"/web/fileUpload/app_grzx/"+ loginInfo.getId()+"/" +uploadHidden);
HttpURLConnection connection = (HttpURLConnection)httpUrl.openConnection();
connection.setDoInput(true);
connection.setDoOutput(true);
connection.connect();
//重命名图片
InputStream tmpInputStream = connection.getInputStream();
File tmp = new File(System.getProperty(“user.dir”)+"/WebRoot/web/fileUpload/app_grzx/"+ loginInfo.getId()+"/2.png");
org.apache.commons.io.FileUtils.copyInputStreamToFile(tmpInputStream,tmp);
//截取后流
FileInputStream inputStream = new FileInputStream(tmp);
BufferedImage src = javax.imageio.ImageIO.read(inputStream);
//判断图片像素大小
int realHeight = src.getHeight();
int realWidth = src.getWidth();
if (realHeight<300 ||realWidth<300) {
return Message.FAILED();
}
BufferedImage result = cropImage(src,x,y,width,height);
//转流
ByteArrayOutputStream oStream = new ByteArrayOutputStream();
ImageIO.write(result,type, oStream);
iStream = new ByteArrayInputStream(oStream.toByteArray());
//重新上传无裁剪
}else if(file != null && (width== 0 && height == 0)){
iStream = file.getInputStream();
fileName = file.getOriginalFilename();
FileInputStream inputStream = (FileInputStream)file.getInputStream();
//判断图片像素大小
BufferedImage src = javax.imageio.ImageIO.read(inputStream);
int realHeight = src.getHeight();
int realWidth = src.getWidth();
if (realHeight<300 ||realWidth<300) {
return Message.FAILED();
}
//重新上传有裁剪
}else if(file != null && (width!= 0 && height != 0)){
fileName = file.getOriginalFilename();
type = fileName.substring(fileName.lastIndexOf(".")+1);
//截取后流
FileInputStream inputStream = (FileInputStream)file.getInputStream();
BufferedImage src = javax.imageio.ImageIO.read(inputStream);
int realHeight = src.getHeight();
int realWidth = src.getWidth();
if (realHeight<300 ||realWidth<300) {
return Message.FAILED();
}
BufferedImage result = cropImage(src,x,y,width,height);
//转流
ByteArrayOutputStream oStream = new ByteArrayOutputStream();
ImageIO.write(result,type, oStream);
iStream = new ByteArrayInputStream(oStream.toByteArray());
}
//删除之前存在库里的关于此user_id存储的图片信息
PersonImg personImg = new PersonImg();
this.personalCenterService.deletePersonImg(loginInfo.getId());
personImg.setImgName(fileName);
File pathFile = new File(System.getProperty(“user.dir”)+"/WebRoot/web/fileUpload/app_grzx/"+ loginInfo.getId()+"/" +fileName);
FileUtils.writeFile(pathFile, iStream);
String filePath = pathFile.getAbsolutePath().replaceAll("\\", “/”);
personImg.setDownloadUrl(filePath);
personImg.setUserId(loginInfo.getId());
this.personalCenterService.createPersonImg(personImg);
return Message.SUCCESS();
}

public  BufferedImage cropImage(BufferedImage bufferedImage, int startX, int startY, int endX, int endY) {
    int width = bufferedImage.getWidth();
    int height = bufferedImage.getHeight();
    if (startX == -1) {
        startX = 0;
    }
    if (startY == -1) {
        startY = 0;
    }
    if (endX == -1) {
        endX = width - 1;
    }
    if (endY == -1) {
        endY = height - 1;
    }
    BufferedImage result = new BufferedImage(endX - startX, endY - startY, 4);
    for (int x = startX; x < endX; ++x) {
        for (int y = startY; y < endY; ++y) {
            int rgb = bufferedImage.getRGB(x, y);
            result.setRGB(x - startX, y - startY, rgb);
        }
    }
    return result;
}

**

html及js部分代码

**


<c:hidden id=“startX”/>
<c:hidden id=“startY”/>
<c:hidden id=“width”/>
<c:hidden id=“height”/>


<c:hidden id=“uploadHidden”/>
<d:control style=“height:300px”>






</d:control>
<d:control style=“height:60px”>
上传

(推荐300x300以上像素 , JPG/PNG ,5M以内。头像修改后,部分页面次日生效)
</d:control>
<d:control>
<c:button value=“保存” onClick=“onSave()”/>
<c:button value=“取消” onClick=“window.onActionCompleted()”/>
</d:control>



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值