Mac上搭建OpenCV(Java版)环境

一、安装 MacPorts

1
2
3
4
5
6
7
8
9
10
11
# 安装 Xcode 和 Xcode Command Line Tools

# 同意 Xcode License
sudo xcodebuild -license

# 下载并安装 MacPorts for macOS Mojave v10.14
# 安装的过程需要断网, 安装完成后会自动配置好环境变量
https://www.macports.org/install.php

# 更新 Ports Tree 和 MacPorts 版本
sudo port -v selfupdate

二、安装 OpenCV+Java

1
2
3
4
5
6
7
8
9
10
11
12
# 安装 db48
sudo port selfupdate
sudo port install db48

# 安装 opencv +java
sudo port install opencv +java

# 安装确认
port contents opencv | grep java
# 出现以下结果表示安装完成
/opt/local/share/OpenCV/java/libopencv_java343.dylib
/opt/local/share/OpenCV/java/opencv-343.jar

三、IDEA 中使用

  1. 添加依赖
1
2
# File > Project Structure > Libraries > +
/opt/local/share/OpenCV/java/
  1. 配置 VM options
1
2
# Run > Edit Configurations > VM options
-Djava.library.path=/opt/local/share/OpenCV/java
  1. 测试
1
2
3
4
5
6
7
8
9
10
11
import org.opencv.core.Core;

public class OpenCV_Demo {
    static {
        System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
    }

    public static void main(String[] args) {
        System.out.println("OpenCV version: " + Core.VERSION);
    }
}

参考文章

  1. Mac+OpenCV+IntelliJ+JAVA环境搭建
  2. Mac+Java+openCV 配置详解
  3. Mac中MacPorts安装和使用
  4. The MacPorts Project