无法在OpenCV中访问IP摄像头

时间:2016-12-07 22:54:19

标签: java opencv ffmpeg ip-camera

我尝试使用Javafx和OpenCV通过无线访问网络摄像头(Axis M1013),为我的FRC团队运行视觉处理。当我运行我的代码时,我可以访问使用Scenebuilder创建的GUI,但是当我尝试启动相机时,程序崩溃了。在我看来,它似乎无法使用VideoCapture类。我知道我的问题不在于相机,因为我可以通过浏览器源访问Feed,而我的笔记本电脑的网络摄像头也能正常工作。我使用的是OpevCV版本2.4.13,jdk 8u101x64和ffmpeg 3.2

我查看过其他帖子:

stackoverflow.com/questions/18625948/opencv-java-unsatisfiedlinkerror

answers.opencv.org/question/21720/java-webcam-capture

answers.opencv.org/question/20071/unsatisfiedlinkerror-given-by-highghuiimread-on-java

我尝试了所有这些中提到的解决方案。到目前为止,没有人解决我的问题。

我的代码主要基于以下教程:

opencv-java-tutorials.readthedocs.io/en/latest/03-first-javafx-application-with-opencv.html

这是我的主要java代码:

public class SampleController {
@FXML
private Button Start_btn;

@FXML
private ImageView currentFrame;
private ScheduledExecutorService timer;
private VideoCapture capture;
private boolean cameraActive = false;

@FXML
public void startCamera()
{
    System.loadLibrary("opencv_ffmpeg_64");
    if (!this.cameraActive)
    {
        try
        {
         capture = new VideoCapture("http://FRC:FRC@axis-camera-223-catapult.lan:554/mjpg/1/video.mjpg");
        }
        catch (Exception e)
        {
            System.out.println("an error occured when attempting to access camera.");
        }
        // is the video stream available?
        if (this.capture.isOpened())
        {
            this.cameraActive = true;

            // grab a frame every 33 ms (30 frames/sec)
            Runnable frameGrabber = new Runnable() {

                @Override
                public void run()
                {
                    // effectively grab and process a single frame
                    Mat frame = grabFrame();
                    // convert and show the frame
                    Image imageToShow = Utils.mat2Image(frame);
                    updateImageView(currentFrame, imageToShow);
                }
            };

            this.timer = Executors.newSingleThreadScheduledExecutor();
            this.timer.scheduleAtFixedRate(frameGrabber, 0, 33, TimeUnit.MILLISECONDS);

            // update the button content
            this.Start_btn.setText("Stop Camera");
        }
        else
        {
            // log the error
            System.err.println("Impossible to open the camera connection...");
        }
    }
    else
    {
        // the camera is not active at this point
        this.cameraActive = false;
        // update again the button content
        this.Start_btn.setText("Start Camera");

        // stop the timer
        this.stopAquisition();
    }

    System.out.println("Camera is now on.");
}

/**
 * Get frame from open video
 * @return
 */
    private Mat grabFrame()

    {
        Mat frame = new Mat();
        if (this.capture.isOpened())
        {
            try
            {
                this.capture.read(frame);

                }
        catch (Exception e)
        {
            System.err.println("Exception during the image elaboration: " + e);
        }
    }
    return frame;
}
    private void stopAquisition()
    {
        if (this.timer!=null && !this.timer.isShutdown())
        {
            try
            {
                this.timer.shutdown();
                this.timer.awaitTermination(33, TimeUnit.MILLISECONDS);
            }
            catch (InterruptedException e)
            {
                System.err.println("Exception in stopping the frame capture, trying to release camera now..." + e);

            }
        }
        if (this.capture.isOpened())
        {
            this.capture.release();
        }
    }
    private void updateImageView(ImageView view, Image image)
    {
        Utils.onFXThread(view.imageProperty(), image);
    }
    protected void setClosed()
    { 
        this.stopAquisition();
    }
}

这是我的相机控制器:

Exception in thread "JavaFX Application Thread" java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1774)
at javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1657)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:49)
at javafx.event.Event.fireEvent(Event.java:198)
at javafx.scene.Node.fireEvent(Node.java:8411)
at javafx.scene.control.Button.fire(Button.java:185)
at com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(ButtonBehavior.java:182)
at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:96)
at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:89)
at com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:218)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
at javafx.event.Event.fireEvent(Event.java:198)
at javafx.scene.Scene$MouseHandler.process(Scene.java:3757)
at javafx.scene.Scene$MouseHandler.access$1500(Scene.java:3485)
at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1762)
at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2494)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:380)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:294)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$354(GlassViewEventHandler.java:416)
at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:389)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:415)
at com.sun.glass.ui.View.handleMouseEvent(View.java:555)
at com.sun.glass.ui.View.notifyMouse(View.java:937)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$148(WinApplication.java:191)
at java.lang.Thread.run(Unknown Source)
Caused by: 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 sun.reflect.misc.Trampoline.invoke(Unknown Source)
at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at sun.reflect.misc.MethodUtil.invoke(Unknown Source)
at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1771)
... 48 more
Caused by: java.lang.UnsatisfiedLinkError:     org.opencv.highgui.VideoCapture.VideoCapture_1(Ljava/lang/String;)J
at org.opencv.highgui.VideoCapture.VideoCapture_1(Native Method)
at org.opencv.highgui.VideoCapture.<init>(VideoCapture.java:128)
at jfxtest1.SampleController.startCamera(SampleController.java:36)
... 58 more

当我尝试按照之前的说法运行时,GUI将启动,但是当我尝试打开相机时,我收到一条错误消息:

Private Sub TextBox46_Change()

    If DataInput.TextBox46.Value > 0 And DataInput.TextBox46.Value < 1000 Then
       DataInput.TextBox54.Value = 100 * ((DataInput.TextBox46.Value - DataInput.TextBox14.Value) / (DataInput.TextBox30.Value - DataInput.TextBox14.Value))
       DataInput.TextBox62.Value = 100 - DataInput.TextBox54.Value
    Else: MsgBox ("Revise Inputs")
    End If
    End Sub

如果有人可以帮助我,那将非常感激。

提前致谢。

1 个答案:

答案 0 :(得分:0)

在你的主要java代码中,需要有这个:

public static void main(String[] args) {
    // load the native OpenCV library
    System.loadLibrary(Core.NATIVE_LIBRARY_NAME);               
    launch(args);
}