IntelliJ does not find native libraries for OpenCV when adding jar as a dependency for a Play project

I am currently working on a Play 2.1 project in which requests for a web service will process the loading of user images, their re-sorting and re-cropping, as well as filtering of known bad photos (for example, we do not want users to upload company logos). We are trying to use OpenCV to handle background work, but I can't get IntelliJ to add an OpenCV banner in a way that works with a java project.

I was able to create OpenCV from source without problems. This left me with the following folder: / home / charles / opencv / letting go

Inside this folder, I have three files of interest:

  • bin / opencv -246.jar
  • Library / cv 2.so
  • Library / libopencv _java246.so

If I try to add the jar file to IntelliJ as a new Java library, it seems to find all the classes / methods, and I can write the code using autocomplete. I can also click on the appropriate classes or methods, and this leads me to the correct files.

However, when I try to start the Play project, I get this error:

[info] Loading project definition from /home/charles/Github/ImageProject [info] Set current project to ImageProject (in build file:/home/charles/Github/ImageProject/) --- (Running the application from SBT, auto-reloading is enabled) --- [info] play - Listening for HTTP on /0:0:0:0:0:0:0:0:9000 Server started, use Alt+D to stop [info] Compiling 1 Java source to /home/charles/Github/ImageProject/target/scala-2.10/classes... [error] /home/charles/Github/ImageProject/app/controllers/Application.java:7: error: package org.opencv.core does not exist [error] import org.opencv.core.Core; [error] ^ 

I also tried adding a copy of the jar file directly to the project (to put opencv-246.jar in ImageProject / lib), and then instead adding the java library from this place. But that just leaves me with another error:

 java.lang.UnsatisfiedLinkError: no opencv_java246 in java.library.path 

I suspect that part of the problem may be related to the native libraries that the Java OpenCV shell uses (file 2 or 3 above). In Eclipse, when you add a jar file, you can explicitly specify the location of your own library, which makes OpenCV great. I read suggestions about using this to fix the problem:

 -Djava.library.path=/home/charles/opencv/release/lib 

But this does not work (although maybe I am installing it in the wrong place? I tried to install it as a JVM parameter in the startup configuration for the project and in the IDE settings, but none seem to be used or respected).

Note. To clarify again, this is a Play2 project, not an Android project. It seems that there is some special help from Android, which in this case is not relevant.

It seems like it should be fairly straightforward, but I have been trying to find the answer at this point for several days and still have nothing. Any ideas?

Additional Information: I also tried following the “Launches SBT Samples” OpenCV documentation here: http://docs.opencv.org/doc/tutorials/introduction/desktop_java/java_dev_intro.html

And I also get a similar error:

 charles@charles-VirtualBox :~/JavaSample$ sbt run [info] Loading project definition from /home/charles/JavaSample/project [info] Set current project to JavaSample (in build file:/home/charles/JavaSample/) [info] Compiling 1 Java source to /home/charles/JavaSample/target/scala-2.10/classes... [info] Running HelloOpenCV Hello, OpenCV [error] (run-main) java.lang.UnsatisfiedLinkError: no opencv_java246 in java.library.path java.lang.UnsatisfiedLinkError: no opencv_java246 in java.library.path at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1856) at java.lang.Runtime.loadLibrary0(Runtime.java:845) at java.lang.System.loadLibrary(System.java:1084) at HelloOpenCV.main(HelloOpenCV.java:47) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:601) [trace] Stack trace suppressed: run last compile:run for the full output. java.lang.RuntimeException: Nonzero exit code: 1 at scala.sys.package$.error(package.scala:27) [trace] Stack trace suppressed: run last compile:run for the full output. [error] (compile:run) Nonzero exit code: 1 [error] Total time: 2 s, completed Jul 17, 2013 5:11:39 PM 
+6
source share
3 answers

If you want to use OpenCV or any other proprietary libraries in Playframework, you MUST launch your application using the "start the game" command, not the "start" command.

Command

"play run" launches your application in development mode, and the "start game" command starts in production mode. I don’t know every difference between them, but one obvious thing,

Only when we use "play start", a new JVM is launched for your application, and it loads its own libraries that you specified System.load ("/ absolute / path / to / your / so / or / jnilib / inOSX / not / dylib /filename.jnilib ");

How to load your own lib.

Create Global.java that has an empty package name. (see this link )

 public class Global extends GlobalSettings { @Override public void beforeStart(Application app) { // TODO Auto-generated method stub super.beforeStart(app); String libopencv_java = "/Users/yoonjechoi/git/myFirstApp/target/native_libraries/64bits/libopencv_java246.jnilib"; System.load(libopencv_java); } } 

then you can use the OpenCV classes in your Play application controllers.

System.loadLibrary ("opencv_java246") does not work. I do not know why. I don’t have time to snatch why. -_-;

Please give hints if you know why.

+1
source

for 64-bit Windows

-Djava.library.path = / full path / OpenCV / assembly / Java / x64

0
source

I would use lib / libopencv_java246.so

It is always important to display the .so file to create the library. Suppose it works out of the box

0
source

Source: https://habr.com/ru/post/949673/


All Articles