下面咱們寫一個shell命名爲build.sh放在opencv的根目錄下面,代碼以下:java
mkdir $1 cd $1 cmake -DWITH_QT=ON -DWITH_OPENGL=ON -DFORCE_VTK=ON -DWITH_TBB=ON -DWITH_GDAL=ON -DWITH_XINE=ON -DBUILD_EXAMPLES=ON -DBUILD_opencv_java=ON BUILD_opencv_test_java=OFF .. make -j8
其餘編譯參數請參考博客c++
在Windows下的編譯能夠直接使用CMake GUI進行設置配置
如圖 shell
選擇本身須要的類型的配置參數(如編譯器類型,編譯參數)apache
接下來咱們運行一下
sh ./build.sh build
windows
便可在bin目錄下找到生成的opencv buildoracle
生成opencv的Java包一直是一個會困擾Java黨萌新的問題
按照官方給出的安裝教程並不會生成OpenCV的jar包
首先咱們要安裝ant
輸入指令sudo apt-get install ant
在windows下ant 在安裝ant須要去下載 apache 的ant包並設置好系統環境變量ANT_HOME
爲相似 D:\apache-ant-1.10.1
安裝目錄ide
在上面c++安裝編譯的時候參數-DBUILD_opencv_java=ON
就已經幫咱們生成了jar的包在bin目錄下
注:opencv生成的包分爲靜態包和動態包,推薦生成靜態包,靜態調用會省去許多沒必要要或者是不知道的動態連接庫的加載。 接下來咱們把包導入到idea中。 下面給出一個基於Config的opencv啓動器
project/src/org/uestc/configui
package org.uestc.config; import java.io.*; import java.util.*; public class mainConfig{ BufferedInputStream user_in=null; BufferedInputStream sys_in=null; FileOutputStream user_file=null; FileOutputStream sys_file=null; private String sysPath="./config/sys.properties"; private String userPath="./config/config.properties"; public mainConfig(){ try { try { user_in = new BufferedInputStream(new FileInputStream(this.userPath)); sys_in = new BufferedInputStream(new FileInputStream(sysPath)); } catch (IOException e) { try { user_file = new FileOutputStream(userPath, true); sys_file = new FileOutputStream(sysPath, true); user_in = new BufferedInputStream(new FileInputStream(userPath)); sys_in = new BufferedInputStream(new FileInputStream(sysPath)); user_file.close(); sys_file.close(); } catch (FileNotFoundException e1) { e1.printStackTrace(); } System.out.println("No Config file , System will auto Created"); } }catch (Exception e){ System.out.println("I/O Error"); } } private void setDefaultConfig(Map<String,String> info) throws IOException { Properties pps = new Properties(); Iterator<String> it=info.keySet().iterator(); while (it.hasNext()){ String key=it.next(); pps.setProperty(key,info.get(key)); } pps.store(user_file, "The New properties user_file"); } public Map<String,String> getUserConfig(){ Properties pps = new Properties(); Map<String,String> info=new HashMap<String,String>(); try { pps.load(user_in); Iterator<String> it=pps.stringPropertyNames().iterator(); while(it.hasNext()){ String key=it.next(); info.put(key,pps.getProperty(key)); } return info; } catch (IOException e1) { e1.printStackTrace(); } return info; } }
配置文件
project/config/config.propertiesthis
max_camera=100
入口文件idea
/** * Created by Summer-V on 17-4-12. */ import org.opencv.videoio.VideoCapture; import org.opencv.core.*; import java.util.HashMap; import java.util.Iterator; import java.util.Map; public class Main { public void video_start(Map<Integer,String> camera){ Iterator<Integer> cap=camera.keySet().iterator(); while (cap.hasNext()){ int index=cap.next(); new Thread(new Runnable() { @Override public void run() { videoViewer video=new videoViewer(); video.openWindow(index,camera.get(index),100); } }).start(); } public static void main(String[] args){ System.loadLibrary(Core.NATIVE_LIBRARY_NAME);//加載opencv庫 mainConfig Config=new mainConfig(); Map<String,String> info=Config.getUserConfig(); Iterator<String> it=info.keySet().iterator(); while (it.hasNext()){ String key =it.next(); } VideoCapture cae=new VideoCapture(); for(int i=0;i<Integer.parseInt(info.get("max_camera"));i+=1){ Map<Integer,String> cap=new HashMap<Integer,String>(); if (cae.open(i)){ cap.put(i,"Camera"+i); video_start(cap); }else { break; } } } }
注:若是不存在引用關係的話,每一次都要加載opencv庫