A project which shows how to use JavaFX together with OpenCV / JavaCV bindings.
This project can be used as a starting point for experiments with OpenCV and Java. It showcases the usage of the PixelBuffer API which permits a fast display / rendering of a webcam image stream with JavaFX.
Furthermore it uses javafx-maven-plugin to generate a custom java image.
You will need at least Java11 and Maven 3.6.2 to build.
For a quick glimpse of what this project does just checkout the code and enter
mvn javafx:run -Djavacpp.platform.custom -Djavacpp.platform.host
Note: Providing javacpp properties is not mandatory, but it reduces download size for bytedeco significantly (only fetches libs for your platform - remember to always provide those parameters though).
After downloading dependencies and compiling a window should appear with your webcam image stream.
If you want to create a custom jre distribution, enter
mvn javafx:jlink -Djavacpp.platform.custom -Djavacpp.platform.host
which creates a custom JDK11 based distribution. You can start the application by entering
./target/javacv-webcam/bin/jwebcam
If you peek into the javacv-webcam
directory.
Attention: If you are using Windows or Linux you'll have to adapt module-info.java to include native libraries of suitable for those platforms. On MacOsX, you may have problems with permissions to access your webcam from a java application, make sure to allow access in your system preferences.
This project contains also a configuration for compiling it to a native image using GraalVM. You have to download and install GraalVM in a recent version (I tested it with 20.2.0) and then compile and run it with following command:
mvn clean client:build client:run -Djavacpp.platform.custom -Djavacpp.platform.host -Dgraalvm.home=/path/to/your/graalvm/distribution
After downloading necessary toolings, dependencies and compiling and linking (takes some time) a JavaFX application which uses your webcam will start.
- Have a look at https://openjfx.io where you can find more information and introductory examples for developing JavaFX applications.
- https://github.com/bytedeco/javacv is the home of JavaCV which enables Java Developers to use native libraries like OpenCV.
- Some posts on http://ladstatt.blogspot.com/ may be of interest, too.