📄 imageprocessor.java
字号:
package com.eline.vod.utils;
import java.io.File;
import com.blue.imageio.ImageGenerator;
import com.blue.imageio.Size;
import com.blue.web.common.util.AppSettings;
import com.eline.vod.HttpContext;
public class ImageProcessor {
public static String getThumb(String sourcePath, int width, int height) throws Exception {
int n1 = sourcePath.lastIndexOf("\\");
if (n1 < 0)
throw new Exception("invalid path format.");
String path = sourcePath.substring(0, n1);
String file = sourcePath.substring(n1+1);
n1 = file.lastIndexOf(".");
if (n1 < 0)
throw new Exception("invalid extension specified.");
String fileWithoutExt = file.substring(0, n1);
file = "thumb_" + fileWithoutExt + "-" + width + "x" + height + ".gif";
String physicalPath = path + "\\" + file;
File f = new File(physicalPath);
if (!f.exists()) {
ImageGenerator.generateThumb(sourcePath, physicalPath, new Size(width, height));
}
return file;
}
/**
* Http专用函数,获取缩略图,不抛异常,如果失败的话返回null
* @param url
* @param width
* @param height
* @return
*/
public static String getThumbFileURL(String url, int width, int height) {
HttpContext ctx = HttpContext.getCurrent();
String thumbURL = null;
try {
AppSettings.getInstance().getProperty(AppKeys.UPLOAD_ROOT);
String imageURL = AppSettings.getInstance().getProperty(AppKeys.UPLOAD_ROOT)
+ "/pictures" + url;
int n1 = imageURL.lastIndexOf('/');
// 获取缩略图目录url
String urlPrefix = null;
if (n1 >= 0)
urlPrefix = imageURL.substring(0, n1);
String origionFile = ctx.getRequest().getSession().getServletContext().getRealPath(imageURL);
// 获取缩略图文件名(如果不存在则生成此图片)
String thumbFile = null;
try {
thumbFile = ImageProcessor.getThumb(origionFile, width, height);
} catch (Exception e) {
System.out.println("ERROR: failure to get thumb file." + e);
}
if (urlPrefix != null && thumbFile != null)
thumbURL = urlPrefix + "/" + thumbFile;
} catch (Exception e) {
e.printStackTrace();
}
return thumbURL;
}
/**
*
* @param sourcePath
* @param width
* @param height
* @return
* @throws Exception
*/
public static String getPostImage(String sourcePath, int width, int height) throws Exception {
int n1 = sourcePath.lastIndexOf("\\");
if (n1 < 0)
throw new Exception("invalid path format.");
String path = sourcePath.substring(0, n1);
String file = sourcePath.substring(n1+1);
n1 = file.lastIndexOf(".");
if (n1 < 0)
throw new Exception("invalid extension specified.");
String fileWithoutExt = file.substring(0, n1);
file = "post_" + fileWithoutExt + "-" + width + "x" + height + ".jpg";
String physicalPath = path + "\\" + file;
File f = new File(physicalPath);
if (!f.exists()) {
ImageGenerator.generateThumb(sourcePath, physicalPath, new Size(width, height));
}
return file;
}
/**
*
* @param url
* @param width
* @param height
* @return
*/
public static String getPostImageFileURL(String url, int width, int height) {
HttpContext ctx = HttpContext.getCurrent();
String resultURL = null;
try {
AppSettings.getInstance().getProperty(AppKeys.UPLOAD_ROOT);
String imageURL = AppSettings.getInstance().getProperty(AppKeys.UPLOAD_ROOT)
+ "/pictures" + url;
int n1 = imageURL.lastIndexOf('/');
// 获取缩略图目录url
String urlPrefix = null;
if (n1 >= 0)
urlPrefix = imageURL.substring(0, n1);
String origionFile = ctx.getRequest().getSession().getServletContext().getRealPath(imageURL);
// 获取缩略图文件名(如果不存在则生成此图片)
String thumbFile = null;
try {
thumbFile = ImageProcessor.getPostImage(origionFile, width, height);
} catch (Exception e) {
System.out.println("ERROR: failure to get thumb file." + e);
}
if (urlPrefix != null && thumbFile != null)
resultURL = urlPrefix + "/" + thumbFile;
} catch (Exception e) {
e.printStackTrace();
}
return resultURL;
}
public static void main(String[] args) {
try {
System.out.println(ImageProcessor.getThumb("c:\\logs\\Img243507655.jpg", 100, 150));
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -