From 15b6fd2ec467afb1e4ff45c402b3873c5771720a Mon Sep 17 00:00:00 2001 From: Alexander Alekhin Date: Thu, 5 Jun 2014 12:29:36 +0400 Subject: [PATCH] android: allow to build & run examples without Android OpenCV Manager --- CMakeLists.txt | 1 + cmake/OpenCVDetectAndroidSDK.cmake | 10 ++++++++++ cmake/copyAndroidLibs.cmake | 8 ++++++++ .../org/opencv/samples/puzzle15/Puzzle15Activity.java | 8 +++++++- .../cameracalibration/CameraCalibrationActivity.java | 8 +++++++- .../colorblobdetect/ColorBlobDetectionActivity.java | 8 +++++++- .../src/org/opencv/samples/facedetect/FdActivity.java | 8 +++++++- .../imagemanipulations/ImageManipulationsActivity.java | 8 +++++++- .../samples/NativeActivity/CvNativeActivity.java | 8 +++++++- .../opencv/samples/tutorial1/Tutorial1Activity.java | 8 +++++++- .../opencv/samples/tutorial2/Tutorial2Activity.java | 8 +++++++- .../opencv/samples/tutorial3/Tutorial3Activity.java | 8 +++++++- 12 files changed, 82 insertions(+), 9 deletions(-) create mode 100644 cmake/copyAndroidLibs.cmake diff --git a/CMakeLists.txt b/CMakeLists.txt index 081e5a15fd..96f104a987 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -216,6 +216,7 @@ OCV_OPTION(ENABLE_NOISY_WARNINGS "Show all warnings even if they are too no OCV_OPTION(OPENCV_WARNINGS_ARE_ERRORS "Treat warnings as errors" OFF ) OCV_OPTION(ENABLE_WINRT_MODE "Build with Windows Runtime support" OFF IF WIN32 ) OCV_OPTION(ENABLE_WINRT_MODE_NATIVE "Build with Windows Runtime native C++ support" OFF IF WIN32 ) +OCV_OPTION(ANDROID_EXAMPLES_WITH_LIBS "Build binaries of Android examples with native libraries" OFF IF ANDROID ) # ---------------------------------------------------------------------------- diff --git a/cmake/OpenCVDetectAndroidSDK.cmake b/cmake/OpenCVDetectAndroidSDK.cmake index 7fc45108cb..90e11761e7 100644 --- a/cmake/OpenCVDetectAndroidSDK.cmake +++ b/cmake/OpenCVDetectAndroidSDK.cmake @@ -335,6 +335,16 @@ macro(add_android_project target path) add_dependencies(${target} ${android_proj_native_deps}) endif() + if(ANDROID_EXAMPLES_WITH_LIBS) + add_custom_target( + ${target}_copy_libs + COMMAND ${CMAKE_COMMAND} -DSRC_DIR=${OpenCV_BINARY_DIR}/lib -DDST_DIR=${android_proj_bin_dir}/libs -P ${OpenCV_SOURCE_DIR}/cmake/copyAndroidLibs.cmake + WORKING_DIRECTORY ${OpenCV_BINARY_DIR}/lib + DEPENDS "${OpenCV_BINARY_DIR}/bin/classes.jar.dephelper" opencv_java + ) + add_dependencies(${target} ${target}_copy_libs) + endif() + if(__android_project_chain) add_dependencies(${target} ${__android_project_chain}) endif() diff --git a/cmake/copyAndroidLibs.cmake b/cmake/copyAndroidLibs.cmake new file mode 100644 index 0000000000..4e9e17f4c1 --- /dev/null +++ b/cmake/copyAndroidLibs.cmake @@ -0,0 +1,8 @@ +# helper file for Android samples build + +file(GLOB_RECURSE LIBS RELATIVE ${SRC_DIR} "*.so") + +foreach(l ${LIBS}) + message(STATUS " Copying: ${l} ...") + execute_process(COMMAND ${CMAKE_COMMAND} -E copy_if_different ${SRC_DIR}/${l} ${DST_DIR}/${l}) +endforeach() diff --git a/samples/android/15-puzzle/src/org/opencv/samples/puzzle15/Puzzle15Activity.java b/samples/android/15-puzzle/src/org/opencv/samples/puzzle15/Puzzle15Activity.java index ebd34fc7e2..a87aafed41 100644 --- a/samples/android/15-puzzle/src/org/opencv/samples/puzzle15/Puzzle15Activity.java +++ b/samples/android/15-puzzle/src/org/opencv/samples/puzzle15/Puzzle15Activity.java @@ -76,7 +76,13 @@ public class Puzzle15Activity extends Activity implements CvCameraViewListener, public void onResume() { super.onResume(); - OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_2_4_3, this, mLoaderCallback); + if (!OpenCVLoader.initDebug()) { + Log.d(TAG, "Internal OpenCV library not found. Using OpenCV Manager for initialization"); + OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_2_4_3, this, mLoaderCallback); + } else { + Log.d(TAG, "OpenCV library found inside package. Using it!"); + mLoaderCallback.onManagerConnected(LoaderCallbackInterface.SUCCESS); + } } public void onDestroy() { diff --git a/samples/android/camera-calibration/src/org/opencv/samples/cameracalibration/CameraCalibrationActivity.java b/samples/android/camera-calibration/src/org/opencv/samples/cameracalibration/CameraCalibrationActivity.java index 33c9bbbf46..058497303f 100644 --- a/samples/android/camera-calibration/src/org/opencv/samples/cameracalibration/CameraCalibrationActivity.java +++ b/samples/android/camera-calibration/src/org/opencv/samples/cameracalibration/CameraCalibrationActivity.java @@ -92,7 +92,13 @@ public class CameraCalibrationActivity extends Activity implements CvCameraViewL public void onResume() { super.onResume(); - OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_2_4_2, this, mLoaderCallback); + if (!OpenCVLoader.initDebug()) { + Log.d(TAG, "Internal OpenCV library not found. Using OpenCV Manager for initialization"); + OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_2_4_2, this, mLoaderCallback); + } else { + Log.d(TAG, "OpenCV library found inside package. Using it!"); + mLoaderCallback.onManagerConnected(LoaderCallbackInterface.SUCCESS); + } } public void onDestroy() { diff --git a/samples/android/color-blob-detection/src/org/opencv/samples/colorblobdetect/ColorBlobDetectionActivity.java b/samples/android/color-blob-detection/src/org/opencv/samples/colorblobdetect/ColorBlobDetectionActivity.java index 276b03aeba..9fec67877d 100644 --- a/samples/android/color-blob-detection/src/org/opencv/samples/colorblobdetect/ColorBlobDetectionActivity.java +++ b/samples/android/color-blob-detection/src/org/opencv/samples/colorblobdetect/ColorBlobDetectionActivity.java @@ -88,7 +88,13 @@ public class ColorBlobDetectionActivity extends Activity implements OnTouchListe public void onResume() { super.onResume(); - OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_2_4_3, this, mLoaderCallback); + if (!OpenCVLoader.initDebug()) { + Log.d(TAG, "Internal OpenCV library not found. Using OpenCV Manager for initialization"); + OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_2_4_3, this, mLoaderCallback); + } else { + Log.d(TAG, "OpenCV library found inside package. Using it!"); + mLoaderCallback.onManagerConnected(LoaderCallbackInterface.SUCCESS); + } } public void onDestroy() { diff --git a/samples/android/face-detection/src/org/opencv/samples/facedetect/FdActivity.java b/samples/android/face-detection/src/org/opencv/samples/facedetect/FdActivity.java index b06b2cc1c5..d752f2a503 100644 --- a/samples/android/face-detection/src/org/opencv/samples/facedetect/FdActivity.java +++ b/samples/android/face-detection/src/org/opencv/samples/facedetect/FdActivity.java @@ -139,7 +139,13 @@ public class FdActivity extends Activity implements CvCameraViewListener2 { public void onResume() { super.onResume(); - OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_2_4_3, this, mLoaderCallback); + if (!OpenCVLoader.initDebug()) { + Log.d(TAG, "Internal OpenCV library not found. Using OpenCV Manager for initialization"); + OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_2_4_3, this, mLoaderCallback); + } else { + Log.d(TAG, "OpenCV library found inside package. Using it!"); + mLoaderCallback.onManagerConnected(LoaderCallbackInterface.SUCCESS); + } } public void onDestroy() { diff --git a/samples/android/image-manipulations/src/org/opencv/samples/imagemanipulations/ImageManipulationsActivity.java b/samples/android/image-manipulations/src/org/opencv/samples/imagemanipulations/ImageManipulationsActivity.java index 38f1d5959b..f17a2731fd 100644 --- a/samples/android/image-manipulations/src/org/opencv/samples/imagemanipulations/ImageManipulationsActivity.java +++ b/samples/android/image-manipulations/src/org/opencv/samples/imagemanipulations/ImageManipulationsActivity.java @@ -111,7 +111,13 @@ public class ImageManipulationsActivity extends Activity implements CvCameraView public void onResume() { super.onResume(); - OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_2_4_3, this, mLoaderCallback); + if (!OpenCVLoader.initDebug()) { + Log.d(TAG, "Internal OpenCV library not found. Using OpenCV Manager for initialization"); + OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_2_4_3, this, mLoaderCallback); + } else { + Log.d(TAG, "OpenCV library found inside package. Using it!"); + mLoaderCallback.onManagerConnected(LoaderCallbackInterface.SUCCESS); + } } public void onDestroy() { diff --git a/samples/android/native-activity/src/org/opencv/samples/NativeActivity/CvNativeActivity.java b/samples/android/native-activity/src/org/opencv/samples/NativeActivity/CvNativeActivity.java index b9db22de1f..7d4de93b2c 100644 --- a/samples/android/native-activity/src/org/opencv/samples/NativeActivity/CvNativeActivity.java +++ b/samples/android/native-activity/src/org/opencv/samples/NativeActivity/CvNativeActivity.java @@ -39,6 +39,12 @@ public class CvNativeActivity extends Activity { public void onResume() { super.onResume(); - OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_2_4_3, this, mLoaderCallback); + if (!OpenCVLoader.initDebug()) { + Log.d(TAG, "Internal OpenCV library not found. Using OpenCV Manager for initialization"); + OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_2_4_3, this, mLoaderCallback); + } else { + Log.d(TAG, "OpenCV library found inside package. Using it!"); + mLoaderCallback.onManagerConnected(LoaderCallbackInterface.SUCCESS); + } } } diff --git a/samples/android/tutorial-1-camerapreview/src/org/opencv/samples/tutorial1/Tutorial1Activity.java b/samples/android/tutorial-1-camerapreview/src/org/opencv/samples/tutorial1/Tutorial1Activity.java index cbac1649bc..746eb40777 100644 --- a/samples/android/tutorial-1-camerapreview/src/org/opencv/samples/tutorial1/Tutorial1Activity.java +++ b/samples/android/tutorial-1-camerapreview/src/org/opencv/samples/tutorial1/Tutorial1Activity.java @@ -76,7 +76,13 @@ public class Tutorial1Activity extends Activity implements CvCameraViewListener2 public void onResume() { super.onResume(); - OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_2_4_3, this, mLoaderCallback); + if (!OpenCVLoader.initDebug()) { + Log.d(TAG, "Internal OpenCV library not found. Using OpenCV Manager for initialization"); + OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_2_4_3, this, mLoaderCallback); + } else { + Log.d(TAG, "OpenCV library found inside package. Using it!"); + mLoaderCallback.onManagerConnected(LoaderCallbackInterface.SUCCESS); + } } public void onDestroy() { diff --git a/samples/android/tutorial-2-mixedprocessing/src/org/opencv/samples/tutorial2/Tutorial2Activity.java b/samples/android/tutorial-2-mixedprocessing/src/org/opencv/samples/tutorial2/Tutorial2Activity.java index eb84a1ec61..1dbcff2cab 100644 --- a/samples/android/tutorial-2-mixedprocessing/src/org/opencv/samples/tutorial2/Tutorial2Activity.java +++ b/samples/android/tutorial-2-mixedprocessing/src/org/opencv/samples/tutorial2/Tutorial2Activity.java @@ -97,7 +97,13 @@ public class Tutorial2Activity extends Activity implements CvCameraViewListener2 public void onResume() { super.onResume(); - OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_2_4_3, this, mLoaderCallback); + if (!OpenCVLoader.initDebug()) { + Log.d(TAG, "Internal OpenCV library not found. Using OpenCV Manager for initialization"); + OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_2_4_3, this, mLoaderCallback); + } else { + Log.d(TAG, "OpenCV library found inside package. Using it!"); + mLoaderCallback.onManagerConnected(LoaderCallbackInterface.SUCCESS); + } } public void onDestroy() { diff --git a/samples/android/tutorial-3-cameracontrol/src/org/opencv/samples/tutorial3/Tutorial3Activity.java b/samples/android/tutorial-3-cameracontrol/src/org/opencv/samples/tutorial3/Tutorial3Activity.java index 28c00f3b1c..8e3e162592 100644 --- a/samples/android/tutorial-3-cameracontrol/src/org/opencv/samples/tutorial3/Tutorial3Activity.java +++ b/samples/android/tutorial-3-cameracontrol/src/org/opencv/samples/tutorial3/Tutorial3Activity.java @@ -88,7 +88,13 @@ public class Tutorial3Activity extends Activity implements CvCameraViewListener2 public void onResume() { super.onResume(); - OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_2_4_3, this, mLoaderCallback); + if (!OpenCVLoader.initDebug()) { + Log.d(TAG, "Internal OpenCV library not found. Using OpenCV Manager for initialization"); + OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_2_4_3, this, mLoaderCallback); + } else { + Log.d(TAG, "OpenCV library found inside package. Using it!"); + mLoaderCallback.onManagerConnected(LoaderCallbackInterface.SUCCESS); + } } public void onDestroy() {