diff --git a/webcam-capture-examples/webcam-capture-javafx/.classpath b/webcam-capture-examples/webcam-capture-javafx/.classpath new file mode 100644 index 00000000..03d699bf --- /dev/null +++ b/webcam-capture-examples/webcam-capture-javafx/.classpath @@ -0,0 +1,27 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/webcam-capture-examples/webcam-capture-javafx/.project b/webcam-capture-examples/webcam-capture-javafx/.project new file mode 100644 index 00000000..1fb499ae --- /dev/null +++ b/webcam-capture-examples/webcam-capture-javafx/.project @@ -0,0 +1,23 @@ + + + webcam-capture-example-executable + + + + + + org.eclipse.jdt.core.javabuilder + + + + + org.eclipse.m2e.core.maven2Builder + + + + + + org.eclipse.m2e.core.maven2Nature + org.eclipse.jdt.core.javanature + + diff --git a/webcam-capture-examples/webcam-capture-javafx/README.md b/webcam-capture-examples/webcam-capture-javafx/README.md new file mode 100644 index 00000000..81cd4c66 --- /dev/null +++ b/webcam-capture-examples/webcam-capture-javafx/README.md @@ -0,0 +1,14 @@ +# JavaFX Example With Webcam Capture API + +This simple example, generously provided by Rakesh Bhatt ([rakeshbhatt10](https://github.com/rakeshbhatt10)), +demonstrates how to use Webcam Capture API inside JavaFX application. +FXML is not used at all and complete scene structure is composed directly +in the Java code. For the FXML demonstration check other example. + +## Screenshoots + +[TBA] + +## License + +Copyright (C) 2014 Rakesh Bhatt (rakeshbhatt10) diff --git a/webcam-capture-examples/webcam-capture-javafx/pom.xml b/webcam-capture-examples/webcam-capture-javafx/pom.xml new file mode 100644 index 00000000..6f64322f --- /dev/null +++ b/webcam-capture-examples/webcam-capture-javafx/pom.xml @@ -0,0 +1,60 @@ + + + 4.0.0 + + + com.github.sarxos + webcam-capture-examples + 0.3.10-SNAPSHOT + + + webcam-capture-example-javafx + jar + + Webcam Capture - JavaFX Example + + Example demonstrating how to use Webcam Capture API inside JavaFX + application without FXML. For the FXML support check other example. + + + + + com.github.sarxos + webcam-capture + ${project.version} + + + + + + jdk17 + + 1.7 + + + + com.oracle + javafx + 2.2 + ${java.home}/lib/jfxrt.jar + system + + + + + + + WebCamAppLauncher + + + org.apache.maven.plugins + maven-compiler-plugin + 2.5.1 + + 1.7 + 1.7 + + + + + diff --git a/webcam-capture-examples/webcam-capture-javafx/src/etc/resources/javafx1.png b/webcam-capture-examples/webcam-capture-javafx/src/etc/resources/javafx1.png new file mode 100644 index 00000000..4ba1478a Binary files /dev/null and b/webcam-capture-examples/webcam-capture-javafx/src/etc/resources/javafx1.png differ diff --git a/webcam-capture-examples/webcam-capture-javafx/src/etc/resources/javafx2.png b/webcam-capture-examples/webcam-capture-javafx/src/etc/resources/javafx2.png new file mode 100644 index 00000000..4e883d86 Binary files /dev/null and b/webcam-capture-examples/webcam-capture-javafx/src/etc/resources/javafx2.png differ diff --git a/webcam-capture-examples/webcam-capture-javafx/src/main/java/WebCamAppLauncher.java b/webcam-capture-examples/webcam-capture-javafx/src/main/java/WebCamAppLauncher.java new file mode 100644 index 00000000..998c574f --- /dev/null +++ b/webcam-capture-examples/webcam-capture-javafx/src/main/java/WebCamAppLauncher.java @@ -0,0 +1,296 @@ +import java.awt.image.BufferedImage; + +import javafx.application.Application; +import javafx.application.Platform; +import javafx.beans.property.ObjectProperty; +import javafx.beans.property.SimpleObjectProperty; +import javafx.beans.value.ChangeListener; +import javafx.beans.value.ObservableValue; +import javafx.collections.FXCollections; +import javafx.collections.ObservableList; +import javafx.concurrent.Task; +import javafx.embed.swing.SwingFXUtils; +import javafx.event.ActionEvent; +import javafx.event.EventHandler; +import javafx.geometry.Orientation; +import javafx.geometry.Pos; +import javafx.scene.Scene; +import javafx.scene.control.Button; +import javafx.scene.control.ComboBox; +import javafx.scene.control.Label; +import javafx.scene.image.Image; +import javafx.scene.image.ImageView; +import javafx.scene.layout.BorderPane; +import javafx.scene.layout.FlowPane; +import javafx.stage.Stage; + +import com.github.sarxos.webcam.Webcam; + + +/** + * This example demonstrates how to use Webcam Capture API in a JavaFX + * application. + * + * @author Rakesh Bhatt (rakeshbhatt10) + */ +@SuppressWarnings("restriction") +public class WebCamAppLauncher extends Application { + + private class WebCamInfo { + + private String webCamName; + private int webCamIndex; + + public String getWebCamName() { + return webCamName; + } + + public void setWebCamName(String webCamName) { + this.webCamName = webCamName; + } + + public int getWebCamIndex() { + return webCamIndex; + } + + public void setWebCamIndex(int webCamIndex) { + this.webCamIndex = webCamIndex; + } + + @Override + public String toString() { + return webCamName; + } + } + + private FlowPane bottomCameraControlPane; + private FlowPane topPane; + private BorderPane root; + private String cameraListPromptText = "Choose Camera"; + private ImageView imgWebCamCapturedImage; + private Webcam webCam = null; + private boolean stopCamera = false; + private BufferedImage grabbedImage; + private ObjectProperty imageProperty = new SimpleObjectProperty(); + private BorderPane webCamPane; + private Button btnCamreaStop; + private Button btnCamreaStart; + private Button btnCameraDispose; + + @Override + public void start(Stage primaryStage) { + + primaryStage.setTitle("Connecting Camera Device Using Webcam Capture API"); + + root = new BorderPane(); + topPane = new FlowPane(); + topPane.setAlignment(Pos.CENTER); + topPane.setHgap(20); + topPane.setOrientation(Orientation.HORIZONTAL); + topPane.setPrefHeight(40); + root.setTop(topPane); + webCamPane = new BorderPane(); + webCamPane.setStyle("-fx-background-color: #ccc;"); + imgWebCamCapturedImage = new ImageView(); + webCamPane.setCenter(imgWebCamCapturedImage); + root.setCenter(webCamPane); + createTopPanel(); + bottomCameraControlPane = new FlowPane(); + bottomCameraControlPane.setOrientation(Orientation.HORIZONTAL); + bottomCameraControlPane.setAlignment(Pos.CENTER); + bottomCameraControlPane.setHgap(20); + bottomCameraControlPane.setVgap(10); + bottomCameraControlPane.setPrefHeight(40); + bottomCameraControlPane.setDisable(true); + createCameraControls(); + root.setBottom(bottomCameraControlPane); + + primaryStage.setScene(new Scene(root)); + primaryStage.setHeight(700); + primaryStage.setWidth(600); + primaryStage.centerOnScreen(); + primaryStage.show(); + + Platform.runLater(new Runnable() { + + @Override + public void run() { + setImageViewSize(); + } + }); + + } + + protected void setImageViewSize() { + + double height = webCamPane.getHeight(); + double width = webCamPane.getWidth(); + + imgWebCamCapturedImage.setFitHeight(height); + imgWebCamCapturedImage.setFitWidth(width); + imgWebCamCapturedImage.prefHeight(height); + imgWebCamCapturedImage.prefWidth(width); + imgWebCamCapturedImage.setPreserveRatio(true); + + } + + private void createTopPanel() { + + int webCamCounter = 0; + Label lbInfoLabel = new Label("Select Your WebCam Camera"); + ObservableList options = FXCollections.observableArrayList(); + + topPane.getChildren().add(lbInfoLabel); + + for (Webcam webcam : Webcam.getWebcams()) { + WebCamInfo webCamInfo = new WebCamInfo(); + webCamInfo.setWebCamIndex(webCamCounter); + webCamInfo.setWebCamName(webcam.getName()); + options.add(webCamInfo); + webCamCounter++; + } + + ComboBox cameraOptions = new ComboBox(); + cameraOptions.setItems(options); + cameraOptions.setPromptText(cameraListPromptText); + cameraOptions.getSelectionModel().selectedItemProperty().addListener(new ChangeListener() { + + @Override + public void changed(ObservableValue arg0, WebCamInfo arg1, WebCamInfo arg2) { + if (arg2 != null) { + System.out.println("WebCam Index: " + arg2.getWebCamIndex() + ": WebCam Name:" + arg2.getWebCamName()); + initializeWebCam(arg2.getWebCamIndex()); + } + } + }); + topPane.getChildren().add(cameraOptions); + } + + protected void initializeWebCam(final int webCamIndex) { + + Task webCamTask = new Task() { + + @Override + protected Void call() throws Exception { + + if (webCam != null) { + disposeWebCamCamera(); + } + + webCam = Webcam.getWebcams().get(webCamIndex); + webCam.open(); + + startWebCamStream(); + + return null; + } + }; + + Thread webCamThread = new Thread(webCamTask); + webCamThread.setDaemon(true); + webCamThread.start(); + + bottomCameraControlPane.setDisable(false); + btnCamreaStart.setDisable(true); + } + + protected void startWebCamStream() { + + stopCamera = false; + + Task task = new Task() { + + @Override + protected Void call() throws Exception { + + while (!stopCamera) { + try { + if ((grabbedImage = webCam.getImage()) != null) { + + Platform.runLater(new Runnable() { + + @Override + public void run() { + Image mainiamge = SwingFXUtils.toFXImage(grabbedImage, null); + imageProperty.set(mainiamge); + } + }); + + grabbedImage.flush(); + } + } catch (Exception e) { + e.printStackTrace(); + } + } + + return null; + } + }; + + Thread th = new Thread(task); + th.setDaemon(true); + th.start(); + imgWebCamCapturedImage.imageProperty().bind(imageProperty); + + } + + private void createCameraControls() { + + btnCamreaStop = new Button(); + btnCamreaStop.setOnAction(new EventHandler() { + + @Override + public void handle(ActionEvent arg0) { + + stopWebCamCamera(); + } + }); + btnCamreaStop.setText("Stop Camera"); + btnCamreaStart = new Button(); + btnCamreaStart.setOnAction(new EventHandler() { + + @Override + public void handle(ActionEvent arg0) { + startWebCamCamera(); + } + }); + btnCamreaStart.setText("Start Camera"); + btnCameraDispose = new Button(); + btnCameraDispose.setText("Dispose Camera"); + btnCameraDispose.setOnAction(new EventHandler() { + + @Override + public void handle(ActionEvent arg0) { + disposeWebCamCamera(); + } + }); + bottomCameraControlPane.getChildren().add(btnCamreaStart); + bottomCameraControlPane.getChildren().add(btnCamreaStop); + bottomCameraControlPane.getChildren().add(btnCameraDispose); + } + + protected void disposeWebCamCamera() { + stopCamera = true; + webCam.close(); + Webcam.shutdown(); + btnCamreaStart.setDisable(true); + btnCamreaStop.setDisable(true); + } + + protected void startWebCamCamera() { + stopCamera = false; + startWebCamStream(); + btnCamreaStop.setDisable(false); + btnCamreaStart.setDisable(true); + } + + protected void stopWebCamCamera() { + stopCamera = true; + btnCamreaStart.setDisable(false); + btnCamreaStop.setDisable(true); + } + + public static void main(String[] args) { + launch(args); + } +}