现在的位置: 首页 > 综合 > 正文

java调用windows/Linux/Unix 命令行执行命令的方法–调用ImageMagick的合并图形的命令

2017年12月02日 ⁄ 综合 ⁄ 共 2386字 ⁄ 字号 评论关闭

在实现图形合并的时候,因为对Jmagick的处理方式还不熟悉,但是Jmagick依赖于imageMagick,imageMagick又刚好有执行图像合并的命令。

因此想到了利用java调用windows命令行执行命令的方式来达到目的。

直接看代码:

	/**
	 * 合并图片
	 * @param srcPathList 源图片路径名称列表
	 * @param toPathFile	合并后图片路径名称
	 * @param orientation 合并方位:横向(+)、纵向(-)
	 * @return
	 */
	public  void convert(List srcPathList,String toPathFile,String orientation){
		/*	合并图片命令格式:
		 * convert +append 1.jpg 2.jpg 3.jpg ... 0.jpg //横向合并(最后一个为合并成功的文件名称)
		 * convert -append 1.jpg 2.jpg 3.jpg ... 0.jpg  //纵向合并(最后一个为合并成功的文件名称)
		*/
		StringBuffer command = new StringBuffer("");
		command.append("convert ");
		command.append(orientation+"append ");
		int length = srcPathList.size();
		for(int i=0;i<length;i++){
			command.append(srcPathList.get(i)+" ");
		}
		command.append(toPathFile);
		String[] str = {command.toString()};
		JmagickTest.exec(str);
	}
	
	/**
	 * 调用windows的命令行并执行命令
	 * @param command 命令行窗口中要执行的命令字符串
	 */
	public static  void  exec(String[] command) {  
        	Process proc;  
        	InputStream is = null;
        	String comman = command[0];
        	System.out.println("命令为:"+comman);
        	String[] cmd = {"cmd.exe","/c",comman};
        	try {
			proc = Runtime.getRuntime().exec(cmd);
			int result = proc.waitFor();
			System.out.println("Process result:" + result);
			is = proc.getInputStream();
			byte[] b = new byte[1024];
			is.read(b);
			System.out.println("b:" + b);
			is.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
    }  

通过将执行命令写入数组变量cmd,然后调用Runtime.getRuntime().exec(cmd)来执行此功能。这样就实现了调用java调用windows命令行执行命令的操作。

---------------------------------------------------------------------------------------------------------------------------------

针对不同的操作系统的判断,进行命令调用:

	/**
	 * 获取操作系统名称
	 * @return
	 */
	public static String getOS(){
		Properties pros = System.getProperties();
		String os = (String) pros.get("os.name");
		System.out.println(os);
		return os;
	}
	
	/**
	 * 根据操作系统类型来执行调用系统命令
	 * @param command
	 */
	public static  void  execWindowsOrLinux(String[] command) {  
    	Process proc = null;  
    	InputStream is = null;
    	String comman = command[0];
    	System.out.println("命令为:"+comman);
    	//String[] cmd = {"cmd.exe","/c",comman};
    	String osName = getOS();
    	try {
	    	if(osName.startsWith("Windows")){//windows下调用系统命令
	    		String[] cmdWindows = {"cmd.exe","/c",comman};
	    		proc = Runtime.getRuntime().exec(cmdWindows);
	    	}else if(osName.startsWith("Linux")){//Linux下调用系统命令
	    		String[] cmdLinux = {"/bin/sh","-c",comman};
	    		proc = Runtime.getRuntime().exec(cmdLinux);
	    	}else if(osName.startsWith("Unix")){
	    		String[] cmdUnix = {comman};
	    		proc = Runtime.getRuntime().exec(cmdUnix);
	    	}
		  int result = proc.waitFor();
		  System.out.println("Process result:" + result);
		  is = proc.getInputStream();
		  byte[] b = new byte[1024];
		  is.read(b);
		  System.out.println("b:" + b);
		  is.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	

抱歉!评论已关闭.