Jython Swing:随机发生的 Java ImageIO IOException

发布于 2024-12-20 03:43:29 字数 1109 浏览 0 评论 0原文

我使用 eclipse 和 pydev 构建了一个 Jython swing 应用程序。我使用了简单的图像作为所有按钮的背景。 95% 的时间里,一切都运转良好。然而,在从屏幕上添加或删除内容时,大约有 5% 的情况会发生这种情况:

Exception in thread "AWT-EventQueue-0"  at com.sun.imageio.plugins.png.PNGImageReader.readImage(PNGImageReader.java:1310)
    at com.sun.imageio.plugins.png.PNGImageReader.read(PNGImageReader.java:1579)
    at javax.imageio.ImageIO.read(ImageIO.java:1438)
    at javax.imageio.ImageIO.read(ImageIO.java:1342)
    at sun.reflect.GeneratedMethodAccessor33.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:616)

javax.imageio.IIOException: javax.imageio.IIOException: Error reading PNG image data

令人沮丧的是,它不容易重现,并且可能发生在我绘制 PNG 的应用程序中的任何位置。我当然可以捕获异常并让它重新导入图像,但我希望找出根本原因。

我这样称呼资源:

notPressed = ImageIO.read(pkg_resources.resource_stream('images', "button-blue.png")).getScaledInstance(width,height, Image.SCALE_SMOOTH )

我唯一的猜测是有时 pkg_resources 可能无法打开文件流,从而导致异常。关于如何追踪这个问题有什么建议吗,或者我应该庆幸我可以让它安静地失败吗?

I've built a Jython swing app using eclipse and pydev. I've used simple images for the backgrounds of all of the buttons. 95% of the time, everything works wonderfully. However, about 5% of the time when adding or removing content from the screen, this occurs:

Exception in thread "AWT-EventQueue-0"  at com.sun.imageio.plugins.png.PNGImageReader.readImage(PNGImageReader.java:1310)
    at com.sun.imageio.plugins.png.PNGImageReader.read(PNGImageReader.java:1579)
    at javax.imageio.ImageIO.read(ImageIO.java:1438)
    at javax.imageio.ImageIO.read(ImageIO.java:1342)
    at sun.reflect.GeneratedMethodAccessor33.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:616)

javax.imageio.IIOException: javax.imageio.IIOException: Error reading PNG image data

The frustrating thing is that it's not readily reproducable, and can happen anywhere in the application that I draw a PNG. I can certainly catch the exception and have it reimport the image, but I'm hoping to figure out the root cause.

I call the resources like so:

notPressed = ImageIO.read(pkg_resources.resource_stream('images', "button-blue.png")).getScaledInstance(width,height, Image.SCALE_SMOOTH )

My only guess is that sometimes pkg_resources might fail to open a filestream, causing the exception. Any suggestions on how to track this down, or should I just be happy that I can make it fail quietly?

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

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

发布评论

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

评论(1

素染倾城色 2024-12-27 03:43:29

什么是更实用的解决方案:让线程将所有图像缓存在应用程序启动时可访问的地方,或者为每个单独的图像发送一个线程。我在整个应用程序中使用了大约 2-3 MB 的图像。

很大程度上取决于预期的延迟和图像大小。这个简单的示例将图像缓存为 List;新图像的轻微延迟是难以察觉的,因为图像是分发 JAR 的一部分。相比之下,这个示例明显滞后,因为应用程序在启动时从网络获取图像。

对于长延迟或大量大图像,您可能需要考虑 SwingWorker,如这篇 Oracle 文章中讨论的如何使用 SwingWorker 提高应用程序性能

What's a more practical solution: having a thread cache all of the images somewhere accessible on application start-up or sending a thread for each individual image. I use roughly 2-3 MBs of images throughout the application.

Much depends on the anticipated latency and image size. This simple example caches images as acuired in a List<ImageIcon>; the slight delay for a new image is imperceptible, as the images are part of the distribution JAR. In contrast, this example lags palpably as the application acquires images from the network at startup.

For long latency or a large number of large images, you may want to consider SwingWorker, as discussed in this Oracle article on how to Improve Application Performance With SwingWorker.

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