daShangDao_psiServer/ocr/ocr.go
2026-06-15 13:47:39 +08:00

56 lines
1.0 KiB
Go
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package ocr
import (
"fmt"
"os"
"os/exec"
"path/filepath"
"psi/config"
"runtime"
)
var (
ocrServiceCmd *exec.Cmd
)
func StartService() {
if err := startOCRService(); err != nil {
fmt.Fprintf(os.Stderr, "启动 OCR 服务失败: %v\n", err)
os.Exit(1)
}
fmt.Println("OCR 服务已就绪")
}
func startOCRService() error {
exePath := getOCRServiceExePath()
if _, err := os.Stat(exePath); os.IsNotExist(err) {
return fmt.Errorf("找不到 OCRService 可执行文件: %s", exePath)
}
ocrServiceCmd = exec.Command(exePath)
ocrServiceCmd.Stdout = os.Stdout
ocrServiceCmd.Stderr = os.Stderr
err := ocrServiceCmd.Start()
if err != nil {
return err
}
fmt.Println("OCR 服务进程已启动PID:", ocrServiceCmd.Process.Pid)
return nil
}
func getOCRServiceExePath() string {
basePath := config.AppConfig.OCR.ExeUrl
if runtime.GOOS == "windows" {
if filepath.Ext(basePath) != ".exe" {
return basePath + ".exe"
}
return basePath
}
if filepath.Ext(basePath) == ".exe" {
return basePath[:len(basePath)-4]
}
return basePath
}