Java ImageIO.write() 最多需要 6 秒

发布于 2024-12-09 05:59:00 字数 628 浏览 0 评论 0原文

我正在编写一个 Web 应用程序,需要将图像从 servlet 发送到客户端。图像是动态生成的并且相当大(+-2MB)。它可能是 jpeg、png 或 gif。

现在,我使用 ImageIO.write() 将图像写入输出流,但速度非常慢。客户端最多需要 6 秒才能看到图像。我需要加快速度。

有什么建议吗?

顺便提一句。我知道 寻找 ImageIO 的更快替代方案主题。但这对我没有帮助。 由于 PNG ImageMagick 速度很慢,因此不是解决方案 我测试过 JAI,结果更糟。

提前致谢

编辑:

向您展示一些代码:

BufferedImage bi = [code to generate Image];
response.setContentType(mime);
ServletOutputStream out = response.getOutputStream();
ImageIO.write(bi,"png",out);

为了可读性,我剥离了异常处理。

I am writing an web application where I need to send an image from servlet to client. Image is generated dynamically and is quite big(+-2MB). It might be jpeg, png, or gif.

Now, I am using ImageIO.write() to write the image to output stream, but its veeeery slow. It takes up to 6 seconds till the client see the image. I need to speed it up.

Any suggestions?

btw. I am aware of Looking for a faster alternative to ImageIO topic. But it didn't help me.
Since it's slow with PNG ImageMagick is not a solution and
I have tested JAI and it was even worse.

Thanks in advance

Edit:

To show you some code:

BufferedImage bi = [code to generate Image];
response.setContentType(mime);
ServletOutputStream out = response.getOutputStream();
ImageIO.write(bi,"png",out);

I stripped down Exception handling for readability.

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

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

发布评论

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

评论(3

ぶ宁プ宁ぶ 2024-12-16 05:59:01

一般来说,java 中的图像编码相当慢,但您可能还想确保安装了本机库,因为它们在性能上产生了相当明显的差异。

http://download.java.net/media /jai-imageio/builds/release/1.1/INSTALL-jai_imageio.html

Image encoding in java is pretty slow in general but you may also want to ensure you have the native libraries installed as they make quite a noticeable difference in performance.

http://download.java.net/media/jai-imageio/builds/release/1.1/INSTALL-jai_imageio.html

Spring初心 2024-12-16 05:59:01

请注意,在创建 ImageInputStreams图像输出流。可以通过调用 来关闭此功能ImageIO.setUseCache(false)

有关更详细的说明,请参阅此答案

Be aware that ImageIO by default uses temporary files as cache when creating ImageInputStreams and ImageOutputStreams. This can be switched off by calling ImageIO.setUseCache(false).

For a more detailed explanation see this answer.

十雾 2024-12-16 05:59:01

您确定

ImageIO

需要这么长时间吗 - 也许还有另一个问题,例如

  • 与客户端的(网络)连接速度慢,
  • 图像的生成(计算)需要很多时间

Are you sure that the

ImageIO

takes so long - maybe there is another problem, e.g.

  • slow (network) connection to the client
  • the generation (calculation) of the image takes a lot of time
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文