Skip to content
This repository has been archived by the owner on Feb 21, 2021. It is now read-only.

Commit

Permalink
published version
Browse files Browse the repository at this point in the history
  • Loading branch information
luigidr committed Nov 6, 2014
0 parents commit 5b406c5
Show file tree
Hide file tree
Showing 16 changed files with 416 additions and 0 deletions.
8 changes: 8 additions & 0 deletions FXHelloCV/.classpath
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="src"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.7"/>
<classpathentry kind="con" path="org.eclipse.jdt.USER_LIBRARY/opencv2"/>
<classpathentry kind="con" path="org.eclipse.fx.ide.jdt.core.JAVAFX_CONTAINER"/>
<classpathentry kind="output" path="bin"/>
</classpath>
1 change: 1 addition & 0 deletions FXHelloCV/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/bin
23 changes: 23 additions & 0 deletions FXHelloCV/.project
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>FXHelloCV</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.xtext.ui.shared.xtextBuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.xtext.ui.shared.xtextNature</nature>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>
11 changes: 11 additions & 0 deletions FXHelloCV/.settings/org.eclipse.jdt.core.prefs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.7
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
org.eclipse.jdt.core.compiler.compliance=1.7
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
org.eclipse.jdt.core.compiler.debug.localVariable=generate
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
org.eclipse.jdt.core.compiler.source=1.7
9 changes: 9 additions & 0 deletions FXHelloCV/build.fxbuild
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="ASCII"?>
<anttasks:AntTask xmi:version="2.0" xmlns:xmi="http://www.omg.org/XMI" xmlns:anttasks="http://org.eclipse.fx.ide.jdt/1.0" buildDirectory="${project}/build" cssToBin="true">
<deploy packagingFormat="image">
<application name="FXHelloCV" mainclass="it.polito.elite.teaching.cv.FXHelloCV" toolkit="fx"/>
<info title="FXHelloCV"/>
</deploy>
<signjar/>
<files key="opencv" value="D:\Applications\opencv\build\java\x64\opencv_java246.dll"/>
</anttasks:AntTask>
21 changes: 21 additions & 0 deletions FXHelloCV/src/it/polito/elite/teaching/cv/FXHelloCV.fxml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.geometry.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.image.*?>


<BorderPane xmlns:fx="http://javafx.com/fxml" fx:controller="it.polito.elite.teaching.cv.FXHelloCVController">
<center>
<ImageView id="currentFrame" />
</center>
<bottom>
<HBox alignment="center" >
<padding>
<Insets top="25" right="25" bottom="25" left="25"/>
</padding>
<Button fx:id="button" alignment="center" text="Start camera" onAction="#startCamera" />
</HBox>
</bottom>
</BorderPane>
77 changes: 77 additions & 0 deletions FXHelloCV/src/it/polito/elite/teaching/cv/FXHelloCV.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package it.polito.elite.teaching.cv;

import org.opencv.core.Core;

import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.layout.BorderPane;
import javafx.fxml.FXMLLoader;

/**
* The main class for a JavaFX application. It creates and handle the main
* window with its resources (style, graphics, etc.).
*
* @author <a href="mailto:[email protected]">Luigi De Russis</a>
* @since 2013-10-20
*
*/
public class FXHelloCV extends Application
{
// the root element in the FXML window
private BorderPane rootElement;

@Override
public void start(Stage primaryStage)
{
try
{
// load the FXML resource
FXMLLoader loader = new FXMLLoader(getClass().getResource("FXHelloCV.fxml"));
// store the root element so that the controllers can use it
this.rootElement = (BorderPane) loader.load();
// create and style a scene
Scene scene = new Scene(this.rootElement, 800, 600);
scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
// create the stage with the given title and the previously created
// scene
primaryStage.setTitle("JavaFX meets OpenCV");
primaryStage.setScene(scene);
// show the GUI
primaryStage.show();

// set a reference of this class for its controller
FXHelloCVController controller = loader.getController();
controller.setMainApp(this);

}
catch (Exception e)
{
e.printStackTrace();
}
}

/**
* Getter for the rootElement
*
* @return the {@link BorderPane} that represents the root element
*/
public BorderPane getRootElement()
{
return this.rootElement;
}

/**
* For launching the application...
*
* @param args
* optional params
*/
public static void main(String[] args)
{
// load the native OpenCV library
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);

launch(args);
}
}
181 changes: 181 additions & 0 deletions FXHelloCV/src/it/polito/elite/teaching/cv/FXHelloCVController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,181 @@
package it.polito.elite.teaching.cv;

import java.io.ByteArrayInputStream;
import java.util.Timer;
import java.util.TimerTask;

import javafx.beans.property.ObjectProperty;
import javafx.beans.property.SimpleObjectProperty;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;

import org.opencv.core.Mat;
import org.opencv.core.MatOfByte;
import org.opencv.highgui.Highgui;
import org.opencv.highgui.VideoCapture;
import org.opencv.imgproc.Imgproc;

/**
* The controller for our application, where the application logic is
* implemented. It handles the button for starting/stopping the camera and the
* acquired video stream.
*
* @author <a href="mailto:[email protected]">Luigi De Russis</a>
* @since 2013-10-20
*
*/
public class FXHelloCVController
{
// the FXML button
@FXML
private Button button;

This comment has been minimized.

Copy link
@zavalyshyn

zavalyshyn Jun 30, 2016

According to the tutorial available here the line should be as following:
private Button start_btn

and further appearance of 'button' should be replaced with 'start_btn' accordingly. Otherwise the code works but the camera is not released after it's started and some parts of the code don't work as intended (for instance, changing the text of the button)

This comment has been minimized.

Copy link
@luigidr

luigidr Jul 1, 2016

Author Member

The tutorial is partially misaligned with the code.
According to the images present in the tutorial I agree with you. The button fx:id must be the same in the FXML file and in the controller.

However, this line of code is correct: in this "version" the fx:id of the button is set to button and not to start_btn.

P.S. If possible, next time, could you open a issue so that it is easier to reply and keep track of the conversation? Thanks! :)


// the main app
private FXHelloCV mainApp;
// a timer for acquiring the video stream
private Timer timer;
// the OpenCV object that realizes the video capture
private VideoCapture capture = new VideoCapture();
// a flag to change the button behavior
private boolean cameraActive = false;

/**
* The action triggered by pushing the button on the GUI
*
* @param event
* the push button event
*/
@FXML
protected void startCamera(ActionEvent event)
{
// check: the main class is accessible?
if (this.mainApp != null)
{
// get the ImageView object for showing the video stream
final ImageView frameView = (ImageView) mainApp.getRootElement().lookup("#currentFrame");
// bind an image property with the container for frames
final ObjectProperty<Image> imageProp = new SimpleObjectProperty<>();
frameView.imageProperty().bind(imageProp);

if (!this.cameraActive)
{
// start the video capture
this.capture.open(0);

// is the video stream available?
if (this.capture.isOpened())
{
this.cameraActive = true;

// grab a frame every 33 ms (30 frames/sec)
TimerTask frameGrabber = new TimerTask() {
@Override
public void run()
{
// update the image property => update the frame
// shown in the UI
imageProp.set(grabFrame());
}
};
this.timer = new Timer();
this.timer.schedule(frameGrabber, 0, 33);

// update the button content
this.button.setText("Stop Camera");
}
else
{
// log the error
System.err.println("Impossible to open the camera connection...");
}
}
else
{
// the camera is not active at this point
this.cameraActive = false;
// update again the button content
this.button.setText("Start Camera");
// stop the timer
if (this.timer != null)
{
this.timer.cancel();
this.timer = null;
}
// release the camera
this.capture.release();
}
}
}

/**
* Get a frame from the opened video stream (if any)
*
* @return the {@link Image} to show
*/
private Image grabFrame()
{
// init everything
Image imageToShow = null;
Mat frame = new Mat();

// check if the capture is open
if (this.capture.isOpened())
{
try
{
// read the current frame
this.capture.read(frame);

// if the frame is not empty, process it
if (!frame.empty())
{
// convert the image to gray scale
Imgproc.cvtColor(frame, frame, Imgproc.COLOR_BGR2GRAY);
// convert the Mat object (OpenCV) to Image (JavaFX)
imageToShow = mat2Image(frame);
}

}
catch (Exception e)
{
// log the error
System.err.println("ERROR: " + e.getMessage());
}
}

return imageToShow;
}

/**
* Convert a Mat object (OpenCV) in the corresponding Image for JavaFX
*
* @param frame
* the {@link Mat} representing the current frame
* @return the {@link Image} to show
*/
private Image mat2Image(Mat frame)
{
// create a temporary buffer
MatOfByte buffer = new MatOfByte();
// encode the frame in the buffer
Highgui.imencode(".png", frame, buffer);
// build and return an Image created from the image encoded in the
// buffer
return new Image(new ByteArrayInputStream(buffer.toArray()));
}

/**
* Set the reference to the main class of the application
*
* @param mainApp
* the {@FXHelloCV} object to set
*/
public void setMainApp(FXHelloCV mainApp)
{
this.mainApp = mainApp;
}

}
1 change: 1 addition & 0 deletions FXHelloCV/src/it/polito/elite/teaching/cv/application.css
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/* intentionally left empty */
7 changes: 7 additions & 0 deletions HelloCV/.classpath
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="src"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.7"/>
<classpathentry kind="con" path="org.eclipse.jdt.USER_LIBRARY/opencv2"/>
<classpathentry kind="output" path="bin"/>
</classpath>
1 change: 1 addition & 0 deletions HelloCV/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/bin
17 changes: 17 additions & 0 deletions HelloCV/.project
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>HelloCV</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>
11 changes: 11 additions & 0 deletions HelloCV/.settings/org.eclipse.jdt.core.prefs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.7
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
org.eclipse.jdt.core.compiler.compliance=1.7
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
org.eclipse.jdt.core.compiler.debug.localVariable=generate
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
org.eclipse.jdt.core.compiler.source=1.7
Binary file added HelloCV/resources/Poli.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit 5b406c5

Please sign in to comment.