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

Commit 5b406c5

Browse files
committed
published version
0 parents  commit 5b406c5

File tree

16 files changed

+416
-0
lines changed

16 files changed

+416
-0
lines changed

FXHelloCV/.classpath

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<classpath>
3+
<classpathentry kind="src" path="src"/>
4+
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.7"/>
5+
<classpathentry kind="con" path="org.eclipse.jdt.USER_LIBRARY/opencv2"/>
6+
<classpathentry kind="con" path="org.eclipse.fx.ide.jdt.core.JAVAFX_CONTAINER"/>
7+
<classpathentry kind="output" path="bin"/>
8+
</classpath>

FXHelloCV/.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/bin

FXHelloCV/.project

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<projectDescription>
3+
<name>FXHelloCV</name>
4+
<comment></comment>
5+
<projects>
6+
</projects>
7+
<buildSpec>
8+
<buildCommand>
9+
<name>org.eclipse.jdt.core.javabuilder</name>
10+
<arguments>
11+
</arguments>
12+
</buildCommand>
13+
<buildCommand>
14+
<name>org.eclipse.xtext.ui.shared.xtextBuilder</name>
15+
<arguments>
16+
</arguments>
17+
</buildCommand>
18+
</buildSpec>
19+
<natures>
20+
<nature>org.eclipse.xtext.ui.shared.xtextNature</nature>
21+
<nature>org.eclipse.jdt.core.javanature</nature>
22+
</natures>
23+
</projectDescription>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
eclipse.preferences.version=1
2+
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
3+
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.7
4+
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
5+
org.eclipse.jdt.core.compiler.compliance=1.7
6+
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
7+
org.eclipse.jdt.core.compiler.debug.localVariable=generate
8+
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
9+
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
10+
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
11+
org.eclipse.jdt.core.compiler.source=1.7

FXHelloCV/build.fxbuild

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?xml version="1.0" encoding="ASCII"?>
2+
<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">
3+
<deploy packagingFormat="image">
4+
<application name="FXHelloCV" mainclass="it.polito.elite.teaching.cv.FXHelloCV" toolkit="fx"/>
5+
<info title="FXHelloCV"/>
6+
</deploy>
7+
<signjar/>
8+
<files key="opencv" value="D:\Applications\opencv\build\java\x64\opencv_java246.dll"/>
9+
</anttasks:AntTask>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
3+
<?import javafx.geometry.*?>
4+
<?import javafx.scene.control.*?>
5+
<?import javafx.scene.layout.*?>
6+
<?import javafx.scene.image.*?>
7+
8+
9+
<BorderPane xmlns:fx="http://javafx.com/fxml" fx:controller="it.polito.elite.teaching.cv.FXHelloCVController">
10+
<center>
11+
<ImageView id="currentFrame" />
12+
</center>
13+
<bottom>
14+
<HBox alignment="center" >
15+
<padding>
16+
<Insets top="25" right="25" bottom="25" left="25"/>
17+
</padding>
18+
<Button fx:id="button" alignment="center" text="Start camera" onAction="#startCamera" />
19+
</HBox>
20+
</bottom>
21+
</BorderPane>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package it.polito.elite.teaching.cv;
2+
3+
import org.opencv.core.Core;
4+
5+
import javafx.application.Application;
6+
import javafx.stage.Stage;
7+
import javafx.scene.Scene;
8+
import javafx.scene.layout.BorderPane;
9+
import javafx.fxml.FXMLLoader;
10+
11+
/**
12+
* The main class for a JavaFX application. It creates and handle the main
13+
* window with its resources (style, graphics, etc.).
14+
*
15+
* @author <a href="mailto:[email protected]">Luigi De Russis</a>
16+
* @since 2013-10-20
17+
*
18+
*/
19+
public class FXHelloCV extends Application
20+
{
21+
// the root element in the FXML window
22+
private BorderPane rootElement;
23+
24+
@Override
25+
public void start(Stage primaryStage)
26+
{
27+
try
28+
{
29+
// load the FXML resource
30+
FXMLLoader loader = new FXMLLoader(getClass().getResource("FXHelloCV.fxml"));
31+
// store the root element so that the controllers can use it
32+
this.rootElement = (BorderPane) loader.load();
33+
// create and style a scene
34+
Scene scene = new Scene(this.rootElement, 800, 600);
35+
scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
36+
// create the stage with the given title and the previously created
37+
// scene
38+
primaryStage.setTitle("JavaFX meets OpenCV");
39+
primaryStage.setScene(scene);
40+
// show the GUI
41+
primaryStage.show();
42+
43+
// set a reference of this class for its controller
44+
FXHelloCVController controller = loader.getController();
45+
controller.setMainApp(this);
46+
47+
}
48+
catch (Exception e)
49+
{
50+
e.printStackTrace();
51+
}
52+
}
53+
54+
/**
55+
* Getter for the rootElement
56+
*
57+
* @return the {@link BorderPane} that represents the root element
58+
*/
59+
public BorderPane getRootElement()
60+
{
61+
return this.rootElement;
62+
}
63+
64+
/**
65+
* For launching the application...
66+
*
67+
* @param args
68+
* optional params
69+
*/
70+
public static void main(String[] args)
71+
{
72+
// load the native OpenCV library
73+
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
74+
75+
launch(args);
76+
}
77+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,181 @@
1+
package it.polito.elite.teaching.cv;
2+
3+
import java.io.ByteArrayInputStream;
4+
import java.util.Timer;
5+
import java.util.TimerTask;
6+
7+
import javafx.beans.property.ObjectProperty;
8+
import javafx.beans.property.SimpleObjectProperty;
9+
import javafx.event.ActionEvent;
10+
import javafx.fxml.FXML;
11+
import javafx.scene.control.Button;
12+
import javafx.scene.image.Image;
13+
import javafx.scene.image.ImageView;
14+
15+
import org.opencv.core.Mat;
16+
import org.opencv.core.MatOfByte;
17+
import org.opencv.highgui.Highgui;
18+
import org.opencv.highgui.VideoCapture;
19+
import org.opencv.imgproc.Imgproc;
20+
21+
/**
22+
* The controller for our application, where the application logic is
23+
* implemented. It handles the button for starting/stopping the camera and the
24+
* acquired video stream.
25+
*
26+
* @author <a href="mailto:[email protected]">Luigi De Russis</a>
27+
* @since 2013-10-20
28+
*
29+
*/
30+
public class FXHelloCVController
31+
{
32+
// the FXML button
33+
@FXML
34+
private Button button;
35+
36+
// the main app
37+
private FXHelloCV mainApp;
38+
// a timer for acquiring the video stream
39+
private Timer timer;
40+
// the OpenCV object that realizes the video capture
41+
private VideoCapture capture = new VideoCapture();
42+
// a flag to change the button behavior
43+
private boolean cameraActive = false;
44+
45+
/**
46+
* The action triggered by pushing the button on the GUI
47+
*
48+
* @param event
49+
* the push button event
50+
*/
51+
@FXML
52+
protected void startCamera(ActionEvent event)
53+
{
54+
// check: the main class is accessible?
55+
if (this.mainApp != null)
56+
{
57+
// get the ImageView object for showing the video stream
58+
final ImageView frameView = (ImageView) mainApp.getRootElement().lookup("#currentFrame");
59+
// bind an image property with the container for frames
60+
final ObjectProperty<Image> imageProp = new SimpleObjectProperty<>();
61+
frameView.imageProperty().bind(imageProp);
62+
63+
if (!this.cameraActive)
64+
{
65+
// start the video capture
66+
this.capture.open(0);
67+
68+
// is the video stream available?
69+
if (this.capture.isOpened())
70+
{
71+
this.cameraActive = true;
72+
73+
// grab a frame every 33 ms (30 frames/sec)
74+
TimerTask frameGrabber = new TimerTask() {
75+
@Override
76+
public void run()
77+
{
78+
// update the image property => update the frame
79+
// shown in the UI
80+
imageProp.set(grabFrame());
81+
}
82+
};
83+
this.timer = new Timer();
84+
this.timer.schedule(frameGrabber, 0, 33);
85+
86+
// update the button content
87+
this.button.setText("Stop Camera");
88+
}
89+
else
90+
{
91+
// log the error
92+
System.err.println("Impossible to open the camera connection...");
93+
}
94+
}
95+
else
96+
{
97+
// the camera is not active at this point
98+
this.cameraActive = false;
99+
// update again the button content
100+
this.button.setText("Start Camera");
101+
// stop the timer
102+
if (this.timer != null)
103+
{
104+
this.timer.cancel();
105+
this.timer = null;
106+
}
107+
// release the camera
108+
this.capture.release();
109+
}
110+
}
111+
}
112+
113+
/**
114+
* Get a frame from the opened video stream (if any)
115+
*
116+
* @return the {@link Image} to show
117+
*/
118+
private Image grabFrame()
119+
{
120+
// init everything
121+
Image imageToShow = null;
122+
Mat frame = new Mat();
123+
124+
// check if the capture is open
125+
if (this.capture.isOpened())
126+
{
127+
try
128+
{
129+
// read the current frame
130+
this.capture.read(frame);
131+
132+
// if the frame is not empty, process it
133+
if (!frame.empty())
134+
{
135+
// convert the image to gray scale
136+
Imgproc.cvtColor(frame, frame, Imgproc.COLOR_BGR2GRAY);
137+
// convert the Mat object (OpenCV) to Image (JavaFX)
138+
imageToShow = mat2Image(frame);
139+
}
140+
141+
}
142+
catch (Exception e)
143+
{
144+
// log the error
145+
System.err.println("ERROR: " + e.getMessage());
146+
}
147+
}
148+
149+
return imageToShow;
150+
}
151+
152+
/**
153+
* Convert a Mat object (OpenCV) in the corresponding Image for JavaFX
154+
*
155+
* @param frame
156+
* the {@link Mat} representing the current frame
157+
* @return the {@link Image} to show
158+
*/
159+
private Image mat2Image(Mat frame)
160+
{
161+
// create a temporary buffer
162+
MatOfByte buffer = new MatOfByte();
163+
// encode the frame in the buffer
164+
Highgui.imencode(".png", frame, buffer);
165+
// build and return an Image created from the image encoded in the
166+
// buffer
167+
return new Image(new ByteArrayInputStream(buffer.toArray()));
168+
}
169+
170+
/**
171+
* Set the reference to the main class of the application
172+
*
173+
* @param mainApp
174+
* the {@FXHelloCV} object to set
175+
*/
176+
public void setMainApp(FXHelloCV mainApp)
177+
{
178+
this.mainApp = mainApp;
179+
}
180+
181+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/* intentionally left empty */

HelloCV/.classpath

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<classpath>
3+
<classpathentry kind="src" path="src"/>
4+
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.7"/>
5+
<classpathentry kind="con" path="org.eclipse.jdt.USER_LIBRARY/opencv2"/>
6+
<classpathentry kind="output" path="bin"/>
7+
</classpath>

HelloCV/.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/bin

HelloCV/.project

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<projectDescription>
3+
<name>HelloCV</name>
4+
<comment></comment>
5+
<projects>
6+
</projects>
7+
<buildSpec>
8+
<buildCommand>
9+
<name>org.eclipse.jdt.core.javabuilder</name>
10+
<arguments>
11+
</arguments>
12+
</buildCommand>
13+
</buildSpec>
14+
<natures>
15+
<nature>org.eclipse.jdt.core.javanature</nature>
16+
</natures>
17+
</projectDescription>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
eclipse.preferences.version=1
2+
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
3+
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.7
4+
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
5+
org.eclipse.jdt.core.compiler.compliance=1.7
6+
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
7+
org.eclipse.jdt.core.compiler.debug.localVariable=generate
8+
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
9+
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
10+
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
11+
org.eclipse.jdt.core.compiler.source=1.7

HelloCV/resources/Poli.jpg

126 KB
Loading

0 commit comments

Comments
 (0)