Mac installs Java OpencV dependencies and is configured for use on IntelliJ

Recently, when applying for a driver’s license, I took a lot of photoshopped photos by myself, thinking of the epidemic. As a result, the elder sister of the city hall measured several photos of P with a ruler and found that either his head was too big, or his clothes and background color were wrong. None of the photos could be used, so I had to start again. So I decided to start a small project that would automatically output a single person’s photo with a solid color background as a standard ID photo.

It’s easy to use Java because the backend is familiar with Java. (in fact, image processing related projects in c++ should be more convenient to write) that first to configure the local environment with Java +opencv, mainly is to download and install opencv Java package and then configure to intellij. Here are the steps:

  1. The command tool must be installed on the Mac
xcode-select --install
Copy the code
  1. Install Ant, you must install Ant (I skipped using Maven and didn’t generate the required Java folder)
brew install ant
Copy the code
  1. Next you need to run Brew Edit OpencV to modify the default configuration of opnecv by changing -dbuild_opencv_java =OFF to -dbuild_opencv_java =ON to ensure that the required JAR files are generated later.

  2. Install OpencV and run Brew Scandisk –build-from-source OpencV if you didn’t have the JAR file installed previously.

brew install --build-from-source opencv
Copy the code
  1. After the installation is successful, the/ usr/local/Cellar/opencv / 4.5.3 _2 / share/Java/opencv4There are two required files:
libopencv_java453.dylib
opencv-453.jar
Copy the code

6. In Intellij, set the folder path to VM Options

7. Add the following code to load the OpencV module before using the OpencV API

System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
Copy the code

FAQ:

  1. [no opencv_java453 in java.library.path] is displayed after the configuration is complete. In this case, the JAR file is not found
  2. UnsatisfiedLinkError: ‘long org. Opencv. Imgcodecs. Imgcodecs. Imread_1 (Java. Lang. String)’, the situation is opencv module did not load so I can’t correct link to the corresponding method.