| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106 |
- import cn.reghao.tnb.file.app.zdisk.model.vo.FileTree;
- import cn.reghao.tnb.oss.api.constant.ObjectType;
- import net.sourceforge.tess4j.Tesseract;
- import org.junit.jupiter.api.Test;
- import org.mockito.junit.MockitoJUnitRunner;
- import java.io.File;
- import java.io.IOException;
- import java.nio.file.*;
- import java.nio.file.attribute.BasicFileAttributes;
- import java.util.ArrayList;
- import java.util.List;
- import java.util.Map;
- import java.util.TreeMap;
- import java.util.stream.Collectors;
- public class DiskUnitTest {
- void walkDir(Path path) throws IOException {
- Files.walkFileTree(path, new FileVisitor<Path>() {
- @Override
- public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
- File dir1 = dir.toFile();
- process(dir1);
- return FileVisitResult.CONTINUE;
- }
- @Override
- public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
- File file1 = file.toFile();
- process(file1);
- return FileVisitResult.CONTINUE;
- }
- @Override
- public FileVisitResult visitFileFailed(Path file, IOException exc) throws IOException {
- return FileVisitResult.CONTINUE;
- }
- @Override
- public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
- return FileVisitResult.CONTINUE;
- }
- });
- }
- List<FileTree> fileTreeList = new ArrayList<>();
- private void process(File file) {
- String basePath = dir.getAbsolutePath();
- String absolutePath = file.getAbsolutePath();
- String path = absolutePath.replace(basePath, "");
- String pid = file.getParent().replace(basePath, "");
- if (path.equals("")) {
- pid = "0";
- } else if (pid.equals("")) {
- pid = "/";
- }
- int fileType = ObjectType.Image.getCode();
- if (file.isDirectory()) {
- fileType = ObjectType.Dir.getCode();
- }
- String fileId = absolutePath.replace(basePath, "");
- FileTree fileTree = new FileTree(pid, fileId, fileType);
- fileTreeList.add(fileTree);
- }
- String baseDir = "/home/reghao/Downloads/";
- File dir = new File(baseDir);
- Map<String, List<FileTree>> getTreeMap() throws IOException {
- Path path = Paths.get(dir.getAbsolutePath());
- walkDir(path);
- Map<String, List<FileTree>> pidGroup = fileTreeList.stream()
- .collect(Collectors.groupingBy(FileTree::getPid));
- return new TreeMap<>(pidGroup);
- }
- public void treeTest() throws Exception {
- /*Map<String, List<FileTree>> pidTreeMap = getTreeMap();
- pidTreeMap.forEach((key, list) -> {
- String pid = key;
- for (FileTree fileTree : list) {
- String fileId = fileTree.getFileId();
- }
- });
- System.out.println("main-thread goto sleep...");*/
- }
- @Test
- public void ocrTest() throws Exception {
- Tesseract tess = new Tesseract();
- // 字库位置
- tess.setDatapath("/home/reghao/Downloads/tess");
- // 中英文混合
- tess.setLanguage("eng+chi_sim");
- //tess.setLanguage("chi_sim");
- // tess.setLanguage("eng");
- String imagePath = "/home/reghao/Downloads/1620438.png";
- String result = tess.doOCR(new File(imagePath));
- System.out.println(result);
- }
- }
|