I am trying to integrate C++ code into Unity, which should use OpenCV 4.8.0 for Android (OpenCV-android-sdk). To create the .so
file, I am using the Android NDK (android-ndk-r26b).
My issue as I suspect, lies in linking the OpenCV library to my code and in the creation of the '.so' file.
Here's what I have tried so far:
- "So far, I have successfully managed to create a
.so
file from a.cpp
file using the Android ndk-build an got in to work in Unity. However, things get complicated when I try to integrate OpenCV into the process." - I have a setup and an Android.mk file which runs without errors, but I'm not sure if it's functioning correctly. When calling the functions in Unity, I encounter an error:
Error Unity at TestCppPlugin.plus (System.Single a, System.Single b) [0x00000] in <00000000000000000000000000000000>:0
andError Unity at TestCppPlugin.Start () [0x00000] in <00000000000000000000000000000000>:0
."
folder structure
├── android-ndk-r26b
│ ├── build
│ ├── ndk-build
│ ├── ...
│ ├── ...
└── cpp-library
├── jni
│ ├── Android.mk
│ ├── Application.mk
│ ├── CppToUnity_plugin.cpp
│ └── CppToUnity_plugin.h
│
├── libs
├── obj
└── OpenCV-android-sdk
CppToUnity_plgin.cpp
#include "CppToUnity_plugin.h"
#include <opencv2/core.hpp>
#include <opencv2/imgproc/imgproc.hpp>
extern "C" float plus(float x, float y) {
return x+y;
}
extern "C" const char* hello_from_cpp() {
return "Hello form C++";
}
extern "C" int test_opencv() {
// Erstellen Sie eine einfache Matrix (Bild)
cv::Mat img(100, 100, CV_8UC3, cv::Scalar(0, 255, 0));
// Führen Sie eine einfache Bildoperation durch (konvertieren Sie es zu Graustufen)
cv::Mat grayImg;
cv::cvtColor(img, grayImg, cv::COLOR_BGR2GRAY);
// Überprüfen Sie, ob die Konvertierung erfolgreich war
if (grayImg.empty()) {
return -1; // Fehler
}
return 0; //Erfolg
}
CppToUnity_plugin.h
extern "C" {
float plus(float x, float y);
const char* hello_from_cpp();
int test_opencv();
}
Android.mk
LOCAL_PATH := $(call my-dir)
CVROOT := /path/to/OpenCV-android-sdk/sdk/native
#CVROOT := $(LOCAL_PATH)/../OpenCV-android-sdk/sdk/native/
include $(CLEAR_VARS)
LOCAL_MODULE := add_prebuilt
LOCAL_SRC_FILES += $(CVROOT)/libs/arm64-v8a/libopencv_java4.so
LOCAL_LDLIBS := -llog
LOCAL_EXPORT_C_INCLUDES := CppToUnity_plugin.h
include $(PREBUILT_SHARED_LIBRARY)
include $(CLEAR_VARS)
LOCAL_C_INCLUDES :=$(LOCAL_PATH) $(CVROOT)/jni/include
LOCAL_MODULE := native
LOCAL_SHARED_LIBRARIES := add_prebuilt
LOCAL_SRC_FILES := CppToUnity_plugin.cpp
include $(BUILD_SHARED_LIBRARY)
TestCppPlugin.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Runtime.InteropServices;
using System;
using UnityEngine.UI;
public class TestCppPlugin : MonoBehaviour
{
public UnityEngine.UI.Text mText;
[DllImport("native")]
private static extern float plus(float a, float b);
[DllImport("native")]
private static extern System.IntPtr hello_from_cpp();
[DllImport("native")]
private static extern int test_opencv();
void Start() {
mText.text = "Testergebnisse:\n";
// Test plus
float summe = plus(2.0f, 3.0f);
Debug.Log("Das Ergebnis von plus_rechnen ist: " + summe);
mText.text += "Das Ergebnis von plus_rechnen(2.0f, 3.0f) ist: " + summe + "\n";
// Test hello_from_cpp
System.IntPtr ptr = hello_from_cpp();
string message = Marshal.PtrToStringAnsi(ptr);
Debug.Log("Nachricht von C++: " + message);
mText.text += "Nachricht von C++: " + message + "\n";
// Test test_opencv
int test = test_opencv();
Debug.Log("test_opencv return value: " + test);
mText.text += "Ergebnis von test_opencv(): " + test + "\n";
}
}
Does anyone have suggestions on how to resolve this issue or what I might be doing wrong?
I've tried many different approaches, but all ended in errors. At least with this current setup, I can see two lib_.so files in the libs directory