package com.zhyc.module.app.controller;

import com.zhyc.common.config.RuoYiConfig;
import com.zhyc.common.core.domain.AjaxResult;
import com.zhyc.module.app.util.OcrRecognizeUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.File;

//文字识别
@RestController
@RequestMapping("/ocr")
public class OrcController {

@PostMapping
public AjaxResult orcSheepNo(String path) {
    // 添加空值检查
    if (path == null || path.trim().isEmpty()) {
        return AjaxResult.error("路径不能为空");
    }

    try {
        OcrRecognizeUtil ocrRecognizeUtil = new OcrRecognizeUtil();
        // 移除 /profile
        path = path.replace("/profile", "");
        path = RuoYiConfig.getProfile() + path;

        // 验证文件是否存在
        File file = new File(path);
        if (!file.exists()) {
            return AjaxResult.error("文件不存在: " + path);
        }
        if (!file.isFile()) {
            return AjaxResult.error("路径不是一个有效文件: " + path);
        }
        if (!file.canRead()) {
            return AjaxResult.error("无权限读取文件: " + path);
        }

        String recognize = ocrRecognizeUtil.recognizeTwo(path);
        return AjaxResult.success("操作成功", recognize);
    } catch (Exception e) {
        return AjaxResult.error("OCR识别失败: " + e.getMessage());
    }
}

}
