|
天堂之光 人间希望
你我共同品味
JAVA的浓香.
Linux的清芬. |
| « | February 2026 | » | | 日 | 一 | 二 | 三 | 四 | 五 | 六 | | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | |
| 链接 |
联系我
msn:zhanglincon@hotmail.com |
| Blog信息 |
|
blog名称: 日志总数:99 评论数量:281 留言数量:4 访问次数:820763 建立时间:2005年11月17日 |

| |
|
使用JMagick为图片打水印 原创空间, 文章收藏, 软件技术
zhanglincon 发表于 2008/2/24 14:05:50 |
|
为了保护图片的版权,我们经常需要在上传的照片上打上版权信息或图标,下面介绍利用JMagick在图片上做标记。引入JMagick需要的类库: import java.awt.Dimension; import java.awt.Rectangle; import magick.CompositeOperator; import magick.ImageInfo; import magick.MagickException; import magick.MagickImage;
下面介绍如何使用JMagick做标记: public static void mask(String logoPath, String srcPathName, String destPathName, int location, int scale) throws MagickException { int width = getWidth(srcPathName); int height = getHeight(srcPathName); int x = 0, y = 0; int w, h; w = scale * 70 / 100; h = scale * 65 / 100; boolean lc = true; if (width < height) { switch (location) { case 0: lc = false; break; case 1: x = width / 4 - w; y = height / 8 - h / 2; break; case 2: x = width / 2 - w; y = height / 8 - h / 2; break; case 3: x = width * 3 / 4 - w; y = height / 8 - h / 2; break; case 4: x = width / 4 - w; y = height / 2 - h / 2; break; case 5: x = width / 2 - w; y = height / 2 - h / 2; break; case 6: x = width * 3 / 4 - w; y = height / 2 - h / 2; break; case 7: x = width / 4 - w; y = height * 7 / 8 - h / 2; break; case 8: x = width / 2 - w; y = height * 7 / 8 - h / 2; break; case 9: x = width * 3 / 4 - w; y = height * 7 / 8 - h / 2; break; } } else { switch (location) { case 0: lc = false; break; case 1: x = width / 7 - w; y = height / 6 - h / 2; break; case 2: x = width / 2 - w; y = height / 6 - h / 2; break; case 3: x = width * 6 / 7 - w; y = height / 6 - h / 2; break; case 4: x = width / 7 - w; y = height / 2 - h / 2; break; case 5: x = width / 2 - w; y = height / 2 - h / 2; break; case 6: x = width * 6 / 7 - w; y = height / 2 - h / 2; break; case 7: x = width / 7 - w; y = height * 5 / 6 - h / 2; break; case 8: x = width / 2 - w; y = height * 5 / 6 - h / 2; break; case 9: x = width * 6 / 7 - w; y = height * 5 / 6 - h / 2; break; } } if (x < 10) { x = 10; } if (x + w * 2 + 10 > width) { x = width - w * 2 - 10; } if (y < 10) { y = 10; } if (y + h + 10 > height) { y = height - h - 10; } if (lc) { ImageInfo info = new ImageInfo(); MagickImage image = null; MagickImage mask = null; MagickImage dest = null; try { image = new MagickImage(new ImageInfo(srcPathName + “[0]”)); mask = new MagickImage(new ImageInfo(logoPath)); image.setFileName(destPathName); image.writeImage(info); dest = new MagickImage(new ImageInfo(destPathName)); dest.compositeImage(CompositeOperator.AtopCompositeOp, mask, x, y); dest.setFileName(destPathName); dest.writeImage(info); } finally { if (image != null) { image.destroyImages(); } if (mask != null) { mask.destroyImages(); } if (dest != null) { dest.destroyImages(); } } } }
getWidth(String src) & getHeight(String src)public static int getWidth(String src) throws MagickException { MagickImage magImage = null; try { ImageInfo info = new ImageInfo(src + “[0]”); magImage = new MagickImage(info); Dimension imageDim = magImage.getDimension(); return imageDim.width; } finally { if (magImage != null) { magImage.destroyImages(); } } } public static int getHeight(String src) throws MagickException { MagickImage magImage = null; try { ImageInfo info = new ImageInfo(src + “[0]”); magImage = new MagickImage(info); Dimension imageDim = magImage.getDimension(); return imageDim.height; } finally { if (magImage != null) { magImage.destroyImages(); } } } 压缩图片大小
我们经常需要实现图片上传的功能,但是光是上传图片可能还是远远不够的,我们必须对我们上传的图片进行处理,改变大小等等。JMagick是ImageMagick提供的一套使用Java调用ImageMagick的API接口,功能非常强大,下面介绍如果使用这个API处理图片的大小。引入JMagick需要的类库: import magick.ImageInfo; import magick.MagickException; import magick.MagickImage;
我们需要把图片保存为两个尺寸,这里要提前定义两种常量: public static String SIZENAME_LARGE = “large”; public static String SIZENAME_SMALL = “small”;
下面介绍如何使用JMagick,里面用到的ImageUtil稍后会介绍,FileUtil是操作文件的工具类,这里就暂时不介绍了: MagickImage source = ImageUtil.getMagickImage(”image file name”); Map map = processHead(source); System.out.ptineln(map.get(SIZENAME_LARGE)); System.out.ptineln(map.get(SIZENAME_SMALL));
ImageUtil.getMagickImage(byte[] byte) public static MagickImage getMagickImage(byte[] byte) throws MagickException { ImageInfo info = new ImageInfo(); return new MagickImage(info, byte); }
processHead(MagickImage source)private Map processHead(MagickImage source) throws Exception { MagickImage regulate = null; MagickImage large = null; MagickImage small = null; //保存图片的临时目录 String tempPath = “….”; //随即生成一个文件名,真是情况生成的目录可能更复杂 String fn = FileUtil.getInstance().getRandName(); String largeName = SIZENAME_LARGE + “_” + fn + “.jpg”; String smallName = SIZENAME_SMALL + “_” + fn + “.jpg”; try { //改变图片大小 regulate = ImageUtil.regulate(source); large = ImageUtil.resizePhoto(regulate, tempPath + largeName, 500, 750); small = ImageUtil.resizePhoto(large, tempPath + smallName, 90, 120); byte[] largeByte = large.imageToBlob(new ImageInfo()); byte[] smallByte = small.imageToBlob(new ImageInfo()); //保存图片的目录 String uploadPath = “….”; //写文件到目录 FileUtil.getInstance().writeFile(uploadPath + largeName, largeByte); FileUtil.getInstance().writeFile(uploadPath + smallName, smallByte); //返回图片地址 Map map = new HashMap(); map.put(SIZENAME_LARGE, “/” + dir + largeName); map.put(SIZENAME_SMALL, “/” + dir + smallName); return map; } catch (Exception e) { throw e; } finally { if (small != null) small.destroyImages(); if (large != null) large.destroyImages(); if (regulate != null) regulate.destroyImages(); } } }
ImageUtil.regulate(MagickImage source) |
|
|