Skip to content

Commit

Permalink
Merge pull request opencv#19597 from komakai:android-ndk-camera
Browse files Browse the repository at this point in the history
Android NDK camera support

* Add native camera video backend for Android

* In the event of a "No buffer available error" wait for the appropriate callback and retry

* Fix stale context when creating a new AndroidCameraCapture

* Add property handling
  • Loading branch information
komakai authored Feb 25, 2021
1 parent 543379d commit 0553543
Show file tree
Hide file tree
Showing 10 changed files with 749 additions and 3 deletions.
3 changes: 3 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -437,6 +437,9 @@ OCV_OPTION(WITH_QUIRC "Include library QR-code decoding" ON
OCV_OPTION(WITH_ANDROID_MEDIANDK "Use Android Media NDK for Video I/O (Android)" (ANDROID_NATIVE_API_LEVEL GREATER 20)
VISIBLE_IF ANDROID
VERIFY HAVE_ANDROID_MEDIANDK)
OCV_OPTION(WITH_ANDROID_NATIVE_CAMERA "Use Android NDK for Camera I/O (Android)" (ANDROID_NATIVE_API_LEVEL GREATER 23)
VISIBLE_IF ANDROID
VERIFY HAVE_ANDROID_NATIVE_CAMERA)
OCV_OPTION(WITH_TENGINE "Include Arm Inference Tengine support" OFF
VISIBLE_IF (ARM OR AARCH64) AND (UNIX OR ANDROID) AND NOT IOS
VERIFY HAVE_TENGINE)
Expand Down
6 changes: 5 additions & 1 deletion cmake/android/android_gradle_projects.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@ set(GRADLE_VERSION "5.6.4" CACHE STRING "Gradle version")
message(STATUS "Gradle version: ${GRADLE_VERSION}")

set(ANDROID_COMPILE_SDK_VERSION "26" CACHE STRING "Android compileSdkVersion")
set(ANDROID_MIN_SDK_VERSION "21" CACHE STRING "Android minSdkVersion")
if(ANDROID_NATIVE_API_LEVEL GREATER 21)
set(ANDROID_MIN_SDK_VERSION "${ANDROID_NATIVE_API_LEVEL}" CACHE STRING "Android minSdkVersion")
else()
set(ANDROID_MIN_SDK_VERSION "21" CACHE STRING "Android minSdkVersion")
endif()
set(ANDROID_TARGET_SDK_VERSION "26" CACHE STRING "Android minSdkVersion")

set(ANDROID_BUILD_BASE_DIR "${OpenCV_BINARY_DIR}/opencv_android" CACHE INTERNAL "")
Expand Down
6 changes: 5 additions & 1 deletion modules/java/android_sdk/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,11 @@ FILE(MAKE_DIRECTORY \"\$ENV{DESTDIR}\${CMAKE_INSTALL_PREFIX}/${JAVA_INSTALL_ROOT

ocv_update(ANDROID_COMPILE_SDK_VERSION "27")
ocv_update(ANDROID_MIN_SDK_VERSION "14")
ocv_update(ANDROID_TARGET_SDK_VERSION "21")
if(ANDROID_NATIVE_API_LEVEL GREATER 21)
ocv_update(ANDROID_TARGET_SDK_VERSION "${ANDROID_NATIVE_API_LEVEL}")
else()
ocv_update(ANDROID_TARGET_SDK_VERSION "21")
endif()
configure_file("${CMAKE_CURRENT_SOURCE_DIR}/build.gradle.in" "${CMAKE_CURRENT_BINARY_DIR}/build.gradle" @ONLY)
install(FILES "${CMAKE_CURRENT_BINARY_DIR}/build.gradle" DESTINATION ${JAVA_INSTALL_ROOT}/.. COMPONENT java)

Expand Down
6 changes: 6 additions & 0 deletions modules/videoio/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,12 @@ if(TARGET ocv.3rdparty.android_mediandk)
list(APPEND tgts ocv.3rdparty.android_mediandk)
endif()

if(TARGET ocv.3rdparty.android_native_camera)
list(APPEND videoio_srcs
${CMAKE_CURRENT_LIST_DIR}/src/cap_android_camera.cpp)
list(APPEND tgts ocv.3rdparty.android_native_camera)
endif()

ocv_set_module_sources(HEADERS ${videoio_ext_hdrs} ${videoio_hdrs} SOURCES ${videoio_srcs})
ocv_module_include_directories()
ocv_create_module()
Expand Down
8 changes: 8 additions & 0 deletions modules/videoio/cmake/detect_android_camera.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# if(ANDROID AND ANDROID_NATIVE_API_LEVEL GREATER_EQUAL 24) <-- would be nicer but requires CMake 3.7 or later
if(ANDROID AND ANDROID_NATIVE_API_LEVEL GREATER 23)
set(HAVE_ANDROID_NATIVE_CAMERA TRUE)
set(libs "-landroid -llog -lcamera2ndk")
ocv_add_external_target(android_native_camera "" "${libs}" "HAVE_ANDROID_NATIVE_CAMERA")
endif()

set(HAVE_ANDROID_NATIVE_CAMERA ${HAVE_ANDROID_NATIVE_CAMERA} PARENT_SCOPE)
1 change: 1 addition & 0 deletions modules/videoio/cmake/init.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,4 @@ add_backend("dshow" WITH_DSHOW)
add_backend("msmf" WITH_MSMF)

add_backend("android_mediandk" WITH_ANDROID_MEDIANDK)
add_backend("android_camera" WITH_ANDROID_NATIVE_CAMERA)
Loading

0 comments on commit 0553543

Please sign in to comment.