记萤石云摄像头保存截图的一个方法(c#)-->20230320
获取截图接口
引入模块
using System;
using System.Data;
using System.ServiceProcess;
using System.Text;
using System.IO;
using System.Configuration;
using System.Net;
using System.Data.SqlClient;
using System.Web.Script.Serialization;
方法编写
private string GetMethodImage(string pathFile, byte[] imgbit)
{
if (!Directory.Exists(pathFile))
{
Directory.CreateDirectory(pathFile);
}
pathFile += $@"{DateTime.Now.ToString("yyMMddHHmmss")}.jpg";
WriteBytesToFile(pathFile, imgbit);
return pathFile;
}
public static void WriteBytesToFile(string fileName, byte[] content)
{
FileStream fs = new FileStream(fileName, FileMode.Create);
BinaryWriter w = new BinaryWriter(fs);
try
{
w.Write(content);
}
finally
{
fs.Close();
w.Close();
}
}
public static string TPost(string Url)
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);
request.Method = "POST";
request.ContentType = "application/json";
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
string encoding = response.ContentEncoding;
if (encoding == null || encoding.Length < 1)
{
encoding = "UTF-8";
}
StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding(encoding));
string retString = reader.ReadToEnd();
return retString;
}
public class Message
{
public string code { get; set; }
public string msg { get; set; }
public Data data { get; set; }
}
public class Data
{
public string accessToken { get; set; }
public string expireTime { get; set; }
public string picUrl { get; set; }
}
static Byte[] GetUrlToImage(string Url)
{
System.Net.WebRequest WebRequest = System.Net.WebRequest.Create(Url);
WebRequest.Method = "GET";
Byte[] filebyteArray;
try
{
using (WebResponse webRes = WebRequest.GetResponse())
{
int length = (int)webRes.ContentLength;
HttpWebResponse response = webRes as HttpWebResponse;
Stream stream = response.GetResponseStream();
MemoryStream MemoryStream = new MemoryStream();
byte[] buffer = new byte[length];
int i;
while ((i = stream.Read(buffer, 0, buffer.Length)) > 0)
{
MemoryStream.Write(buffer, 0, i);
}
filebyteArray = MemoryStream.ToArray();
MemoryStream.Close();
return filebyteArray;
}
}
catch {
return null;
}
}
方法调用
string STCD=“90000000”;
string pathFile = “C:\Users\Desktop\testpic” + string.Format("\{0}\{1}\{2}\", DateTime.Now.ToString(“yyyy”), DateTime.Now.ToString(“MMdd”),STCD);
string login = "0";
string token = "";
string picurl = "";
string source = "";
string Serial = "you Serial"; 替换你的萤石云摄像头信息
string appKey = "you appKey";替换你的萤石云摄像头信息
string appSecret = "you appSecret";替换你的萤石云摄像头信息
byte[] ImgBit = null;
try
{
if (login == "0")
{
source = TPost("https://open.ys7.com/api/lapp/token/get?appKey=" +appKey + "&appSecret=" + appSecret);
JavaScriptSerializer js = new JavaScriptSerializer();
Message str = js.Deserialize<Message>(source);
if (str.code == "200")
{
token = str.data.accessToken;
login = "1";
}
else
{
login = "0";
}
}
if (login == "1")
{
source = TPost("https://open.ys7.com/api/lapp/device/capture?accessToken=" + token + "&channelNo=1" + "&deviceSerial=" +Serial);
JavaScriptSerializer js = new JavaScriptSerializer();
Message str = js.Deserialize<Message>(source);
if (str.code == "200")
{
picurl = str.data.picUrl;
ImgBit = GetUrlToImage(picurl);
string savepath = GetMethodImage(pathFile, ImgBit);
}
else
{
login = "0";
}
}
catch (Exception ex)
{
login = "0";
}