Skip to content

Commit fe0d07a

Browse files
author
Andrey Kamaev
committed
Add experimental support for desktop Java
Based on pull request opencv#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 ..)
1 parent ece71d6 commit fe0d07a

File tree

6 files changed

+65
-10
lines changed

6 files changed

+65
-10
lines changed

CMakeLists.txt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -396,14 +396,16 @@ endif(BUILD_DOCS)
396396
include(cmake/OpenCVDetectPython.cmake)
397397

398398
# --- Java Support ---
399+
include(cmake/OpenCVDetectApacheAnt.cmake)
399400
if(ANDROID)
400-
include(cmake/OpenCVDetectApacheAnt.cmake)
401401
include(cmake/OpenCVDetectAndroidSDK.cmake)
402402

403403
if(NOT ANDROID_TOOLS_Pkg_Revision GREATER 13)
404404
message(WARNING "OpenCV requires Android SDK tools revision 14 or newer. Otherwise tests and samples will no be compiled.")
405405
endif()
406-
endif(ANDROID)
406+
elseif(ANT_EXECUTABLE)
407+
find_package(JNI)
408+
endif()
407409

408410
if(ANDROID AND ANDROID_EXECUTABLE AND ANT_EXECUTABLE AND (ANT_VERSION VERSION_GREATER 1.7) AND (ANDROID_TOOLS_Pkg_Revision GREATER 13))
409411
SET(CAN_BUILD_ANDROID_PROJECTS TRUE)

modules/java/CMakeLists.txt

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,23 @@
11
# ----------------------------------------------------------------------------
22
# CMake file for java support
33
# ----------------------------------------------------------------------------
4-
if(NOT ANDROID OR NOT PYTHON_EXECUTABLE OR ANDROID_NATIVE_API_LEVEL LESS 8)
4+
if(IOS OR NOT PYTHON_EXECUTABLE OR NOT (JNI_FOUND OR (ANDROID AND ANDROID_NATIVE_API_LEVEL GREATER 7)))
55
ocv_module_disable(java)
66
endif()
77

8+
if(NOT ANDROID)
9+
# disable java by default because java support on desktop is experimental
10+
set(BUILD_opencv_java_INIT OFF)
11+
endif()
12+
813
set(the_description "The java bindings")
914
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)
1015
ocv_module_include_directories("${CMAKE_CURRENT_SOURCE_DIR}/generator/src/cpp")
1116

17+
if(NOT ANDROID)
18+
include_directories(${JNI_INCLUDE_DIRS})
19+
endif()
20+
1221
# get list of modules to wrap
1322
string(REPLACE "opencv_" "" OPENCV_JAVA_MODULES "${OPENCV_MODULE_${the_module}_REQ_DEPS};${OPENCV_MODULE_${the_module}_OPT_DEPS}")
1423
foreach(module ${OPENCV_JAVA_MODULES})
@@ -123,7 +132,12 @@ set(JNI_INSTALL_ROOT "sdk/native")
123132

124133
# copy each documented header to the final destination
125134
set(java_files "")
126-
foreach(java_file ${documented_java_files} ${handwrittren_aidl_sources})
135+
set(source_java_files ${documented_java_files} ${handwrittren_aidl_sources})
136+
if(NOT ANDROID)
137+
ocv_list_filterout(source_java_files "/(engine|android)\\\\+")
138+
endif()
139+
140+
foreach(java_file ${source_java_files})
127141
get_filename_component(java_file_name "${java_file}" NAME)
128142
string(REPLACE "-jdoc.java" ".java" java_file_name "${java_file_name}")
129143
string(REPLACE "+" "/" java_file_name "${java_file_name}")
@@ -175,6 +189,7 @@ set_target_properties(${the_module} PROPERTIES
175189

176190
install(TARGETS ${the_module} LIBRARY DESTINATION ${OPENCV_LIB_INSTALL_PATH} COMPONENT main)
177191

192+
set(lib_target ${the_module}_library)
178193
if(ANDROID)
179194
target_link_libraries(${the_module} jnigraphics) # for Mat <=> Bitmap converters
180195

@@ -223,8 +238,6 @@ if(ANDROID)
223238

224239
# create Android library project in build folder
225240
if(ANDROID_EXECUTABLE)
226-
set(lib_target ${the_module}_android_library)
227-
228241
set(lib_target_files ${ANDROID_LIB_PROJECT_FILES})
229242
ocv_list_add_prefix(lib_target_files "${OpenCV_BINARY_DIR}/")
230243

@@ -263,12 +276,26 @@ if(ANDROID)
263276
endif()
264277

265278
add_custom_target(${lib_target} SOURCES ${lib_proj_files} ${lib_target_files} "${OpenCV_BINARY_DIR}/${ANDROID_MANIFEST_FILE}")
266-
267-
add_dependencies(${lib_target} ${api_target})
268-
add_dependencies(${the_module} ${lib_target})
269279
endif()
280+
else(ANDROID)
281+
configure_file("${CMAKE_CURRENT_SOURCE_DIR}/jar/build.xml" "${OpenCV_BINARY_DIR}/build.xml" IMMEDIATE @ONLY)
282+
set(JAR_NAME opencv-${OPENCV_VERSION_MAJOR}.${OPENCV_VERSION_MINOR}.${OPENCV_VERSION_PATCH}.jar)
283+
284+
add_custom_command(
285+
OUTPUT "${OpenCV_BINARY_DIR}/bin/${JAR_NAME}" "${OpenCV_BINARY_DIR}/bin/.${JAR_NAME}.dephelper"
286+
COMMAND ${ANT_EXECUTABLE} -q -noinput -k jar
287+
COMMAND ${CMAKE_COMMAND} -E touch "${OpenCV_BINARY_DIR}/bin/.${JAR_NAME}.dephelper"
288+
WORKING_DIRECTORY "${OpenCV_BINARY_DIR}"
289+
DEPENDS "${OpenCV_BINARY_DIR}/build.xml" ${java_files}
290+
COMMENT "Generating ${JAR_NAME}"
291+
)
292+
293+
add_custom_target(${lib_target} SOURCES "${OpenCV_BINARY_DIR}/bin/${JAR_NAME}")
270294
endif(ANDROID)
271295

296+
add_dependencies(${lib_target} ${api_target})
297+
add_dependencies(${the_module} ${lib_target})
298+
272299
#android test project
273300
if(ANDROID AND BUILD_TESTS)
274301
add_subdirectory(android_test)

modules/java/generator/gen_java.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -871,7 +871,7 @@ def gen(self, srcfiles, module, output_path):
871871
872872
#include "converters.h"
873873
874-
#ifdef DEBUG
874+
#if defined DEBUG && defined ANDROID
875875
#include <android/log.h>
876876
#define MODULE_LOG_TAG "OpenCV.%(m)s"
877877
#define LOGD(...) ((void)__android_log_print(ANDROID_LOG_DEBUG, MODULE_LOG_TAG, __VA_ARGS__))

modules/java/generator/src/cpp/Mat.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
#include "converters.h"
44

5+
#ifdef ANDROID
6+
57
#include <android/log.h>
68
#define LOG_TAG "org.opencv.core.Mat"
79
#define LOGE(...) ((void)__android_log_print(ANDROID_LOG_ERROR, LOG_TAG, __VA_ARGS__))
@@ -11,6 +13,11 @@
1113
#define LOGD(...)
1214
#endif //DEBUG
1315

16+
#else
17+
#define LOGE(...)
18+
#define LOGD(...)
19+
#endif
20+
1421
#include "opencv2/core/core.hpp"
1522

1623
using namespace cv;

modules/java/generator/src/cpp/utils.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
#include "opencv2/core/core.hpp"
44
#include "opencv2/imgproc/imgproc.hpp"
55

6+
#ifdef ANDROID
7+
68
#include <android/bitmap.h>
79

810
#include <android/log.h>
@@ -165,3 +167,5 @@ JNIEXPORT void JNICALL Java_org_opencv_android_Utils_nMatToBitmap
165167
}
166168

167169
} // extern "C"
170+
171+
#endif //ANDROID

modules/java/jar/build.xml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<!-- This is an Apache Ant build file. As part of the desktop java build -->
2+
<!-- process, this config is used to package the autogenerated .java -->
3+
<!-- interface files into OpenCV.jar -->
4+
<project name="OpenCV">
5+
<target name="jar">
6+
<!-- This is to make a jar with a source attachment, for e.g. easy -->
7+
<!-- navigation in Eclipse. See this question: -->
8+
<!-- http://stackoverflow.com/questions/3584968/ant-how-to-compile-jar-that-includes-source-attachment -->
9+
<javac sourcepath="" srcdir="src" destdir="src" debug="on" includeantruntime="false" >
10+
<include name="**/*.java"/>
11+
</javac>
12+
13+
<jar basedir="src" destfile="bin/opencv-@OPENCV_VERSION_MAJOR@.@OPENCV_VERSION_MINOR@.@OPENCV_VERSION_PATCH@.jar"/>
14+
</target>
15+
</project>

0 commit comments

Comments
 (0)