I have a sample code from https://ratiler.wordpress.com/2014/09/08/detection-de-mouvement-avec-javacv/ with small difference in loading the open cv library in the static block using nu.pattern.OpenCV.loadShared();
:
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import org.opencv.core.CvType;
import org.opencv.core.Mat;
import org.opencv.core.MatOfByte;
import org.opencv.core.Size;
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.videoio.VideoCapture;
import org.opencv.imgproc.Imgproc;
public class JavaCVPrjt01 {
static {
//System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
nu.pattern.OpenCV.loadShared();
}
public static void main(String[] args) {
JFrame jframe = new JFrame("HUMAN MOTION DETECTOR FPS");
jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JLabel vidpanel = new JLabel();
jframe.setContentPane(vidpanel);
jframe.setSize(1280,720) ;
jframe.setVisible(true);
Mat frame = new Mat();
Mat outerBox = new Mat();
Size sz = new Size(1280,720);
VideoCapture camera = new VideoCapture("D:/Downloads/video4.mp4");
while (true) {
if (camera.read(frame)) {
Imgproc.resize(frame,frame, sz );
outerBox = new Mat(frame.size(), CvType.CV_8UC1);
Imgproc.cvtColor(frame, outerBox, Imgproc.COLOR_BGR2GRAY);
Imgproc.GaussianBlur(outerBox, outerBox, new Size(3, 3), 0);
ImageIcon image = new ImageIcon(Mat2bufferedImage(outerBox));
vidpanel.setIcon(image);
vidpanel.repaint();
}
}
}
public static BufferedImage Mat2bufferedImage(Mat image) {
MatOfByte bytemat = new MatOfByte();
Imgcodecs.imencode(".jpg", image, bytemat);
byte[] bytes = bytemat.toArray();
InputStream in = new ByteArrayInputStream(bytes);
BufferedImage img = null;
try {
img = ImageIO.read(in);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return img;
}
}
and of course, the needed dependencies were added:
<dependency>
<groupId>org.bytedeco.javacpp-presets</groupId>
<artifactId>opencv</artifactId>
<version>3.2.0-1.3</version>
</dependency>
<dependency>
<groupId>org.openpnp</groupId>
<artifactId>opencv</artifactId>
<version>3.2.0-1</version>
</dependency>
so, code is executed right before
VideoCapture camera = new VideoCapture("D:/Downloads/video4.mp4");
where it hangs and I have no clues to check what is wrong on the JNI level there.
It was a licensing issue that made impossible to include ffmpeg to
<dependency>
<groupId>org.openpnp</groupId>
<artifactId>opencv</artifactId>
<version>3.2.0-1</version>
</dependency>
(and it looks like it occurs in all versions, I have tried all available in maven)
https://github.com/openpnp/opencv/issues/55
UPD.
so it is still required to load dlls explicitly. In my case
static {
//System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
System.load("D:\\Downloads\\opencv\\build\\java\\x64\\opencv_java2411.dll");
System.load("D:\\Downloads\\opencv\\build\\x64\\vc12\\bin\\opencv_ffmpeg2411_64.dll");
}
explicit load of opencv_java
and opencv_ffmpeg
made the trick. Without loading opencv_ffmpeg
explicitly the VideoCapture's
.read(frame)
was just always returning false with no any mention on any missing native ffmpeg
dependency problem.
And the required java dependencies in pom.xml
are:
<dependency>
<groupId>org.bytedeco.javacpp-presets</groupId>
<artifactId>opencv</artifactId>
<version>2.4.11-0.11</version>
</dependency>
<dependency>
<groupId>org.openpnp</groupId>
<artifactId>opencv</artifactId>
<version>2.4.11-2</version>
</dependency>
(please not i have downgraded the opencv version)