2017-02-27 36 views
0

我想學習與我的學位的OpenCV庫JavaFX。我想製作一個檢測人的程序。我們可以把它命名爲人們反擊。我已經看過一些視頻,我也想這樣做。我不得不開始學習JavaFX和庫OpenCV。 所以我開始創建一個簡單的項目,開始/暫停攝像頭並拍攝快照。我在YouTube上看到有人這樣做,我開始重新創建它。JavaFX with OpenCV開始/暫停攝像頭。 FXMLLoader

的問題是,我有這樣的錯誤:

Exception in Application start method 
java.lang.reflect.InvocationTargetException 

我的代碼是:

Main

package application; 
import javafx.application.Application; 
import javafx.stage.Stage; 
import javafx.stage.WindowEvent; 
import javafx.scene.Scene; 
import javafx.scene.layout.BorderPane; 
import javafx.fxml.FXMLLoader; 


public class Main extends Application { 
    @Override 
    public void start(Stage primaryStage) { 
     try { 
      BorderPane root = (BorderPane)FXMLLoader.load(getClass().getResource("Camera.fxml")); 
      Scene scene = new Scene(root,400,400); 
      scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm()); 
      primaryStage.setScene(scene); 
      primaryStage.show(); 


     } catch(Exception e) { 
      e.printStackTrace(); 
     } 
    } 

    public static void main(String[] args) { 
     launch(args); 
    } 
} 

CameraController

package application; 
import java.awt.Graphics; 
import java.awt.Image; 
import java.awt.image.BufferedImage; 
import java.io.ByteArrayInputStream; 
import javax.imageio.ImageIO; 
import org.opencv.core.Core; 
import org.opencv.core.Mat; 
import org.opencv.core.MatOfByte; 
import org.opencv.imgcodecs.Imgcodecs; 
import org.opencv.videoio.VideoCapture; 

import javafx.embed.swing.SwingFXUtils; 
import javafx.event.ActionEvent; 
import javafx.fxml.FXML; 
import javafx.scene.control.Button; 
import javafx.scene.image.ImageView; 

public class CameraController { 
// definitions 
@FXML 
private Button btn_start; 
@FXML 
private Button btn_pause; 
@FXML 
private Button btn_snapshot; 
@FXML 
private ImageView imgView; 

private DaemonThread myThread = null; 
    int count = 0; 
    VideoCapture webSource = null; 

    Mat frame = new Mat(); 
    MatOfByte mem = new MatOfByte(); /// start button 
    @FXML 
    protected void startClick(ActionEvent event){ 
     webSource =new VideoCapture(0); 
     myThread = new DaemonThread(); 
     Thread t = new Thread(myThread); 
     t.setDaemon(true); 
        myThread.runnable = true; 
        t.start(); 
       btn_start.setDisable(true); 
        btn_pause.setDisable(false); // stop button 
    } 
    @FXML 
    protected void pauseClick(ActionEvent event){} 
    @FXML 
    protected void snapshotClick(ActionEvent event){} 


    class DaemonThread implements Runnable 
    { 
    protected volatile boolean runnable = false; 

    @Override 
    public void run() 
    { 
     synchronized(this) 
     { 
      while(runnable) 
      { 
       if(webSource.grab()) 
       { 
       try 
         { 
          webSource.retrieve(frame); 
       Imgcodecs.imencode(".bmp", frame, mem); 
       Image im = ImageIO.read(new ByteArrayInputStream(mem.toArray())); 

       BufferedImage buff = (BufferedImage) im; 
       imgView.setImage(SwingFXUtils.toFXImage(buff, null)); 

       if(runnable == false) 
          { 
        System.out.println("Going to wait()"); 
        this.wait(); 
       } 
      } 
      catch(Exception ex) 
         { 
       System.out.println("Error"); 
         } 
       } 
      } 
     } 
    } 
    } 
    ///////////////////////////////////////////////////////// 

}

ŧ他的問題是在這條線:

BorderPane root = (BorderPane)FXMLLoader.load(getClass().getResource("Camera.fxml")); 
      Scene scene = new Scene(root,400,400); 

完全控制檯錯誤:

Exception in Application start method 
java.lang.reflect.InvocationTargetException 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) 
    at java.lang.reflect.Method.invoke(Unknown Source) 
    at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:389) 
    at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:328) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) 
    at java.lang.reflect.Method.invoke(Unknown Source) 
    at sun.launcher.LauncherHelper$FXHelper.main(Unknown Source) 
Caused by: java.lang.RuntimeException: Exception in Application start method 
    at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:917) 
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$155(LauncherImpl.java:182) 
    at java.lang.Thread.run(Unknown Source) 
Caused by: java.lang.UnsatisfiedLinkError: org.opencv.core.Mat.n_Mat()J 
    at org.opencv.core.Mat.n_Mat(Native Method) 
    at org.opencv.core.Mat.<init>(Mat.java:24) 
    at application.CameraController.<init>(CameraController.java:34) 
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) 
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source) 
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source) 
    at java.lang.reflect.Constructor.newInstance(Unknown Source) 
    at java.lang.Class.newInstance(Unknown Source) 
    at sun.reflect.misc.ReflectUtil.newInstance(Unknown Source) 
    at javafx.fxml.FXMLLoader$ValueElement.processAttribute(FXMLLoader.java:927) 
    at javafx.fxml.FXMLLoader$InstanceDeclarationElement.processAttribute(FXMLLoader.java:971) 
    at javafx.fxml.FXMLLoader$Element.processStartElement(FXMLLoader.java:220) 
    at javafx.fxml.FXMLLoader$ValueElement.processStartElement(FXMLLoader.java:744) 
    at javafx.fxml.FXMLLoader.processStartElement(FXMLLoader.java:2707) 
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2527) 
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2441) 
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3214) 
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3175) 
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3148) 
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3124) 
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3104) 
    at javafx.fxml.FXMLLoader.load(FXMLLoader.java:3097) 
    at application.Main.start(Main.java:15) 
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$162(LauncherImpl.java:863) 
    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$175(PlatformImpl.java:326) 
    at com.sun.javafx.application.PlatformImpl.lambda$null$173(PlatformImpl.java:295) 
    at java.security.AccessController.doPrivileged(Native Method) 
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$174(PlatformImpl.java:294) 
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95) 
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method) 
    at com.sun.glass.ui.win.WinApplication.lambda$null$148(WinApplication.java:191) 
    ... 1 more 
Exception running application application.Main 

預先感謝您! 對不起,可能重複.. :(

回答

0

我固定只是增加

System.loadLibrary(Core.NATIVE_LIBRARY_NAME);

public static void main(String[] args) {   
     System.loadLibrary(Core.NATIVE_LIBRARY_NAME); 
     launch(args); 
    } 
問題