记萤石云摄像头保存截图的一个方法(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";
        }
   
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,实现安卓app调取萤石摄像头需要用到萤石SDK。下面是一个简单的示例: 1. 在萤石开发者中心申请AppKey和AppSecret,以获取SDK的使用权限。 2. 将萤石SDK添加到您的项目中。您可以通过Gradle添加以下依赖: ```groovy implementation 'com.videogo:ezopenSDK:5.0.0' ``` 3. 在您的代码中调用SDK接口,实现调取摄像头的功能。以下是一个示例代码: ```java import com.videogo.openapi.EZOpenSDK; import com.videogo.openapi.bean.EZAccessToken; import com.videogo.openapi.bean.EZCameraInfo; import com.videogo.openapi.callback.OnEZAccessTokenCallback; import com.videogo.util.LogUtil; public class MainActivity extends AppCompatActivity { private static final String TAG = "MainActivity"; private static final String APP_KEY = "您的AppKey"; private static final String APP_SECRET = "您的AppSecret"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // 初始化EZOpenSDK EZOpenSDK.showSDKLog(true); EZOpenSDK.enableP2P(true); EZOpenSDK.initLib(this, APP_KEY); // 获取AccessToken EZOpenSDK.getInstance().openLoginPage(new OnEZAccessTokenCallback() { @Override public void onAccessTokenResult(final EZAccessToken ezAccessToken) { // 获取AccessToken成功 LogUtil.i(TAG, "onAccessTokenResult: " + ezAccessToken.getAccessToken()); // 获取摄像头信息 EZOpenSDK.getInstance().getCameraInfo("您的摄像头序列号", new OnGetCameraInfoListener() { @Override public void onGetCameraInfoSuccess(EZCameraInfo ezCameraInfo) { // 获取摄像头信息成功 LogUtil.i(TAG, "onGetCameraInfoSuccess: " + ezCameraInfo.getCameraName()); // 打开实时预览页面 EZOpenSDK.getInstance().openCameraLivePlayPage(ezCameraInfo.getCameraId(), 1); } @Override public void onGetCameraInfoError(ErrorInfo errorInfo) { // 获取摄像头信息失败 LogUtil.e(TAG, "onGetCameraInfoError: " + errorInfo.getErrorCode()); } }); } @Override public void onAccessTokenError(ErrorInfo errorInfo) { // 获取AccessToken失败 LogUtil.e(TAG, "onAccessTokenError: " + errorInfo.getErrorCode()); } }, APP_KEY, APP_SECRET); } @Override protected void onDestroy() { super.onDestroy(); // 释放EZOpenSDK资源 EZOpenSDK.getInstance().releaseLib(); } } ``` 这段代码的作用是获取萤石的AccessToken,然后获取指定摄像头的信息,最后打开实时预览页面。请将代码中的“您的AppKey”、“您的AppSecret”和“您的摄像头序列号”替换为实际的值。 需要注意的是,打开实时预览页面需要相应的权限,例如网络访问和摄像头访问权限。您需要在AndroidManifest.xml文件中添加以下权限声明: ```xml <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.CAMERA" /> ``` 这样,就可以实现安卓app调取萤石摄像头的功能了。如果您需要添加更多的功能,例如录制视频或拍照等,可以参考萤石SDK相关文档。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值