生成蜡染背景图像
我有一个生成字节数组的方法,它可以表示为二维图像。我想将此图像显示为蜡染中的背景图像。我不想将数组保存到磁盘(作为图像),然后将其加载到蜡染中。相反,我想将数组提供给蜡染。我虽然使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
最简单的方法是将图像保存到临时文件中,然后只需通过 File.toURI().toURL() 引用它,Batik 就会加载它。为此,您不需要自定义协议处理程序或
ParsedURL
。选择 Batik 支持的标准图像格式作为临时文件,例如PNG
或JPEG
!您可以通过实现
ParsedURLProtocolHandler
和RegistryEntry
来保存临时文件,并将它们注册到 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 orParsedURL
for this. Choose a standard image format supported by Batik such asPNG
orJPEG
for the temp file!You can save the temp file by implementing both a
ParsedURLProtocolHandler
and aRegistryEntry
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 aBufferedImage
directly to Batik without encoding it as a.png
or similar file first.