DiskUnitTest.java 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. import cn.reghao.tnb.file.app.zdisk.model.vo.FileTree;
  2. import cn.reghao.tnb.oss.api.constant.ObjectType;
  3. import net.sourceforge.tess4j.Tesseract;
  4. import org.junit.jupiter.api.Test;
  5. import org.mockito.junit.MockitoJUnitRunner;
  6. import java.io.File;
  7. import java.io.IOException;
  8. import java.nio.file.*;
  9. import java.nio.file.attribute.BasicFileAttributes;
  10. import java.util.ArrayList;
  11. import java.util.List;
  12. import java.util.Map;
  13. import java.util.TreeMap;
  14. import java.util.stream.Collectors;
  15. public class DiskUnitTest {
  16. void walkDir(Path path) throws IOException {
  17. Files.walkFileTree(path, new FileVisitor<Path>() {
  18. @Override
  19. public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
  20. File dir1 = dir.toFile();
  21. process(dir1);
  22. return FileVisitResult.CONTINUE;
  23. }
  24. @Override
  25. public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
  26. File file1 = file.toFile();
  27. process(file1);
  28. return FileVisitResult.CONTINUE;
  29. }
  30. @Override
  31. public FileVisitResult visitFileFailed(Path file, IOException exc) throws IOException {
  32. return FileVisitResult.CONTINUE;
  33. }
  34. @Override
  35. public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
  36. return FileVisitResult.CONTINUE;
  37. }
  38. });
  39. }
  40. List<FileTree> fileTreeList = new ArrayList<>();
  41. private void process(File file) {
  42. String basePath = dir.getAbsolutePath();
  43. String absolutePath = file.getAbsolutePath();
  44. String path = absolutePath.replace(basePath, "");
  45. String pid = file.getParent().replace(basePath, "");
  46. if (path.equals("")) {
  47. pid = "0";
  48. } else if (pid.equals("")) {
  49. pid = "/";
  50. }
  51. int fileType = ObjectType.Image.getCode();
  52. if (file.isDirectory()) {
  53. fileType = ObjectType.Dir.getCode();
  54. }
  55. String fileId = absolutePath.replace(basePath, "");
  56. FileTree fileTree = new FileTree(pid, fileId, fileType);
  57. fileTreeList.add(fileTree);
  58. }
  59. String baseDir = "/home/reghao/Downloads/";
  60. File dir = new File(baseDir);
  61. Map<String, List<FileTree>> getTreeMap() throws IOException {
  62. Path path = Paths.get(dir.getAbsolutePath());
  63. walkDir(path);
  64. Map<String, List<FileTree>> pidGroup = fileTreeList.stream()
  65. .collect(Collectors.groupingBy(FileTree::getPid));
  66. return new TreeMap<>(pidGroup);
  67. }
  68. public void treeTest() throws Exception {
  69. /*Map<String, List<FileTree>> pidTreeMap = getTreeMap();
  70. pidTreeMap.forEach((key, list) -> {
  71. String pid = key;
  72. for (FileTree fileTree : list) {
  73. String fileId = fileTree.getFileId();
  74. }
  75. });
  76. System.out.println("main-thread goto sleep...");*/
  77. }
  78. @Test
  79. public void ocrTest() throws Exception {
  80. Tesseract tess = new Tesseract();
  81. // 字库位置
  82. tess.setDatapath("/home/reghao/Downloads/tess");
  83. // 中英文混合
  84. tess.setLanguage("eng+chi_sim");
  85. //tess.setLanguage("chi_sim");
  86. // tess.setLanguage("eng");
  87. String imagePath = "/home/reghao/Downloads/1620438.png";
  88. String result = tess.doOCR(new File(imagePath));
  89. System.out.println(result);
  90. }
  91. }