public byte[] createImage2(String processInstanceId) {
//1.获取当前的流程实例
ProcessInstance processInstance = runtimeService.createProcessInstanceQuery().processInstanceId(processInstanceId).singleResult();
String processDefinitionId = null;
List<String> activeActivityIds = null;
//2.获取所有的历史轨迹对象 fhadmin.org
List<HistoricActivityInstance> list = historyService.createHistoricActivityInstanceQuery()
.processInstanceId(processInstanceId).list();
Map<String, HistoricActivityInstance> hisActivityMap = new HashMap<>();
list.forEach(historicActivityInstance -> {
if (!hisActivityMap.containsKey(historicActivityInstance.getActivityId())) {
hisActivityMap.put(historicActivityInstance.getActivityId(), historicActivityInstance);
}
});
//3. 获取流程定义id和高亮的节点id
if (processInstance != null) {
//3.1. 正在运行的流程实例
processDefinitionId = processInstance.getProcessDefinitionId();
activeActivityIds = runtimeService.getActiveActivityIds(processInstanceId);
} else {
//3.2. 已经结束的流程实例
HistoricProcessInstance historicProcessInstance = historyService.createHistoricProcessInstanceQuery().processInstanceId(processInstanceId).singleResult();
processDefinitionId = historicProcessInstance.getProcessDefinitionId();
activeActivityIds = new ArrayList<>();
List<EndEvent> endEvents = bpmnModelService.findEndFlowElement(processDefinitionId);
List<String> finalActiveActivityIds = activeActivityIds;
endEvents.forEach(endEvent -> {
if (hisActivityMap.containsKey(endEvent.getId())) {
finalActiveActivityIds.add(endEvent.getId());
}
});
}
//4. 获取流程定义的所有节点信息 fhadmin.org
List<FlowNode> flowNodes = bpmnModelService.findFlowNodes(processDefinitionId);
Map<String, FlowNode> activityMap = flowNodes.stream().collect(Collectors.toMap(FlowNode::getId, flowNode -> flowNode));
List<String> highLightedFlows = new ArrayList<>();
//5. 递归得到高亮线
activeActivityIds.forEach(activeActivityId -> this.getHighLightedFlows(activityMap, hisActivityMap, activeActivityId, highLightedFlows, activeActivityId));
//6. 获取bpmnModel对象 fhadmin.org
BpmnModel bpmnModel = bpmnModelService.getBpmnModelByProcessDefId(processDefinitionId);
//7. 生成图片流
InputStream inputStream = flowProcessDiagramGenerator.generateDiagram(bpmnModel, activeActivityIds, highLightedFlows);
//8. 转化成byte便于网络传输
byte[] datas = IoUtil.readInputStream(inputStream, "image inputStream name");
return datas;
}