版本声明:Activiti 5.22.0 Activiti在流程部署(直接部署BPMN或者由模板转换流程)时,生成的流程图源码里是这样确定尺寸的:
意思是:按画好的流程图最右+10、最下+10的尺寸确定图片的尺寸,而左边的空隙和上边的空隙Activiti并没有帮我们自动去除,这样会导致我们在展示流程图时很难做到将流程图真正置于窗口的中央位置,对画流程图的节点位置很有关系。我们希望最后生成的流程图应该是这个样子的:
这样展示起来就美观多了,好了,大致需求就是这样:在流程部署时,自动根据流程图的左侧节点和上侧节点边缘实现裁切图片; 思路:重写DefaultProcessDiagramGenerator类的generateDiagram方法:
@Override public InputStream generateDiagram(BpmnModel bpmnModel, String imageType, ListhighLightedActivities, List highLightedFlows, String activityFontName, String labelFontName, String annotationFontName, ClassLoader customClassLoader, double scaleFactor, Color[] colors) { CustomProcessDiagramCanvas customProcessDiagramCanvas = generateProcessDiagram(bpmnModel, imageType, highLightedActivities, highLightedFlows, activityFontName, labelFontName, annotationFontName, customClassLoader, scaleFactor,colors); BufferedImage bufferedImage = customProcessDiagramCanvas.generateBufferedImage(imageType); ByteArrayOutputStream bs = new ByteArrayOutputStream(); ImageOutputStream imOut; try { imOut = ImageIO.createImageOutputStream(bs); ImageIO.write(bufferedImage, "PNG", imOut); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } InputStream is = new ByteArrayInputStream(bs.toByteArray()); return is;// return generateProcessDiagram(bpmnModel, imageType, highLightedActivities, highLightedFlows, // activityFontName, labelFontName, annotationFontName, customClassLoader, scaleFactor,colors)// .generateImage(imageType); }
如果需要将这个裁切保留边距设置为可配置参数的话,还需重写DefaultProcessDiagramCanvas类的generateBufferedImage方法:
@Override public BufferedImage generateBufferedImage(String imageType) { if (closed) { throw new ActivitiImageException("ProcessDiagramGenerator already closed"); } // Try to remove white space minX = (minX <= WorkflowConstants.PROCESS_PADDING) ? WorkflowConstants.PROCESS_PADDING : minX; minY = (minY <= WorkflowConstants.PROCESS_PADDING) ? WorkflowConstants.PROCESS_PADDING : minY; BufferedImage imageToSerialize = processDiagram; if (minX >= 0 && minY >= 0) { imageToSerialize = processDiagram.getSubimage( minX - WorkflowConstants.PROCESS_PADDING, minY - WorkflowConstants.PROCESS_PADDING, canvasWidth - minX + WorkflowConstants.PROCESS_PADDING, canvasHeight - minY + WorkflowConstants.PROCESS_PADDING); } return imageToSerialize; }
这样可以随时改变要生成的流程图的保留边距。