簡體   English   中英

如何將ScalaFX根轉換為圖像,然后將其保存在本地

[英]How to convert ScalaFX root to image and then save it in local

我正在使用scalaFX如下創建“折線圖”,並希望將場景另存為本地圖像。

我知道scalaFX中有一個功能-快照,但我不知道如何使用它。 我在任何地方都找不到任何示例。

這是一個相對簡單的示例(基於ScalaFX Catagory 折線圖演示 ),您應該能夠適應自己的要求:

import java.awt.image.BufferedImage
import java.io.File
import javax.imageio.ImageIO
import scalafx.application.JFXApp
import scalafx.collections.ObservableBuffer
import scalafx.embed.swing.SwingFXUtils
import scalafx.geometry.Side
import scalafx.Includes._
import scalafx.scene.{Node, Scene}
import scalafx.scene.chart.{CategoryAxis, LineChart, NumberAxis, XYChart}

object TakeSnapshot
extends JFXApp {

  val dataPairs = Seq(
    ("Alpha", 50),
    ("Beta", 80),
    ("RC1", 90),
    ("RC2", 30),
    ("1.0", 122),
    ("1.1", 10),
  )

  stage = new JFXApp.PrimaryStage {
    title.value = "Take a Snapshot"
    width = 640
    height = 400
    scene = new Scene {
      root = new LineChart(CategoryAxis("X Axis"), NumberAxis("Y Axis")) {
        title = "LineChart with Category Axis"
        legendSide = Side.Right
        data = XYChart.Series[String, Number](
          "Series 1",
          ObservableBuffer(dataPairs.map {case (x, y) => XYChart.Data[String, Number](x, y)})
        )
      }
    }
  }

  // Take the snapshot.
  takeSnapshot(stage.scene.root(), new File("MyLineChart.png"))

  // Take a snapshot of the specified node, writing it into the specified file. The dimensions of
  // the image will match the size of the node (and its child nodes) on the screen in pixels.
  def takeSnapshot(node: Node, file: File) {

    // Take the snapshot, which returns a WritableImage instance.
    //
    // Note:
    // 1. The first argument is a SnapshotParameters instance. If null, it will use the associated
    //    scene's settings. More here:
    //    https://docs.oracle.com/javase/8/javafx/api/javafx/scene/SnapshotParameters.html
    // 2. The second argument is a WritableImage instance. If null, it will creat a new
    //    WritableImage instance, with the dimensions of the associated node, will be created.
    //    More here:
    //    https://docs.oracle.com/javase/8/javafx/api/javafx/scene/image/WritableImage.html
    //
    // Scala frowns on the use of null values, but there's really no alternative here.
    val image = node.snapshot(null, null)

    // Convert the image to a buffered image. Passing null for the BufferedImage instance argument
    // will ensure one is created for you.
    val bufferedImage = SwingFXUtils.fromFXImage(image, null)
    assert(bufferedImage ne null)

    // Now write the buffered image into a file (in Portable Network Graphics, PNG, format.)
    // This may throw an exception if the file isn't writable, etc.
    ImageIO.write(bufferedImage, "png", file)
  }
}

運行該程序后,您應該在項目的根目錄中找到一個名為“ MyLineChart.png”的文件。

可以在此處獲得有關snapshot()更多JavaFX文檔。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM