Trouble Linking OpenCV with Godot: Windows vs Android

Godot Version

Godot Version v4.2.3.rc.custom_build

Question

I’m currently in the process of crafting a C++ Custom Module for Godot to harness some of OpenCV’s functionalities directly from GDScript. While I’ve managed to successfully link the OpenCV library on Windows and utilize it for tasks like video capture and image reading from paths or base64 strings, I’ve hit a snag when attempting to replicate this success on Android.

On Windows, I’ve smoothly integrated OpenCV, enabling functions like video capture and image previewing within Godot. However, the transition to Android has been fraught with challenges. Despite achieving some milestones, such as printing the OpenCV library version using get_opencv_version and successfully reading base64 strings and previewing images in Godot using ImageTexture, I’ve encountered errors when trying to incorporate video capture functionalities.

Here are the specific errors I’m encountering during the compilation of my C++ Custom Module using SCons on Android:

ld: error: undefined symbol: AMediaCodec_dequeueInputBuffer
>>> referenced by cap_android_mediandk.cpp
>>>               cap_android_mediandk.cpp.o:(AndroidMediaNdkCapture::decodeFrame()) in archive D:\OpenCV-android-sdk\sdk\native\staticlibs\arm64-v8a\libopencv_videoio.a
>>> referenced by cap_android_mediandk.cpp
>>>               cap_android_mediandk.cpp.o:(AndroidMediaNdkVideoWriter::writeBytes(unsigned char*, unsigned long)) in archive D:\OpenCV-android-sdk\sdk\native\staticlibs\arm64-v8a\libopencv_videoio.a

ld: error: too many errors emitted, stopping now (use -error-limit=0 to see all errors)
clang++: error: linker command failed with exit code 1 (use -v to see invocation)

=====
scons: *** [bin\libgodot.android.template_debug.arm64.so] Error 1
scons: building terminated because of errors.
[Time elapsed: 00:00:49.527]

Here is the full bug list: opencv-godot/log.txt at main · azoozs/opencv-godot · GitHub

The errors primarily revolve around undefined symbols related to AMediaExtractor and AMediaCodec, such as AMediaExtractor_new, AMediaCodec_createDecoderByType, and others. These symbols seem crucial for integrating OpenCV’s video IO functionalities on the Android platform.

Any insights or suggestions on resolving these linker errors and successfully integrating OpenCV with Godot on Android would be greatly appreciated. Thank you in advance for your assistance!

Here are the complete files related to my custom C++ module:

GDScript side:

extends Control


var thread
var opencv_module = opencv.new()

func _ready():
	if OS.request_permissions():
		print("yes azooz4 grant permisions")
	else:
		print("no azooz4 grant don't permisions, I will try again")
		OS.request_permissions()

func _thread_function():
	opencv_module.set_video_bool(true)
	while(opencv_module.get_video_bool() == true):
		#print("true")
		opencv_module.video_capture(0)
	
func _process(delta):
	$TextureRect.texture = opencv_module.get_frame()
	if opencv_module.get_video_bool() == false:
		#print("into process and false")
		$TextureRect.texture = null

func _on_start_pressed():
	thread = Thread.new()
	thread.start(self._thread_function)

func _on_end_pressed():
	opencv_module.set_video_bool(false)

func _exit_tree():
	if thread:
		thread.wait_to_finish()