0

I'm trying to build OpencCV for Java on raspberry pi 4B. I am quite new in this sphere and do not understand almost everything. At first, I just wanted to unload my .jar file to the raspberry and run it there. This .jar file is a compiled project in which the OpenCV library is used. As I understand it, just using OpenCV library in your project is not enough and you also need to install OpenCV on the raspberry itself. I found two articles on how to build it on a raspberry First one and Second one. I have tried to build OpenCV by following the second article and built it. But I have got only libopencv_java440.so file without other files (like libopencv_core.so). But anyway I have tried to run my program and tried to save an image from camera placed on raspberry, but got only a strange image (colored stripes). After this I have tried to run my Python program on raspberry and it worked fine.
Then I have tried the first article but into cmake I also included this lines from the second article -D JAVA_INCLUDE_PATH=$JAVA_HOME/include -D JAVA_AWT_LIBRARY=$JAVA_HOME/jre/lib/amd64/libawt.so -D JAVA_JVM_LIBRARY=$JAVA_HOME/jre/lib/arm/server/libjvm.so. After this everything built and I got all the files that were missing before, but now I have lost libopencv_java440.so file.
This is as awful as possible, because I also ran into a lot of errors just doing sudo apt-get update and sudo apt-get upgrade. And the question itself - How can I use OpenCV from Java on raspberry pi? What should be installed and how should it be installed? Are there any articles about this somewhere? Thanks for any advice and help.

1 Answer 1

0

And again I answer my own question. This is quite strange community, it's not like SOF where everyone know the solution and can help you. So about the solution. As you could see I had a problem when I built OpenCV by following the first article I didn't get the libopencv_java440.so file. But I can get it after building OpenCV like in the second article! So in my java code:

System.load("/opt/opencv-4.4.0/lib/libopencv_core.so");
System.load("/opt/opencv-4.4.0/lib/libopencv_imgproc.so");
System.load("/opt/opencv-4.4.0/lib/libopencv_imgcodecs.so");
System.load("/opt/opencv-4.4.0/lib/libopencv_img_hash.so");
System.load("/opt/opencv-4.4.0/lib/libopencv_photo.so");

System.load("/opt/opencv-4.4.0/lib/libopencv_xphoto.so");
System.load("/opt/opencv-4.4.0/lib/libopencv_flann.so");
System.load("/opt/opencv-4.4.0/lib/libopencv_features2d.so");
System.load("/opt/opencv-4.4.0/lib/libopencv_calib3d.so");
System.load("/opt/opencv-4.4.0/lib/libopencv_phase_unwrapping.so");
System.load("/opt/opencv-4.4.0/lib/libopencv_structured_light.so");
System.load("/opt/opencv-4.4.0/lib/libopencv_xfeatures2d.so");

System.load("/opt/opencv-4.4.0/lib/libopencv_video.so");
System.load("/opt/opencv-4.4.0/lib/libopencv_ximgproc.so");
System.load("/opt/opencv-4.4.0/lib/libopencv_aruco.so");
System.load("/opt/opencv-4.4.0/lib/libopencv_bgsegm.so");
System.load("/opt/opencv-4.4.0/lib/libopencv_bioinspired.so");
System.load("/opt/opencv-4.4.0/lib/libopencv_objdetect.so");
System.load("/opt/opencv-4.4.0/lib/libopencv_face.so");

System.load("/opt/opencv-4.4.0/lib/libopencv_dnn.so");
System.load("/opt/opencv-4.4.0/lib/libopencv_tracking.so");
System.load("/opt/opencv-4.4.0/lib/libopencv_plot.so");
System.load("/opt/opencv-4.4.0/lib/libopencv_ml.so");
System.load("/opt/opencv-4.4.0/lib/libopencv_ml.so");
System.load("/opt/opencv-4.4.0/lib/libopencv_text.so");
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);

I load .so files from the first build. But how do java understands which Core.NATIVE_LIBRARY_NAME I want to load? It is simple - when I run my .jar file on raspberry I pass an argument:
-Djava.library.path=/usr/local/share/java/opencv4/. So this is the solution. But for the first time it didn't work for me and gave some strange errors. Raspberry could detect my camera but couldn't get an image from it, but then I have changed the camera to another one and it started working. So the code that I used to test camera:

VideoCapture camera = new VideoCapture(0);

if (camera.isOpened())
{
    System.out.println("Video is captured");
}
else
{
    System.out.println("Camera isn't opened");
}
Mat mat = new Mat();
while (!camera.read(mat)) {}
Imgcodecs imageCodecs = new Imgcodecs(); 
imageCodecs.imwrite("/home/pi/testt.jpg", mat);
System.out.println("Image saved");

And here it is, this is the solution!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.