javafx 大图像崩溃

javafx large image crash

JavaFX 新手

此示例适用于小图像。 但是大图像会使 ImageView 崩溃。

我的示例代码有缺陷吗? JavaFX 中的大图像有问题吗? 还有别的吗?

我从网上抓取了一个例子:

http://www.java2s.com/Code/Java/JavaFX/JavaFXImageZoomExample.htm

我下载并处理一个 PDF 文件:

http://optics.byu.edu/BYUOpticsBook_2013.pdf

gs -sDEVICE=png16m -dNOPAUSE -dBATCH -dSAFER \
   -r600 -dFirstPage=1 -dLastPage=1 \
   -sOutputFile=001.png BYUOpticsBook_2013.pdf 

这给了我一张 5100 × 6600 像素的图像。

我尝试查看图像: 几秒钟后出现一个 window 和一个空的 滚动窗格和发送到控制台的堆栈跟踪。

import javafx.application.Application;
import javafx.beans.InvalidationListener;
import javafx.beans.Observable;
import javafx.beans.property.DoubleProperty;
import javafx.beans.property.SimpleDoubleProperty;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.ScrollPane;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.input.ScrollEvent;
import javafx.stage.Stage;

/**
 *
 * @author O.J. Sousa Rodrigues (office at halbgasse.at)
 */
public class ZoomExample extends Application {

  private ImageView imageView = new ImageView();
  private ScrollPane scrollPane = new ScrollPane();
  final DoubleProperty zoomProperty = new SimpleDoubleProperty(200);

  @Override
  public void start(Stage stage) throws Exception {

    zoomProperty.addListener(new InvalidationListener() {
      @Override
      public void invalidated(Observable arg0) {
        imageView.setFitWidth(zoomProperty.get() * 4);
        imageView.setFitHeight(zoomProperty.get() * 3);
      }
    });

    scrollPane.addEventFilter(ScrollEvent.ANY,
        new EventHandler<ScrollEvent>() {

      @Override
      public void handle(ScrollEvent event) {
        if (event.getDeltaY() > 0) {
          zoomProperty.set(zoomProperty.get() * 1.1);
        } else if (event.getDeltaY() < 0) {
          zoomProperty.set(zoomProperty.get() / 1.1);
        }
      }
    });

    imageView.setImage(new Image("file:///home/jeff/001.png"));
    imageView.preserveRatioProperty().set(true);
    scrollPane.setContent(imageView);

    stage.setScene(new Scene(scrollPane, 400, 300));
    stage.show();

  }
  public static void main(String[] args) {
    launch(args);
  }
} 

堆栈跟踪:

java.lang.NullPointerException
at com.sun.prism.sw.SWGraphics.drawTexture(SWGraphics.java:686)
at com.sun.prism.sw.SWGraphics.drawTexture(SWGraphics.java:665)
at com.sun.prism.sw.SWGraphics.drawTexture(SWGraphics.java:648)
at com.sun.javafx.sg.prism.NGImageView.renderContent(NGImageView.java:123)
at com.sun.javafx.sg.prism.NGNode.doRender(NGNode.java:2067)
at com.sun.javafx.sg.prism.NGImageView.doRender(NGImageView.java:103)
at com.sun.javafx.sg.prism.NGNode.render(NGNode.java:1959)
at com.sun.javafx.sg.prism.NGGroup.renderContent(NGGroup.java:235)
at com.sun.javafx.sg.prism.NGRegion.renderContent(NGRegion.java:576)
at com.sun.javafx.sg.prism.NGNode.doRender(NGNode.java:2067)
at com.sun.javafx.sg.prism.NGNode.render(NGNode.java:1959)
at com.sun.javafx.sg.prism.NGGroup.renderContent(NGGroup.java:235)
at com.sun.javafx.sg.prism.NGRegion.renderContent(NGRegion.java:576)
at com.sun.javafx.sg.prism.NGNode.renderForClip(NGNode.java:2308)
at com.sun.javafx.sg.prism.NGNode.renderRectClip(NGNode.java:2202)
at com.sun.javafx.sg.prism.NGNode.renderClip(NGNode.java:2228)
at com.sun.javafx.sg.prism.CacheFilter.impl_renderNodeToCache(CacheFilter.java:663)
at com.sun.javafx.sg.prism.CacheFilter.render(CacheFilter.java:567)
at com.sun.javafx.sg.prism.NGNode.renderCached(NGNode.java:2372)
at com.sun.javafx.sg.prism.NGNode.doRender(NGNode.java:2058)
at com.sun.javafx.sg.prism.NGNode.render(NGNode.java:1959)
at com.sun.javafx.sg.prism.NGGroup.renderContent(NGGroup.java:235)
at com.sun.javafx.sg.prism.NGRegion.renderContent(NGRegion.java:576)
at com.sun.javafx.sg.prism.NGNode.doRender(NGNode.java:2067)
at com.sun.javafx.sg.prism.NGNode.render(NGNode.java:1959)
at com.sun.javafx.tk.quantum.ViewPainter.doPaint(ViewPainter.java:474)
at com.sun.javafx.tk.quantum.ViewPainter.paintImpl(ViewPainter.java:327)
at com.sun.javafx.tk.quantum.UploadingPainter.run(UploadingPainter.java:135)
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:125)
at java.lang.Thread.run(Thread.java:745) 

Jira 上报告了几个错误(您需要注册才能阅读):

  • RT-21998 渲染图像时出现 NullPointerException
  • RT-22669渲染大图时需要处理纹理加载失败的情况
  • RT-22073 如果维度大于最大纹理大小,则从快照中抛出异常 ...

启用此选项 -Dprism.verbose=true 以检查您的设置。

另外,在这个 link 之后,有一个选项可以设置显存。尝试“-Dprism.poolstats=true”来监控纹理池的实际使用情况,以便更好地确定上限,并使用该限制设置“-Dprism.maxvram=XX”(XX=500m,... )

不过我还没有测试过这个。你可以 post 一个 link 到你的图像吗?