本站首页    管理页面    写新日志    退出


天堂之光 人间希望

你我共同品味

JAVA的浓香.

Linux的清芬.

«February 2026»
1234567
891011121314
15161718192021
22232425262728


我的分类(专题)

日志更新

最新评论

留言板

链接

联系我

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)


阅读全文(4389) | 回复(0) | 编辑 | 精华
 



发表评论:
昵称:
密码:
主页:
标题:
验证码:  (不区分大小写,请仔细填写,输错需重写评论内容!)



站点首页 | 联系我们 | 博客注册 | 博客登陆

Sponsored By W3CHINA
W3CHINA Blog 0.8 Processed in 0.656 second(s), page refreshed 144809769 times.
《全国人大常委会关于维护互联网安全的决定》  《计算机信息网络国际联网安全保护管理办法》
苏ICP备05006046号