【问题标题】:OpenCV not working with JavaFxOpenCV 不适用于 JavaFx
【发布时间】:2016-02-15 14:34:02
【问题描述】:

我用 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);
}

【问题讨论】:

  • 信息不足。你认为任何人都可以从堆栈跟踪中的行号中做出一些事情吗?请提供MCVE。您是否尝试过使用普通的 JavaFX,然后它可以工作吗?
  • @Roland 我只是更改并使用 javafx,而不是使用 javafx 和 swing 的混合,我会尽快发布我所做的

标签: java swing opencv javafx


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-05-25
    • 2018-01-21
    • 1970-01-01
    • 2017-03-13
    • 1970-01-01
    • 2015-07-02
    • 2016-06-23
    相关资源
    最近更新 更多