在JavaFX中结合使用FileChooser和Thread时出现的问题

时间:2018-07-06 08:13:54

标签: multithreading javafx java-threads thread-synchronization filechooser

首先,我正在创建一个应用程序,用户可以在其中执行以下两项操作

  1. 使用网络摄像头从视频流中捕获图像
     使用VideoCapture()方法
  2. 完成视频流
  3. 或者用户可以选择图片 但我面临一些问题
    1. 如果我捕获了图像并将其设置在ImageView上,它将在一段时间恢复视频流后仅出现一段时间
    2. 如果我尝试选择一个图像并将其设置在ImageView上,则它将在一段时间后恢复正常显示。

因此,为了克服这些问题,我尝试使用Thread,代码如下:

class setup {

    VideoCaptureDifficultWayController v = new VideoCaptureDifficultWayController();

    public synchronized void VideoCapture() {
        if (VideoCaptureDifficultWayController.loop) {
            while (VideoCaptureDifficultWayController.loop) {
                Webcam webcam = VideoCaptureDifficultWayController.webcam;
                BufferedImage image = webcam.getImage();

                System.out.println(image);
                WritableImage card = SwingFXUtils.toFXImage(image, null);
                VideoCaptureDifficultWayController.video.setImage(card);
            }
        } else {
            try {
                wait();
            } catch (InterruptedException ex) {
                Logger.getLogger(setup.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }

    public synchronized void SelectFile() {
        try {
            FileChooser fc = new FileChooser();
            fc.getExtensionFilters().add(new ExtensionFilter("JPG Files", "*.jpg"));
            File file = fc.showOpenDialog(null);
            v.file = file;
            FilePath = file.toURI().toURL().toString();
            Image image = new Image(FilePath);
            v.video.setImage(image);
            VideoCaptureDifficultWayController.loop=false;
        } catch (MalformedURLException ex) {
            Logger.getLogger(setup.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

}
    class videoTaker extends Thread {

        setup s;

        public videoTaker() {
            s = new setup();
            Thread t = new Thread(this, "videoTaker");
            t.start();
        }

        public void run() {
            s.camera();
        }
    }
}
class other extends Thread {

    setup s;

    public other() {
        s = new setup();
        Thread t = new Thread(this, "other");
        t.start();
    }

    public void run() {
        s.select();
    }
}

视频流没有问题,但是文件选择器报告了以下问题:

Exception in thread "other" java.lang.IllegalStateException: This operation is permitted on the event thread only; currentThread = other
    at com.sun.glass.ui.Application.checkEventThread(Application.java:443)
    at com.sun.glass.ui.CommonDialogs$ExtensionFilter.<init>(CommonDialogs.java:75)
    at com.sun.javafx.tk.quantum.QuantumToolkit.convertExtensionFilters(QuantumToolkit.java:1571)
    at com.sun.javafx.tk.quantum.QuantumToolkit.showFileChooser(QuantumToolkit.java:1514)
    at javafx.stage.FileChooser.showDialog(FileChooser.java:416)
    at javafx.stage.FileChooser.showOpenDialog(FileChooser.java:350)
    at music.VideoCaptureDifficultWayController$setup.select(VideoCaptureDifficultWayController.java:184)
    at music.VideoCaptureDifficultWayController$other.run(VideoCaptureDifficultWayController.java:223)
    at java.lang.Thread.run(Thread.java:748)
BufferedImage@1b6c8208: type = 0 ColorModel: #pixelBits = 24 numComponents = 3 color space = java.awt.color.ICC_ColorSpace@1bf30eec transparency = 1 has alpha = false isAlphaPre = false sun.awt.image.SunWritableRaster@6f881fa2
BufferedImage@b68474f: type = 0 ColorModel: #pixelBits = 24 numComponents = 3 color space = java.awt.color.ICC_ColorSpace@1bf30eec transparency = 1 has alpha = false isAlphaPre = false sun.awt.image.SunWritableRaster@74ebf8e8
null
Exception in thread "videoTaker" java.lang.NullPointerException
    at javafx.embed.swing.SwingFXUtils.toFXImage(SwingFXUtils.java:85)
    at music.VideoCaptureDifficultWayController$setup.camera(VideoCaptureDifficultWayController.java:168)
    at music.VideoCaptureDifficultWayController$videoTaker.run(VideoCaptureDifficultWayController.java:208)
    at java.lang.Thread.run(Thread.java:748)

那我现在该怎么办

0 个答案:

没有答案