AndroidApp/ReportGeneratorHelper.java

90 lines
3.0 KiB
Java
Raw Normal View History

2025-07-11 18:32:05 +08:00
package com.example.myapplication.Tool;
import android.content.Context;
import com.chaquo.python.Python;
import com.chaquo.python.android.AndroidPlatform;
import com.google.gson.Gson;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class ReportGeneratorHelper {
private Context context;
public ReportGeneratorHelper(Context context) {
this.context = context;
if (!Python.isStarted()) {
Python.start(new AndroidPlatform(context));
}
}
public void generateReport(String templateContent, String outputPath,
String turbineId, String testDate,
List<ImageData> imageDataList,
String weather, String temperature, String humidity,
String startDate, String endDate,
ReportGenerationCallback callback) {
try {
Python py = Python.getInstance();
// 转换Java的ImageData列表为Python可接受的格式
List<List<Object>> pyImageData = new ArrayList<>();
for (ImageData data : imageDataList) {
List<Object> item = new ArrayList<>();
item.add(data.getImagePath());
item.add(data.getResistance());
pyImageData.add(item);
}
// 准备配置字典
Map<String, Object> config = new HashMap<>();
config.put("template_content", templateContent);
config.put("output_path", outputPath);
config.put("turbine_id", turbineId);
config.put("test_date", testDate);
config.put("image_data", pyImageData);
config.put("weather", weather);
config.put("temperature", temperature);
config.put("humidity", humidity);
config.put("start_date", startDate);
config.put("end_date", endDate);
String jsonConfig = new Gson().toJson(config);
// 调用Python模块
py.getModule("report_generator")
.callAttr("generate_report_from_java", jsonConfig);
// 成功回调
callback.onSuccess(outputPath);
} catch (Exception e) {
callback.onError(e.getMessage());
}
}
public interface ReportGenerationCallback {
void onSuccess(String outputPath);
void onError(String errorMessage);
}
public static class ImageData {
private String imagePath;
private String resistance;
public ImageData(String imagePath, String resistance) {
this.imagePath = imagePath;
this.resistance = resistance;
}
public String getImagePath() {
return imagePath;
}
public String getResistance() {
return resistance;
}
}
}