使用JavaFX和openCV 3时出现“未知异常”

时间:2016-12-03 23:41:50

标签: java opencv javafx data-binding webcam

我正在读串口并正确绘制数据。我正在使用LineChart<Number, Number>系列来创建某个图表。有一种changeListener方法可以在数据进入时更新此系列。

在第二阶段,我正在使用网络摄像头通过openCV创建实时视频。我也在它上面叠了一条线。

该点,该行的一端取决于我LineChart<>系列的最后一个条目的数据。我正在使用以下代码来同步更新。它工作正常,但有时两个阶段都冻结,我得到这个错误:

Exception duting the image elaboration : java.lang.Exception: unknown exception

真的很奇怪。如果它不起作用,为什么不尽快或至少总是抛出这个例外? 任何帮助表示赞赏。非常感谢。

代码很长。我刚发布相关的文章:
所有代码都在一个类中:

相关实例变量:

 private Series<Number, Number> referenceSeries;
 private Button activateCameraButton = new Button("Activate Camera");
    private ImageView currentFrame = new ImageView();
    private ScheduledExecutorService timer;
    private VideoCapture capture = new VideoCapture();
    private boolean cameraActive = false;
    private ObjectProperty<Number> currentRefValNumber;
    private double currentRefVal = 0;

在listner设置方法中:

currentRefValNumber = referenceSeries.dataProperty().get().get(
                                referenceSeries.getData().size() - 1).YValueProperty();

最后这里设置了网络摄像头视图部分:

private void setUpWebcamView() {

        Stage secondStage = new Stage();
        BorderPane root2 = new BorderPane();
        activateCameraButton.setOnAction(e -> startCamera(e));
        root2.setCenter(currentFrame);
        root2.setTop(activateCameraButton);
        root2.setId("webcamViewID");
        //root2.getStylesheets().add("-fx-background-image: url('myimage.jpg')");
        Scene scene2 = new Scene(root2, 800, 600);
        scene2.getStylesheets().addAll(this.getClass().getResource("Style.css").toExternalForm());
        secondStage .setTitle("webcam view");
        secondStage .setScene(scene2);
        secondStage .show();

    }

    private void startCamera(ActionEvent event) {
        this.currentFrame.setPreserveRatio(true);
        if (!this.cameraActive) {
            this.capture.open(0);

            if (this.capture.isOpened()) {
                this.cameraActive = 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);
                this.activateCameraButton.setText("Stop Camera");
            } else {
                System.err.println("Camera Error");
            }
        } else {
            this.cameraActive = false;
            this.activateCameraButton.setText("Start Camera");
            try {
                this.timer.shutdown();
                this.timer.awaitTermination(33, TimeUnit.MILLISECONDS);
            } catch (InterruptedException e) {
                System.err.println(
                        "Exceprion in stopping the frame capture, trying to stop the camera now....");
            }

            this.capture.release();
            this.currentFrame.setImage(null);
        }

    }



    private Image grabFrame() {
        Image imageToShow = null;
        Mat frame = new Mat();
        if (this.capture.isOpened()) {
            try {
                this.capture.read(frame);
                if (!frame.empty()) {
                    imageToShow = mat2Image(frame);
                }
            } catch (Exception e) {
                System.err.println("Exception duting the image elaboration : " + e);

            }
        }
        return imageToShow;
    }

    private Image mat2Image(Mat frame) {
        Core.flip(frame, frame, 1);

        currentRefVal = currentRefValNumber.getValue().doubleValue();
        //System.out.println(currentRefVal);
        Imgproc.line(frame, new Point(frame.size().width / 2, frame.size().height)
                , new Point(frame.size().width / 2 - (frame.size().width / 40)  * (currentRefVal), 0), new Scalar(0, 255, 0), 5);
        MatOfByte buffer = new MatOfByte();
        Imgcodecs.imencode(".bmp", frame, buffer);
        return new Image(new ByteArrayInputStream(buffer.toArray()));
    }

我再说一次,我的代码实际上有效,但并非总是如此。我正在系统化摄像头部分中的currentRefVal,它正在获取正确的数字。

更新
这是代码仍然有效的堆栈跟踪:

Exception duting the image elaboration : java.lang.NullPointerException
java.lang.NullPointerException
    at myPackage.MainClass.mat2Image(MainClass.java:486)
    at moradi.MainClass.grabFrame(MainClass.java:473)
    at moradi.MainClass.access$0(MainClass.java:466)
    at moradi.MainClass$1.run(MainClass.java:435)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
    at java.util.concurrent.FutureTask.runAndReset(FutureTask.java:308)
    at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$301(ScheduledThreadPoolExecutor.java:180)
    at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:294)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:745)

这是导致我的应用程序冻结和崩溃的堆栈跟踪

java.lang.Exception: unknown exception
    at org.opencv.core.Mat.nGetB(Native Method)
    at org.opencv.core.Mat.get(Mat.java:1013)
    at org.opencv.core.MatOfByte.toArray(MatOfByte.java:58)
    at myPackage.MainClass.mat2Image(MainClass.java:493)
    at myPackage.MainClass.grabFrame(MainClass.java:473)
    at myPackage.MainClass.access$0(MainClass.java:466)
    at myPackage.MainClass$1.run(MainClass.java:435)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
    at java.util.concurrent.FutureTask.runAndReset(FutureTask.java:308)
    at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$301(ScheduledThreadPoolExecutor.java:180)
    at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:294)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:745)

0 个答案:

没有答案