公司要預研AR方面的技術,就試試研究artoolkit,網上都是關於編譯artoolkit5的例子,artoolkit5已經很久沒更新了,而artoolkitx是最新版的,可是沒找到關於編譯artoolkitx的方法,就試着自己編譯試試
先說環境:Windows10,Android Studio3.5.3,artoolkitx1.0.6,ndkr20b
首先先下載artoolkitx
再自己創建個新的項目
在新建的項目裏新建個module
選Android Library
module名填arxj,包名填org.artoolkitx.arx.arxj
在app的build.gradle的dependencies{}內
implementation project(path: ':arxj')
進入下載下來的artoolkitx源碼內的Source文件夾內,把除ARXJ文件夾以外的所有文件夾和文件全都複製到剛纔創建的arxj的cpp文件夾下,沒有自己創建一個
修改arxj的build.gradle,在android{}內添加關聯CMakeLists.txt代碼
externalNativeBuild {
cmake {
path "src/main/cpp/CMakeLists.txt"
}
}
再在defaultConfig{}添加代碼
externalNativeBuild {
cmake {
arguments "-DANDROID_STL=c++_shared", "-DANDROID_CPP_FEATURES=rtti exceptions"
cppFlags"-std=c++11"
}
}
Run 'app'一下,等了半天報錯
Build command failed.
Error while executing process x:\xxx\xxx\cmake\3.6.4111459\bin\cmake.exe with arguments {--build x:\xxxx\xxxx\AndroidArtoolkitx\arxj\.cxx\cmake\debug\arm64-v8a --target ARX}
拉到下邊看到報錯代碼
點進去,原來是找不到arRefineCorners函數
再點進arRefineCorners.h發現是#if HAVE_OPENCV判斷隱藏了
通過#if HAVE_OPENCV來判斷當前是否用了opencv,如果沒有就隱藏了,可是arDetectMarker.c裏沒有進行opencv判斷,然後在arRefineCorners.cpp發現也有opencv判斷
那arRefineCorners.h裏判斷opencv的意義其實不大,兩種方法,一種直接刪除報錯代碼,一種就是在arRefineCorners.h內不進行opencv判斷,我選擇了後一種,把#if HAVE_OPENCV註釋掉
//#if HAVE_OPENCV
#ifdef __cplusplus
extern "C" {
#endif
// Given corner locations 'vertex' in observed coordinates, refine location.
// buff is a luma-only buffer of dimensions width x height.
void arRefineCorners(float vertex[4][2], const unsigned char *buff, int width, int height);
#ifdef __cplusplus
}
#endif
//#endif // HAVE_OPENCV
再Run 'app'一下,繼續報錯
x:\xxx\xxx\AndroidArtoolkitx\arxj\src\main\cpp\ARX\ARUtil/nftw.c:123: error: undefined reference to 'fts_close'
點開報錯文件,原來是給低SDK版本系統用的代碼報錯,可能是我的ndk版本太高了吧,不想一個一個版本的試,那就直接註釋掉,把最低SDK版本設置成21
再Run 'app'一下,運行成功,到這編譯就成功了,然後把demo裏的arxj的java代碼複製到自己的arxj內
再然後呢就是把res文件夾內的文件或文件夾複製到自己的arxj的res內
之後是運行demo了,demo在源碼的Examples文件夾內,我測試了下發現2d tracking example這個運行不了,因爲這個demo有用到opencv,我沒用opencv,所以就用Square tracking example進行測試
把Square tracking example的assets文件夾,java代碼,layout佈局都複製到自己的項目的相應位置,還要把Source/etc/android下的cparam_cache文件夾複製到assets文件夾
最後修改AndroidManifest.xml
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-feature android:name="android.hardware.camera.any" />
<uses-feature android:name="android.hardware.camera"
android:required="true" />
<uses-feature android:name="android.hardware.camera2"
android:required="true"/>
<uses-feature android:name="android.hardware.camera.autofocus"
android:required="false" />
<uses-feature android:glEsVersion="0x00020000" android:required="true" />
<supports-screens
android:anyDensity="true"
android:largeScreens="true"
android:normalScreens="true"
android:smallScreens="true"
android:xlargeScreens="true" />
<application
android:name="org.artoolkitx.arx.arsquaretracking.ARSquareTrackingApplication"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name="org.artoolkitx.arx.arsquaretracking.ARSquareTrackingActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="org.artoolkitx.arx.arxj.camera.CameraPreferencesActivity"/>
</application>
測試圖片都在Documentation/Square marker pattern images文件夾下的pdf
啓動很慢,默認橫屏,代碼還要研究,應該可以優化