mac m1上java调用opencv

  • mac1
  • openjdk17
    官网上没有编译好的mac m1上二进制库.需要自己下载源代码编译.
  1. https://opencv.org/releases/从这里下载源代码.如opencv-4.7.0.zip解压
  2. 把 opencv-4.7.0文件夹重命名为opencv
  3. 在opencv同级目录下建一个文件夹build_opencv
  4. cd到build_opencv,然后执行
    cmake -DBUILD_SHARED_LIBS=OFF ../opencv
    
    
    注意确认OpenCV modules里面有java
  5. 执行
    make -j8
    
    会在build_opencv里编出来Opencv的jar包和链接库.
    新建一个文件SimpleSample.java内容为
    import org.opencv.core.Core;
    import org.opencv.core.Mat;
    import org.opencv.core.CvType;
    import org.opencv.core.Scalar;
    public class SimpleSample {
      static{ System.loadLibrary(Core.NATIVE_LIBRARY_NAME); }
      public static void main(String[] args) {
        System.out.println("Welcome to OpenCV " + Core.VERSION);
        Mat m = new Mat(5, 10, CvType.CV_8UC1, new Scalar(0));
        System.out.println("OpenCV Mat: " + m);
        Mat mr1 = m.row(1);
        mr1.setTo(new Scalar(1));
        Mat mc5 = m.col(5);
        mc5.setTo(new Scalar(5));
        System.out.println("OpenCV Mat data:\n" + m.dump());
      }
    }
    
    
  6. 执行
    javac -cp /Users/zhouyang/Documents/code/cpp/build_opencv/java_test/bin/opencv-470.jar SimpleSample.java
    java -Djava.library.path=/Users/zhouyang/Documents/code/cpp/build_opencv/lib -cp /Users/zhouyang/Documents/code/cpp/build_opencv/java_test/bin/opencv-470.jar:./ SimpleSample
    

参考资料

opencv java官方文档

posted on 2023-11-06 13:49  荷楠仁  阅读(314)  评论(0编辑  收藏  举报

导航