【问题标题】:Is there a way to load a (PlantUML-) generated image, without saving to the file system?有没有办法加载(PlantUML-)生成的图像,而不保存到文件系统?
【发布时间】:2021-07-15 00:37:08
【问题描述】:

我正在使用 Plant UML API 在我的 Java 项目中生成测试的 UML 图。 API 生成图像并将其保存到文件系统中的 PNG 或 SVG 文件中。

我想知道是否有一种方法可以直接在控制台中加载图像而不将其保存到文件系统(或加载文件系统中已经存在的文件)。

作为参考,我使用的是 PUML 提供的文档 here,我正在调用类似的内容:

import net.sourceforge.plantuml.SourceStringReader;
OutputStream png = ...;
String source = "@startuml\n";
source += "Bob -> Alice : hello\n";
source += "@enduml\n";

SourceStringReader reader = new SourceStringReader(source);
// Write the first image to "png"
String desc = reader.outputImage(png).getDescription();
// Return a null string if no generation

【问题讨论】:

    标签: java image file serialization plantuml


    【解决方案1】:

    我想这应该可以完成这项工作。

    public byte[] createUmlImage(String source){
        try {
            SourceStringReader reader = new SourceStringReader(source);
            final ByteArrayOutputStream os = new ByteArrayOutputStream();
            reader.outputImage(os);
            return os.toByteArray();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    

    我还会提出另一个建议。使用StringBuilderappend() 代替+ 运算符进行字符串连接。

    【讨论】:

    • 谢谢!我认为它不起作用:(我在 Eclipse 中运行它,可悲的是没有弹出任何内容。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-19
    • 1970-01-01
    相关资源
    最近更新 更多