添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

I'm trying to use OpenCv 4 on Xamarin.Android by Java Binding Template. These are the steps that i've done: 0) I've compiled opencv binaries through cmake and mingw64 to get .jar and .a

  • I've put the .jar and the static libraries (.a) in Jars forlder of Xamarin Java Binding Template and i've compiled the template. 1.a) .jar Build Action is EmbeddedJar 1.b) libs Build Action is EmbeddedNativeLibrary
  • I've added a reference to that template in my Xamarin.Android project: the opencv methods were recognized correctly!
  • But, when i try to execute:

    Mat Source = Imgcodecs.Imread(ImagePath, Imgcodecs.ImreadGrayscale);
    

    i get an error:

    Java.Lang.UnsatisfiedLinkError: 'No implementation found for long org.opencv.imgcodecs.Imgcodecs.imread_0(java.lang.String, int) (tried Java_org_opencv_imgcodecs_Imgcodecs_imread_10 and Java_org_opencv_imgcodecs_Imgcodecs_imread_10__Ljava_lang_String_2I)'
    

    I think that there could be a missmatch of method name, maybe due to a wrong java parsing.

    I've also tried to use shared libries (.so) by loading them through JavaSystem.LoadLibrary("LibraryNameWithoutInitialLib"), but i have the same error :/

    Do you know why?

    You cannot link static libraries with Xamarin.Android as the Xamarin/Mono NDK-based runtime is a static main entry executable and does not dynamically get built per project. If you do not need to use a .jar/.aar high-level wrapper, then you will need to use runtime shared libraries and define DllImportAttribute entries for the exported functions that you need to call.

    Xamarin.Android supports the use of native libraries via the standard PInvoke mechanism.

    Using Native Libraries

    Use C/C++ libraries with Xamarin

    Note: There are numerous OpenCV C# wrappers / DllImport files in open source ( i.e. a github search away 😁)

    Note: If you are using a 3rd-party .jar/.aar , make sure that they are using OpenCV shared libraries and thus not requiring a gradle script to link them into an NDK-based Android app.

    Hi! thanks for you answer! I'm new in using native share library in xamarin...so...after about 2 hours of attempts, i really admit to don't have understood so much :')....i've tried to follow "Use C/C++ libraries with Xamarin" without success...i've understand to how import methods from .so with DllImport (i think, i didn't test it yet), but what about if i don't know the names of the methods in native library? In fact i don't know where are c++ source files in my opencv dir :/ (such as the cpp and hpp of imgcodecs).And what about the import of type such as "Mat"? I can't do it with dllimport. – feduss Oct 15, 2020 at 19:56 @feduss There are OSS projects like NAXAM/opencv-android-binding ( github.com/NAXAM/opencv-android-binding , which is based on an another project (out of date, even NAXAM's repo/nuget is out-of-date and needs a newer OpenCV .aar) that use the OpenCV for Android official .aar and thus expose the OpenCV Java wrapper to Xamarin.Android/C#, while other projects provide a higher level C# wrapper via DllImport usage and skip the Java "middleman". – SushiHangover Oct 15, 2020 at 20:36 @feduss Personally I have used both of these styles for some Xamarin/OpenCV projects by borrowing the source (where allowed by the project's OSS license) and bending it to our project needs. It really depends upon your needs.... FYI: Mat in C++ is a class and yes, you are correct, you can not "import" it. Project like opencvsharp have rewritten the whole Mat class in C# and map/marshal the C#/C++ structures bi-directionally, ie. github.com/shimat/opencvsharp/blob/… – SushiHangover Oct 15, 2020 at 20:40 Skipping the Java wrapper and using project like opencvsharp is "my preferred" way as Xamarin.Android's C# to Java wrapper adds a translation layer and then the Java to OpenCV adds another layer (memory, performance, etc... hits that you can not control). But there was a project that had to use existing OpenCV Java code, mix of native Android and Xamarin/C# devs, etc.. so we used the OpenCV for Android wrapper (like NAXAM's project) and called it a day... Note: There are other projects out there that do this and I am not recommending a single OSS project as everyone's needs are different. – SushiHangover Oct 15, 2020 at 20:48 thanks again for you answers :)! And yes, i agree with avoiding java layer. I'm trying to use Mat.cs implementation and the DllImport c++ methods, but i'm not able to import them (System.EntryPointNotFoundException) because i'm writing the wrong native name i think (for ex: the "imread" function in c++ is called "imread", but it isn't found in "libopencv_imgcodecs.so"). Btw, i need opencv "only" to scan document in my app. – feduss Oct 15, 2020 at 21:58

    Thanks for contributing an answer to Stack Overflow!

    • Please be sure to answer the question. Provide details and share your research!

    But avoid

    • Asking for help, clarification, or responding to other answers.
    • Making statements based on opinion; back them up with references or personal experience.

    To learn more, see our tips on writing great answers.