如何使用java代码自动生成对应的krpano全景

  由于公司业务的需求,需要对app中用户与全景的数据对接到web页面上,就需要使用到krpano全景工具,这个工具可以到百度进行搜索,到对应的官网上面就可以进行下载了。

但是关于对krpano全景工具进行功能化,进行自动生成的教程和博客的话,就少之又少,因为是新手,我也是通过参考下面两个网站,来创建一个自己的生成全景工具。

http://blog.csdn.net/u012084981/article/details/76382991这个是原创的博客,提供使用java代码生成对应的全景。

http://www.krpano360.com/这里面就有关于krpano全景工具的一些文件和用法有一个详细的教程。(里面的东西已经全面介绍了工具是怎么使用的)


前面那些生成全景的就不说了,上面的网站都有,可以自己去看看。

这里贴出的是一个生成后的文件夹内容。

这个就是生成之后的文件夹。

在上面链接中说到了几个重要的文件


这些文件虽然都是在每个全景中都有所不同,但是我因为新手比较笨,而且需要吧每个全景文件复制到项目中

不同的文件夹中方便管理。如果只提取这几个文件的话,在公有的几个文件夹中

如这些,它里面也有对应的xml文件,里面所对应的url路径就会有所改变。

所以我就直接将整个文件复制到自己对应的文件夹中,然后进行操作。

其中只提取了一个公用的tour.html,(因为这个完全相同)。


这里贴出核心工具类

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import java.io.*;

public class CmdBat {
   
   public static void main(String[] args) {
//    Room r = new Room();
//    String dpath = "D:\\Tomcat 8.5\\webapps\\ujum-file\\vshow";
//    String file = "0049132f-25dd-4984-8505-f6bdf624c6c3";
//    String[] fn1 = { "1049132f-25dd-4984-8505-f6bdf624c6c3",
//          "2049132f-25dd-4984-8505-f6bdf624c6c3","3049132f-25dd-4984-8505-f6bdf624c6c3" };
//    String[] fn2 = { "客厅", "卧室","大客厅" };
//    String title = "哈哈哈哈哈哈哈哈";
//    try {
//       setKrpano(r,dpath, file, fn1, fn2, title,music);
//    } catch (InterruptedException e) {
//       e.printStackTrace();
//       System.out.println("上传失败");
//    }
//
      String dpath = "D:\\project\\ualbum3\\WebRoot\\static\\vr";//图片需要复制的路径
      final String temppath = "D:\\temp-room";//图片存放的位置
      String[] fn1={"1","2","3","4","5","6"};
      String title = "哈哈哈哈";
      String file = "\\102";
      try {
         setKrpano(dpath, temppath,file, fn1, fn1, title);
      } catch (InterruptedException e) {
         System.out.println("上传失败" );
      }
   }

   public static void setKrpano(final String dpath,final String temppath, final String file,
         final String[] fn1, final String[] fn2, final String title)
         throws InterruptedException {
//    final String temppath = "D:\\temp-room";//图片存放的位置
      String ex = "krpanotools64.exe makepano -config=templates\\vtour-multires.config "
            + temppath + "*.jpg";//这里是krpano的文件路径
      Runtime runtime = Runtime.getRuntime();
      boolean flag = true;
      Process p = null;
      try {
         p = runtime.exec("cmd /c start D:\\krpano\\" + ex);//这里是在Windows系统中使用命令行生成全景
      } catch (Exception e) {
         flag = false;
      }
      if (flag) {
         final InputStream is1 = p.getInputStream();
         final InputStream is2 = p.getErrorStream();
         new Thread() {
            public void run() {
               BufferedReader br1 = new BufferedReader(
                     new InputStreamReader(is1));
               try {
                  String line1 = null;
                  while ((line1 = br1.readLine()) != null) {
                     if (line1 != null) {
                        System.out.println("=AA==========line1======"
                              + line1);
                     }
                  }
               } catch (IOException e) {
                  e.printStackTrace();
               } finally {
                  try {
                     is1.close();
                     // 执行文件复制
                     File f = new File(dpath  + file);
                     f.mkdirs();// 创建目录
                     // 复制文件
                     boolean b1 = copyFile(temppath
                           + "vtour\\tour.js", dpath  + file
                           + "tour.js");
                     if (b1) {
                        boolean b2 = copyFile(temppath
                              + "vtour\\tour.swf", dpath
                              + file + "tour.swf");
                        if (b2) {
                           boolean b3 = copyFile(temppath
                                 + "vtour\\tour.xml", dpath
                                  + file + "tour.xml");
                           if (b3) {
                              // 复制文件夹
                              boolean b4 = copyFolder(
                                    temppath
                                          + "vtour\\panos",
                                    dpath + file + "panos");
                              boolean b5 = copyFolder(
                                    dpath
                                          + "images",
                                    dpath  + file + "images");
                              boolean b6 = copyFolder(
                                    dpath
                                          + "skin",
                                    dpath  + file + "skin");
                              //复制公用插件
                              boolean b7 = copyFolder(
                                    dpath
                                          + "plugins",
                                    dpath + file + "plugins");
                              if (b4 && b7 && b6) {
                                 // 删除临时生成文件
                                 delFolder(dpath+"\\"+"vtour");
                                 // 修改krpano文件内容
                                 String xmlPath = dpath
                                       + file + "tour.xml";
                                 File xmlFile = new File(xmlPath);
                                 DocumentBuilderFactory dbFactory = DocumentBuilderFactory
                                       .newInstance();
                                 DocumentBuilder dBuilder;
                                 try {
                                    dBuilder = dbFactory
                                          .newDocumentBuilder();
                                    Document doc = dBuilder
                                          .parse(xmlFile);
                                    doc.getDocumentElement()
                                          .normalize();
                                    for (int i = 0; i < fn1.length; i++) {
                                       //修改文件名称
                                       updateAttributeValue(doc,
                                             fn1[i], fn2[i]);
                                    }

                                    // update Element value
                                    updateElementValue(doc, title);

                                    // delete element
                                    deleteElement(doc);

                                    // add new element
                                    addElement(doc);

                                    updateAttributeColorValue(doc,
                                          "0x000000");
//                                  addMusicElement(doc,music);
                                    // write the updated document to
                                    // file or console
                                    doc.getDocumentElement()
                                          .normalize();
                                    TransformerFactory transformerFactory = TransformerFactory
                                          .newInstance();
                                    Transformer transformer = transformerFactory
                                          .newTransformer();
                                    DOMSource source = new DOMSource(
                                          doc);
                                    StreamResult result = new StreamResult(
                                          new File(xmlPath));
                                    transformer.setOutputProperty(
                                          OutputKeys.INDENT,
                                          "yes");
                                    transformer.transform(source,
                                          result);
                                    //生成成功
//                                  r.setMark("1");
//                                  AdminService as = ContextUtil.getBean(AdminService.class, "adminService");
//                                  as.updateRoom(r);
                                    /*System.out
                                          .println("XML file updated successfully");*/

                                 } catch (Exception e1){
                                    e1.printStackTrace();
                                    //生成失败
//                                  r.setMark("2");
//                                  AdminService as = ContextUtil.getBean(AdminService.class, "adminService");
//                                  as.updateRoom(r);
                                 }

                              }
                           }
                        }
                     }
                  } catch (IOException e) {
                     e.printStackTrace();
                  }
               }
            }
         }.start();
         new Thread() {
            public void run() {
               BufferedReader br2 = new BufferedReader(
                     new InputStreamReader(is2));
               try {
                  String line2 = null;
                  while ((line2 = br2.readLine()) != null) {
                     if (line2 != null) {
                        System.out.println("=AA==========line2======"
                              + line2);
                     }
                  }
               } catch (IOException e) {
                  e.printStackTrace();
               } finally {
                  try {
                     is2.close();
                  } catch (IOException e) {
                     e.printStackTrace();
                  }
               }
            }
         }.start();
         p.waitFor();
         p.destroy();
      } else {
         System.out.println("上传失败");
      }

   }

   /**
    * 复制单个文件
    * 
    * @param oldPath
    *            String 原文件路径 如:c:/fqf.txt
    * @param newPath
    *            String 复制后路径 如:f:/fqf.txt
    * @return boolean
    */
   public static boolean copyFile(String oldPath, String newPath) {
      try {
         int bytesum = 0;
         int byteread = 0;
         File oldfile = new File(oldPath);
         if (oldfile.exists()) { // 文件存在时
            InputStream inStream = new FileInputStream(oldPath); // 读入原文件
            FileOutputStream fs = new FileOutputStream(newPath);
            byte[] buffer = new byte[1444];
            int length;
            while ((byteread = inStream.read(buffer)) != -1) {
               bytesum += byteread; // 字节数 文件大小
               // System.out.println(bytesum);
               fs.write(buffer, 0, byteread);
            }
            inStream.close();
         }
      } catch (Exception e) {
         // System.out.println("复制单个文件操作出错");
         e.printStackTrace();
         return false;
      }
      return true;
   }

   /**
    * 复制整个文件夹内容
    * 
    * @param oldPath
    *            String 原文件路径 如:c:/fqf
    * @param newPath
    *            String 复制后路径 如:f:/fqf/ff
    * @return boolean
    */
   public static boolean copyFolder(String oldPath, String newPath) {
      try {
         (new File(newPath)).mkdirs(); // 如果文件夹不存在 则建立新文件夹
         File a = new File(oldPath);
         String[] file = a.list();
         File temp = null;
         for (int i = 0; i < file.length; i++) {
            if (oldPath.endsWith(File.separator)) {
               temp = new File(oldPath + file[i]);
            } else {
               temp = new File(oldPath + File.separator + file[i]);
            }

            if (temp.isFile()) {
               FileInputStream input = new FileInputStream(temp);
               FileOutputStream output = new FileOutputStream(newPath
                     + "/" + (temp.getName()).toString());
               byte[] b = new byte[1024 * 5];
               int len;
               while ((len = input.read(b)) != -1) {
                  output.write(b, 0, len);
               }
               output.flush();
               output.close();
               input.close();
            }
            if (temp.isDirectory()) {// 如果是子文件夹
               copyFolder(oldPath + "/" + file[i], newPath + "/" + file[i]);
            }
         }
      } catch (Exception e) {
         // System.out.println("复制整个文件夹内容操作出错");
         e.printStackTrace();
         return false;
      }
      return true;
   }

   // 删除文件夹
   public static void delFolder(String folderPath) {
      try {
         delAllFile(folderPath); // 删除完里面所有内容
         String filePath = folderPath;
         filePath = filePath.toString();
         java.io.File myFilePath = new java.io.File(filePath);
         myFilePath.delete(); // 删除空文件夹
      } catch (Exception e) {
         e.printStackTrace();
      }
   }

   public static boolean delAllFile(String path) {
      boolean flag = false;
      File file = new File(path);
      if (!file.exists()) {
         return flag;
      }
      if (!file.isDirectory()) {
         return flag;
      }
      String[] tempList = file.list();
      File temp = null;
      for (int i = 0; i < tempList.length; i++) {
         if (path.endsWith(File.separator)) {
            temp = new File(path + tempList[i]);
         } else {
            temp = new File(path + File.separator + tempList[i]);
         }
         if (temp.isFile()) {
            temp.delete();
         }
         if (temp.isDirectory()) {
            delAllFile(path + "/" + tempList[i]);// 先删除文件夹里面的文件
            delFolder(path + "/" + tempList[i]);// 再删除空文件夹
            flag = true;
         }
      }
      return flag;
   }

   private static void addElement(Document doc) {
      NodeList employees = doc.getElementsByTagName("krpano");
      Element emp = null;

      // loop for each employee
      for (int i = 0; i < employees.getLength(); i++) {
         emp = (Element) employees.item(i);
         Element skin = doc.createElement("include");
         skin.setAttribute("url", "skin/ac-black/skin.xml");
         emp.appendChild(skin);
         Element showtext = doc.createElement("include");
         showtext.setAttribute("url", "%SWFPATH%/plugins/showtext.xml");
         emp.appendChild(showtext);
         Element start = doc.createElement("include");
         start.setAttribute("url", "plugins/acp/start.xml");
         emp.appendChild(start);
         Element anihost = doc.createElement("include");
         anihost.setAttribute("url", "plugins/acp/anihost.xml");
         emp.appendChild(anihost);
      }
   }
   private static void addMusicElement(Document doc,String music) {
      NodeList employees = doc.getElementsByTagName("krpano");
      Element emp = null;

      // loop for each employee
      for (int i = 0; i < employees.getLength(); i++) {
         emp = (Element) employees.item(i);
         Element musicEl = doc.createElement("action");
         musicEl.setAttribute("name", "bgsnd_action");
         musicEl.setAttribute("autorun", "onstart");
         musicEl.appendChild(doc.createTextNode("playsound(bgsnd, '"+music+"', 0);"));
         emp.appendChild(musicEl);
      }
   }
   private static void deleteElement(Document doc) {
      NodeList employees = doc.getElementsByTagName("krpano");
      Element emp = null;
      // loop for each employee
      for (int i = 0; i < employees.getLength(); i++) {
         emp = (Element) employees.item(i);
         Node genderNode = emp.getElementsByTagName("include").item(0);
         emp.removeChild(genderNode);
      }

   }

   private static void updateElementValue(Document doc, String title) {
      NodeList employees = doc.getElementsByTagName("krpano");
      Element emp = null;
      // loop for each employee
      for (int i = 0; i < employees.getLength(); i++) {
         emp = (Element) employees.item(i);
         emp.setAttribute("title", title);
      }
   }

   private static void updateAttributeValue(Document doc, String oldname,
         String newname) {
      NodeList employees = doc.getElementsByTagName("scene");
      Element emp = null;
      // loop for each employee
      for (int i = 0; i < employees.getLength(); i++) {
         emp = (Element) employees.item(i);
//       if (emp.getAttribute("title").equals(oldname)) {
            emp.setAttribute("title", newname);
//          break;
//       }
      }
   }

   private static void updateAttributeColorValue(Document doc, String newname) {
      NodeList employees = doc.getElementsByTagName("skin_settings");
      Element emp = null;
      // loop for each employee
      for (int i = 0; i < employees.getLength(); i++) {
         emp = (Element) employees.item(i);
         emp.setAttribute("design_bgcolor", newname);
         emp.setAttribute("design_bgalpha", "0.8");
      }
   }
}


最主要的还是对路径理清楚之后,就使用起来了。

第一次写博客,感觉文笔不是太好,只希望能帮到别人之余,还可以让自己巩固一下知识。

这里可能有些写法不是太好,而却krpano中还有很多用法还没有懂,也希望看过的朋友,有什么好的改进意见可以在留言区

大家探讨一下。

还有就是文件生成的问题。这个生成全景的文件大概十多m一个,可能对项目服务器的存储大小是一个压力,我也积极在

为这个问题进行解决,一样是如果有什么好的改进建议可以在下面的留言区大家探讨一下。大笑

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值