因为项目当中用到图片裁剪,本来可以不用到后台进行裁剪的,但是要兼容万恶的IE浏览器,所以不得不使用后台进行裁剪。
这次使用到插件获取需要裁剪区域的坐标,再由后台进行裁剪操作。先上个效果图再说
但是这里有一个坑就是上传的图片过大,可能会造成裁剪的区域跟插件中显示的不一样,所以得现在后台对云图片进行压缩在裁剪。
*/
public static void saveMinPhoto(String srcURL,String deskURL,double comBase,double scale) throws Exception {
File srcFile = new java.io.File(srcURL);
String ext = srcURL.substring(srcURL.lastIndexOf(".") + 1);
Image src = ImageIO.read(srcFile);
int srcHeight = src.getHeight(null);
int srcWidth = src.getWidth(null);
int deskHeight = 0;// 缩略图高
int deskWidth = 0;// 缩略图宽
double srcScale = (double) srcHeight / srcWidth;
/*缩略图宽高算法/
if ((double) srcHeight > comBase || (double) srcWidth > comBase) {
if (srcScale >= scale || 1 / srcScale > scale) {
if (srcScale >= scale) {
deskHeight = (int) comBase;
deskWidth = srcWidth deskHeight / srcHeight;
} else {
deskWidth = (int) comBase;
deskHeight = srcHeight deskWidth / srcWidth;
}
} else {
if ((double) srcHeight > comBase) {
deskHeight = (int) comBase;
deskWidth = srcWidth deskHeight / srcHeight;
} else {
deskWidth = (int) comBase;
deskHeight = srcHeight deskWidth / srcWidth;
}
}
} else {
deskHeight = srcHeight;
deskWidth = srcWidth;
}
BufferedImage tag = new BufferedImage(deskWidth,deskHeight,BufferedImage.TYPE_3BYTE_BGR);
tag.getGraphics().drawImage(src,deskWidth,null); //绘制缩小后的图
FileOutputStream deskImage = new FileOutputStream(deskURL); //输出到文件流
ImageIO.write(tag,ext,new File(deskURL));
deskImage.close();
}
这就是压缩之后在进行裁剪了,好了上完整代码先
<div class="jb51code">
<pre class="brush:xhtml;">
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">