记萤石云摄像头保存截图的一个方法(c#)
·
获取截图接口
引入模块
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);
//获取token
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);//此时可以将ImgBit存入sqlserver数据库,字段类型为image
string savepath = GetMethodImage(pathFile, ImgBit); //调用保存图片方法,返回保存路径
}
else
{
login = "0";
}
}
catch (Exception ex)
{
login = "0";
}
魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。
更多推荐


所有评论(0)