OBJToGLBUtil.java 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. package com.fdkankan.common.util;
  2. import cn.hutool.core.img.ImgUtil;
  3. import cn.hutool.core.io.FileUtil;
  4. import cn.hutool.core.io.file.FileReader;
  5. import cn.hutool.core.util.RuntimeUtil;
  6. import com.fdkankan.common.constant.ErrorCode;
  7. import com.fdkankan.common.exception.BusinessException;
  8. import java.awt.image.BufferedImage;
  9. import java.io.File;
  10. import java.util.LinkedHashSet;
  11. import java.util.List;
  12. import java.util.Locale;
  13. import lombok.extern.slf4j.Slf4j;
  14. @Slf4j
  15. public class OBJToGLBUtil {
  16. /**
  17. *
  18. * @param objPath obj文件的目录
  19. * @param glbPath glb文件的最对路径
  20. */
  21. public static void objToGlb(String objPath, String glbPath) {
  22. OBJToGLBUtil.checkObj(objPath);
  23. objPath = OBJToGLBUtil.getObj(objPath);
  24. String command = "obj2gltf -i " + objPath + " -o " + glbPath;
  25. log.info("开始执行obj转换gbl命令-{}", command);
  26. Process exec = RuntimeUtil.exec(command);
  27. log.info("结束执行obj转换gbl命令-{}", command);
  28. }
  29. static String getObj(String objPath) {
  30. List<File> files = FileUtil.loopFiles(objPath);
  31. if (objPath.toLowerCase(Locale.ROOT).contains(".obj")) {
  32. return objPath;
  33. }
  34. for (File file2 : files) {
  35. if (file2.isDirectory()) {
  36. continue;
  37. }
  38. if (FileUtil.extName(file2.getName()).toLowerCase(Locale.ROOT).equals("obj")) {
  39. return file2.getAbsolutePath();
  40. }
  41. }
  42. return "";
  43. }
  44. private static boolean checkObj(String objPath) {
  45. if (objPath.contains(".obg")) {
  46. objPath = FileUtil.file(objPath).getParent();
  47. }
  48. File file1 = new File(objPath);
  49. File[] files = file1.listFiles();
  50. if (files == null || files.length <= 0) {
  51. throw new BusinessException(ErrorCode.FAILURE_CODE_7014);
  52. }
  53. File mtlFile = null;
  54. File objFile = null;
  55. for (File file2 : files) {
  56. if (file2.getName().endsWith(".obj")) {
  57. // if (StringUtils.isChinese(file2.getName())) {
  58. // throw new ServiceException(HttpStatus.e7008.getMsg(), HttpStatus.e7008.getCode());
  59. // }
  60. objFile = file2;
  61. }
  62. if (file2.getName().endsWith(".mtl")) {
  63. // if (StringUtils.isChinese(file2.getName())) {
  64. // throw new ServiceException(HttpStatus.e7008.getMsg(), HttpStatus.e7008.getCode());
  65. // }
  66. mtlFile = file2;
  67. }
  68. if (FileUtil.getType(file2).equals("jpg") || FileUtil.getType(file2).equals("png")) {
  69. BufferedImage read = ImgUtil.read(file2);
  70. int widthImg = read.getWidth();
  71. int heightImg = read.getHeight();
  72. if (widthImg > 2048 && heightImg > 2048) {
  73. OBJToGLBUtil.log.info("尺寸大2k,执行压缩");
  74. ImgUtil.scale(file2,
  75. file2,
  76. 2048, 2048, null);
  77. }
  78. }
  79. }
  80. if (mtlFile == null || objFile == null) {
  81. throw new BusinessException(ErrorCode.FAILURE_CODE_5059);
  82. }
  83. return OBJToGLBUtil.checkMtl(file1, mtlFile);
  84. }
  85. private static boolean checkMtl(File allFile, File file) {
  86. if (!file.getName().endsWith(".mtl")) {
  87. return false;
  88. }
  89. LinkedHashSet<String> imgName = new LinkedHashSet<>();
  90. if (allFile == null || allFile.length() <= 0) {
  91. return false;
  92. }
  93. File[] files = allFile.listFiles();
  94. if (files == null || files.length <= 0) {
  95. return false;
  96. }
  97. for (File listFile : files) {
  98. String modelName = listFile.getName();
  99. imgName.add(modelName);
  100. }
  101. LinkedHashSet<String> imgMtl = OBJToGLBUtil.readMtlFile(file.getPath());
  102. for (String mtlName : imgMtl) {
  103. if (!imgName.contains(mtlName)) {
  104. throw new BusinessException(ErrorCode.FAILURE_CODE_5065);
  105. }
  106. }
  107. return true;
  108. }
  109. private static LinkedHashSet<String> readMtlFile(String mtlPath) {
  110. LinkedHashSet<String> imgName = new LinkedHashSet<>();
  111. FileReader fileReader = new FileReader(mtlPath);
  112. try {
  113. List<String> lines = fileReader.readLines();
  114. for (String line : lines) {
  115. String[] tempsa = line.split("[ ]+");
  116. if (tempsa[0].trim().equals("map_Ka")) {
  117. imgName.add(tempsa[1]);
  118. }
  119. if (tempsa[0].trim().equals("map_Kd")) {
  120. imgName.add(tempsa[1]);
  121. }
  122. }
  123. } catch (Exception e) {
  124. e.printStackTrace();
  125. }
  126. return imgName;
  127. }
  128. public static void main(String[] args) {
  129. System.out.println(OBJToGLBUtil.checkObj("F:\\test\\新建文件夹\\police模型"));
  130. }
  131. }