生成蜡染背景图像

发布于 2024-11-03 08:09:50 字数 800 浏览 0 评论 0原文

我有一个生成字节数组的方法,它可以表示为二维图像。我想将此图像显示为蜡染中的背景图像。我不想将数组保存到磁盘(作为图像),然后将其加载到蜡染中。相反,我想将数组提供给蜡染。我虽然使用 ParsedURLData 可以帮助我,但我不知道如何让它工作。有什么建议吗?

我调用 ParsedURL.registerHandler(new MyProtocolHanlder("myprotocol")); ,并且 MyProtocolHanlder.parseURL 返回 MyParsedURLData 。

我以为返回我自己的流会起作用,但事实并非如此。在下面的示例中,我只是从磁盘加载图像并尝试显示它。

class MyParsedURLData extends ParsedURLData {
    public MyParsedURLData() {
    }

    @Override
    public InputStream openStreamRaw(String arg0, Iterator arg1) throws IOException {
        return new File("some_image_here").toURI().toURL().openStream();
    }

}

如果在 MyParsedURLData 的构造函数中我设置了 protocol = "file"path="another_image",那么无论怎样,都会加载另一个图像openStreamRaw 返回什么流。

有什么建议吗?

I have a method that generates an array of bytes, which can be represented as a two dimensional image. I would like to show this image as a background image in batik. I do not want to save the array to disk (as image) and then load it into batik. Instead I would like to provide the array to the batik. I though that using ParsedURLData can help me, but I can not figure out how to make it work. Any suggestions?

I call ParsedURL.registerHandler(new MyProtocolHanlder("myprotocol")); and MyProtocolHanlder.parseURL returns MyParsedURLData.

I thought that returning my own stream would work, but it does not. In the example bellow I simply load an image from disk and try to display it.

class MyParsedURLData extends ParsedURLData {
    public MyParsedURLData() {
    }

    @Override
    public InputStream openStreamRaw(String arg0, Iterator arg1) throws IOException {
        return new File("some_image_here").toURI().toURL().openStream();
    }

}

If in the constructor for MyParsedURLData I set protocol = "file" and path="another_image", then another image will be loaded, no matter what stream is returned by openStreamRaw.

Any suggestions?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

流殇 2024-11-10 08:09:50

最简单的方法是将图像保存到临时文件中,然后只需通过 File.toURI().toURL() 引用它,Batik 就会加载它。为此,您不需要自定义协议处理程序或 ParsedURL。选择 Batik 支持的标准图像格式作为临时文件,例如 PNGJPEG

您可以通过实现 ParsedURLProtocolHandlerRegistryEntry 来保存临时文件,并将它们注册到 Batik。网站上有一些关于这方面的文档,但仍然很痛苦。我花了几个小时才弄清楚(代码还没有真正准备好共享),但我现在可以将 BufferedImage 直接传递给 Batik,而无需将其编码为 .png或类似的文件先。

The simplest way is to save the image to a temp file, then just reference it via File.toURI().toURL(), and Batik will load it. You don't need a custom protocol handler or ParsedURL for this. Choose a standard image format supported by Batik such as PNG or JPEG for the temp file!

You can save the temp file by implementing both a ParsedURLProtocolHandler and a RegistryEntry and register them with Batik. There is a little bit of documentation on that on the web site, but it is still a pain to do. Took me some hours to figure out (and the code is not really ready for sharing), but I can now pass a BufferedImage directly to Batik without encoding it as a .png or similar file first.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文