package com.xinwei;import java.io.File;import java.io.InputStream;import java.util.ArrayList;import java.util.List;import org.activiti.bpmn.model.BpmnModel;import org.activiti.engine.ProcessEngine;import org.activiti.engine.ProcessEngines;import org.activiti.engine.TaskService;import org.activiti.engine.task.Task;import org.activiti.image.ProcessDiagramGenerator;import org.apache.commons.io.FileUtils;import org.apache.commons.io.IOUtils;public class TraceProcessInstaceUtil { public static void main(String[] args) { generateImage("42501"); } public static byte[] generateImage(String processInstanceId){ //1.创建核心引擎流程对象processEngine ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine(); TaskService taskService = processEngine.getTaskService(); Task task = taskService.createTaskQuery().processInstanceId(processInstanceId).singleResult(); //流程定义 BpmnModel bpmnModel = processEngine.getRepositoryService().getBpmnModel(task.getProcessDefinitionId()); //正在活动节点 ListactiveActivityIds = processEngine.getRuntimeService().getActiveActivityIds(task.getExecutionId()); ProcessDiagramGenerator pdg = processEngine.getProcessEngineConfiguration().getProcessDiagramGenerator(); //得到图片输出流(这样加可防止生成的流程图片乱码) InputStream inputStream = processEngine.getProcessEngineConfiguration().getProcessDiagramGenerator() .generateDiagram(bpmnModel, "png", activeActivityIds, new ArrayList(), processEngine.getProcessEngineConfiguration().getActivityFontName(), processEngine.getProcessEngineConfiguration().getLabelFontName(), null, 1.0); try { //生成本地图片 File file = new File("D:/test1.png"); FileUtils.copyInputStreamToFile(inputStream, file); System.out.println("生成流程图成功"); return IOUtils.toByteArray(inputStream); } catch (Exception e) { throw new RuntimeException("生成流程图异常!", e); } finally { IOUtils.closeQuietly(inputStream); } }}
附: