Detective_Khalifah
Detective_Khalifah

Reputation: 53

How do import OpenCV to a JavaFX (non-modular project) on IntelliJ IDEA 2022.1.1 (Community Edition)?

I'm trying out this project from GitHub. There isn't much documentation on the developer's part regarding how to setup the project and whatnot. Googling a way to run the JavaFX app, I understood the way to set it up and get it running is to create a JavaFX app on IntelliJ and copy over the files from that project (from GitHub) to the new project, I was able to copy the files, but I realised I had to import the opencv project library, used by many classes in the project.

I Googled again, downloaded opencv, added it as a Global Library, and restart IntelliJ. But try as I might, the project doesn't sense the presence opencv; I've just been bumping into error after error, without progress so far as running the app is in consideration.

I tried adding it to the build.gradle file, but my search so far yielded no documentation or instruction regarding setup using build.gradle, save Baeldung's; I added the dependency and it pops errors at sync.

Here are screenshots of the setup and errors I'm facing at the moment:

OpenCV disappears from Modules in Project Structure, and I have to re-add it sometimes.

I'm using:

Stack Trace:

21:46:27: Executing ':Main.main()'...


> Configure project :
Project : => no module-info.java found

> Task :compileJava
C:\Users\mich\IdeaProjects\DjikstraAlgo\src\main\java\org\openjfx\djikstraalgo\Algorithm\ConvertToArray.java:4: error: package org.opencv.core does not exist
import org.opencv.core.Core;
                  ^
C:\Users\mich\IdeaProjects\DjikstraAlgo\src\main\java\org\openjfx\djikstraalgo\Algorithm\ConvertToArray.java:5: error: package org.opencv.core does not exist
import org.opencv.core.Mat;
                  ^
C:\Users\mich\IdeaProjects\DjikstraAlgo\src\main\java\org\openjfx\djikstraalgo\Algorithm\ConvertToArray.java:6: error: package org.opencv.imgcodecs does not exist
import org.opencv.imgcodecs.Imgcodecs;
                       ^
C:\Users\mich\IdeaProjects\DjikstraAlgo\src\main\java\org\openjfx\djikstraalgo\Algorithm\ConvertToArray.java:16: error: cannot find symbol
    Mat img;
    ^
  symbol:   class Mat
  location: class ConvertToArray
C:\Users\mich\IdeaProjects\DjikstraAlgo\src\main\java\org\openjfx\djikstraalgo\Algorithm\ConvertToArray.java:21: error: cannot find symbol
    public Mat getMatrix(){
       ^
  symbol:   class Mat
  location: class ConvertToArray
C:\Users\mich\IdeaProjects\DjikstraAlgo\src\main\java\org\openjfx\djikstraalgo\Algorithm\Djikstra.java:7: error: package org.opencv.core does not exist
import org.opencv.core.Mat;
                  ^
C:\Users\mich\IdeaProjects\DjikstraAlgo\src\main\java\org\openjfx\djikstraalgo\Algorithm\Djikstra.java:16: error: cannot find symbol
    Mat img;
    ^
  symbol:   class Mat
  location: class Djikstra
C:\Users\mich\IdeaProjects\DjikstraAlgo\src\main\java\org\openjfx\djikstraalgo\Algorithm\Djikstra.java:20: error: cannot find symbol
    public Djikstra(Mat img, int[] source, int[] sink){
                ^
  symbol:   class Mat
  location: class Djikstra
C:\Users\mich\IdeaProjects\DjikstraAlgo\src\main\java\org\openjfx\djikstraalgo\Controller.java:19: error: package org.opencv.core does not exist
import org.opencv.core.Mat;
                  ^
C:\Users\mich\IdeaProjects\DjikstraAlgo\src\main\java\org\openjfx\djikstraalgo\Algorithm\ConvertToArray.java:19: error: cannot find symbol
        img = Imgcodecs.imread(System.getProperty("user.dir") + "\\src\\images\\" +filepath);
          ^
  symbol:   variable Imgcodecs
  location: class ConvertToArray
C:\Users\mich\IdeaProjects\DjikstraAlgo\src\main\java\org\openjfx\djikstraalgo\Controller.java:116: error: cannot find symbol
        Mat img = img_grid.getMatrix();
        ^
  symbol:   class Mat
  location: class Controller
11 errors

> Task :compileJava FAILED

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':compileJava'.
> Compilation failed; see the compiler error output for details.

* Try:
> Run with --stacktrace option to get the stack trace.
> Run with --info or --debug option to get more log output.
> Run with --scan to get full insights.

* Get more help at https://help.gradle.org

BUILD FAILED in 8s

Deprecated Gradle features were used in this build, making it incompatible with Gradle 8.0.

You can use '--warning-mode all' to show the individual deprecation warnings and determine if they come from your own scripts or plugins.

See https://docs.gradle.org/7.4.2/userguide/command_line_interface.html#sec:command_line_warnings
1 actionable task: 1 executed
21:46:38: Execution finished ':Main.main()'.

Upvotes: 2

Views: 637

Answers (1)

Detective_Khalifah
Detective_Khalifah

Reputation: 53

Part of the hits I dug online was an addition to build.gradle:

compile fileTree(dir:'path:\\to\\opencv\\lib\\', includes: ['*jar'])

I was using

implementation fileTree(dir:'D:\\...\\opencv-4.6.0-vc14_vc15\\build\\java\\x64\\', includes: ['*jar'])

which has the opencv_java460 file.

**Removing 64\\ part solved it. I'm using

implementation fileTree(dir:'D:\\...\\opencv-4.6.0-vc14_vc15\\build\\java\\', includes: ['*jar'])

Upvotes: 2

Related Questions