To add OpenCV to your Maven project, you can use the following dependency in your pom.xml
:
xml<dependency>
<groupId>org.openpnpgroupId>
<artifactId>opencvartifactId>
<version>3.4.10-1version>
dependency>
This will fetch the OpenCV library from the Maven repository. However, you’ll still need to ensure that the native OpenCV library (opencv_javaXXX.dll
for Windows, libopencv_javaXXX.so
for Linux, etc.) is available in your system’s library path and can be loaded by your Java application.
To resolve the java.lang.UnsatisfiedLinkError
, you need to specify the location of the native library to the JVM. You can do this by adding the following code before using any OpenCV functionality:
javaSystem.loadLibrary(Core.NATIVE_LIBRARY_NAME);
Make sure that the native library is available in the specified path. You can also set the library path programmatically using System.setProperty("java.library.path", "path_to_opencv_native_library")
, but it’s generally not recommended as it can cause issues with other libraries that rely on the system’s library path.
Additionally, you can use tools like Maven Shade Plugin or Maven Assembly Plugin to create a fat JAR that includes the native libraries, but this approach may increase the size of your final JAR file significantly.
المزيد من المعلومات
بالطبع! إذا كنت تستخدم نظام التشغيل Windows، يمكنك تنزيل ملف الـ OpenCV الخاص بنظام التشغيل الخاص بك من موقع OpenCV الرسمي (https://opencv.org/releases/)، ومن ثم إضافته إلى مسار النظام الخاص بالمكتبة (java.library.path
). يمكنك فعل ذلك بإضافة الخطوط التالية إلى بداية الطريقة main
في تطبيقك:
javaSystem.setProperty("java.library.path", "path_to_opencv_native_library");
ثم يمكنك استدعاء System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
بعد تعيين مسار المكتبة الجديد. وتأكد من استبدال path_to_opencv_native_library
بالمسار الفعلي لملفات المكتبة الخاصة بـ OpenCV على نظام التشغيل الخاص بك.
بالنسبة لأنظمة التشغيل الأخرى مثل Linux و MacOS، يمكنك تعيين مسار المكتبة الجافا بنفس الطريقة، ولكنك قد تحتاج أيضًا إلى تغيير صلاحيات الملف الثنائي لـ OpenCV بحيث يمكن للمستخدمين الآخرين استخدامه. توجد تفاصيل حول كيفية تحديد مسار المكتبة الجافا وصلاحيات الملفات في الوثائق الرسمية لـ Java.