From fe0d07ae53ba9e6dcd1e470165ed0a85cbfce006 Mon Sep 17 00:00:00 2001 From: Andrey Kamaev Date: Sat, 15 Sep 2012 15:46:34 +0400 Subject: [PATCH] Add experimental support for desktop Java Based on pull request #31 (thanks to Eric Christiansen) Support for Java is turned off by default and can be enabled setting BUILD_opencv_java to ON (e.g. cmake -DBUILD_opencv_java=ON ..) --- CMakeLists.txt | 6 ++-- modules/java/CMakeLists.txt | 41 ++++++++++++++++++++---- modules/java/generator/gen_java.py | 2 +- modules/java/generator/src/cpp/Mat.cpp | 7 ++++ modules/java/generator/src/cpp/utils.cpp | 4 +++ modules/java/jar/build.xml | 15 +++++++++ 6 files changed, 65 insertions(+), 10 deletions(-) create mode 100644 modules/java/jar/build.xml diff --git a/CMakeLists.txt b/CMakeLists.txt index 42dfad7b0..243b7a524 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -396,14 +396,16 @@ endif(BUILD_DOCS) include(cmake/OpenCVDetectPython.cmake) # --- Java Support --- +include(cmake/OpenCVDetectApacheAnt.cmake) if(ANDROID) - include(cmake/OpenCVDetectApacheAnt.cmake) include(cmake/OpenCVDetectAndroidSDK.cmake) if(NOT ANDROID_TOOLS_Pkg_Revision GREATER 13) message(WARNING "OpenCV requires Android SDK tools revision 14 or newer. Otherwise tests and samples will no be compiled.") endif() -endif(ANDROID) +elseif(ANT_EXECUTABLE) + find_package(JNI) +endif() if(ANDROID AND ANDROID_EXECUTABLE AND ANT_EXECUTABLE AND (ANT_VERSION VERSION_GREATER 1.7) AND (ANDROID_TOOLS_Pkg_Revision GREATER 13)) SET(CAN_BUILD_ANDROID_PROJECTS TRUE) diff --git a/modules/java/CMakeLists.txt b/modules/java/CMakeLists.txt index 50592ea22..29b6a0b58 100644 --- a/modules/java/CMakeLists.txt +++ b/modules/java/CMakeLists.txt @@ -1,14 +1,23 @@ # ---------------------------------------------------------------------------- # CMake file for java support # ---------------------------------------------------------------------------- -if(NOT ANDROID OR NOT PYTHON_EXECUTABLE OR ANDROID_NATIVE_API_LEVEL LESS 8) +if(IOS OR NOT PYTHON_EXECUTABLE OR NOT (JNI_FOUND OR (ANDROID AND ANDROID_NATIVE_API_LEVEL GREATER 7))) ocv_module_disable(java) endif() +if(NOT ANDROID) + # disable java by default because java support on desktop is experimental + set(BUILD_opencv_java_INIT OFF) +endif() + set(the_description "The java bindings") ocv_add_module(java BINDINGS opencv_core opencv_imgproc OPTIONAL opencv_objdetect opencv_features2d opencv_video opencv_highgui opencv_ml opencv_calib3d opencv_photo opencv_nonfree opencv_contrib) ocv_module_include_directories("${CMAKE_CURRENT_SOURCE_DIR}/generator/src/cpp") +if(NOT ANDROID) + include_directories(${JNI_INCLUDE_DIRS}) +endif() + # get list of modules to wrap string(REPLACE "opencv_" "" OPENCV_JAVA_MODULES "${OPENCV_MODULE_${the_module}_REQ_DEPS};${OPENCV_MODULE_${the_module}_OPT_DEPS}") foreach(module ${OPENCV_JAVA_MODULES}) @@ -123,7 +132,12 @@ set(JNI_INSTALL_ROOT "sdk/native") # copy each documented header to the final destination set(java_files "") -foreach(java_file ${documented_java_files} ${handwrittren_aidl_sources}) +set(source_java_files ${documented_java_files} ${handwrittren_aidl_sources}) +if(NOT ANDROID) + ocv_list_filterout(source_java_files "/(engine|android)\\\\+") +endif() + +foreach(java_file ${source_java_files}) get_filename_component(java_file_name "${java_file}" NAME) string(REPLACE "-jdoc.java" ".java" java_file_name "${java_file_name}") string(REPLACE "+" "/" java_file_name "${java_file_name}") @@ -175,6 +189,7 @@ set_target_properties(${the_module} PROPERTIES install(TARGETS ${the_module} LIBRARY DESTINATION ${OPENCV_LIB_INSTALL_PATH} COMPONENT main) +set(lib_target ${the_module}_library) if(ANDROID) target_link_libraries(${the_module} jnigraphics) # for Mat <=> Bitmap converters @@ -223,8 +238,6 @@ if(ANDROID) # create Android library project in build folder if(ANDROID_EXECUTABLE) - set(lib_target ${the_module}_android_library) - set(lib_target_files ${ANDROID_LIB_PROJECT_FILES}) ocv_list_add_prefix(lib_target_files "${OpenCV_BINARY_DIR}/") @@ -263,12 +276,26 @@ if(ANDROID) endif() add_custom_target(${lib_target} SOURCES ${lib_proj_files} ${lib_target_files} "${OpenCV_BINARY_DIR}/${ANDROID_MANIFEST_FILE}") - - add_dependencies(${lib_target} ${api_target}) - add_dependencies(${the_module} ${lib_target}) endif() +else(ANDROID) + configure_file("${CMAKE_CURRENT_SOURCE_DIR}/jar/build.xml" "${OpenCV_BINARY_DIR}/build.xml" IMMEDIATE @ONLY) + set(JAR_NAME opencv-${OPENCV_VERSION_MAJOR}.${OPENCV_VERSION_MINOR}.${OPENCV_VERSION_PATCH}.jar) + + add_custom_command( + OUTPUT "${OpenCV_BINARY_DIR}/bin/${JAR_NAME}" "${OpenCV_BINARY_DIR}/bin/.${JAR_NAME}.dephelper" + COMMAND ${ANT_EXECUTABLE} -q -noinput -k jar + COMMAND ${CMAKE_COMMAND} -E touch "${OpenCV_BINARY_DIR}/bin/.${JAR_NAME}.dephelper" + WORKING_DIRECTORY "${OpenCV_BINARY_DIR}" + DEPENDS "${OpenCV_BINARY_DIR}/build.xml" ${java_files} + COMMENT "Generating ${JAR_NAME}" + ) + + add_custom_target(${lib_target} SOURCES "${OpenCV_BINARY_DIR}/bin/${JAR_NAME}") endif(ANDROID) +add_dependencies(${lib_target} ${api_target}) +add_dependencies(${the_module} ${lib_target}) + #android test project if(ANDROID AND BUILD_TESTS) add_subdirectory(android_test) diff --git a/modules/java/generator/gen_java.py b/modules/java/generator/gen_java.py index 0471b494e..474f53a0c 100644 --- a/modules/java/generator/gen_java.py +++ b/modules/java/generator/gen_java.py @@ -871,7 +871,7 @@ public class %(jc)s { #include "converters.h" -#ifdef DEBUG +#if defined DEBUG && defined ANDROID #include #define MODULE_LOG_TAG "OpenCV.%(m)s" #define LOGD(...) ((void)__android_log_print(ANDROID_LOG_DEBUG, MODULE_LOG_TAG, __VA_ARGS__)) diff --git a/modules/java/generator/src/cpp/Mat.cpp b/modules/java/generator/src/cpp/Mat.cpp index 344c38d3c..ddafe4d23 100644 --- a/modules/java/generator/src/cpp/Mat.cpp +++ b/modules/java/generator/src/cpp/Mat.cpp @@ -2,6 +2,8 @@ #include "converters.h" +#ifdef ANDROID + #include #define LOG_TAG "org.opencv.core.Mat" #define LOGE(...) ((void)__android_log_print(ANDROID_LOG_ERROR, LOG_TAG, __VA_ARGS__)) @@ -11,6 +13,11 @@ #define LOGD(...) #endif //DEBUG +#else +#define LOGE(...) +#define LOGD(...) +#endif + #include "opencv2/core/core.hpp" using namespace cv; diff --git a/modules/java/generator/src/cpp/utils.cpp b/modules/java/generator/src/cpp/utils.cpp index 3193d6be2..9dbc093dc 100644 --- a/modules/java/generator/src/cpp/utils.cpp +++ b/modules/java/generator/src/cpp/utils.cpp @@ -3,6 +3,8 @@ #include "opencv2/core/core.hpp" #include "opencv2/imgproc/imgproc.hpp" +#ifdef ANDROID + #include #include @@ -165,3 +167,5 @@ JNIEXPORT void JNICALL Java_org_opencv_android_Utils_nMatToBitmap } } // extern "C" + +#endif //ANDROID \ No newline at end of file diff --git a/modules/java/jar/build.xml b/modules/java/jar/build.xml new file mode 100644 index 000000000..71c1b1fef --- /dev/null +++ b/modules/java/jar/build.xml @@ -0,0 +1,15 @@ + + + + + + + + + + + + + + + \ No newline at end of file