目錄
問題內容
解決方法
版本
原始碼
gradle 檔案
執行
首頁 Java 載入 JavaFX 映像並將其儲存到資料庫

載入 JavaFX 映像並將其儲存到資料庫

Feb 22, 2024 pm 02:49 PM
overflow

JavaFX是Java程式語言的圖形使用者介面工具包,它可以輕鬆創建豐富、現代化的使用者介面。在實際開發中,有時候需要載入JavaFX映像並將其儲存到資料庫中。本文將為您介紹如何實現這項操作,透過簡單易懂的步驟,幫助您解決這個問題。讓我們一起來探討吧!

問題內容

我現在正在努力將一個小圖像保存在資料庫中並將其加載以用作 javafx.scene.image.Image。我嘗試過的幾個解決方案涉及使用 SwingFXUtils.fromFXImage 和 SwingFXUtils.toFxImage,但該類別似乎需要 module-info.java 檔案中的一個條目,而我甚至不打算使用該條目。

我在嘗試使用 SwingFXUtils 時遇到此例外:java.lang.NoClassDefFoundError: javafx/embed/swing/SwingFXUtils

當我使用模組系統時,它破壞了我正在使用的另一個庫(itextpdf)。 所以我想避免這種情況,只是找到另一種方法來保存和載入資料庫中的 JavaFX 映像。 有什麼建議嗎?

解決方法

您提到:

這不是真的。儘管 javafx 僅支援作為命名模組載入1,但 javafx 並不要求您自己的程式碼是模組化的。獲得 noclassdeffounderror 的唯一方法是在執行時間未能將類別包含在模組路徑/類別路徑中(它必須在編譯時存在,否則您的程式碼將無法編譯)。我建議閱讀 Getting Started with JavaFX,了解如何使用主要 java ide 和/或建置工具之一來設定基本 javafx 應用程式。但是,如果不知道您如何配置項目以及如何運行項目,我們將無法告訴您您的設定具體出了什麼問題。

也就是說,您想要完成的整體目標確實是可能的。以下是一個範例,可讓您瀏覽電腦上的圖像並將其保存到記憶體中的 h2 資料庫中。映像的 id 和名稱被放入 tableview 中,其中包含一個帶有「開啟」按鈕的列,可讓您查看從記憶體資料庫載入的映像。影像以 png 格式作為 blob 儲存在資料庫中,無論其原始格式為何。 swingfxutilsimageio 類別用於將 javafx 映像轉換為 png「檔案」2

此範例未向您展示如何部署應用程式(例如,透過 jpackage)。

版本

以下是我用於建立和運行範例的庫和工具的版本。

  • java 21.0.1(eclipse adoptium / temurin)

  • javafx 21.0.1

  • h2 2.2.224

  • gradle 8.4

  • JavaFX Gradle Plugin 0.1.0

commentprevious question2 中,您聲明您正在使用gradle,因此我在範例中使用的是gradle。

原始碼

沒有 module-info.java 檔案。

imagerecord.java

package com.example;

public record imagerecord(int id, string name) {}
登入後複製

main.java

#
package com.example;

import java.io.file;
import java.util.concurrent.executor;
import java.util.concurrent.executors;
import java.util.function.consumer;
import javafx.application.application;
import javafx.beans.property.simpleintegerproperty;
import javafx.beans.property.simpleobjectproperty;
import javafx.beans.property.simplestringproperty;
import javafx.concurrent.task;
import javafx.geometry.insets;
import javafx.geometry.pos;
import javafx.scene.scene;
import javafx.scene.control.button;
import javafx.scene.control.scrollpane;
import javafx.scene.control.tablecell;
import javafx.scene.control.tablecolumn;
import javafx.scene.control.tableview;
import javafx.scene.image.image;
import javafx.scene.image.imageview;
import javafx.scene.layout.borderpane;
import javafx.scene.layout.hbox;
import javafx.stage.filechooser;
import javafx.stage.stage;
import javafx.stage.stagestyle;
import javafx.stage.window;

public class main extends application {

  private final executor executor = executors.newvirtualthreadpertaskexecutor();
  private final imagesdatabase db = new imagesdatabase("test");
  private final imagerepository imagerepo = new imagerepository(db);
  private file lastdirectory;

  @override
  public void start(stage primarystage) {
    var table = createtable(record -> displayimage(primarystage, record));

    var choosebtn = new button("choose image...");
    choosebtn.setonaction(
        e -> {
          e.consume();
          var image = chooseimage(primarystage);
          if (image != null) {
            executor.execute(createsaveimagetask(image, table.getitems()::add));
          }
        });

    var root = new borderpane();
    root.settop(choosebtn);
    root.setcenter(table);
    borderpane.setalignment(choosebtn, pos.center);
    borderpane.setmargin(choosebtn, new insets(10));

    primarystage.setscene(new scene(root, 600, 400));
    primarystage.show();
  }

  @override
  public void stop() throws exception {
    db.close();
  }

  private image chooseimage(window owner) {
    var chooser = new filechooser();
    chooser.settitle("choose image file");
    chooser
        .getextensionfilters()
        .add(new filechooser.extensionfilter("image files", "*.jpeg", "*.jpg", "*.png"));
    if (lastdirectory != null) {
      chooser.setinitialdirectory(lastdirectory);
    }

    var file = chooser.showopendialog(owner);
    if (file != null) {
      lastdirectory = file.getparentfile();
      return new image(file.touri().tostring());
    }
    return null;
  }

  private void displayimage(window owner, imagerecord record) {
    var view = new imageview();

    var task = creategetimagetask(record, view::setimage);
    executor.execute(task);

    var sp = new scrollpane(view);
    sp.setpannable(true);

    var window = new stage(stagestyle.utility);
    window.initowner(owner);
    window.settitle(record.name());
    window.setscene(new scene(sp, 500, 300));
    window.setonhiding(e -> task.cancel());
    window.show();
  }

  private tableview<imagerecord> createtable(consumer<imagerecord> onopen) {
    var table = new tableview<imagerecord>();
    table.setcolumnresizepolicy(tableview.constrained_resize_policy_flex_last_column);

    var idcol = new tablecolumn<imagerecord, number>("id");
    idcol.setcellvaluefactory(data -> new simpleintegerproperty(data.getvalue().id()));
    table.getcolumns().add(idcol);

    var namecol = new tablecolumn<imagerecord, string>("name");
    namecol.setcellvaluefactory(data -> new simplestringproperty(data.getvalue().name()));
    table.getcolumns().add(namecol);

    var openbtncol = new tablecolumn<imagerecord, imagerecord>();
    openbtncol.setcellvaluefactory(data -> new simpleobjectproperty<>(data.getvalue()));
    openbtncol.setcellfactory(tc -> createopenbuttoncell(onopen));
    table.getcolumns().add(openbtncol);

    return table;
  }

  private tablecell<imagerecord, imagerecord> createopenbuttoncell(consumer<imagerecord> onopen) {
    return new tablecell<>() {
      final hbox container = new hbox();
      final button openbutton = new button("open");

      {
        container.getchildren().add(openbutton);
        container.setalignment(pos.center);
        openbutton.setonaction(
            e -> {
              e.consume();
              var item = isempty() ? null : getitem();
              if (item != null) {
                onopen.accept(item);
              }
            });
      }

      @override
      protected void updateitem(imagerecord item, boolean empty) {
        super.updateitem(item, empty);
        if (empty || item == null) {
          setgraphic(null);
        } else {
          setgraphic(container);
        }
      }
    };
  }

  private task<?> createsaveimagetask(image image, consumer<imagerecord> onsuccess) {
    return new task<imagerecord>() {
      @override
      protected imagerecord call() throws exception {
        return imagerepo.insertimage(image);
      }

      @override
      protected void succeeded() {
        onsuccess.accept(getvalue());
      }

      @override
      protected void failed() {
        getexception().printstacktrace();
      }
    };
  }

  private task<?> creategetimagetask(imagerecord record, consumer<image> onsuccess) {
    return new task<image>() {
      @override
      protected image call() throws exception {
        return imagerepo.getimage(record).orelsethrow();
      }

      @override
      protected void succeeded() {
        onsuccess.accept(getvalue());
      }

      @override
      protected void failed() {
        getexception().printstacktrace();
      }
    };
  }
}
登入後複製

imagerepository.java

package com.example;

import static java.sql.statement.return_generated_keys;

import java.io.bytearrayinputstream;
import java.io.bytearrayoutputstream;
import java.io.ioexception;
import java.io.inputstream;
import java.io.uncheckedioexception;
import java.sql.sqlexception;
import java.util.arraylist;
import java.util.list;
import java.util.objects;
import java.util.optional;
import java.util.concurrent.atomic.atomicinteger;
import javafx.embed.swing.swingfxutils;
import javafx.scene.image.image;
import javax.imageio.imageio;

public class imagerepository {

  private static final string select_all_records_sql = "select id, name from images";
  private static final string select_image_sql = "select image from images where id = ?";
  private static final string insert_sql = "insert into images (name, image) values (?, ?)";

  private final atomicinteger generatednamecount = new atomicinteger();
  private final imagesdatabase db;

  public imagerepository(imagesdatabase db) {
    this.db = db;
  }

  public list<imagerecord> getrecords() throws sqlexception {
    return db.execute(
        conn -> {
          try (var stat = conn.createstatement()) {
            var result = stat.executequery(select_all_records_sql);

            var records = new arraylist<imagerecord>();
            while (result.next()) {
              int id = result.getint(1);
              var name = result.getstring(2);
              records.add(new imagerecord(id, name));
            }
            return records;
          }
        });
  }

  public optional<image> getimage(imagerecord record) throws sqlexception {
    return getimage(record.id());
  }

  public optional<image> getimage(int recordid) throws sqlexception {
    if (recordid <= 0) {
      throw new illegalargumentexception("recordid <= 0: " + recordid);
    }
    return db.execute(
        conn -> {
          try (var stat = conn.preparestatement(select_image_sql)) {
            stat.setint(1, recordid);

            var result = stat.executequery();
            if (result.next()) {
              var image = new image(result.getbinarystream(1));
              return optional.of(image);
            } else {
              return optional.empty();
            }
          }
        });
  }

  public imagerecord insertimage(image image) throws sqlexception {
    objects.requirenonnull(image);
    return db.execute(
        conn -> {
          try (var stat = conn.preparestatement(insert_sql, return_generated_keys)) {
            var name = getimagename(image);

            stat.setstring(1, name);
            stat.setbinarystream(2, imagetoinputstream(image));
            stat.executeupdate();

            var keys = stat.getgeneratedkeys();
            if (keys.next()) {
              int id = keys.getint(1);
              return new imagerecord(id, name);
            } else {
              throw new illegalstateexception("generated key not returned");
            }
          }
        });
  }

  private string getimagename(image image) {
    var source = image.geturl();
    return source == null ? generateimagename() : source;
  }

  private string generateimagename() {
    return "generated image name " + generatednamecount.incrementandget();
  }

  private inputstream imagetoinputstream(image image) {
    var out = new bytearrayoutputstream();
    try {
      imageio.write(swingfxutils.fromfximage(image, null), "png", out);
    } catch (ioexception ex) {
      throw new uncheckedioexception(ex);
    }
    return new bytearrayinputstream(out.tobytearray());
  }
}
登入後複製

imagesdatabase.java

package com.example;

import java.sql.connection;
import java.sql.sqlexception;
import java.util.objects;
import java.util.concurrent.locks.lock;
import java.util.concurrent.locks.reentrantlock;
import javax.sql.datasource;
import org.h2.jdbcx.jdbcdatasource;

public class imagesdatabase implements autocloseable {

  private static final string create_table_sql =
      "create table images (id identity, name varchar(255), image blob)";

  @functionalinterface
  public interface sqlfunction<t> {

    t execute(connection connection) throws sqlexception;
  }

  private final lock mutex = new reentrantlock();

  private final datasource source;
  private connection connection;
  private boolean open = true;
  private boolean initialized;

  public imagesdatabase(string name) {
    if (name.isblank()) {
      throw new illegalargumentexception("blank name");
    }
    var source = new jdbcdatasource();
    source.seturl("jdbc:h2:mem:" + name + ";db_close_delay=-1");
    this.source = source;
  }

  public <t> t execute(sqlfunction<t> function) throws sqlexception {
    objects.requirenonnull(function);
    mutex.lock();
    try {
      checkopen();
      return function.execute(getoropenconnection());
    } finally {
      mutex.unlock();
    }
  }

  private connection getoropenconnection() throws sqlexception {
    if (connection == null || connection.isclosed()) {
      connection = source.getconnection();
      initialize(connection);
    }
    return connection;
  }

  private void initialize(connection conn) throws sqlexception {
    if (!initialized) {
      try (var stat = conn.createstatement()) {
        stat.executeupdate(create_table_sql);
      }
      initialized = true;
    }
  }

  private void shutdown() throws sqlexception {
    if (initialized) {
      try (var conn = getoropenconnection();
          var stat = conn.createstatement()) {
        stat.execute("shutdown");
      }
      connection = null;
    }
  }

  private void checkopen() {
    if (!open) {
      throw new illegalstateexception("closed");
    }
  }

  @override
  public void close() throws sqlexception {
    mutex.lock();
    try {
      if (open) {
        open = false;
        shutdown();
      }
    } finally {
      mutex.unlock();
    }
  }
}
登入後複製

gradle 檔案

我使用了 kotlin dsl,但如果您願意,您也可以使用 groovy dsl。

settings.gradle.kts

rootproject.name = "h2images-example"
登入後複製

build.gradle.kts

#
plugins {
    id("org.openjfx.javafxplugin") version "0.1.0"
    application
}

group = "com.example"
version = "1.0"

javafx {
    modules("javafx.controls", "javafx.swing")
    version = "21.0.1"
}

application {
    mainclass.set("com.example.main")
}

repositories {
    mavencentral()
}

dependencies {
    implementation("com.h2database:h2:2.2.224")
}
登入後複製

執行

您可以使用以下命令執行上述內容:

./gradlew run
登入後複製

注意 ./gradlew 呼叫 Gradle Wrapper。如果您的電腦上安裝了 gradle 版本,則可以透過以下方式產生版本 8.4 的包裝器:

gradle wrapper --gradle-version 8.4
登入後複製

1. javafx 在技術上不支援從類別路徑載入。這意味著理想情況下,javafx 模組應該位於模組路徑上並解析為命名模組,即使您自己的程式碼和其他依賴項是從類別路徑載入的。但是,我不知道如果 javafx 位於類別路徑上(至少從 javafx 21 開始),會發生什麼中斷,除了您的主類別不能再是 javafx.application 的子類別。 application(您需要一個單獨的「啟動器類別」作為主類別)。只需要知道,由於 javafx 位於類別路徑上而導致的任何問題都不太可能被 javafx 團隊修復。

請注意,openjfx 提供的 gradle 和 maven 外掛程式會配置這些建置工具以將 javafx 放在模組路徑上。

2. 根據您的兩個問題的上下文,該範例將 image 物件轉換為 png 位元組。但是,如果您已經以位元組形式接收映像(即作為本地或遠端檔案),那麼將這些位元組直接放入資料庫可能會更容易、更有效率。

以上是載入 JavaFX 映像並將其儲存到資料庫的詳細內容。更多資訊請關注PHP中文網其他相關文章!

本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

AI Hentai Generator

AI Hentai Generator

免費產生 AI 無盡。

熱門文章

R.E.P.O.能量晶體解釋及其做什麼(黃色晶體)
1 個月前 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳圖形設置
1 個月前 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.如果您聽不到任何人,如何修復音頻
1 個月前 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.聊天命令以及如何使用它們
1 個月前 By 尊渡假赌尊渡假赌尊渡假赌

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

比特幣誕生至今歷史價格總覽 比特幣歷史價格趨勢大全 比特幣誕生至今歷史價格總覽 比特幣歷史價格趨勢大全 Jan 15, 2025 pm 08:14 PM

比特币,作为一种加密货币,自问世以来经历了显著的市场波动。本文将提供比特币自诞生以来的历史价格总览,帮助读者了解其价格趋势和关键时刻。通过分析比特币的历史价格数据,我们可以了解市场对其价值评估、影响其波动的因素,并为未来投资决策提供依据。

比特幣誕生至今歷史價格一覽 BTC歷史價格行情趨勢圖(最新匯總) 比特幣誕生至今歷史價格一覽 BTC歷史價格行情趨勢圖(最新匯總) Feb 11, 2025 pm 11:36 PM

比特幣自 2009 年創世以來,價格經歷多次大幅波動,最高漲至 2021 年 11 月的 69,044.77 美元,最低跌至 2018 年 12 月的 3,191.22 美元。截至 2024 年 12 月,最新價格突破 100,204 美元。

2018-2024年比特幣最新價格美元大全 2018-2024年比特幣最新價格美元大全 Feb 15, 2025 pm 07:12 PM

實時比特幣美元價格 影響比特幣價格的因素 預測比特幣未來價格的指標 以下是 2018-2024 年比特幣價格的一些關鍵信息:

H5頁面製作是前端開發嗎 H5頁面製作是前端開發嗎 Apr 05, 2025 pm 11:42 PM

是的,H5頁面製作是前端開發的重要實現方式,涉及HTML、CSS和JavaScript等核心技術。開發者通過巧妙結合這些技術,例如使用&lt;canvas&gt;標籤繪製圖形或使用JavaScript控制交互行為,構建出動態且功能強大的H5頁面。

如何通過CSS自定義resize符號並使其與背景色統一? 如何通過CSS自定義resize符號並使其與背景色統一? Apr 05, 2025 pm 02:30 PM

CSS自定義resize符號的方法與背景色統一在日常開發中,我們經常會遇到需要自定義用戶界面細節的情況,比如調...

如何通過JavaScript或CSS控制瀏覽器打印設置中的頁首和頁尾? 如何通過JavaScript或CSS控制瀏覽器打印設置中的頁首和頁尾? Apr 05, 2025 pm 10:39 PM

如何使用JavaScript或CSS控制瀏覽器打印設置中的頁首和頁尾在瀏覽器的打印設置中,有一個選項可以控制是否顯�...

為什麼inline-block元素會出現錯位現象?如何解決這個問題? 為什麼inline-block元素會出現錯位現象?如何解決這個問題? Apr 04, 2025 pm 10:39 PM

關於inline-block元素錯位顯示的原因及解決方案在編寫網頁佈局時,我們常常會遇到一些看似奇怪的顯示問題。比...

Flex佈局下文字超出省略卻撐開容器?如何解決? Flex佈局下文字超出省略卻撐開容器?如何解決? Apr 05, 2025 pm 11:00 PM

Flex佈局下文字超出省略導致容器撐開的問題及解決方法在使用Flex...