呈现大图时的javafx NullPointerException [英] javafx NullPointerException when rendering big image

查看:133
本文介绍了呈现大图时的javafx NullPointerException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用JavaFX 8下的 GraphicsContext.drawImage(...)方法渲染.png图像。我的代码可以很好地处理图像尺寸~1000px x 2000px。
但不幸的是,我需要渲染一个大小为7000px x 14000px的图像。加载此图像也可以正常工作,但是当调用 drawImage(image,0,0,canvas.getWidth(),canvas.getHeight())方法时,我得到以下内容错误输出:

I'm trying to render a .png Image using the GraphicsContext.drawImage(...) method under JavaFX 8. My code works perfectly fine for an image of size ~1000px x 2000px. But unfortunately I need to render an image of size 7000px x 14000px. Loading this image works fine as well, but when calling the drawImage(image, 0, 0, canvas.getWidth(), canvas.getHeight()) method I get the following error output:

java.lang.NullPointerException
at com.sun.prism.impl.BaseGraphics.drawTexture(BaseGraphics.java:389)
at com.sun.prism.impl.ps.BaseShaderGraphics.drawTexture(BaseShaderGraphics.java:139)
at com.sun.javafx.sg.prism.NGCanvas.handleRenderOp(NGCanvas.java:1228)
at com.sun.javafx.sg.prism.NGCanvas.renderStream(NGCanvas.java:997)
at com.sun.javafx.sg.prism.NGCanvas.renderContent(NGCanvas.java:578)
... more rendering stuff here
at com.sun.javafx.sg.prism.NGNode.doRender(NGNode.java:2043)
at com.sun.javafx.sg.prism.NGNode.render(NGNode.java:1951)
at com.sun.javafx.tk.quantum.ViewPainter.doPaint(ViewPainter.java:469)
at com.sun.javafx.tk.quantum.ViewPainter.paintImpl(ViewPainter.java:317)
at com.sun.javafx.tk.quantum.PresentingPainter.run(PresentingPainter.java:89)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
at java.util.concurrent.FutureTask.runAndReset(FutureTask.java:308)
at com.sun.javafx.tk.RenderJob.run(RenderJob.java:58)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at com.sun.javafx.tk.quantum.QuantumRenderer$PipelineRunnable.run(QuantumRenderer.java:129)
at java.lang.Thread.run(Thread.java:745)

如果没有区别的话我尝试在绘制到画布时调整图像大小,或者尝试渲染整个图像。

It doesn't make a difference if I try to resize the image while drawing to the canvas or if I try to render the whole image.

我的猜测是图像尺寸太大而无法渲染,但我无法找到任何来源来验证这一点,我也找不到任何解决我问题的方法。

My guess is that the image size is simply too big to render, but I could'nt find any source to validate this and neither could I find anything to solve my problem.

我还对Java堆进行了分析(使用Eclipse Memory分析器)显示ed图像大小约为376 MB。

I also made an analysis of the Java heap (with Eclipse Memory Analyzer) which showed an image size of approximately 376 MB.

基本上我的问题是:
1.为什么我的程序崩溃了?是因为图像太大了吗?
2.如果我的图像太大,我怎样才能增加Java的可用空间?我的机器有8GB RAM,显卡有1GB RAM,所以&400MB的图像应该不是问题。

So basically my question are: 1. Why is my program crashing? Is it because the image is too big? 2. If my image is too big, how can I increase the available space for Java? My machine has 8GB RAM and the graphics card has 1GB RAM, so an image of <400MB should not really be a problem.

推荐答案

正如有人在评论中指出的那样 - 只有4k / 8k纹理才能适合你。这是因为JavaFX可能使用GPU来渲染图像。纹理大小限制可能是无法处理更大纹理的GPU或GPU驱动程序的限制。它不能创建如此大的纹理,因此它返回null(这解释了NullPointerException)。

As somebody pointed in comments - only up tp 4k/8k textures will work for you. This is because JavaFX probably uses GPU to render images. Texture size limit is probably the limit of yout GPU or GPU drivers that can't handle bigger textures. It can't create so big texture so it returns null (and that explains the NullPointerException).

修复它的唯一方法是避免使用如此大的纹理或获取更好的硬件来支持更大的纹理。使用软件渲染(可能非常慢)可以通过使用以下参数运行java来修复它:

The only way to fix it is to avoid using so big textures or get better hardware to support bigger textures. Using software rendering (which may be really slow) may fix it by running java with the following parameters:

-Dprism.order=j2d

-Dprism.order=sw

但我不确定这是你想要的实现。

But I'm not sure it's what you want to achieve.

这篇关于呈现大图时的javafx NullPointerException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆