2012-08-15 136 views
0

將IP攝像頭連接到C#應用程序時,我在Aforge/Samples/Player中找到了簡單的播放器。它只需要修改ip字符串,所以我加了我的 admin:[email protected]:81/videostream.cgi?rate = 11得到MJPEG視頻流。在編譯時,我得到錯誤爲 遠程服務器返回錯誤(401)未經授權。 Andre Kirillow在MJPEGstream.cs文件中提到 某些攝像頭會產生HTTP頭,嚴重不符合標準,導致.NET異常。爲避免這種異常,應使用useUnsafeHeaderParsing配置選項httpWebRequest應該設置什麼,使用應用程序配置文件可以做些什麼。IP Camera 401未授權錯誤

<configuration> 
    <system.net> 
     <settings> 
      <httpWebRequest useUnsafeHeaderParsing="true" /> 
     </settings> 
    </system.net> 
</configuration> 

好吧有兩種方法可以按照Dinis Cruz的建議做到這一點。我用上面的代碼添加了.config文件,並且它也編程了,但同樣的錯誤依然存在。該播放器程序編碼是

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Text; 
using System.Windows.Forms; 
using System.Diagnostics; 

using AForge.Video; 
using AForge.Video.DirectShow; 

using System.Reflection; 
using System.Net.Configuration; 
using System.Net; 

namespace Player 
{ 
    public partial class MainForm : Form 
    { 
    private Stopwatch stopWatch = null; 

    // Class constructor 
    public MainForm() 
    { 
     InitializeComponent(); 
    } 

    private void MainForm_FormClosing(object sender, FormClosingEventArgs e) 
    { 
     CloseCurrentVideoSource(); 
    } 

    // "Exit" menu item clicked 
    private void exitToolStripMenuItem_Click(object sender, EventArgs e) 
    { 
     this.Close(); 
    } 

    // Open local video capture device 
    private void localVideoCaptureDeviceToolStripMenuItem_Click(object sender, EventArgs e) 
    { 
     VideoCaptureDeviceForm form = new VideoCaptureDeviceForm(); 

     if (form.ShowDialog(this) == DialogResult.OK) 
     { 
      // create video source 
      VideoCaptureDevice videoSource = form.VideoDevice; 

      // open it 
      OpenVideoSource(videoSource); 
     } 
    } 

    // Open video file using DirectShow 
    private void openVideofileusingDirectShowToolStripMenuItem_Click(object sender, EventArgs e) 
    { 
     if (openFileDialog.ShowDialog() == DialogResult.OK) 
     { 
      // create video source 
      FileVideoSource fileSource = new FileVideoSource(openFileDialog.FileName); 

      // open it 
      OpenVideoSource(fileSource); 
     } 
    } 

    // Open JPEG URL 
    private void openJPEGURLToolStripMenuItem_Click(object sender, EventArgs e) 
    { 
     URLForm form = new URLForm(); 

     form.Description = "Enter URL of an updating JPEG from a web camera:"; 
     form.URLs = new string[] 
      { 
       "http://195.243.185.195/axis-cgi/jpg/image.cgi?camera=1", 
      }; 

     if (form.ShowDialog(this) == DialogResult.OK) 
     { 
      // create video source 
      JPEGStream jpegSource = new JPEGStream(form.URL); 

      // open it 
      OpenVideoSource(jpegSource); 
     } 
    } 

    // Open MJPEG URL 
    private void openMJPEGURLToolStripMenuItem_Click(object sender, EventArgs e) 
    { 
     URLForm form = new URLForm(); 

     form.Description = "Enter URL of an MJPEG video stream:"; 
     form.URLs = new string[] 
      { 
       "http://[email protected]:81/videostream.cgi?rate=11", 
       "mjpegSource by mobby" 
      }; 

     if (form.ShowDialog(this) == DialogResult.OK) 
     { 
      // create video source 
      MJPEGStream mjpegSource = new MJPEGStream(form.URL); 

      // open it 
      OpenVideoSource(mjpegSource); 
     } 
    } 

    // Open video source 
    private void OpenVideoSource(IVideoSource source) 
    { 
     // set busy cursor 
     this.Cursor = Cursors.WaitCursor; 

     // stop current video source 
     //CloseCurrentVideoSource(); 

     // start new video source 
     videoSourcePlayer.VideoSource = source; 
     videoSourcePlayer.Start(); 

     // reset stop watch 
     stopWatch = null; 

     // start timer 
     timer.Start(); 

     this.Cursor = Cursors.Default; 
    } 

    // Close video source if it is running 
    private void CloseCurrentVideoSource() 
    { 
     if (videoSourcePlayer.VideoSource != null) 
     { 
      videoSourcePlayer.SignalToStop(); 

      // wait ~ 3 seconds 
      for (int i = 0; i < 30; i++) 
      { 
       if (!videoSourcePlayer.IsRunning) 
        break; 
       System.Threading.Thread.Sleep(100); 
      } 

      if (videoSourcePlayer.IsRunning) 
      { 
       videoSourcePlayer.Stop(); 
      } 

      videoSourcePlayer.VideoSource = null; 
     } 
    } 

    // New frame received by the player 
    private void videoSourcePlayer_NewFrame(object sender, ref Bitmap image) 
    { 
     DateTime now = DateTime.Now; 
     Graphics g = Graphics.FromImage(image); 

     // paint current time 
     SolidBrush brush = new SolidBrush(Color.Red); 
     g.DrawString(now.ToString(), this.Font, brush, new PointF(5, 5)); 
     brush.Dispose(); 

     g.Dispose(); 
    } 

    // On timer event - gather statistics 
    private void timer_Tick(object sender, EventArgs e) 
    { 
     IVideoSource videoSource = videoSourcePlayer.VideoSource; 

     if (videoSource != null) 
     { 
      // get number of frames since the last timer tick 
      int framesReceived = videoSource.FramesReceived; 

      if (stopWatch == null) 
      { 
       stopWatch = new Stopwatch(); 
       stopWatch.Start(); 
      } 
      else 
      { 
       stopWatch.Stop(); 

       float fps = 1000.0f * framesReceived/stopWatch.ElapsedMilliseconds; 
       fpsLabel.Text = fps.ToString("F2") + " fps"; 

       stopWatch.Reset(); 
       stopWatch.Start(); 
      } 
     } 
    } 

    public static bool SetAllowUnsafeHeaderParsing20() 
    { 
     //Get the assembly that contains the internal class 
     Assembly aNetAssembly = Assembly.GetAssembly(typeof(System.Net.Configuration.SettingsSection)); 
     if (aNetAssembly != null) 
     { 
      //Use the assembly in order to get the internal type for the internal class 
      Type aSettingsType = aNetAssembly.GetType("System.Net.Configuration.SettingsSectionInternal"); 
      if (aSettingsType != null) 
      { 
       //Use the internal static property to get an instance of the internal settings class. 
       //If the static instance isn't created allready the property will create it for us. 
       object anInstance = aSettingsType.InvokeMember("Section", 
       BindingFlags.Static | BindingFlags.GetProperty | BindingFlags.NonPublic, null, null, new object[] { }); 
       if (anInstance != null) 
       { 
        //Locate the private bool field that tells the framework is unsafe header parsing should be allowed or not 
        FieldInfo aUseUnsafeHeaderParsing = aSettingsType.GetField("useUnsafeHeaderParsing", BindingFlags.NonPublic | BindingFlags.Instance); 
        if (aUseUnsafeHeaderParsing != null) 
        { 
         aUseUnsafeHeaderParsing.SetValue(anInstance, true); 
         return true; 
        } 
       } 
      } 
     } 
     return false; 
    } 
    } 
} 

從谷歌我加了一些開放的IP攝像機和有視頻流被抓獲,並能正常工作,但我的相機返回錯誤401然後我用這是使用內置最近Sean Tearney做出ISPY軟件相同的Aforge庫,它捕捉我的相機的視頻流:第。現在我不知道簡單播放器的編碼有什麼問題。 如果有人能幫助我從攝像頭獲取視頻流,請加以幫助。 Thankyou

回答

1

很可能是因爲相機受密碼保護。爲了使您能夠查看信息流,請爲您嘗試訪問的相機提供用戶名和密碼。 爲JPEG流:

jpegSource.Login = "your username"; 
jpegSource.Password = "your password"; 

爲MJPEG流:

mjpegSource.Login = "your username"; 
mjpegSource.Password = "your password";