OpenCV不使用JavaFx
我用intellij IDEA正确地设置了OpenCV 3.0.0,但在编译代码时仍然会出错。我尝试将它与嵌入Javafx中的swing一起使用 程序运行,但OpenCv似乎不工作,因为网络摄像头未打开,日志错误似乎与OpenCv有关 这是我的密码OpenCV不使用JavaFx,java,swing,opencv,javafx,Java,Swing,Opencv,Javafx,我用intellij IDEA正确地设置了OpenCV 3.0.0,但在编译代码时仍然会出错。我尝试将它与嵌入Javafx中的swing一起使用 程序运行,但OpenCv似乎不工作,因为网络摄像头未打开,日志错误似乎与OpenCv有关 这是我的密码 private void createFrameContent(SwingNode swingNode){ SwingUtilities.invokeLater(new Runnable() { @Overri
private void createFrameContent(SwingNode swingNode){
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
JPanel frame=new JPanel();
swingNode.setContent(frame);
JLabel label=new JLabel();
frame.add(label);
ImageProcessor imageProcessor=new ImageProcessor();
Mat webCamImageMat=new Mat();
Image tempImage;
VideoCapture capture=new VideoCapture(0);
capture.set(Videoio.CAP_PROP_FRAME_WIDTH,320);
capture.set(Videoio.CAP_PROP_FRAME_HEIGHT,240);
if(capture.isOpened()){
while (true){
capture.read(webCamImageMat);
if(!webCamImageMat.empty()){
tempImage=imageProcessor.toBufferedImage(webCamImageMat);
ImageIcon imageIcon=new ImageIcon(tempImage,"");
label.setIcon(imageIcon);
}else{
System.out.println("not Captured...");
break;
}
}
}else{
System.out.println("Capture failed !");
}
}
});
}
这些是日志错误
Exception in thread "AWT-EventQueue-0" java.security.PrivilegedActionException: java.lang.Exception: unknown exception
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:726)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java: 105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
Caused by: java.lang.Exception: unknown exception
at org.opencv.videoio.VideoCapture.VideoCapture_2(Native Method)
at org.opencv.videoio.VideoCapture.<init>(VideoCapture.java:54)
at sample.Main$WindowPane$1.run(Main.java:68)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:311)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:756)
at java.awt.EventQueue.access$500(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:709)
at java.awt.EventQueue$3.run(EventQueue.java:703)
... 9 more
我没有使用嵌入JavaFX的Swing,而是只使用JavaFX,这就是我所做的
public class Main extends Application {
@Override
public void start(Stage primaryStage) throws Exception{
StackPane root=new StackPane(new MainWindow());
Scene scene =new Scene(root,800,575);
primaryStage.setTitle("Cam capture");
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
launch(args);
}
class MainWindow extends BorderPane{
private ImageView currentFrame;
private boolean isCameraActive=false;
private Button button;
private ScheduledExecutorService timer;
private VideoCapture videoCapture=new VideoCapture();
public MainWindow(){
currentFrame=new ImageView();
button=new Button("Start");
this.setCenter(currentFrame);
this.setBottom(button);
button.setOnAction(event->{
if(!this.isCameraActive){
this.videoCapture.open(0);
if(this.videoCapture.isOpened()){
this.isCameraActive=true;
Runnable frameGrabber=new Runnable() {
@Override
public void run() {
Image imageToShow=grabFrame();
currentFrame.setImage(imageToShow);
}
};
this.timer= Executors.newSingleThreadScheduledExecutor();
this.timer.scheduleAtFixedRate(frameGrabber, 0, 33, TimeUnit.MILLISECONDS);
}else{
System.err.println("Impossible to open the camera connection...");
}
} else{
System.out.println("Cam is already active");
}
});
}
private Image grabFrame()
{
// init everything
Image imageToShow = null;
Mat frame = new Mat();
// check if the capture is open
if (this.videoCapture.isOpened())
{
try
{
// read the current frame
this.videoCapture.read(frame);
// if the frame is not empty, process it
if (!frame.empty())
{
// convert the image to gray scale
//Imgproc.cvtColor(frame, frame, Imgproc.COLOR_BGR2GRAY);
// convert the Mat object (OpenCV) to Image (JavaFX)
imageToShow = mat2Image(frame);
}
}
catch (Exception e)
{
// log the error
System.err.println("Exception during the image elaboration: " + e);
}
}
return imageToShow;
}
/**
* Convert a Mat object (OpenCV) in the corresponding Image for JavaFX
*
* @param frame
* the {@link Mat} representing the current frame
* @return the {@link Image} to show
*/
private Image mat2Image(Mat frame)
{
// create a temporary buffer
MatOfByte buffer = new MatOfByte();
// encode the frame in the buffer
Imgcodecs.imencode(".png", frame, buffer);
// build and return an Image created from the image encoded in the
// buffer
return new Image(new ByteArrayInputStream(buffer.toArray()));
}
}
}
虽然我不知道为什么我一开始就遇到了错误,而不是使用java fx中嵌入的Swing,但我只使用了java fx,这就是我所做的
public class Main extends Application {
@Override
public void start(Stage primaryStage) throws Exception{
StackPane root=new StackPane(new MainWindow());
Scene scene =new Scene(root,800,575);
primaryStage.setTitle("Cam capture");
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
launch(args);
}
class MainWindow extends BorderPane{
private ImageView currentFrame;
private boolean isCameraActive=false;
private Button button;
private ScheduledExecutorService timer;
private VideoCapture videoCapture=new VideoCapture();
public MainWindow(){
currentFrame=new ImageView();
button=new Button("Start");
this.setCenter(currentFrame);
this.setBottom(button);
button.setOnAction(event->{
if(!this.isCameraActive){
this.videoCapture.open(0);
if(this.videoCapture.isOpened()){
this.isCameraActive=true;
Runnable frameGrabber=new Runnable() {
@Override
public void run() {
Image imageToShow=grabFrame();
currentFrame.setImage(imageToShow);
}
};
this.timer= Executors.newSingleThreadScheduledExecutor();
this.timer.scheduleAtFixedRate(frameGrabber, 0, 33, TimeUnit.MILLISECONDS);
}else{
System.err.println("Impossible to open the camera connection...");
}
} else{
System.out.println("Cam is already active");
}
});
}
private Image grabFrame()
{
// init everything
Image imageToShow = null;
Mat frame = new Mat();
// check if the capture is open
if (this.videoCapture.isOpened())
{
try
{
// read the current frame
this.videoCapture.read(frame);
// if the frame is not empty, process it
if (!frame.empty())
{
// convert the image to gray scale
//Imgproc.cvtColor(frame, frame, Imgproc.COLOR_BGR2GRAY);
// convert the Mat object (OpenCV) to Image (JavaFX)
imageToShow = mat2Image(frame);
}
}
catch (Exception e)
{
// log the error
System.err.println("Exception during the image elaboration: " + e);
}
}
return imageToShow;
}
/**
* Convert a Mat object (OpenCV) in the corresponding Image for JavaFX
*
* @param frame
* the {@link Mat} representing the current frame
* @return the {@link Image} to show
*/
private Image mat2Image(Mat frame)
{
// create a temporary buffer
MatOfByte buffer = new MatOfByte();
// encode the frame in the buffer
Imgcodecs.imencode(".png", frame, buffer);
// build and return an Image created from the image encoded in the
// buffer
return new Image(new ByteArrayInputStream(buffer.toArray()));
}
}
}
虽然我不知道为什么我首先会收到错误这是不够的信息。你认为任何人怎么能从stacktrace中的行号中做出一些东西?请提供一份报告。你试过纯JavaFX了吗?然后它工作了吗?@Roland我刚刚改变了,只使用了JavaFX,而不是混合使用JavaFX和swing,我会尽快发布我所做的这还不够信息。你认为任何人怎么能从stacktrace中的行号中做出一些东西?请提供一份报告。你试过纯JavaFX了吗?然后它工作了吗?@Roland我刚刚改变了,只使用了JavaFX,而不是混合使用JavaFX和swing,我很快就会发布我所做的