Java调用python的核心代码

发布于:2024-04-01 ⋅ 阅读:(50) ⋅ 点赞:(0)
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//



import java.io.BufferedReader;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;

public class PPGPreprocessor {
    String exe_path = null;

    public PPGPreprocessor(String exe_path) {
        this.exe_path = exe_path;
    }

    public Wave preprocess(String file_path) {
        Process pr = null;

        try {
            pr = Runtime.getRuntime().exec(this.exe_path + " --file_path " + file_path);
        } catch (IOException var12) {
            throw new InvalidWaveException("Running exe error", -1);
        }

        BufferedReader in = new BufferedReader(new InputStreamReader(pr.getInputStream()));
        String line = null;
        ArrayList<Double> rawDCDataPoints = new ArrayList();
        ArrayList<Integer> startIndices = new ArrayList();
        ArrayList endIndices = new ArrayList();

        try {
            line = in.readLine();
            if (line.startsWith("Error")) {
                if (line.endsWith("0")) {
                    throw new InvalidWaveException("Reading data error", 0);
                }

                if (line.endsWith("1")) {
                    throw new InvalidWaveException("Wave not long enough", 1);
                }

                if (line.endsWith("2")) {
                    throw new InvalidWaveException("No valid wavelets detected", 2);
                }

                if (line.endsWith("3")) {
                    throw new InvalidWaveException("No valid waveform detected", 3);
                }
            }

            String[] var11;
            int var10 = (var11 = line.split(",")).length;

            String idx;
            int var9;
            for(var9 = 0; var9 < var10; ++var9) {
                idx = var11[var9];
                rawDCDataPoints.add(Double.parseDouble(idx.trim()));
            }

            line = in.readLine();
            var10 = (var11 = line.split(",")).length;

            for(var9 = 0; var9 < var10; ++var9) {
                idx = var11[var9];
                startIndices.add(Integer.parseInt(idx.trim()));
            }

            line = in.readLine();
            var10 = (var11 = line.split(",")).length;

            for(var9 = 0; var9 < var10; ++var9) {
                idx = var11[var9];
                endIndices.add(Integer.parseInt(idx.trim()));
            }

            in.close();
            pr.destroy();
            return new Wave(rawDCDataPoints, startIndices, endIndices);
        } catch (IOException var13) {
            throw new InvalidWaveException("Running exe error", -1);
        }
    }

    public Wave preprocess(double[] data) {
        String file_path = (new File(this.exe_path)).getParentFile().getAbsolutePath();
        File tempFile = null;

        FileWriter f;
        try {
            new File(file_path);
            tempFile = File.createTempFile("tempPPGData", ".txt");
            f = new FileWriter(tempFile);
            double[] var9 = data;
            int var8 = data.length;

            for(int var7 = 0; var7 < var8; ++var7) {
                double x = var9[var7];
                f.write(String.valueOf(x) + " ");
            }

            f.write(10);
            f.flush();
            f.close();
        } catch (IOException var16) {
            var16.printStackTrace();
        }

        file_path = tempFile.getAbsolutePath();
        f = null;

        Process pr;
        try {
            pr = Runtime.getRuntime().exec(this.exe_path + " --file_path " + file_path + " --file_format " + "array");
        } catch (IOException var14) {
            throw new InvalidWaveException("Running exe error", -1);
        }

        BufferedReader in = new BufferedReader(new InputStreamReader(pr.getInputStream()));
        String line = null;
        ArrayList<Double> rawDCDataPoints = new ArrayList();
        ArrayList<Integer> startIndices = new ArrayList();
        ArrayList endIndices = new ArrayList();

        try {
            line = in.readLine();
            if (line.startsWith("Error")) {
                if (line.endsWith("0")) {
                    throw new InvalidWaveException("Reading data error", 0);
                }

                if (line.endsWith("1")) {
                    throw new InvalidWaveException("Wave not long enough", 1);
                }

                if (line.endsWith("2")) {
                    throw new InvalidWaveException("No valid wavelets detected", 2);
                }

                if (line.endsWith("3")) {
                    throw new InvalidWaveException("No valid waveform detected", 3);
                }
            }

            String[] var13;
            int var12 = (var13 = line.split(",")).length;

            String idx;
            int var11;
            for(var11 = 0; var11 < var12; ++var11) {
                idx = var13[var11];
                rawDCDataPoints.add(Double.parseDouble(idx.trim()));
            }

            line = in.readLine();
            var12 = (var13 = line.split(",")).length;

            for(var11 = 0; var11 < var12; ++var11) {
                idx = var13[var11];
                startIndices.add(Integer.parseInt(idx.trim()));
            }

            line = in.readLine();
            var12 = (var13 = line.split(",")).length;

            for(var11 = 0; var11 < var12; ++var11) {
                idx = var13[var11];
                endIndices.add(Integer.parseInt(idx.trim()));
            }

            in.close();
            pr.destroy();
            tempFile.delete();
            return new Wave(rawDCDataPoints, startIndices, endIndices);
        } catch (IOException var15) {
            throw new InvalidWaveException("Running exe error", -1);
        }
    }
}

本文含有隐藏内容,请 开通VIP 后查看

网站公告

今日签到

点亮在社区的每一天
去签到