sarxos / webcam-capture

The goal of this project is to allow integrated or USB-connected webcams to be accessed directly from Java. Using provided libraries users are able to read camera images and detect motion. Main project consist of several sub projects - the root one, which contains required classes, build-in webcam driver compatible with Windows, Linux and Mac OS, which can stream images as fast as your camera can serve them (up to 50 FPS). Main project can be used standalone, but user is able to replace build-in driver with different one - such as OpenIMAJ, GStreamer, V4L4j, JMF, LTI-CIVIL, FMJ, etc.
http://webcam-capture.sarxos.pl
MIT License
2.26k stars 1.11k forks source link

Webcam Capture API JavaFX example #241

Open uyyelik opened 10 years ago

uyyelik commented 10 years ago

hi Dear all Swing is dead Simple webcam capture for javafx example

please

sarxos commented 10 years ago

Hi @kypdk,

All, I will be glad to merge any JavaFX example if someone write it :)

I'm completely unfamiliar with this technology, so sorry, I can't do it myself :( Need your help here.

Cheers!

uyyelik commented 10 years ago

public class FXMLDocumentController implements Initializable {

@FXML
private Pane webpanel; 
private WebcamPanel panel = null;
private Webcam webcam = null;
private ImageView imagev = null;

@Override
public void initialize(URL url, ResourceBundle rb) {
        webcam = Webcam.getDefault();
        webpanel = new WebcamPanel(webcam);
        webpanel = new Pane(panel);  (error : not converted webpanel to Pane)
        webpanel.getChildren().add(panel);  (error : not converted webpanel to Pane)

 }
}

javafx not support ? i'm trying but it did not

sarxos commented 10 years ago

This is completely invalid:

webpanel = new WebcamPanel(webcam);
webpanel = new Pane(panel);  (error : not converted webpanel to Pane)

Because Pane is not a subclass of WebcamPanel. Have you tried with SwingNode from Java 8? Example I found here (I just replaced JButton by WebcamPanel):

import javafx.application.Application;
import javafx.embed.swing.SwingNode;
import javafx.scene.Scene;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;

import javax.swing.SwingUtilities;

import com.github.sarxos.webcam.Webcam;
import com.github.sarxos.webcam.WebcamPanel;

public class SwingFx extends Application {

    private Webcam webcam = Webcam.getDefault();
    private WebcamPanel webcamPanel = new WebcamPanel(webcam, false);

    @Override
    public void start(Stage stage) {
        final SwingNode swingNode = new SwingNode();

        createSwingContent(swingNode);

        StackPane pane = new StackPane();
        pane.getChildren().add(swingNode);

        stage.setTitle("Swing in JavaFX");
        stage.setScene(new Scene(pane, 250, 150));
        stage.show();
    }

    private void createSwingContent(final SwingNode swingNode) {
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                swingNode.setContent(webcamPanel);
                webcamPanel.start();
            }
        });
    }
}

I cannot give you more details because I'm not familiar with JavaFX.

rakeshbhatt10 commented 10 years ago

Little bit late kypdk i had also written one another example to connect with webcam using sarxos API. The mentioned code from sarxos example works fine only there is main method missing.

Link for my sample class: https://github.com/rakeshbhatt10/WebCamJavaFXSampleSarcoxAPI/blob/master/WebCamCaptureJavaFx/src/webCamAppLauncher/WebCamAppLauncher.java

While trying to attach WebCamPanel from FXML Controller can be little bit of mess. You can try this example to update ImageView from webcam feed.

Thanks, Rakesh

sarxos commented 10 years ago

Hi @rakeshbhatt10,

This is awesome :) Can I use your code as one of the examples in the Webcam Capture API sources?

uyyelik commented 10 years ago

thank you so much bro best javafx+sarxos webcam example

rakeshbhatt10 commented 10 years ago

Hi @sarxos ,

Sorry for misspelling of your name in my code and repo link. It will be my pleasure if you consider my example to be included in your project thanks a lot. I had to improve JavaFX example code little bit. I wrote it in hurry I will improve it and push it today.

I had not tested this example so can't say anything about performance . I tried to implement this example with separate Pane or Canvas but it was not that much successful.

In this example I had coded GUI. But i will post one more example of webcam capture using JavaFX with FXML that will give you @kypdk more clarity i suppose.

Thanks, Rakesh

sarxos commented 10 years ago

Hi @rakeshbhatt10,

Don't worry :) I tested it and it works very well on Java 8 :) I didn't fork whole the project, but just the WebCamAppLauncher.java file. There were JARs I didn't want to include in repo since Maven is working hard to manage all the dependencies mess for us.

rakeshbhatt10 commented 10 years ago

Okey that will be fine @sarxos you can use this class as you want but i think in case of JavaFx it will be good if there is also an example using FXML. If you want i can write a sample for that too

uyyelik commented 10 years ago

thank you so much bro best javafx+sarxos-webcam example, look forward to fxml example

sarxos commented 10 years ago

@rakeshbhatt10, if you have time and resources for that, please feel free to craft FXML example as well :) I will be happy to include both in the Webcam Capture API examples.

rakeshbhatt10 commented 10 years ago

@kypdk I am sharing you a sample i had written using JavaFX with FXML

https://github.com/rakeshbhatt10/WebCamJavaFXFXMLSample/blob/master/WebCamCaptureFXMLExample/src/laucher/WebCamAppLauncher.java

@sarxos I was not successful at using WebCamPanel by adding it to SwingNode. It is not giving any output and also no error.

While shared sample is running code but it uses Webcam class to get image and update it to ImageView

sarxos commented 10 years ago

@rakeshbhatt10

I hoped it will work - this would make it much easier :( Maybe in some time I will learn more about JavaFX and craft dedicated component.

Later this month (need to wait for the vacation to have some more time for coding) I will convert your projects to build with Maven and put them in the examples folder of Webcam Capture API.

Thank you!

carldea commented 10 years ago

Sarxos, I am really interested in offering any JavaFX assistance.

I have things working in a demo. http://m.youtube.com/watch?v=op2mJaufOw8

Thanks!, Carl

sarxos commented 10 years ago

Dear Team :)

Sorry for no response for the time. I just came back from the vacation few days ago, full of energy to play with some new stuff :)

@rakeshbhatt10,

I integrated both your examples in the Webcam Capture examples root. Your projects has been Mavenized, some small changes in the code (mostly automated formatting rules) introduced as well, but overall code mechanic remains unchanged. Thank you for this contribution :) By integrating them I managed to learn JavaFX a little bit as well.

@carldea,

I will be happy to use your knowledge and merge pull requests you prepare and/or integrate the code from the example you posted on the youtube (it's awesome) :) The JavaFX seems to be very feature rich since I was completely unaware that one can control application by the JavaScript code. I'm looking forward to hear from you.

rakeshbhatt10 commented 10 years ago

Hi @sarxos great work and thanks again. Quite few updates on JavaFx as @carldea has provided one more solution based on JavaFx scripting i suppose

JavaFx script is available in JavaFx 1.x release which is now obsolete as well as oracle support for it has been ended you can check these link http://www.oracle.com/technetwork/java/javafx/overview/faq-1446554.html#8 http://stackoverflow.com/questions/21055710/what-is-the-difference-between-javafx-scripting-and-using-regular-java-syntax-wh http://java.dzone.com/articles/oracle-discontinue-javafx

Now in JavaFX 2.x series. There is no requirement for any scripting language and it is possible to code in Java API. Instead of using any Scripting to build GUI use FXML which also can be build with scenebuilder. For creating a thread safe API you can check javafx.concurrent package i think it will help you more

Thanks,

sarxos commented 10 years ago

@rakeshbhatt10, than you for the links, I will check them :) I'm planning to craft dedicated component for JavaFX (like WebcamPanel for Swing).

Splashmou commented 1 year ago

hi Dear all Swing is dead Simple webcam capture for javafx example

please

ALPS