我正在学习OpenCV。我正在使用OpenCV 3.2.0 jar。我试图用它打开我的网络摄像头,但收到错误。这是我的代码
爪哇:
import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.videoio.VideoCapture;
public class WebCam {
public static void main(String[] args) {
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
VideoCapture videoCapture = new VideoCapture(0);
videoCapture.open("the video");
Mat frame = new Mat();
while (true){
videoCapture.read(frame);
}
}
}
错误:
#
# A fatal error has been detected by the Java Runtime Environment:
#
# SIGSEGV (0xb) at pc=0x00007fffbe7d605d, pid=922, tid=0x0000000000000307
#
# JRE version: Java(TM) SE Runtime Environment (8.0_101-b13) (build 1.8.0_101-b13)
# Java VM: Java HotSpot(TM) 64-Bit Server VM (25.101-b13 mixed mode bsd-amd64 compressed oops)
# Problematic frame:
# C [libobjc.A.dylib+0x705d] objc_msgSend+0x1d
#
# Failed to write core dump. Core dumps have been disabled. To enable core dumping, try "ulimit -c unlimited" before starting Java again
#
# An error report file with more information is saved as:
# /path /hs_err_pid922.log
#
# If you would like to submit a bug report, please visit:
# http://bugreport.java.com/bugreport/crash.jsp
#
Process finished with exit code 134 (interrupted by signal 6: SIGABRT)
我已经按照python教程编写了这段代码
import numpy as np
import cv2
cap = cv2.VideoCapture(0)
while 1:
ret, img = cap.read()
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
cv2.imshow('THE CAMERA FOR FACE AND EYE',img)
k = cv2.waitKey(30) & 0xff
if k == 27:
break
cap.release()
cv2.destroyAllWindows()
它有效。
请帮帮我。我怎么能解决这个问题。另一方面,我在网上找不到足够好的解释材料。请建议我一些好的资源。谢谢你提前
答案 0 :(得分:1)
您的代码存在的问题是您尝试使用videocapture.open("the video")
再次打开VideoCapture而不传递文件类型。从网络摄像头捕获流的代码应该是:
import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.videoio.VideoCapture;
public class WebCam {
public static void main(String[] args) {
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
VideoCapture videoCapture = new VideoCapture(0);
Mat frame = new Mat();
while (true){
videoCapture.read(frame);
showResult(frame);
}
}
public static void showResult(Mat img) {
Imgproc.resize(img, img, new Size(640, 480));
MatOfByte matOfByte = new MatOfByte();
Highgui.imencode(".jpg", img, matOfByte);
byte[] byteArray = matOfByte.toArray();
BufferedImage bufImage = null;
try {
InputStream in = new ByteArrayInputStream(byteArray);
bufImage = ImageIO.read(in);
JFrame frame = new JFrame();
frame.getContentPane().add(new JLabel(new ImageIcon(bufImage)));
frame.pack();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
}
为了显示框架,您需要使用提到的here解决方案,因为还没有Java highgui包装器。 我希望这会有所帮助。