1. 安装过程(如图所示)

、Exceptionin thread "main" java.lang.UnsatisfiedLinkError:C:\WINDOWS\system32\jmagick.dll: Can't find dependent libraries

我在Windows xp上按照上面的流程安装完成,按理说应该没有问题了,但出现了以上错误。

我打开Eclipse已工作数个小时,然后在开发机器(我本机)Windows xp上安装ImageMagick-6.3.9-0-Q16-windows-dll.exe。也把jmagick.dll放入system32,在你的java项目中使用jmagick.jar包处理图片。

就这么几步当然不会错,但在运行程序时还是出现了上面的错误。

解决:重启Eclipse。加载新的环境变量。

 

七、代码实现

package com.xxx.tpl.utils;

 

import java.awt.Dimension;

import java.awt.Point;

import java.awt.Rectangle;

import java.io.File;

import java.io.IOException;

 

import magick.CompositeOperator;

import magick.CompressionType;

import magick.DrawInfo;

import magick.ImageInfo;

import magick.MagickException;

import magick.MagickImage;

import magick.PixelPacket;

import magick.PreviewType;

 

public class JmagickUtil {

         public static void main(String[] args) throws MagickException, IOException {

 

                   // test for function imageResize

 

                   JmagickUtil.imageResize("E:/UCMSServer/tomcat/webapps/temp/tpl/snapshot/2016/9/25955ea769ff47ada6b84f7b39cee904.png",

                                                     "D:/Installed/tomcat/webapps/tpl/special/2016/9/d26076f602284c3eac1abaa71d2378b2/thumbnail/big/2016/9/25955ea769ff47ada6b84f7b39cee904.png", 160, 160);

                   // JMagicjWrapper.imageResize("pics.jpg", "reSize250x200.jpeg", 250,

                   // 200);

                   // JMagicjWrapper.imageResize("pics.jpg", "reSize50x50.jpg", 50, 50);

                   // JMagicjWrapper.imageResize("pics.jpg", "reSize120x120.bmp", 120,

                   // 120);

                   // JMagicjWrapper.imageResize("pics.jpg", "reSize.tif", 20, 30);//not

                   // create file

                   //           

 

                   // test for function createWaterPrintByImg

//               JmagickUtil.createWaterPrintByImg("f://1.jpg", "f://4.jpg",

//                                 "f://watermark.gif", new Point(0, 0));

                   // JMagicjWrapper.imageResize("wpl.gif", "logo250x200.gif", 250, 200);

                   // Because file "wpl.gif" may not be release, the later function cause a

                   // error, can not open file handle.

                   // JMagicjWrapper.createWaterPrintByImg("pics.jpg", "wpl.gif",

                   // "logoFull.jpg", new Point(1680,1050));//not create file

                   // JMagicjWrapper.createWaterPrintByImg("pics.jpg", "wpl.gif",

                   // "logoExt.jpg", new Point(2000,1000));//not create file

 

                   // test for function createWaterPrintByText

                   // This function can not handle Chinese Character, I'll continue to

                   // takle the issue

                   // JMagicjWrapper.createWaterPrintByText("pics1.jpg", "wpt.gif",

                   // "For Test", new Point(300,300), 100);

         }

 

         private static final String[] Type = { ".JPG", ".JPEG", ".BMP", ".GIF",

                            ".PNG" };

 

         public static boolean checkType(String path) {

                   for (int i = 0; i < Type.length; i++) {

                            if (path.toUpperCase().endsWith(Type[i])) {

                                     return true;

                            } else {

                                     continue;

                            }

                   }

                   return false;

         }

 

         /**

          * 改变图片大小

          *

          * @param filePath

          *            原图片位置 toPath 新图片位置 width 新图片的宽度 height 新图片的高度

          * @return

          * @throw

          * @author  2010-8-11

          * @throws IOException

          */

         public static void imageResize(String filePath, String toPath, int width,

                            int height) throws MagickException, IOException {

                   //必须写,否则jmagick.jar的路径找不到

        System.setProperty("jmagick.systemclassloader","no");

                  

        File fileOri = new File(filePath);

        File fileDes = new File(toPath);

        if (!fileOri.exists()) {

                            return;

                   }

        if (!fileDes.exists()) {

                            String parentPath = fileDes.getParent();

                            new File(parentPath).mkdirs();

                            fileDes.createNewFile();

                   }

       

                   ImageInfo info = null;

                   MagickImage image = null;

                   Dimension imageDim = null;

                   MagickImage scaled = null;

 

                   if (!checkType(filePath) || !checkType(toPath)) {

                            return;

                   }

 

                   try {

                            info = new ImageInfo(filePath);

                            image = new MagickImage(info);

                            imageDim = image.getDimension();

                            if (width <= 0 || height <= 0) {

                                     height = 120;

                                     width = 120;

                            }

                            scaled = image.scaleImage(width, height);

                            scaled.setFileName(toPath);

                            scaled.writeImage(info);

                   } finally {

                            if (scaled != null) {

                                     scaled.destroyImages();

                            }

                   }

         }

 

         /**

          * 创建图片水印

          *

          * @param filePath

          *            源文件路径 toImg 生成文件位置 logoPath logo路径 pos logo在源图片中的相对位置,以像素点为单位

          * @return

          * @throw MagickException

          * @author  2010-8-11

          */

         public static void createWaterPrintByImg(String filePath, String toImg,

                            String logoPath, Point pos) throws MagickException {

                   if (!checkType(filePath) || !checkType(toImg) || !checkType(logoPath)) {

                            return;

                   }

 

                   ImageInfo info = new ImageInfo();

                   MagickImage fImage = null;

                   MagickImage sImage = null;

                   MagickImage fLogo = null;

                   MagickImage sLogo = null;

                   Dimension imageDim = null;

                   Dimension logoDim = null;

                   try {

                            // 原来图片

                            fImage = new MagickImage(new ImageInfo(filePath));

                            imageDim = fImage.getDimension();

                            int width = imageDim.width;

                            int height = imageDim.height;

                            sImage = fImage.scaleImage(width, height);

 

                            fLogo = new MagickImage(new ImageInfo(logoPath));

                            logoDim = fLogo.getDimension();

                            int lw = logoDim.width;

                            int lh = logoDim.height;

                            sLogo = fLogo.scaleImage(lw, lh);

 

                            // 开始打水印,从左上角开始;如果到右边界则重新开始一行的打印(x=0,y=y+h)

                            int startX = 0;

                            int startY = 0;

                            do {

                                     sImage.compositeImage(CompositeOperator.AtopCompositeOp, sLogo,

                                                        startX, startY);

                                     startX += (logoDim.width + 60);

                                     if (startX >= width) {

                                               startY += logoDim.height * 2;

                                               startX = 0;

                                     }

                            } while (startY <= height);

 

                            sImage.setFileName(toImg);

                            sImage.writeImage(info);

                   } finally {

                            if (fImage != null) {

                                     fImage.destroyImages();

                            }

                            if (sImage != null) {

                                     sImage.destroyImages();

                            }

                            if (fLogo != null) {

                                     fLogo.destroyImages();

                            }

                            if (sLogo != null) {

                                     sLogo.destroyImages();

                            }

                   }

         }

 

         /**

          * 创建文字水印

          *

          * @param filePath

          *            源文件路径 toImg 生成文件位置 text 水印文本 pos logo在源图片中的相对位置,以像素点为单位

          *            pointSize 用于设置点阵大小

          * @return

          * @throw MagickException

          * @author  2010-8-11

          */

         public static void createWaterPrintByText(String filePath, String toImg,

                            String text, Point pos, int pointSize) throws MagickException {

                   if (!checkType(filePath) || !checkType(toImg)) {

                            return;

                   }

 

                   if (null == text || "".equals(text)) {

                            text = "";

                   }

 

                   ImageInfo info = new ImageInfo(filePath);

                   if (filePath.toUpperCase().endsWith("JPG")

                                     || filePath.toUpperCase().endsWith("JPEG")) {

                            info.setCompression(CompressionType.JPEGCompression); // 压缩类别为JPEG格式

                            info.setPreviewType(PreviewType.JPEGPreview); // 预览格式为JPEG格式

                            info.setQuality(95);

                   }

                   MagickImage aImage = new MagickImage(info);

                   Dimension imageDim = aImage.getDimension();

                   int width = imageDim.width;

                   int height = imageDim.height;

 

                   if (width <= (int) pos.getX() || height <= (int) pos.getY()) {

                            pos.setLocation(0, 0);

                   }

 

                   int a = 0;

                   int b = 0;

                   String[] as = text.split("");

                   for (String string : as) {

                            if (string.matches("[/u4E00-/u9FA5]")) {

                                     a++;

                            }

                            if (string.matches("[a-zA-Z0-9]")) {

                                     b++;

                            }

                   }

                   int tl = a * 12 + b * 6;// 字符长度

                   MagickImage scaled = aImage.scaleImage(width, height);

                   if (width > tl && height > 5) {

                            DrawInfo aInfo = new DrawInfo(info);

                            aInfo.setFill(PixelPacket.queryColorDatabase("white"));

                            aInfo.setUnderColor(new PixelPacket(65535, 65535, 65535, 65535));// 设置为透明颜色

                            aInfo.setPointsize(pointSize);

                            // 解决中文乱码问题,自己可以去随意定义个自己喜欢字体,我在这用的微软雅黑

                            String fontPath = "C:/WINDOWS/Fonts/MSIMHEI.TTF";

                            // String fontPath = "/usr/maindata/MSYH.TTF";

                            aInfo.setFont(fontPath);

                            aInfo.setTextAntialias(true);

                            aInfo.setOpacity(0);// 透明度

                            aInfo.setText(text);

                            aInfo

                                               .setGeometry("+"

                                                                 + ((int) pos.getX() + "+" + (int) pos.getY()));

                            scaled.annotateImage(aInfo);

                   }

                   scaled.setFileName(toImg);

                   scaled.writeImage(info);

                   scaled.destroyImages();

         }

 

         /**

          * 切取图片

          *

          * @param imgPath

          *            原图路径 toPath 生成文件位置 w 左上位置横坐标 h 左上位置竖坐标 x 右下位置横坐标 y 右下位置竖坐标

          * @return

          * @throw MagickException

          * @author  2010-8-11

          */

         public static void cutImg(String imgPath, String toPath, int w, int h,

                            int x, int y) throws MagickException {

                   ImageInfo infoS = null;

                   MagickImage image = null;

                   MagickImage cropped = null;

                   Rectangle rect = null;

                   try {

                            infoS = new ImageInfo(imgPath);

                            image = new MagickImage(infoS);

                            rect = new Rectangle(x, y, w, h);

                            cropped = image.cropImage(rect);

                            cropped.setFileName(toPath);

                            cropped.writeImage(infoS);

 

                   } finally {

                            if (cropped != null) {

                                     cropped.destroyImages();

                            }

                   }

         }

 

         /**

          * 删除图片文件

          *

          * @param src 图片位置

          * @return

          * @throw

          * @author 2010-8-11

          */

         public static boolean removeFile(String src) throws SecurityException {

                   try {

                            if (!checkType(src)) {

                                     return false;

                            }

 

                            File file = new File(src);

                            return file.delete();

                   } catch (Exception e) {

                            e.printStackTrace();

                            return false;

                   }

         }

 

}

 



在windows和Linux上安装ImageMagick与jmagick,Maven配置、Java图片压缩代码(整理网上、结合自己情况、编写出来的新安装方式)的更多相关文章

  1. Linux安装ImageMagick与JMagick完成过程及配置

    原文地址:http://www.iitshare.com/linux-install-imagemagick-jmagick.html 一.安装背景 最近在服务器上部署了HapiCMS的产品,因为其对 ...

  2. WINDOWS和Linux上安装php7 alpha 并安装 yaf

    WINDOWS和Linux上安装php7 alpha 并安装 yaf PHP技术  widuu  2个月前 (06-15)  126浏览  0评论 windows 1.windows上安装 php7 ...

  3. Windows 和 Linux 上Redis的安装守护进程配置

    # Windows 和 Linux 上Redis的安装守护进程配置 Redis 简介 ​ Redis是目前最常用的非关系型数据库(NOSql)之一,常以Key-Value的形式存储.Redis读写速度 ...

  4. 使用 MAUI 在 Windows 和 Linux 上绘制 PPT 的图表

    我在做一个图表工具软件,这个软件使用 MAUI 开发.我的需求是图表的内容需要和 PPT 的图表对接,需要用到 OpenXML 解析 PPT 内容,读取到 PPT 图表元素的内容,接着使用 MAUI ...

  5. 在Windows和Linux上安装paramiko模块以及easy_install的安装方法

    一.paramiko模块有什么用? paramiko是用python语言写的一个模块,遵循SSH2协议,支持以加密和认证的方式,进行远程服务器的连接.由于使用的是python这样的能够跨平台运行的语言 ...

  6. 在Windows和Linux上安装paramiko模块

    一.paramiko模块有什么用? paramiko是用python语言写的一个模块,遵循SSH2协议,支持以加密和认证的方式,进行远程服务器的连接.由于使用的是python这样的能够跨平台运行的语言 ...

  7. 从Windows想Linux上传文件 Linux(CentOS) 上安装vsftpd

    今天想在Linux上搭建个LAMP环境,以前用的Linux都安装了图形界面,但是这次用的阿里云服务器是纯命令模式,用起来有点不大适应. 最大的不适应就是获取apache等软件了,以前直接登录相应网站, ...

  8. mysql-5.7.xx在lcentos7下的安装以及mysql在windows以及linux上的性能差异

    前言: 在centos上安装mysql,整整折腾了将近一天,因为是第一次安装,的确是踩了不少坑,这里详细记录下来,方便各位有同样需求的小伙伴参考. 该选择什么版本? mysql5.7有很多小版本,但是 ...

  9. 使用Nginx在windows和linux上搭建集群

    Nginx Nginx (engine x) 是一个高性能的HTTP和反向代理服务器,也是一个IMAP/POP3/SMTP服务器 特点:反向代理 负载均衡 动静分离… 反向代理(Reverse Pro ...

随机推荐

  1. mysql之子查询作业

    #数据准备drop table if exists class;create table class(    class_no int(2) unsigned zerofill primary key ...

  2. Struts2--标签tag

    在Struts2框架中提供了一套标签库,可以与struts2无缝结合. 数据标签a.action.bean.date.debug.i18n.include.param.property.push.se ...

  3. bzoj 4547 小奇的集合

    Description 有一个大小为n的可重集S,小奇每次操作可以加入一个数a+b(a,b均属于S),求k次操作后它可获得的S的和的最大 值.(数据保证这个值为非负数) Input 第一行有两个整数n ...

  4. POJ1743 Musical Theme(二分+后缀数组)

    题目大概是给n个数组成的串,求是否有多个“相似”且不重叠的子串的长度大于等于5,两个子串相似当且仅当长度相等且每一位的数字差都相等. 这题是传说中楼教主男人八题之一,虽然已经是用后缀数组解决不可重叠最 ...

  5. 2015 多校联赛 ——HDU5353(构造)

    Each soda has some candies in their hand. And they want to make the number of candies the same by do ...

  6. bzoj 4010: [HNOI2015]菜肴制作

    Description 知名美食家小 A被邀请至ATM 大酒店,为其品评菜肴. ATM 酒店为小 A 准备了 N 道菜肴,酒店按照为菜肴预估的质量从高到低给予 1到N的顺序编号,预估质量最高的菜肴编号 ...

  7. [bzoj4822][Cqoi2017]老C的任务&[bzoj1935][Shoi2007]Tree 园丁的烦恼

    来自FallDream的博客,未经允许,请勿转载,谢谢. 老 C 是个程序员.     最近老 C 从老板那里接到了一个任务——给城市中的手机基站写个管理系统.作为经验丰富的程序员,老 C 轻松地完成 ...

  8. bzoj1767[Ceoi2009]harbingers 斜率优化dp

    1767: [Ceoi2009]harbingers Time Limit: 10 Sec  Memory Limit: 64 MBSubmit: 421  Solved: 112[Submit][S ...

  9. dev gridcontrol 无法编辑 解决方案

    1.确认表格打开编辑 gridView1.OptionsBehavior.Editable = True 2.确认列打开编辑 gridView1.Columns("Name").O ...

  10. 使用PHP脚本远程部署git项目

    准备工作: 1.coding.net创建私有项目 2.安装了Web服务 Git服务的服务器 服务器端: 1.nginx.php-fpm统一用www用户 www 目录,这个可以通过修改配置文件实现. [ ...