opencv-java / getting-started

Getting started with OpenCV and JavaFX
105 stars 176 forks source link

Set Image on Imageview #3

Closed maxizu closed 8 years ago

maxizu commented 8 years ago

FXHelloCVController.java Line 72. It's not allowed to set a new Image (ImageViewer) from another Thread(Runnable). Possible fix:

FXHelloCVController.java Line72 setFrametoImageView(imageToShow) instead of currentFrame.setImage(imageToShow);

FXHelloCVController.java Line 113

private void setFrametoImageView(Image frame){
   Platform.runLater(() -> {
      currentFrame.setImage(frame);
      //set Image height/width by window size
   });
}
hal-m commented 8 years ago

Great! It works for me. Thanks.

maxizu commented 8 years ago

@hal-m you are welcome. In addition: Always set UI Objects from the main thread with Platform.runLater and you will not get any errors anymore. Possible fix: Add FXHelloCVController.java Line73: updateUIObjects(>static vars or fct vars<)

FXHelloCVController.java Line 113

private void updateUIObjects(){
   Platform.runLater(() -> {
      //Update UI Objects like: Textfield.setText() , Button.set..() , Window.Resize...()
   });
}
maxizu commented 8 years ago

Have a look at prq #4 . Fixed #2 and #3

luigidr commented 8 years ago

Hi,

I need to solve this issue asap, thanks for your issue, @maxizu!

In a previous version of the projects available in this organization I created a onFXThread() method that performed similarly to your setFrametoImageView(), but with generics, so that it can automatically adapt to the various buttons, imageViews, text fields, etc.

However, with the Java 8 version available at the beginning of 2016, this "feature" was removed for setting ImageViews and I removed that method, too. Probably, it was temporary omitted, only. For coherence with the way of working of JavaFX, in any case, it is always better/mandatory to update the UI from the JavaFX thread.

As for the issue #2, could you tell me what OS, Java version and OpenCV version are you using?

Thank you!

maxizu commented 8 years ago

Hey @luigidr , I am running OpenCV 3.1.0 , Java 1.8.0_91-b14 on ubuntu 14.04.4. This problem was a pain in the.... . I wrote a ticket in the oracle forum about this problem updating fx elements from other threads. The answer was ...don't know but that can cause the problem. Your onFxThread() solutions sounds nice! Have a good one

luigidr commented 8 years ago

The "Not on FX application thread" error is present since JavaFX 2 (Java 7). In the JavaFX documentation (Concurrency in JavaFX) is, in fact, reported that:

The JavaFX scene graph, which represents the graphical user interface of a JavaFX application, is not thread-safe and can only be accessed and modified from the UI thread also known as the JavaFX Application thread.

So, officially, all the update to the UI must happen from the JavaFX application thread. Why this was not "mandatory" for ImageViews in late-2015 versions of Java 8 was probably a bug...

luigidr commented 8 years ago

Solved by bc68dd37a5df6538eaa1a05fd895a79ca758777f.