gpt4 book ai didi

java - OpenCV 不适用于 JavaFx

转载 作者:太空宇宙 更新时间:2023-11-03 23:19:11 25 4
gpt4 key购买 nike

我使用 intellij IDEA 正确设置了 OpenCV 3.0.0,但在编译我的代码时仍然出现错误。我正在尝试将它与嵌入在 Javafx 中的 swing 一起使用。

程序运行但似乎 OpenCv 不工作,因为网络摄像头没有打开并且日志错误似乎与 opencv 相关

这是我的代码。

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

我还添加了

static{
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
}

最佳答案

我没有使用嵌入在 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()));
}
}
}

虽然我不知道为什么我一开始就收到错误

关于java - OpenCV 不适用于 JavaFx,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33702438/

25 4 0
文章推荐: node.js - Mac安装openssl,环境找不到
文章推荐: c# - 当 T 是对象的后代时,为什么不能将 Predicate 分配给 Predicate