简体   繁体   English

如何使用C#捕获IP摄像机

[英]how to capture ip camera with C#

This below is the code that I have trying to capture the Wanscam ip camera however does not work now is raising some exceptions and does at all and I really do not know whats going on 下面是我试图捕获Wanscam ip摄像机的代码,但是现在无法正常工作,正在引发一些异常并且完全没有异常,我真的不知道发生了什么

The remote server returned an error: (401) Unauthorized. 远程服务器返回错误:(401)未经授权。 sometimes this is the exception but many times does not even know what kind exception happened. 有时这是例外,但很多时候甚至都不知道发生了什么样的例外。

Please anyone can spot something, since now thank you all. 因为现在谢谢大家,请任何人都可以发现一些东西。

there is just a button in the form, and as long this button display Stop the loop will go around updating the picture box. 表单中只有一个按钮,只要此按钮显示“停止”,循环就会更新图片框。

and the camera is a Wanscam AJ-C2WA-C198 I know this is not the greatest camera in the world. 相机是Wanscam AJ-C2WA-C198,我知道这不是世界上最出色的相机。

and my admin name and my password are match right. 和我的管理员名称和密码正确匹配。

EDIT:: I notice that on VLC I see that image after I enter the admin and password again but I already did on the URL that is weird 编辑::我注意到在VLC上,我再次输入管理员和密码后会看到该图像,但是我已经在奇怪的URL上了

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace cam01
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }


        public Thread _cameraThread;
        private string metaUrl = "http://home-ip-camera.dyndns-server.com/videostream.asf?user=<USER_NAME>&pwd=<PASSWORD>resolution=64&rate=0";

        public HttpWebRequest req;

        public WebResponse res;

        public System.IO.Stream stream;
        private void button1_Click(object sender, EventArgs e)
        {
            if (btnMain.Text.Equals("Start"))
            {
                if (_cameraThread == null)
                    _cameraThread = new Thread(new ThreadStart(Run));

                _cameraThread.Start();
                btnMain.Text = "Stop";
            }
            else
            {
                btnMain.Text = "Start";
                _cameraThread.Abort();
                _cameraThread = null;
            }
        }

        private void Run()
        {
            while (btnMain.Text.Equals("Stop"))
            {
                try
                {

                    req = (HttpWebRequest)HttpWebRequest.Create(metaUrl);

                    req.AllowWriteStreamBuffering = true;

                    req.Timeout = 20000;

                    res = req.GetResponse();

                    stream = res.GetResponseStream();

                    pictureBox1.Image = Image.FromStream(stream);

                    res.Close();
                }
                catch (Exception e)
                {
                    Console.WriteLine("Exception: {0}", e.ToString());
                }
            }
        }

    }
}
`

You can check, that http port are setted correct. 您可以检查http端口设置是否正确。 (Route on your DDNS or specify in get-query). (在您的DDNS上路由或在get-query中指定)。 By default port is 99, not 80! 默认端口是99,而不是80!

Also, you can try to use /videostream.cgi?user=<USENAME>&pwd=<PASSWORD> - it get you MJPEG stream instead FFMPEG, provided by videostream.asf 此外,您还可以尝试使用/videostream.cgi?user=<USENAME>&pwd=<PASSWORD> -它让你MJPEG流,而不是FFMPEG,提供videostream.asf

Finally, try to specify creditals in your request: 最后,尝试在您的请求中指定信用:

req.Credentials = new NetworkCredential("<USENAME>", "<PASSWORD>");

and try another query params: loginuse=<USENAME>&loginpass=<PASSWORD> 并尝试另一个查询参数: loginuse=<USENAME>&loginpass=<PASSWORD>

Try find the sdk(software development kit) of the camera either in CD or on the company website.Mostly, Cameras SDKs are written in c/c++ for performance reasons.Try to wrapper them in C#. 尝试在CD或公司网站上找到相机的sdk(软件开发套件)。出于性能原因,大多数相机SDK是用c / c ++编写的,请尝试将它们包装在C#中。 Pointers are strongly discouraged in c# but has included just for the reason of Microsoft Backward compatibility for unmanaged codes. 强烈建议不要在c#中使用指针,但仅出于Microsoft向后兼容非托管代码的原因而包含指针。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM