潘志宝
2024-11-14 afa8fc57084c423218c6816b710dfb2f0b87ea89
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
package com.iailab.module.data.video.hikvision.utils;
 
import lombok.extern.slf4j.Slf4j;
 
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
 
/**
 * @author PanZhibao
 * @Description
 * @createTime 2023年01月11日 11:03:00
 */
@Slf4j
public class PythonUtil {
 
    public static List<String> execPy(String[] command) {
        List<String> lines = new ArrayList<>();
        try {
            Process proc = Runtime.getRuntime().exec(command);
            BufferedReader in = new BufferedReader(new InputStreamReader(proc.getInputStream(),"GBK"));
            String line = null;
 
            while ((line = in.readLine()) != null) {
                log.info(line);
                lines.add(line);
            }
            // 0表示成功,1表示远程调用失败,2表示python脚本出错
            int waitFor = proc.waitFor();
            log.info("waitFor=" + waitFor);
            if (waitFor != 0) {
                throw new Exception("调用python失败");
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            log.error(ex.getMessage());
            log.error("运行python异常!command=" + command.toString());
        }
        return lines;
    }
}