记萤石云摄像头保存截图的一个方法(c#)

记萤石云摄像头保存截图的一个方法(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); 
    //获取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";
        }
   
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值