浏览 3208 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
|
|
---|---|
作者 | 正文 |
发表时间:2010-11-12
最后修改:2010-11-12
在上传头像时,需要对图片进行剪切,而且要指定剪切的图片的大小,此时,我给大家推荐一个 API 。java.awt.Dimension。 不罗嗦了,我在此处只说明用法。
用法如下: package com.transnal.web.utils; import java.awt.Dimension; import magick.ImageInfo; import magick.MagickImage; public class ImageUtil { static { // 不能漏掉这个,不然jmagick.jar的路径找不到 System.setProperty("jmagick.systemclassloader", "no"); } /** * @param width * @param height * @param maxValue * @return */ private static int[] getWidthHeight(int width, int height, int maxValue) { int nWidth = 0; int nHeight = 0; if (width < height) { nWidth = width * maxValue / height; nHeight = maxValue; } else if (width > height) { nHeight = height * maxValue / width; nWidth = maxValue; } else { nWidth = width * maxValue / height; nHeight = height * maxValue / width; } return new int[] { nWidth, nHeight }; } /** * 压缩图片 * * @param filePath * 源文件路径 * @param toPath * 缩略图路径 */ public static void resetDimension(String filePath, String toPath, int maxValue) { ImageInfo info = null; MagickImage image = null; Dimension imageDim = null; MagickImage scaled = null; try { info = new ImageInfo(filePath); image = new MagickImage(info); imageDim = image.getDimension(); int wideth = imageDim.width; int height = imageDim.height; int[] wh = getWidthHeight(wideth, height, maxValue); wideth = wh[0]; height = wh[1]; scaled = image.scaleImage(wideth, height);// 小图片文件的大小. scaled.setFileName(toPath); scaled.writeImage(info); } catch (Exception e) { e.printStackTrace(); } finally { if (scaled != null) { scaled.destroyImages(); } } } }
第二步: /**生产blog头像 尺寸65*65 * @return */ public void generateBlogGravatarPic(String picName) { // floderPath最终为:E:\cun\ String folderPath = ResourceBundle.getBundle("sysConfig").getString( "userDirectory.path"); // filePath最终为:E:\cun\当前用户名 String filePath = this.createUserFolderInOS(userName, folderPath); // filePath最终为:E:\cun\当前用户名\gravatar\ filePath = filePath + File.separator + "gravatar" + File.separator; String newFilePath = filePath + picName; ImageUtil.resetDimension(newFilePath, filePath+"blog_"+picName, 65); }
第三步: 安装API 包.见附件. (1): 将 jmagick.rar 解压后 将其 dll 文件 放入 c:/windows/system32 下. (2): 将 ImageMagick-6.3.9-0-Q8-windows-dll.rar 解压并安装。
声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
返回顶楼 | |