Browse Source

添加“转图转Base64”功能

pull/1/head
fajiao 3 years ago
parent
commit
8a8612e236
  1. 6
      BaseModule/EC.Helper/Common/ConfigHelper.cs
  2. 12
      BaseModule/EC.Helper/Common/FileHelper.cs
  3. 73
      BaseModule/EC.Helper/Common/JsonHelper.cs
  4. 1
      BaseModule/EC.Helper/EC.Helper.csproj
  5. 10
      BaseModule/EC.Onvif/OnvifClient.cs
  6. 6
      BusinessModule/EC.App.ThatBLL/Onvif/IMediaBLL.cs
  7. 38
      BusinessModule/EC.App.ThatBLL/Onvif/Impl/MediaBLL.cs
  8. 4
      OnvifSocketServer/Manager/AppConfig.cs
  9. 1
      OnvifSocketServer/OnvifSocketServer.csproj
  10. 74
      OnvifSocketServer/Program.cs
  11. 2
      OnvifSocketServer/appsettings.json
  12. 12
      OnvifWebServer/.config/dotnet-tools.json
  13. 20
      OnvifWebServer/Controllers/Onvif/MediaController.cs

6
BaseModule/EC.Helper/FileExt/JsonHelper.cs → BaseModule/EC.Helper/Common/ConfigHelper.cs

@ -1,14 +1,14 @@
using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Configuration;
using System.IO; using System.IO;
namespace EC.Helper.FileExt namespace EC.Helper.Common
{ {
public class JsonHelper public class ConfigHelper
{ {
public static IConfiguration ReadConfiguration(string configPath) public static IConfiguration ReadConfiguration(string configPath)
{ {
ConfigurationBuilder builder = new(); ConfigurationBuilder builder = new();
if (!File.Exists(configPath)) { return builder.Build(); } if (!File.Exists(configPath)) { return null; }
builder.AddJsonFile(configPath); builder.AddJsonFile(configPath);
return builder.Build(); return builder.Build();
} }

12
BaseModule/EC.Helper/Common/FileHelper.cs

@ -0,0 +1,12 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace EC.Helper.Common
{
public class FileHelper
{
}
}

73
BaseModule/EC.Helper/Common/JsonHelper.cs

@ -0,0 +1,73 @@
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.Collections.Generic;
namespace EC.Helper.Common
{
public class JsonHelper
{
/// <summary>
/// Object To Json
/// </summary>
/// <param name="obj"></param>
/// <returns></returns>
public static string ToJson(object obj)
{
return JsonConvert.SerializeObject(obj);
}
/// <summary>
/// Json To Object
/// </summary>
/// <param name="json"></param>
/// <returns></returns>
public static object ToObject(string json)
{
return !string.IsNullOrEmpty(json) ? JsonConvert.DeserializeObject(json) : default;
}
/// <summary>
/// Json To T Object
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="json"></param>
/// <returns></returns>
public static T ToObject<T>(string json) where T : class
{
return !string.IsNullOrEmpty(json) ? JsonConvert.DeserializeObject<T>(json) : default;
}
/// <summary>
/// Json To JObject
/// </summary>
/// <param name="json"></param>
/// <returns></returns>
public static JObject ToJObject(string json)
{
return ToObject<JObject>(json);
}
/// <summary>
/// Json To Object List
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="json"></param>
/// <returns></returns>
public static List<T> ToList<T>(string json) where T : class
{
return ToObject<List<T>>(json);
}
/// <summary>
/// 深克隆
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="obj"></param>
/// <returns></returns>
public static T DeepClone<T>(T obj) where T : class
{
string json = ToJson(obj);
return ToObject<T>(json);
}
}
}

1
BaseModule/EC.Helper/EC.Helper.csproj

@ -7,6 +7,7 @@
<ItemGroup> <ItemGroup>
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="6.0.0" /> <PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="6.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="6.0.0" /> <PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="6.0.0" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
</ItemGroup> </ItemGroup>
</Project> </Project>

10
BaseModule/EC.Onvif/OnvifClient.cs

@ -13,15 +13,15 @@ namespace EC.Onvif
{ {
#region Attr #region Attr
private string Hostname { get; set; } public string Hostname { get; private set; }
private string Username { get; set; } public string Username { get; private set; }
private string Password { get; set; } public string Password { get; private set; }
public static float atomDist { get; set; } = 0.01f; public static float atomDist { get; private set; } = 0.01f;
public static float atomSpeed { get; set; } = 0.1f; public static float atomSpeed { get; private set; } = 0.1f;
#endregion Attr #endregion Attr

6
BusinessModule/EC.App.ThatBLL/Onvif/IMediaBLL.cs

@ -13,8 +13,10 @@ namespace EC.App.ThatBLL.Onvif
/// <returns></returns> /// <returns></returns>
bool IsConnected(string ip); bool IsConnected(string ip);
Task<string> GetStreamUri(string ip); Task<string> GetStreamUrl(string ip);
Task<string> GetSnapshotUri(string ip); Task<string> GetSnapshotUrl(string ip);
Task<string> GetSnapshot(string ip);
} }
} }

38
BusinessModule/EC.App.ThatBLL/Onvif/Impl/MediaBLL.cs

@ -1,5 +1,8 @@
using EC.App.ThatService.Onvif.Impl; using EC.App.ThatService.Onvif.Impl;
using EC.Onvif; using EC.Onvif;
using System;
using System.IO;
using System.Net;
using System.Threading.Tasks; using System.Threading.Tasks;
namespace EC.App.ThatBLL.Onvif.Impl namespace EC.App.ThatBLL.Onvif.Impl
@ -20,20 +23,43 @@ namespace EC.App.ThatBLL.Onvif.Impl
return ret; return ret;
} }
public async Task<string> GetStreamUri(string ip) public async Task<string> GetStreamUrl(string ip)
{ {
bool flag = _ocService.TryGet(ip, out OnvifClient onvifClient); bool flag = _ocService.TryGet(ip, out OnvifClient onvifClient);
if (!flag) return string.Empty; if (!flag) return string.Empty;
string uri = await onvifClient.GetStreamUri(); string url = await onvifClient.GetStreamUri();
return uri; return url;
} }
public async Task<string> GetSnapshotUri(string ip) public async Task<string> GetSnapshotUrl(string ip)
{ {
bool flag = _ocService.TryGet(ip, out OnvifClient onvifClient); bool flag = _ocService.TryGet(ip, out OnvifClient onvifClient);
if (!flag) return string.Empty; if (!flag) return string.Empty;
string uri = await onvifClient.GetSnapshotUri(); string url = await onvifClient.GetSnapshotUri();
return uri; return url;
}
public async Task<string> GetSnapshot(string ip)
{
bool flag = _ocService.TryGet(ip, out OnvifClient onvifClient);
if (!flag) return string.Empty;
string url = await onvifClient.GetSnapshotUri();
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Method = "GET";
request.PreAuthenticate = true;
request.Credentials = new NetworkCredential(onvifClient.Username, onvifClient.Password);
using HttpWebResponse response = (HttpWebResponse)request.GetResponse();
using Stream stream = response.GetResponseStream();
using MemoryStream mStream = new();
byte[] buffer = new byte[1024];
int byteCount;
do
{
byteCount = stream.Read(buffer, 0, buffer.Length);
mStream.Write(buffer, 0, byteCount);
} while (byteCount > 0);
mStream.Position = 0;
return Convert.ToBase64String(mStream.ToArray());
} }
} }
} }

4
OnvifSocketServer/Manager/AppConfig.cs

@ -1,4 +1,4 @@
using EC.Helper.FileExt; using EC.Helper.Common;
using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Configuration;
using System.IO; using System.IO;
@ -17,7 +17,7 @@ namespace OnvifSocketServer
if (config == null) if (config == null)
{ {
string configPath = Path.Combine(Directory.GetCurrentDirectory(), "appsettings.json"); string configPath = Path.Combine(Directory.GetCurrentDirectory(), "appsettings.json");
config = JsonHelper.ReadConfiguration(configPath); config = ConfigHelper.ReadConfiguration(configPath);
} }
return config; return config;
} }

1
OnvifSocketServer/OnvifSocketServer.csproj

@ -10,6 +10,7 @@
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ProjectReference Include="..\BaseModule\EC.App\EC.App.csproj" />
<ProjectReference Include="..\BaseModule\EC.Helper\EC.Helper.csproj" /> <ProjectReference Include="..\BaseModule\EC.Helper\EC.Helper.csproj" />
</ItemGroup> </ItemGroup>

74
OnvifSocketServer/Program.cs

@ -1,7 +1,10 @@
using Flurl; using EC.App.Entity;
using Flurl;
using Flurl.Http; using Flurl.Http;
using Newtonsoft.Json.Linq; using Newtonsoft.Json.Linq;
using System; using System;
using System.IO;
using System.Net;
using System.Net.Http; using System.Net.Http;
namespace OnvifSocketServer namespace OnvifSocketServer
@ -21,58 +24,69 @@ namespace OnvifSocketServer
string onvifHttpsUrl = AppConfig.GetOnvifHttpsUrl(); string onvifHttpsUrl = AppConfig.GetOnvifHttpsUrl();
Uri baseUri = new Uri(onvifHttpUrl); Uri baseUri = new Uri(onvifHttpUrl);
JObject result; JObject result;
Console.WriteLine(onvifHttpUrl);
// Get result = Request<JObject>(baseUri, "onvif/Curd/IsExist", HttpMethod.Get, new { ip = "192.168.1.65" });
result = baseUri.AbsoluteUri result = Request<JObject>(baseUri, "onvif/Curd/Add", HttpMethod.Post,
.AppendPathSegment("onvif/Curd/IsExist") new { ip = "192.168.1.65", username = "admin", password = "hk123456" });
.SetQueryParams(new { ip = "192.168.1.108" }) result = Request<JObject>(baseUri, "onvif/Media/GetStreamUri", HttpMethod.Get, new { ip = "192.168.1.65" });
.GetAsync() result = Request<JObject>(baseUri, "onvif/Media/GetSnapshotUri", HttpMethod.Get, new { ip = "192.168.1.65" });
.ReceiveJson<JObject>().Result; DownloadSnapShot(result);
Console.WriteLine(result);
// Post // 192.168.1.108
result = baseUri.AbsoluteUri //result = Request<JObject>(baseUri, "onvif/Curd/Add", HttpMethod.Post,
.AppendPathSegment("onvif/Curd/Add") // new { ip = "192.168.1.108", username = "admin", password = "hk123456" });
.SetQueryParams(new { ip = "192.168.1.65", username = "admin", password = "hk123456" }) //result = Request<JObject>(baseUri, "onvif/Media/GetStreamUri", HttpMethod.Get, new { ip = "192.168.1.108" });
.PostAsync() //result = Request<JObject>(baseUri, "onvif/Media/GetSnapshotUri", HttpMethod.Get, new { ip = "192.168.1.108" });
.ReceiveJson<JObject>().Result; }
Console.WriteLine(result);
result = Request(baseUri, "onvif/Media/GetStreamUri", HttpMethod.Get, new { ip = "192.168.1.65" }); private static void DownloadSnapShot(JObject jObj)
result = Request(baseUri, "onvif/Media/GetSnapshotUri", HttpMethod.Get, new { ip = "192.168.1.65" }); {
RespParam<object> result = jObj.ToObject<RespParam<object>>();
string snapshotUrl = result.Data.ToString();
if (string.IsNullOrEmpty(snapshotUrl)) return;
// Post HttpWebRequest request = (HttpWebRequest)WebRequest.Create(snapshotUrl);
result = Request(baseUri, "onvif/Curd/Add", HttpMethod.Post, request.Method = "GET";
new { ip = "192.168.1.108", username = "admin", password = "hk123456" }); request.PreAuthenticate = true;
request.Credentials = new NetworkCredential("admin", "hk123456");
result = Request(baseUri, "onvif/Media/GetStreamUri", HttpMethod.Get, new { ip = "192.168.1.108" }); using HttpWebResponse response = (HttpWebResponse)request.GetResponse();
result = Request(baseUri, "onvif/Media/GetSnapshotUri", HttpMethod.Get, new { ip = "192.168.1.108" }); using Stream stream = response.GetResponseStream();
using MemoryStream mStream = new();
byte[] buffer = new byte[1024];
int byteCount;
do
{
byteCount = stream.Read(buffer, 0, buffer.Length);
mStream.Write(buffer, 0, byteCount);
} while (byteCount > 0);
mStream.Position = 0;
string base64Str = Convert.ToBase64String(mStream.ToArray());
} }
public static JObject Request(Uri uri, string relativePath, HttpMethod httpMethod, object data) public static T Request<T>(Uri uri, string relativePath, HttpMethod httpMethod, object data)
{ {
Url url = uri.AbsoluteUri.AppendPathSegment(relativePath).SetQueryParams(data); Url url = uri.AbsoluteUri.AppendPathSegment(relativePath).SetQueryParams(data);
JObject result; T result;
if (httpMethod == HttpMethod.Get) if (httpMethod == HttpMethod.Get)
{ {
result = url.GetAsync().ReceiveJson<JObject>().Result; result = url.GetAsync().ReceiveJson<T>().Result;
} }
else if (httpMethod == HttpMethod.Post) else if (httpMethod == HttpMethod.Post)
{ {
result = url.PostAsync().ReceiveJson<JObject>().Result; result = url.PostAsync().ReceiveJson<T>().Result;
} }
else if (httpMethod == HttpMethod.Put) else if (httpMethod == HttpMethod.Put)
{ {
result = url.PutAsync().ReceiveJson<JObject>().Result; ; result = url.PutAsync().ReceiveJson<T>().Result; ;
} }
else if (httpMethod == HttpMethod.Delete) else if (httpMethod == HttpMethod.Delete)
{ {
result = url.DeleteAsync().ReceiveJson<JObject>().Result; ; result = url.DeleteAsync().ReceiveJson<T>().Result; ;
} }
else else
{ {
result = null; result = default;
} }
Console.WriteLine(result); Console.WriteLine(result);
return result; return result;

2
OnvifSocketServer/appsettings.json

@ -1,6 +1,6 @@
{ {
"Apis": { "Apis": {
"onvif_http_url": "http://localhost:6000", "onvif_http_url": "http://192.168.1.119:10011",
"onvif_https_url": "https://localhost:6001" "onvif_https_url": "https://localhost:6001"
} }
} }

12
OnvifWebServer/.config/dotnet-tools.json

@ -0,0 +1,12 @@
{
"version": 1,
"isRoot": true,
"tools": {
"dotnet-ef": {
"version": "6.0.2",
"commands": [
"dotnet-ef"
]
}
}
}

20
OnvifWebServer/Controllers/Onvif/MediaController.cs

@ -48,8 +48,8 @@ namespace OnvifWebServer.Controllers.Onvif
public async Task<string> GetStreamUri( public async Task<string> GetStreamUri(
[Required][DataValidation(ValidationTypes.IPv4)] string ip) [Required][DataValidation(ValidationTypes.IPv4)] string ip)
{ {
string uri = await _mediaBLL.GetStreamUri(ip); string url = await _mediaBLL.GetStreamUrl(ip);
return uri; return url;
} }
/// <summary> /// <summary>
@ -61,8 +61,20 @@ namespace OnvifWebServer.Controllers.Onvif
public async Task<string> GetSnapshotUri( public async Task<string> GetSnapshotUri(
[Required][DataValidation(ValidationTypes.IPv4)] string ip) [Required][DataValidation(ValidationTypes.IPv4)] string ip)
{ {
string uri = await _mediaBLL.GetSnapshotUri(ip); string url = await _mediaBLL.GetSnapshotUrl(ip);
return uri; return url;
}
/// <summary>
/// 获取抓图 Base64
/// </summary>
/// <param name="ip"></param>
/// <returns></returns>
public async Task<string> GetSnapshot(
[Required][DataValidation(ValidationTypes.IPv4)] string ip)
{
string snapshotBase64 = await _mediaBLL.GetSnapshot(ip);
return snapshotBase64;
} }
} }
} }
Loading…
Cancel
Save