tesseract ocr的java变量问题

lrpiutwd  于 2021-06-29  发布在  Java
关注(0)|答案(0)|浏览(235)

我在springbootjava中使用了这段代码来创建ocr应用程序。

@Controller
public class FileUploadController {

    @RequestMapping("/")
    public String index() {
        return "upload";
    }

    @RequestMapping(value = "/upload", method = RequestMethod.POST)
    public RedirectView singleFileUpload(@RequestParam("file") MultipartFile file,
                                   RedirectAttributes redirectAttributes, Model model) throws IOException, TesseractException {

        byte[] bytes = file.getBytes();
        Path path = Paths.get("C:/Users/ashwi/Downloads/javaocr9/src/main/resources/static" + file.getOriginalFilename());
        Files.write(path, bytes);

        File convFile = convert(file);
        Tesseract tesseract = new Tesseract();
        tesseract.setDatapath("C:/Users/ashwi/Downloads/ocr-tess4j-example-master/ocr-tess4j-example-master/tessdata");
        String text = tesseract.doOCR(file2);
        redirectAttributes.addFlashAttribute("file", file);
        redirectAttributes.addFlashAttribute("text", text);
        return new RedirectView("result");
    }

    @RequestMapping("/result")
    public String result() {
        return "result";
    }

    public static File convert(MultipartFile file) throws IOException {
        File convFile = new File(file.getOriginalFilename());
        convFile.createNewFile();
        FileOutputStream fos = new FileOutputStream(convFile);
        fos.write(file.getBytes());
        fos.close();
        return convFile;
    }
}

但是,以下位置似乎有错误:

String text = tesseract.doOCR(file2);

在visualstudio代码中,我遇到一个错误:“file2不能解析为变量”。
有什么想法吗?我是java新手,在修复它时遇到了困难。
这个版本的代码来自:https://stackabuse.com/tesseract-simple-java-optical-character-recognition/

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题