实现抓图
This commit is contained in:
@@ -47,6 +47,7 @@ namespace Test
|
||||
this.txtIpAddress = new System.Windows.Forms.TextBox();
|
||||
this.btnSetIP = new System.Windows.Forms.Button();
|
||||
this.camera = new System.Windows.Forms.PictureBox();
|
||||
this.btnTestOnvifSnapshot = new System.Windows.Forms.Button();
|
||||
((System.ComponentModel.ISupportInitialize)(this.LedImage)).BeginInit();
|
||||
this.statusStrip1.SuspendLayout();
|
||||
this.groupBox1.SuspendLayout();
|
||||
@@ -254,11 +255,23 @@ namespace Test
|
||||
this.camera.TabIndex = 14;
|
||||
this.camera.TabStop = false;
|
||||
//
|
||||
// btnTestOnvifSnapshot
|
||||
//
|
||||
this.btnTestOnvifSnapshot.Location = new System.Drawing.Point(811, 604);
|
||||
this.btnTestOnvifSnapshot.Margin = new System.Windows.Forms.Padding(4, 4, 4, 4);
|
||||
this.btnTestOnvifSnapshot.Name = "btnTestOnvifSnapshot";
|
||||
this.btnTestOnvifSnapshot.Size = new System.Drawing.Size(148, 39);
|
||||
this.btnTestOnvifSnapshot.TabIndex = 15;
|
||||
this.btnTestOnvifSnapshot.Text = "10. ONVIF抓图测试";
|
||||
this.btnTestOnvifSnapshot.UseVisualStyleBackColor = true;
|
||||
this.btnTestOnvifSnapshot.Click += new System.EventHandler(this.btnTestOnvifSnapshot_Click);
|
||||
//
|
||||
// Form1
|
||||
//
|
||||
this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 15F);
|
||||
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
|
||||
this.ClientSize = new System.Drawing.Size(1067, 718);
|
||||
this.Controls.Add(this.btnTestOnvifSnapshot);
|
||||
this.Controls.Add(this.camera);
|
||||
this.Controls.Add(this.groupBox1);
|
||||
this.Controls.Add(this.statusStrip1);
|
||||
@@ -312,5 +325,6 @@ namespace Test
|
||||
private System.Windows.Forms.TextBox txtIpAddress;
|
||||
private System.Windows.Forms.Button btnSetIP;
|
||||
private System.Windows.Forms.PictureBox camera;
|
||||
private System.Windows.Forms.Button btnTestOnvifSnapshot = null;
|
||||
}
|
||||
}
|
||||
@@ -5,6 +5,7 @@ using System.Data;
|
||||
using System.Drawing;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Net;
|
||||
using System.Text;
|
||||
using System.Threading;
|
||||
using System.Windows.Forms;
|
||||
@@ -778,5 +779,100 @@ namespace Test
|
||||
txtLog.AppendText($"[{timestamp}] {message}" + Environment.NewLine);
|
||||
txtLog.ScrollToCaret();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 10. ONVIF抓图测试
|
||||
/// </summary>
|
||||
private void btnTestOnvifSnapshot_Click(object sender, EventArgs e)
|
||||
{
|
||||
try
|
||||
{
|
||||
LogMessage("正在测试ONVIF抓图...");
|
||||
|
||||
var imageStream = GetSnapshot();
|
||||
if (imageStream != null)
|
||||
{
|
||||
// 显示捕获的图像
|
||||
using (var image = System.Drawing.Image.FromStream(imageStream))
|
||||
{
|
||||
// 清理之前的图像
|
||||
if (LedImage.Image != null)
|
||||
{
|
||||
var oldImage = LedImage.Image;
|
||||
LedImage.Image = null;
|
||||
oldImage.Dispose();
|
||||
}
|
||||
|
||||
LedImage.Image = (System.Drawing.Image)image.Clone();
|
||||
LedImage.SizeMode = PictureBoxSizeMode.Zoom;
|
||||
LogMessage("ONVIF抓图成功!");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
LogMessage("ONVIF抓图失败: 无法获取图像流");
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
LogMessage($"ONVIF抓图错误: {ex.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 使用ONVIF协议获取快照
|
||||
/// </summary>
|
||||
public System.IO.Stream GetSnapshot()
|
||||
{
|
||||
var request = (HttpWebRequest)WebRequest.Create("http://192.168.100.10:8899/cgi-bin/snapshot.cgi");
|
||||
request.Method = "POST";
|
||||
request.ContentType = "application/json";
|
||||
request.Timeout = 10000; // 10秒超时
|
||||
request.ReadWriteTimeout = 10000;
|
||||
|
||||
// 添加认证信息
|
||||
string username = "admin";
|
||||
string password = "";
|
||||
string credentials = Convert.ToBase64String(Encoding.ASCII.GetBytes($"{username}:{password}"));
|
||||
request.Headers["Authorization"] = $"Basic {credentials}";
|
||||
|
||||
// 发送JSON格式的POST数据
|
||||
string postData = "{\"action\": \"snapshot\"}";
|
||||
byte[] data = Encoding.UTF8.GetBytes(postData);
|
||||
request.ContentLength = data.Length;
|
||||
|
||||
using (var stream = request.GetRequestStream())
|
||||
{
|
||||
stream.Write(data, 0, data.Length);
|
||||
}
|
||||
|
||||
// 发送请求
|
||||
var response = (HttpWebResponse)request.GetResponse();
|
||||
if (response.StatusCode == HttpStatusCode.OK)
|
||||
{
|
||||
// 检查响应内容类型
|
||||
string contentType = response.ContentType;
|
||||
if (contentType.StartsWith("image/"))
|
||||
{
|
||||
// 返回图片二进制流
|
||||
return response.GetResponseStream();
|
||||
}
|
||||
else
|
||||
{
|
||||
// 读取JSON错误信息
|
||||
using (var reader = new StreamReader(response.GetResponseStream()))
|
||||
{
|
||||
string errorMessage = reader.ReadToEnd();
|
||||
response.Close();
|
||||
throw new Exception($"ONVIF抓图失败: {errorMessage}");
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
response.Close();
|
||||
throw new Exception($"ONVIF抓图失败,状态码: {response.StatusCode}");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user