summaryrefslogtreecommitdiff
path: root/src/main/java/info/selflearner/ocr/controller/ApiController.java
blob: d3723cab4ccab6a93b22a869b14bd512dd30da6f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package info.selflearner.ocr.controller;

import info.selflearner.ocr.util.Excel;
import info.selflearner.ocr.util.OCR;
import info.selflearner.ocr.model.Passport;
import net.sourceforge.tess4j.TesseractException;
import org.springframework.core.io.FileSystemResource;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.io.*;
import java.util.ArrayList;

@Controller
public class ApiController {

    @GetMapping("/")
    public String index(Model model) {
        String ocr = (String) model.asMap().get("ocr");
        model.addAttribute("ocr", ocr);
        return "index";
    }

    @PostMapping("/api/files")
    public String files(@RequestParam("files") MultipartFile[] files, String eng, String rus, String uzb, String uzb_cyrl, String documentType, RedirectAttributes redirectAttributes) throws IOException {
        ArrayList<String> ocrResults = new ArrayList<>();
        ArrayList<Passport> passportArrayList = new ArrayList<>();
        StringBuilder languages = new StringBuilder();

        if ("other".equals(documentType)) {
            if (eng != null) languages.append(eng).append("+");
            if (rus != null) languages.append(rus).append("+");
            if (uzb != null) languages.append(uzb).append("+");
            if (uzb_cyrl != null) languages.append(uzb_cyrl).append("+");
            languages.deleteCharAt(languages.toString().length() - 1);
        }

        for (MultipartFile file : files) {
            File savedFile = new File(System.getProperty("java.io.tmpdir") + "/" + file.getOriginalFilename());
            savedFile.createNewFile();
            try (InputStream inputStream = file.getInputStream();
                 OutputStream outputStream = new FileOutputStream(savedFile)) {
                int read;
                byte[] bytes = new byte[1024];

                while ((read = inputStream.read(bytes)) != -1) {
                    outputStream.write(bytes, 0, read);
                }
                if ("other".equals(documentType)) {
                    ocrResults.add(OCR.perform(savedFile, languages.toString()));
                } else if ("passport".equals(documentType)) {
                    passportArrayList.add(OCR.performPassport(savedFile));
                }
            } catch (IOException | TesseractException | InterruptedException exception) {
                throw new RuntimeException(exception);
            }
        }
        if ("other".equals(documentType)) {
            redirectAttributes.addFlashAttribute("ocrs", ocrResults);
        } else if ("passport".equals(documentType)) {
            redirectAttributes.addFlashAttribute("passports", passportArrayList);
            Excel.write(passportArrayList);
        }

        return "redirect:/";
    }

    @GetMapping("/api/passport/excel")
    @ResponseBody
    public FileSystemResource downloadPassportExcel(@RequestParam(value = "filename") String filename) {
        return new FileSystemResource(new File(System.getProperty("java.io.tmpdir") + "/" + filename));
    }

}