首先,我正在创建一个应用程序,用户可以在其中执行以下两项操作
VideoCapture()
方法因此,为了克服这些问题,我尝试使用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)
那我现在该怎么办