20 changed files with 3581 additions and 0 deletions
@ -0,0 +1,31 @@ |
|||||||
|
|
||||||
|
Microsoft Visual Studio Solution File, Format Version 12.00 |
||||||
|
# Visual Studio Version 17 |
||||||
|
VisualStudioVersion = 17.9.34622.214 |
||||||
|
MinimumVisualStudioVersion = 10.0.40219.1 |
||||||
|
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DevicesService", "DevicesService\DevicesService.csproj", "{5299A736-BD76-47C4-8B3C-C6D396D7D200}" |
||||||
|
EndProject |
||||||
|
Global |
||||||
|
GlobalSection(SolutionConfigurationPlatforms) = preSolution |
||||||
|
Debug|Any CPU = Debug|Any CPU |
||||||
|
Debug|x86 = Debug|x86 |
||||||
|
Release|Any CPU = Release|Any CPU |
||||||
|
Release|x86 = Release|x86 |
||||||
|
EndGlobalSection |
||||||
|
GlobalSection(ProjectConfigurationPlatforms) = postSolution |
||||||
|
{5299A736-BD76-47C4-8B3C-C6D396D7D200}.Debug|Any CPU.ActiveCfg = Debug|x86 |
||||||
|
{5299A736-BD76-47C4-8B3C-C6D396D7D200}.Debug|Any CPU.Build.0 = Debug|x86 |
||||||
|
{5299A736-BD76-47C4-8B3C-C6D396D7D200}.Debug|x86.ActiveCfg = Debug|x86 |
||||||
|
{5299A736-BD76-47C4-8B3C-C6D396D7D200}.Debug|x86.Build.0 = Debug|x86 |
||||||
|
{5299A736-BD76-47C4-8B3C-C6D396D7D200}.Release|Any CPU.ActiveCfg = Release|Any CPU |
||||||
|
{5299A736-BD76-47C4-8B3C-C6D396D7D200}.Release|Any CPU.Build.0 = Release|Any CPU |
||||||
|
{5299A736-BD76-47C4-8B3C-C6D396D7D200}.Release|x86.ActiveCfg = Release|x86 |
||||||
|
{5299A736-BD76-47C4-8B3C-C6D396D7D200}.Release|x86.Build.0 = Release|x86 |
||||||
|
EndGlobalSection |
||||||
|
GlobalSection(SolutionProperties) = preSolution |
||||||
|
HideSolutionNode = FALSE |
||||||
|
EndGlobalSection |
||||||
|
GlobalSection(ExtensibilityGlobals) = postSolution |
||||||
|
SolutionGuid = {3745E484-8939-422B-90A5-CBFB608C07E7} |
||||||
|
EndGlobalSection |
||||||
|
EndGlobal |
@ -0,0 +1,10 @@ |
|||||||
|
<?xml version="1.0" encoding="utf-8" ?> |
||||||
|
<configuration> |
||||||
|
<appSettings> |
||||||
|
<!--这里key和valu是以键值对存储,可以使用key获得对应value的值--> |
||||||
|
<add key="COMName" value="COM4"/> |
||||||
|
<!--高拍仪图片旋转方向逆时针为例--> |
||||||
|
<add key="xzds" value="90"/> |
||||||
|
<add key="ysb" value="10"/> |
||||||
|
</appSettings> |
||||||
|
</configuration> |
@ -0,0 +1,366 @@ |
|||||||
|
|
||||||
|
using DevicesService.Commen; |
||||||
|
using Functions.FileExt; |
||||||
|
using NAudio.CoreAudioApi; |
||||||
|
using Newtonsoft.Json; |
||||||
|
using Newtonsoft.Json.Linq; |
||||||
|
using SkiaSharp; |
||||||
|
using System; |
||||||
|
using System.Buffers.Text; |
||||||
|
using System.Collections.Generic; |
||||||
|
using System.ComponentModel.Design; |
||||||
|
using System.Configuration; |
||||||
|
using System.Drawing; |
||||||
|
using System.Globalization; |
||||||
|
using System.IO; |
||||||
|
using System.IO.Ports; |
||||||
|
using System.Linq; |
||||||
|
using System.Net.Http; |
||||||
|
using System.Text; |
||||||
|
using System.Threading; |
||||||
|
using System.Threading.Tasks; |
||||||
|
using System.Timers; |
||||||
|
using System.Xml.Linq; |
||||||
|
using static System.Net.Mime.MediaTypeNames; |
||||||
|
using Timer = System.Timers.Timer; |
||||||
|
namespace DevicesService.Common |
||||||
|
{ |
||||||
|
public class COMUtils |
||||||
|
{ |
||||||
|
private SerialPort serialPort = new SerialPort(); |
||||||
|
private ScriptCallbackObject scriptCallback = new ScriptCallbackObject(); |
||||||
|
private int maxChunkSize = 10240; |
||||||
|
public string _json = string.Empty; |
||||||
|
public COMUtils() |
||||||
|
{ |
||||||
|
try |
||||||
|
{ |
||||||
|
string COMName = ConfigurationManager.AppSettings["COMName"].ToString(); |
||||||
|
// 设置COM口,波特率,奇偶校验,数据位,停止位 |
||||||
|
serialPort.PortName = COMName; // 请替换为你的串口名称 |
||||||
|
serialPort.BaudRate = 115200; // 设置波特率 |
||||||
|
serialPort.Parity = Parity.None; |
||||||
|
serialPort.DataBits = 8; |
||||||
|
//serialPort.WriteBufferSize =40960; |
||||||
|
// serialPort.ReadBufferSize = 40960; |
||||||
|
serialPort.StopBits = StopBits.One; |
||||||
|
serialPort.Handshake = Handshake.None; |
||||||
|
serialPort.Encoding = Encoding.UTF8; // 设置正确的编码 |
||||||
|
serialPort.DtrEnable = true; //启用控制终端就续信号 |
||||||
|
//serialPort.RtsEnable = true; //启用请求发送信号 |
||||||
|
serialPort.NewLine = "\n"; |
||||||
|
serialPort.DataReceived += new SerialDataReceivedEventHandler(DataReceivedHandler); |
||||||
|
if (!serialPort.IsOpen) |
||||||
|
{ |
||||||
|
serialPort.Open(); |
||||||
|
} |
||||||
|
Timer timer = new Timer(1000);//1秒钟的时间间隔 |
||||||
|
timer.Elapsed += OnTimedEvent; |
||||||
|
timer.AutoReset = true;//重复执行 |
||||||
|
timer.Enabled = true;//启动定时器 |
||||||
|
Log.Info("硬件服务启动成功"); |
||||||
|
} |
||||||
|
catch (Exception ex) |
||||||
|
{ |
||||||
|
Log.Error("服务启动异常: " + ex.Message + ""); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
/// <summary> |
||||||
|
/// 接受数据 |
||||||
|
/// </summary> |
||||||
|
/// <param name="sender"></param> |
||||||
|
/// <param name="e"></param> |
||||||
|
public static string bsid = string.Empty; |
||||||
|
public static string bsext = string.Empty; |
||||||
|
public static string bspath = string.Empty; |
||||||
|
public static bool sfjswc = false; |
||||||
|
private void DataReceivedHandler(object sender, SerialDataReceivedEventArgs e) |
||||||
|
{ |
||||||
|
try |
||||||
|
{ |
||||||
|
SerialPort sp = (SerialPort)sender; |
||||||
|
string indata = sp.ReadExisting(); |
||||||
|
if (!string.IsNullOrEmpty(indata)) |
||||||
|
{ |
||||||
|
if (indata.Contains("\n")) |
||||||
|
{ |
||||||
|
indata = _json + indata; |
||||||
|
string result = Util.Base64str2(indata); |
||||||
|
Log.Info("接收到COM口传来数据:" + result); |
||||||
|
if (!string.IsNullOrEmpty(result)) |
||||||
|
{ |
||||||
|
JObject jo = (JObject)JsonConvert.DeserializeObject(result); |
||||||
|
int type = Convert.ToInt32(jo["type"].ToString()); |
||||||
|
string param = jo["param"].ToString(); |
||||||
|
string resultback = string.Empty; |
||||||
|
string base64 = string.Empty; |
||||||
|
string data = string.Empty; |
||||||
|
string callback = jo["callback"].ToString(); |
||||||
|
JObject joparam; |
||||||
|
switch (type) |
||||||
|
{ |
||||||
|
//{"type":"1","param":{"data":""}} |
||||||
|
case Func.IDCardRead: |
||||||
|
#region 读取身份证 |
||||||
|
joparam = (JObject)JsonConvert.DeserializeObject(param); |
||||||
|
data = joparam["data"].ToString(); |
||||||
|
resultback = scriptCallback.IDCardRead(data, callback); |
||||||
|
//Console.WriteLine(resultback); |
||||||
|
Log.Info("读取身份证 返回数据:" + resultback); |
||||||
|
base64 = Util.str2Base64(resultback); |
||||||
|
//向com对方发送数据 |
||||||
|
SendData(base64); |
||||||
|
#endregion |
||||||
|
break; |
||||||
|
//{"type":"2","param":{"ph":"A012","ddrs":"10","qrcode":"www.baidu.com/s?wd=国家赔偿","ywmc":"国家赔偿"}} |
||||||
|
case Func.SendByPrint: |
||||||
|
#region 打印排队票据 |
||||||
|
joparam = (JObject)JsonConvert.DeserializeObject(param); |
||||||
|
string ph = joparam["ph"].ToString(); |
||||||
|
string ddrs = joparam["ddrs"].ToString(); |
||||||
|
string qrcode = joparam["qrcode"].ToString(); |
||||||
|
string ywmc = joparam["ywmc"].ToString(); |
||||||
|
resultback = scriptCallback.SendByPrint(ph, ddrs, qrcode, ywmc, callback); |
||||||
|
Log.Info("打印排队票据 返回数据:" + resultback); |
||||||
|
base64 = Util.str2Base64(resultback); |
||||||
|
//向com对方发送数据 |
||||||
|
SendData(base64); |
||||||
|
#endregion |
||||||
|
break; |
||||||
|
//{"type":"4","param":{"content":"欢迎使用阿凯思控诉业务一体机","phone":"13333333333"}} |
||||||
|
case Func.SendSSM: |
||||||
|
#region 发送短信 |
||||||
|
joparam = (JObject)JsonConvert.DeserializeObject(param); |
||||||
|
string content = joparam["content"].ToString(); |
||||||
|
string phone = joparam["phone"].ToString(); |
||||||
|
resultback = scriptCallback.SendSSM(content, phone, callback); |
||||||
|
Log.Info("发送短信 返回数据:" + resultback); |
||||||
|
base64 = Util.str2Base64(resultback); |
||||||
|
//向com对方发送数据 |
||||||
|
SendData(base64); |
||||||
|
#endregion |
||||||
|
break; |
||||||
|
//{"type":"5","param":{"url":"http://192.168.0.34:92/api/UploadFP/UploadFP"}} |
||||||
|
case Func.openCamera: |
||||||
|
#region 打开高拍仪并且进行快速扫描文件 |
||||||
|
joparam = (JObject)JsonConvert.DeserializeObject(param); |
||||||
|
data = joparam["url"].ToString(); |
||||||
|
resultback = scriptCallback.openCamera(data, callback); |
||||||
|
Log.Info("打开高拍仪并且进行快速扫描文件 返回数据:" + resultback); |
||||||
|
//base64 = Util.str2Base64(resultback); |
||||||
|
//向com对方发送数据 |
||||||
|
SendData(resultback); |
||||||
|
#endregion |
||||||
|
break; |
||||||
|
//{"type":"6","param":{"data":""}} |
||||||
|
case Func.OpenSign: |
||||||
|
#region 打开签字版数据 |
||||||
|
joparam = (JObject)JsonConvert.DeserializeObject(param); |
||||||
|
data = joparam["data"].ToString(); |
||||||
|
resultback = scriptCallback.OpenSign(data, callback); |
||||||
|
Log.Info("打开签字版数据 返回数据:" + resultback); |
||||||
|
base64 = Util.str2Base64(resultback); |
||||||
|
//向com对方发送数据 |
||||||
|
SendData(base64); |
||||||
|
#endregion |
||||||
|
break; |
||||||
|
//{"type":"7","param":{"data":""}} |
||||||
|
case Func.CloseSign: |
||||||
|
#region 关闭签字版数据 |
||||||
|
joparam = (JObject)JsonConvert.DeserializeObject(param); |
||||||
|
data = joparam["data"].ToString(); |
||||||
|
resultback = scriptCallback.CloseSign(data, callback); |
||||||
|
Log.Info("关闭签字版数据 返回数据:" + resultback); |
||||||
|
base64 = Util.str2Base64(resultback); |
||||||
|
//向com对方发送数据 |
||||||
|
SendData(base64); |
||||||
|
#endregion |
||||||
|
break; |
||||||
|
//{"type":"8","param":{"url":"http://192.168.0.34:92/api/UploadFP/UploadFP"}} |
||||||
|
case Func.GetSignData: |
||||||
|
#region 获取签字版数据 |
||||||
|
string sourcepaht = Environment.CurrentDirectory + "\\fiveInch.png"; |
||||||
|
FileInfo fileInfo = new FileInfo(sourcepaht); |
||||||
|
if (fileInfo.Exists) |
||||||
|
{ |
||||||
|
double size = fileInfo.Length / 1024.0; |
||||||
|
if (size > 10) |
||||||
|
{ |
||||||
|
FileStream fs1 = fileInfo.OpenRead(); |
||||||
|
fs1.Close(); |
||||||
|
resultback = Util.ImgToBase64String(sourcepaht); |
||||||
|
int width = 800; |
||||||
|
int height = 394; |
||||||
|
using (Bitmap blankImage = new Bitmap(width, height)) |
||||||
|
{ |
||||||
|
// 设置图片背景为完全透明 |
||||||
|
using (Graphics g = Graphics.FromImage(blankImage)) |
||||||
|
{ |
||||||
|
// 使用白色背景填充图片 |
||||||
|
g.Clear(Color.Transparent); |
||||||
|
} |
||||||
|
// 保存图片到文件系统 |
||||||
|
blankImage.Save(sourcepaht); |
||||||
|
} |
||||||
|
} |
||||||
|
else |
||||||
|
{ |
||||||
|
resultback = Util.str2Base64("400"); |
||||||
|
} |
||||||
|
} |
||||||
|
else |
||||||
|
{ |
||||||
|
resultback = Util.str2Base64("400"); |
||||||
|
} |
||||||
|
Log.Info("获取签字版数据 返回数据:" + resultback); |
||||||
|
// base64 = Util.str2Base64(resultback); |
||||||
|
//向com对方发送数据 |
||||||
|
SendData(resultback); |
||||||
|
#endregion |
||||||
|
break; |
||||||
|
default: |
||||||
|
string str = "{\"callback\":\"" + "callback" + "\",\"message\":\"fali\",\"code\":\"400\",\"status\":false,\"data\":\"" + "调用方法不存在" + "\"}"; |
||||||
|
base64 = Util.str2Base64(str); |
||||||
|
SendData(base64); |
||||||
|
break; |
||||||
|
} |
||||||
|
} |
||||||
|
else |
||||||
|
{ |
||||||
|
string str = "{\"callback\":\"" + "callback" + "\",\"message\":\"fali\",\"code\":\"400\",\"status\":false,\"data\":\"" + "参数不能为空" + "\"}"; |
||||||
|
string base64 = Util.str2Base64(str); |
||||||
|
SendData(base64); |
||||||
|
} |
||||||
|
_json = string.Empty; |
||||||
|
} |
||||||
|
else |
||||||
|
{ |
||||||
|
_json = _json + indata; |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
} |
||||||
|
catch (Exception ex) |
||||||
|
{ |
||||||
|
_json = string.Empty; |
||||||
|
string str = "{\"callback\":\"" + "callback" + "\",\"message\":\"fali\",\"code\":\"400\",\"status\":false,\"data\":\"" + ex.Message + "\"}"; |
||||||
|
string base64 = Util.str2Base64(str); |
||||||
|
SendData(base64); |
||||||
|
Log.Error("接受数据异常: " + ex.Message + ""); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
/// <summary> |
||||||
|
/// COM接收文件 |
||||||
|
/// </summary> |
||||||
|
private void NewMethod1(SerialPort sp, string path) |
||||||
|
{ |
||||||
|
byte[] buffer = new byte[sp.BytesToRead]; |
||||||
|
sp.Read(buffer, 0, buffer.Length); |
||||||
|
|
||||||
|
// 保存或处理接收到的字节数据,不做任何编码转换 |
||||||
|
File.WriteAllBytes(path, buffer); |
||||||
|
} |
||||||
|
|
||||||
|
/// <summary> |
||||||
|
/// COM口公共发送文件 |
||||||
|
/// </summary> |
||||||
|
/// <param name="url"></param> |
||||||
|
private void NewMethod(string url, string id) |
||||||
|
{ |
||||||
|
string ext = Path.GetExtension(url); |
||||||
|
serialPort.WriteLine("Start_" + id + "_" + ext); |
||||||
|
//读取文件开始传输文件 |
||||||
|
using (FileStream fileStream = new FileStream(url, FileMode.Open)) |
||||||
|
{ |
||||||
|
byte[] buffer = new byte[serialPort.WriteBufferSize]; |
||||||
|
int bytesRead = 0; |
||||||
|
while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) > 0) |
||||||
|
{ |
||||||
|
serialPort.Write(buffer, 0, bytesRead); |
||||||
|
} |
||||||
|
fileStream.Close(); |
||||||
|
fileStream.Dispose(); |
||||||
|
} |
||||||
|
serialPort.WriteLine("End"); |
||||||
|
} |
||||||
|
|
||||||
|
/// <summary> |
||||||
|
/// 打开串口 |
||||||
|
/// </summary> |
||||||
|
/// <param name="source"></param> |
||||||
|
/// <param name="e"></param> |
||||||
|
private void OnTimedEvent(Object source, ElapsedEventArgs e) |
||||||
|
{ |
||||||
|
try |
||||||
|
{ |
||||||
|
if (!serialPort.IsOpen) |
||||||
|
{ |
||||||
|
serialPort.Open(); |
||||||
|
} |
||||||
|
} |
||||||
|
catch (Exception ex) |
||||||
|
{ |
||||||
|
Log.Error("打开串口异常: " + ex.Message + ""); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
/// <summary> |
||||||
|
/// 发送数据 |
||||||
|
/// </summary> |
||||||
|
/// <param name="data"></param> |
||||||
|
public void SendData(string data) |
||||||
|
{ |
||||||
|
try |
||||||
|
{ |
||||||
|
if (serialPort.IsOpen) |
||||||
|
{ |
||||||
|
if (data.Length <= maxChunkSize) |
||||||
|
{ |
||||||
|
serialPort.WriteLine(data); |
||||||
|
} |
||||||
|
else |
||||||
|
{ |
||||||
|
Task.Run(() => |
||||||
|
{ |
||||||
|
byte[] buffer = Encoding.UTF8.GetBytes(data); |
||||||
|
int dataIndex = 0; |
||||||
|
int chunkSize; |
||||||
|
|
||||||
|
while (dataIndex < buffer.Length) |
||||||
|
{ |
||||||
|
chunkSize = Math.Min(buffer.Length - dataIndex, maxChunkSize); |
||||||
|
serialPort.Write(buffer, dataIndex, chunkSize); |
||||||
|
dataIndex += chunkSize; |
||||||
|
} |
||||||
|
serialPort.WriteLine(""); |
||||||
|
}); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
catch (Exception ex) |
||||||
|
{ |
||||||
|
Log.Error("发送数据异常3: " + ex.Message + ""); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
/// <summary> |
||||||
|
/// 关闭 |
||||||
|
/// </summary> |
||||||
|
public void ClosePort() |
||||||
|
{ |
||||||
|
try |
||||||
|
{ |
||||||
|
if (serialPort.IsOpen) |
||||||
|
{ |
||||||
|
serialPort.Close(); // 关闭串口 |
||||||
|
} |
||||||
|
} |
||||||
|
catch (Exception ex) |
||||||
|
{ |
||||||
|
Log.Error("关闭异常: " + ex.Message + ""); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,59 @@ |
|||||||
|
using Newtonsoft.Json.Linq; |
||||||
|
using Newtonsoft.Json; |
||||||
|
using System; |
||||||
|
using System.Collections.Generic; |
||||||
|
using System.IO; |
||||||
|
using System.Linq; |
||||||
|
using System.Net.Http; |
||||||
|
using System.Net.Http.Headers; |
||||||
|
using System.Text; |
||||||
|
using System.Threading.Tasks; |
||||||
|
using Device.ApiCommonModel.AksModel; |
||||||
|
|
||||||
|
|
||||||
|
namespace DevicesService.Common |
||||||
|
{ |
||||||
|
public class ChunkedUpload |
||||||
|
{ |
||||||
|
private readonly HttpClient _httpClient; |
||||||
|
|
||||||
|
public ChunkedUpload(HttpClient httpClient) |
||||||
|
{ |
||||||
|
_httpClient = httpClient; |
||||||
|
} |
||||||
|
|
||||||
|
public async Task<string> UploadFileAsync(string url, string filePath) |
||||||
|
{ |
||||||
|
string ret = string.Empty; |
||||||
|
using (var fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read)) |
||||||
|
{ |
||||||
|
FileInfo fileInfo = new FileInfo(filePath); |
||||||
|
int totalParts = 1; |
||||||
|
int chunkNumber = 1; |
||||||
|
// 读取文件流其实位置 |
||||||
|
var fileStreamPos = 0; |
||||||
|
var uploadUrl = $"{url}?partNumber={chunkNumber}&chunks={totalParts}&size={fileInfo.Length}&start={fileStreamPos}&end={fileInfo.Length}&total={fileInfo.Length}&FileName={Path.GetFileName(filePath)}"; |
||||||
|
|
||||||
|
using (var client = new HttpClient()) |
||||||
|
{ |
||||||
|
|
||||||
|
var formData = new MultipartFormDataContent(); |
||||||
|
formData.Add(new StreamContent(fileStream, (int)fileStream.Length), "file", Path.GetFileName(filePath) + ".partNumber-1"); |
||||||
|
var response = await client.PostAsync(uploadUrl, formData); |
||||||
|
var responseString = await response.Content.ReadAsStringAsync(); |
||||||
|
fileStream.Close(); |
||||||
|
fileStream.Dispose(); |
||||||
|
ret = responseString; |
||||||
|
JObject jo = (JObject)JsonConvert.DeserializeObject(ret); |
||||||
|
if (Convert.ToBoolean(jo["IsSucceed"].ToString()) == true) |
||||||
|
{ |
||||||
|
string result = jo["result"].ToString(); |
||||||
|
JObject jo1 = (JObject)JsonConvert.DeserializeObject(result); |
||||||
|
ret = jo1["url"].ToString(); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
return ret; |
||||||
|
} |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,126 @@ |
|||||||
|
using System; |
||||||
|
using System.Collections.Generic; |
||||||
|
using System.Diagnostics; |
||||||
|
using System.Text; |
||||||
|
|
||||||
|
namespace DevicesService.Commen |
||||||
|
{ |
||||||
|
|
||||||
|
public static class CommonHelper |
||||||
|
{ |
||||||
|
#region Stopwatch计时器 |
||||||
|
/// <summary> |
||||||
|
/// 计时器开始 |
||||||
|
/// </summary> |
||||||
|
/// <returns></returns> |
||||||
|
public static Stopwatch TimerStart() |
||||||
|
{ |
||||||
|
Stopwatch watch = new Stopwatch(); |
||||||
|
watch.Reset(); |
||||||
|
watch.Start(); |
||||||
|
return watch; |
||||||
|
} |
||||||
|
/// <summary> |
||||||
|
/// 计时器结束 |
||||||
|
/// </summary> |
||||||
|
/// <param name="watch">Stopwatch</param> |
||||||
|
/// <returns></returns> |
||||||
|
public static string TimerEnd(Stopwatch watch) |
||||||
|
{ |
||||||
|
watch.Stop(); |
||||||
|
double costtime = watch.ElapsedMilliseconds; |
||||||
|
return costtime.ToString(); |
||||||
|
} |
||||||
|
#endregion |
||||||
|
|
||||||
|
#region 删除数组中的重复项 |
||||||
|
/// <summary> |
||||||
|
/// 删除数组中的重复项 |
||||||
|
/// </summary> |
||||||
|
/// <param name="values">重复值</param> |
||||||
|
/// <returns></returns> |
||||||
|
public static string[] RemoveDup(string[] values) |
||||||
|
{ |
||||||
|
List<string> list = new List<string>(); |
||||||
|
for (int i = 0; i < values.Length; i++)//遍历数组成员 |
||||||
|
{ |
||||||
|
if (!list.Contains(values[i])) |
||||||
|
{ |
||||||
|
list.Add(values[i]); |
||||||
|
}; |
||||||
|
} |
||||||
|
return list.ToArray(); |
||||||
|
} |
||||||
|
#endregion |
||||||
|
|
||||||
|
#region 自动生成日期编号 |
||||||
|
/// <summary> |
||||||
|
/// 自动生成编号 201008251145409865 |
||||||
|
/// </summary> |
||||||
|
/// <returns></returns> |
||||||
|
public static string CreateNo() |
||||||
|
{ |
||||||
|
Random random = new Random(); |
||||||
|
string strRandom = random.Next(1000, 10000).ToString(); //生成编号 |
||||||
|
string code = DateTime.Now.ToString("yyyyMMddHHmmss") + strRandom;//形如 |
||||||
|
return code; |
||||||
|
} |
||||||
|
#endregion |
||||||
|
|
||||||
|
#region 生成0-9随机数 |
||||||
|
/// <summary> |
||||||
|
/// 生成0-9随机数 |
||||||
|
/// </summary> |
||||||
|
/// <param name="codeNum">生成长度</param> |
||||||
|
/// <returns></returns> |
||||||
|
public static string RndNum(int codeNum) |
||||||
|
{ |
||||||
|
StringBuilder sb = new StringBuilder(codeNum); |
||||||
|
Random rand = new Random(); |
||||||
|
for (int i = 1; i < codeNum + 1; i++) |
||||||
|
{ |
||||||
|
int t = rand.Next(9); |
||||||
|
sb.AppendFormat("{0}", t); |
||||||
|
} |
||||||
|
return sb.ToString(); |
||||||
|
|
||||||
|
} |
||||||
|
|
||||||
|
#endregion |
||||||
|
|
||||||
|
#region 删除最后一个字符之后的字符 |
||||||
|
/// <summary> |
||||||
|
/// 删除最后结尾的一个逗号 |
||||||
|
/// </summary> |
||||||
|
/// <param name="str">字串</param> |
||||||
|
/// <returns></returns> |
||||||
|
public static string DelLastComma(string str) |
||||||
|
{ |
||||||
|
return str.Substring(0, str.LastIndexOf(",")); |
||||||
|
} |
||||||
|
/// <summary> |
||||||
|
/// 删除最后结尾的指定字符后的字符 |
||||||
|
/// </summary> |
||||||
|
/// <param name="str">字串</param> |
||||||
|
/// <param name="strchar">指定的字符</param> |
||||||
|
/// <returns></returns> |
||||||
|
public static string DelLastChar(string str, string strchar) |
||||||
|
{ |
||||||
|
return str.Substring(0, str.LastIndexOf(strchar)); |
||||||
|
} |
||||||
|
/// <summary> |
||||||
|
/// 删除最后结尾的长度 |
||||||
|
/// </summary> |
||||||
|
/// <param name="str">字串</param> |
||||||
|
/// <param name="Length">删除长度</param> |
||||||
|
/// <returns></returns> |
||||||
|
public static string DelLastLength(string str, int Length) |
||||||
|
{ |
||||||
|
if (string.IsNullOrEmpty(str)) |
||||||
|
return ""; |
||||||
|
str = str.Substring(0, str.Length - Length); |
||||||
|
return str; |
||||||
|
} |
||||||
|
#endregion |
||||||
|
} |
||||||
|
} |
File diff suppressed because one or more lines are too long
@ -0,0 +1,108 @@ |
|||||||
|
|
||||||
|
using Newtonsoft.Json; |
||||||
|
using System; |
||||||
|
using System.Collections.Generic; |
||||||
|
using System.Configuration; |
||||||
|
using System.IO; |
||||||
|
|
||||||
|
namespace DevicesService.Commen |
||||||
|
{ |
||||||
|
/// <summary> |
||||||
|
/// 读取dat文件helper |
||||||
|
/// </summary> |
||||||
|
public class DatHelper<T> |
||||||
|
{ |
||||||
|
/// <summary> |
||||||
|
/// 获取配置文件dat |
||||||
|
/// </summary> |
||||||
|
/// <param name="datName"></param> |
||||||
|
/// <returns></returns> |
||||||
|
public static List<T> GetDatList(String datName) |
||||||
|
{ |
||||||
|
try |
||||||
|
{ |
||||||
|
var filepath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Config\\" + datName); |
||||||
|
if (File.Exists(filepath)) |
||||||
|
{ |
||||||
|
using (StreamReader sw = new StreamReader(filepath)) |
||||||
|
{ |
||||||
|
return JsonConvert.DeserializeObject<List<T>>(sw.ReadToEnd()); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
catch (Exception ex) |
||||||
|
{ |
||||||
|
return null; |
||||||
|
} |
||||||
|
return null; |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
public static T GetDat(String datName) |
||||||
|
{ |
||||||
|
try |
||||||
|
{ |
||||||
|
var filepath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Config\\" + datName); |
||||||
|
if (File.Exists(filepath)) |
||||||
|
{ |
||||||
|
using (StreamReader sw = new StreamReader(filepath)) |
||||||
|
{ |
||||||
|
return JsonConvert.DeserializeObject<T>(sw.ReadToEnd()); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
catch (Exception ex) |
||||||
|
{ |
||||||
|
return default(T); |
||||||
|
} |
||||||
|
return default(T); |
||||||
|
} |
||||||
|
|
||||||
|
public static string GetDatStr(String datName) |
||||||
|
{ |
||||||
|
try |
||||||
|
{ |
||||||
|
var filepath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Config\\" + datName); |
||||||
|
if (File.Exists(filepath)) |
||||||
|
{ |
||||||
|
using (StreamReader sw = new StreamReader(filepath)) |
||||||
|
{ |
||||||
|
return sw.ReadToEnd()+""; |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
catch (Exception ex) |
||||||
|
{ |
||||||
|
return default(string); |
||||||
|
} |
||||||
|
return default(string); |
||||||
|
} |
||||||
|
/// <summary> |
||||||
|
/// 保存配置文件dat |
||||||
|
/// </summary> |
||||||
|
/// <param name="datName">文件名称</param> |
||||||
|
/// <param name="content">内容</param> |
||||||
|
public static void SaveDat(String datName,string content) |
||||||
|
{ |
||||||
|
try |
||||||
|
{ |
||||||
|
var filepath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Config\\" + datName); |
||||||
|
//实例化一个文件流--->与写入文件相关联 |
||||||
|
FileStream fs = new FileStream(filepath, FileMode.Create); |
||||||
|
//实例化一个StreamWriter-->与fs相关联 |
||||||
|
StreamWriter sw = new StreamWriter(fs); |
||||||
|
//开始写入 |
||||||
|
sw.Write(content); |
||||||
|
//清空缓冲区 |
||||||
|
sw.Flush(); |
||||||
|
//关闭流 |
||||||
|
sw.Close(); |
||||||
|
fs.Close(); |
||||||
|
} |
||||||
|
catch (Exception ex) |
||||||
|
{ |
||||||
|
|
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,60 @@ |
|||||||
|
using System; |
||||||
|
using System.Collections.Generic; |
||||||
|
using System.Linq; |
||||||
|
using System.Text; |
||||||
|
using System.Threading.Tasks; |
||||||
|
|
||||||
|
namespace DevicesService.Commen |
||||||
|
{ |
||||||
|
public class Func |
||||||
|
{ |
||||||
|
/// <summary> |
||||||
|
/// 读取身份证 |
||||||
|
/// </summary> |
||||||
|
public const int IDCardRead = 1; |
||||||
|
/// <summary> |
||||||
|
/// 打印排队票据 |
||||||
|
/// </summary> |
||||||
|
public const int SendByPrint = 2; |
||||||
|
/// <summary> |
||||||
|
/// 文字语音播报 |
||||||
|
/// </summary> |
||||||
|
public const int payleText = 3; |
||||||
|
/// <summary> |
||||||
|
/// 发送短信 |
||||||
|
/// </summary> |
||||||
|
public const int SendSSM = 4; |
||||||
|
/// <summary> |
||||||
|
/// 打开高拍仪并且进行快速扫描文件 |
||||||
|
/// </summary> |
||||||
|
public const int openCamera = 5; |
||||||
|
/// <summary> |
||||||
|
/// 打开签字版数据 |
||||||
|
/// </summary> |
||||||
|
public const int OpenSign = 6; |
||||||
|
/// <summary> |
||||||
|
/// 关闭签字版 |
||||||
|
/// </summary> |
||||||
|
public const int CloseSign = 7; |
||||||
|
/// <summary> |
||||||
|
/// 获取签字版数据 |
||||||
|
/// </summary> |
||||||
|
public const int GetSignData = 8; |
||||||
|
/// <summary> |
||||||
|
/// 根据文件地址在线打印 |
||||||
|
/// </summary> |
||||||
|
public const int PrintFile = 9; |
||||||
|
/// <summary> |
||||||
|
/// 根据文件base64打印 |
||||||
|
/// </summary> |
||||||
|
public const int PrintBase64 = 10; |
||||||
|
/// <summary> |
||||||
|
/// 开始录音、取消录音、结束录音 |
||||||
|
/// </summary> |
||||||
|
public const int SoundRecording = 11; |
||||||
|
/// <summary> |
||||||
|
/// 音频文件播放与暂停 |
||||||
|
/// </summary> |
||||||
|
public const int Paly = 12; |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,162 @@ |
|||||||
|
using System; |
||||||
|
using System.Collections.Generic; |
||||||
|
using System.IO; |
||||||
|
using System.Linq; |
||||||
|
using System.Net; |
||||||
|
using System.Text; |
||||||
|
using System.Text.RegularExpressions; |
||||||
|
using System.Threading.Tasks; |
||||||
|
|
||||||
|
namespace DevicesService.Commen |
||||||
|
{ |
||||||
|
public class HtmlHelper |
||||||
|
{ |
||||||
|
/// <summary> |
||||||
|
/// 获取字符中指定标签的值 |
||||||
|
/// </summary> |
||||||
|
/// <param name="str">字符串</param> |
||||||
|
/// <param name="title">标签</param> |
||||||
|
/// <returns>值</returns> |
||||||
|
public static string GetTitleContent(string str, string title) |
||||||
|
{ |
||||||
|
string tmpStr = string.Format("<{0}[^>]*?>(?<Text>[^<]*)</{1}>", title, title); //获取<title>之间内容 |
||||||
|
Match TitleMatch = Regex.Match(str, tmpStr, RegexOptions.Multiline); |
||||||
|
string result = TitleMatch.Groups["Text"].Value; |
||||||
|
return result; |
||||||
|
} |
||||||
|
|
||||||
|
/// <summary> |
||||||
|
/// 获取字符中指定标签的值 |
||||||
|
/// </summary> |
||||||
|
/// <param name="str">字符串</param> |
||||||
|
/// <param name="title">标签</param> |
||||||
|
/// <param name="attrib">属性名</param> |
||||||
|
/// <returns>属性</returns> |
||||||
|
public static string GetTitleContent(string str, string title, string attrib) |
||||||
|
{ |
||||||
|
string tmpStr = string.Format("<{0}[^>]*?{1}=(['\"\"]?)(?<url>[^'\"\"\\s>]+)\\1[^>]*>", title, attrib); //获取<title>之间内容 |
||||||
|
Match TitleMatch = Regex.Match(str, tmpStr, RegexOptions.IgnoreCase); |
||||||
|
string result = TitleMatch.Groups["url"].Value; |
||||||
|
return result; |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
/// <summary> |
||||||
|
/// 格式化页面代码 |
||||||
|
/// </summary> |
||||||
|
/// <param name="html"></param> |
||||||
|
/// <returns></returns> |
||||||
|
public static string ReplaceEmpty(string html) |
||||||
|
{ |
||||||
|
html = Regex.Replace(html, "^\\s*", string.Empty, RegexOptions.Compiled | RegexOptions.Multiline);//过滤空格 |
||||||
|
html = Regex.Replace(html, "\\r\\n", string.Empty, RegexOptions.Compiled | RegexOptions.Multiline);//过滤换行 |
||||||
|
return html; |
||||||
|
} |
||||||
|
|
||||||
|
#region 抓取Html 页面内容 |
||||||
|
/// <summary> |
||||||
|
/// 抓取Html 页面内容 |
||||||
|
/// </summary> |
||||||
|
/// <returns></returns> |
||||||
|
public static string GetHtmlContent(string url) |
||||||
|
{ |
||||||
|
if (string.IsNullOrEmpty(url)) |
||||||
|
{ |
||||||
|
return ""; |
||||||
|
} |
||||||
|
try |
||||||
|
{ |
||||||
|
//创建一个请求 |
||||||
|
HttpWebRequest httprequst = (HttpWebRequest)WebRequest.Create(url); |
||||||
|
//不建立持久性链接 |
||||||
|
httprequst.KeepAlive = true; |
||||||
|
//设置请求的方法 |
||||||
|
httprequst.Method = "GET"; |
||||||
|
//设置标头值 |
||||||
|
httprequst.UserAgent = "User-Agent:Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705"; |
||||||
|
httprequst.Accept = "*/*"; |
||||||
|
httprequst.Headers.Add("Accept-Language", "zh-cn,en-us;q=0.5"); |
||||||
|
httprequst.ServicePoint.Expect100Continue = false; |
||||||
|
httprequst.Timeout = 5000; |
||||||
|
httprequst.AllowAutoRedirect = true;//是否允许302 |
||||||
|
ServicePointManager.DefaultConnectionLimit = 30; |
||||||
|
//获取响应 |
||||||
|
HttpWebResponse webRes = (HttpWebResponse)httprequst.GetResponse(); |
||||||
|
//获取响应的文本流 |
||||||
|
string content = string.Empty; |
||||||
|
using (System.IO.Stream stream = webRes.GetResponseStream()) |
||||||
|
{ |
||||||
|
using (System.IO.StreamReader reader = new StreamReader(stream, System.Text.Encoding.GetEncoding("gbk"))) |
||||||
|
{ |
||||||
|
content = reader.ReadToEnd(); |
||||||
|
} |
||||||
|
} |
||||||
|
//取消请求 |
||||||
|
httprequst.Abort(); |
||||||
|
//返回数据内容 |
||||||
|
return content; |
||||||
|
} |
||||||
|
catch (Exception) |
||||||
|
{ |
||||||
|
|
||||||
|
return ""; |
||||||
|
} |
||||||
|
} |
||||||
|
#endregion |
||||||
|
|
||||||
|
|
||||||
|
#region 抓取Html 页面内容 |
||||||
|
///<summary> |
||||||
|
///采用https协议访问网络 |
||||||
|
///</summary> |
||||||
|
///<param name="URL">url地址</param> |
||||||
|
///<param name="strPostdata">发送的数据</param> |
||||||
|
///<returns></returns> |
||||||
|
public static string PostHtmlContent(string URL, string strPostdata, string strEncoding = "gbk") |
||||||
|
{ |
||||||
|
if (string.IsNullOrEmpty(URL)) |
||||||
|
{ |
||||||
|
return ""; |
||||||
|
} |
||||||
|
try |
||||||
|
{ |
||||||
|
Encoding encoding = Encoding.Default; |
||||||
|
HttpWebRequest httprequst = (HttpWebRequest)WebRequest.Create(URL); |
||||||
|
httprequst.Method = "post"; |
||||||
|
httprequst.Accept = "text/html, application/xhtml+xml, */*"; |
||||||
|
httprequst.ContentType = "application/x-www-form-urlencoded"; |
||||||
|
//设置标头值 |
||||||
|
httprequst.UserAgent = "User-Agent:Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705"; |
||||||
|
httprequst.Accept = "*/*"; |
||||||
|
httprequst.Headers.Add("Accept-Language", "zh-cn,en-us;q=0.5"); |
||||||
|
httprequst.ServicePoint.Expect100Continue = false; |
||||||
|
httprequst.Timeout = 5000; |
||||||
|
httprequst.AllowAutoRedirect = true;//是否允许302 |
||||||
|
ServicePointManager.DefaultConnectionLimit = 30; |
||||||
|
|
||||||
|
byte[] buffer = encoding.GetBytes(strPostdata); |
||||||
|
httprequst.ContentLength = buffer.Length; |
||||||
|
httprequst.GetRequestStream().Write(buffer, 0, buffer.Length); |
||||||
|
HttpWebResponse response = (HttpWebResponse)httprequst.GetResponse(); |
||||||
|
//获取响应的文本流 |
||||||
|
string content = string.Empty; |
||||||
|
using (StreamReader reader = new StreamReader(response.GetResponseStream(), System.Text.Encoding.GetEncoding(strEncoding))) |
||||||
|
{ |
||||||
|
content = reader.ReadToEnd(); |
||||||
|
} |
||||||
|
//取消请求 |
||||||
|
httprequst.Abort(); |
||||||
|
//返回数据内容 |
||||||
|
return content; |
||||||
|
} |
||||||
|
catch (Exception) |
||||||
|
{ |
||||||
|
|
||||||
|
return ""; |
||||||
|
} |
||||||
|
} |
||||||
|
#endregion |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
} |
@ -0,0 +1,82 @@ |
|||||||
|
using System; |
||||||
|
using System.Collections.Generic; |
||||||
|
using System.IO; |
||||||
|
using System.Linq; |
||||||
|
using System.Text; |
||||||
|
using System.Threading.Tasks; |
||||||
|
|
||||||
|
namespace DevicesService.Commen |
||||||
|
{ |
||||||
|
public class Log |
||||||
|
{ |
||||||
|
private static StreamWriter streamWriter; //写文件 |
||||||
|
|
||||||
|
public static void Error(string message) |
||||||
|
{ |
||||||
|
try |
||||||
|
{ |
||||||
|
//DateTime dt = new DateTime(); |
||||||
|
string directPath = AppDomain.CurrentDomain.BaseDirectory + "\\logs\\error"; //在获得文件夹路径 |
||||||
|
if (!Directory.Exists(directPath)) //判断文件夹是否存在,如果不存在则创建 |
||||||
|
{ |
||||||
|
Directory.CreateDirectory(directPath); |
||||||
|
} |
||||||
|
directPath += string.Format(@"\{0}.log", DateTime.Now.ToString("yyyy-MM-dd")); |
||||||
|
if (streamWriter == null) |
||||||
|
{ |
||||||
|
streamWriter = !File.Exists(directPath) ? File.CreateText(directPath) : File.AppendText(directPath); |
||||||
|
} |
||||||
|
streamWriter.WriteLine("***********************************************************************"); |
||||||
|
streamWriter.WriteLine(DateTime.Now.ToString("HH:mm:ss")); |
||||||
|
streamWriter.WriteLine("输出信息:错误信息"); |
||||||
|
if (message != null) |
||||||
|
{ |
||||||
|
streamWriter.WriteLine("异常信息:\r\n" + message); |
||||||
|
} |
||||||
|
} |
||||||
|
finally |
||||||
|
{ |
||||||
|
if (streamWriter != null) |
||||||
|
{ |
||||||
|
streamWriter.Flush(); |
||||||
|
streamWriter.Dispose(); |
||||||
|
streamWriter = null; |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
public static void Info(string message) |
||||||
|
{ |
||||||
|
try |
||||||
|
{ |
||||||
|
//DateTime dt = new DateTime(); |
||||||
|
string directPath = AppDomain.CurrentDomain.BaseDirectory + "\\logs\\Info"; //在获得文件夹路径 |
||||||
|
if (!Directory.Exists(directPath)) //判断文件夹是否存在,如果不存在则创建 |
||||||
|
{ |
||||||
|
Directory.CreateDirectory(directPath); |
||||||
|
} |
||||||
|
directPath += string.Format(@"\{0}.log", DateTime.Now.ToString("yyyy-MM-dd")); |
||||||
|
if (streamWriter == null) |
||||||
|
{ |
||||||
|
streamWriter = !File.Exists(directPath) ? File.CreateText(directPath) : File.AppendText(directPath); |
||||||
|
} |
||||||
|
streamWriter.WriteLine("***********************************************************************"); |
||||||
|
streamWriter.WriteLine(DateTime.Now.ToString("HH:mm:ss")); |
||||||
|
streamWriter.WriteLine("输出信息:信息"); |
||||||
|
if (message != null) |
||||||
|
{ |
||||||
|
streamWriter.WriteLine("信息:\r\n" + message); |
||||||
|
} |
||||||
|
} |
||||||
|
finally |
||||||
|
{ |
||||||
|
if (streamWriter != null) |
||||||
|
{ |
||||||
|
streamWriter.Flush(); |
||||||
|
streamWriter.Dispose(); |
||||||
|
streamWriter = null; |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,423 @@ |
|||||||
|
using Microsoft.VisualBasic; |
||||||
|
using System; |
||||||
|
using System.Collections.Generic; |
||||||
|
using System.Linq; |
||||||
|
using System.Text; |
||||||
|
using System.Text.RegularExpressions; |
||||||
|
|
||||||
|
namespace DevicesService.Commen |
||||||
|
{ |
||||||
|
|
||||||
|
public sealed partial class Str |
||||||
|
{ |
||||||
|
#region Empty(空字符串) |
||||||
|
|
||||||
|
/// <summary> |
||||||
|
/// 空字符串 |
||||||
|
/// </summary> |
||||||
|
public static string Empty |
||||||
|
{ |
||||||
|
get { return string.Empty; } |
||||||
|
} |
||||||
|
|
||||||
|
#endregion |
||||||
|
|
||||||
|
#region PinYin(获取汉字的拼音简码) |
||||||
|
/// <summary> |
||||||
|
/// 获取汉字的拼音简码,即首字母缩写,范例:中国,返回zg |
||||||
|
/// </summary> |
||||||
|
/// <param name="chineseText">汉字文本,范例: 中国</param> |
||||||
|
public static string PinYin(string chineseText) |
||||||
|
{ |
||||||
|
if (string.IsNullOrWhiteSpace(chineseText)) |
||||||
|
return string.Empty; |
||||||
|
var result = new StringBuilder(); |
||||||
|
foreach (char text in chineseText) |
||||||
|
result.AppendFormat("{0}", ResolvePinYin(text)); |
||||||
|
return result.ToString().ToLower(); |
||||||
|
} |
||||||
|
|
||||||
|
/// <summary> |
||||||
|
/// 解析单个汉字的拼音简码 |
||||||
|
/// </summary> |
||||||
|
/// <param name="text">单个汉字</param> |
||||||
|
private static string ResolvePinYin(char text) |
||||||
|
{ |
||||||
|
byte[] charBytes = Encoding.Default.GetBytes(text.ToString()); |
||||||
|
if (charBytes[0] <= 127) |
||||||
|
return text.ToString(); |
||||||
|
var unicode = (ushort)(charBytes[0] * 256 + charBytes[1]); |
||||||
|
string pinYin = ResolvePinYinByCode(unicode); |
||||||
|
if (!string.IsNullOrWhiteSpace(pinYin)) |
||||||
|
return pinYin; |
||||||
|
return ResolvePinYinByFile(text.ToString()); |
||||||
|
} |
||||||
|
|
||||||
|
/// <summary> |
||||||
|
/// 使用字符编码方式获取拼音简码 |
||||||
|
/// </summary> |
||||||
|
private static string ResolvePinYinByCode(ushort unicode) |
||||||
|
{ |
||||||
|
if (unicode >= '\uB0A1' && unicode <= '\uB0C4') |
||||||
|
return "A"; |
||||||
|
if (unicode >= '\uB0C5' && unicode <= '\uB2C0' && unicode != 45464) |
||||||
|
return "B"; |
||||||
|
if (unicode >= '\uB2C1' && unicode <= '\uB4ED') |
||||||
|
return "C"; |
||||||
|
if (unicode >= '\uB4EE' && unicode <= '\uB6E9') |
||||||
|
return "D"; |
||||||
|
if (unicode >= '\uB6EA' && unicode <= '\uB7A1') |
||||||
|
return "E"; |
||||||
|
if (unicode >= '\uB7A2' && unicode <= '\uB8C0') |
||||||
|
return "F"; |
||||||
|
if (unicode >= '\uB8C1' && unicode <= '\uB9FD') |
||||||
|
return "G"; |
||||||
|
if (unicode >= '\uB9FE' && unicode <= '\uBBF6') |
||||||
|
return "H"; |
||||||
|
if (unicode >= '\uBBF7' && unicode <= '\uBFA5') |
||||||
|
return "J"; |
||||||
|
if (unicode >= '\uBFA6' && unicode <= '\uC0AB') |
||||||
|
return "K"; |
||||||
|
if (unicode >= '\uC0AC' && unicode <= '\uC2E7') |
||||||
|
return "L"; |
||||||
|
if (unicode >= '\uC2E8' && unicode <= '\uC4C2') |
||||||
|
return "M"; |
||||||
|
if (unicode >= '\uC4C3' && unicode <= '\uC5B5') |
||||||
|
return "N"; |
||||||
|
if (unicode >= '\uC5B6' && unicode <= '\uC5BD') |
||||||
|
return "O"; |
||||||
|
if (unicode >= '\uC5BE' && unicode <= '\uC6D9') |
||||||
|
return "P"; |
||||||
|
if (unicode >= '\uC6DA' && unicode <= '\uC8BA') |
||||||
|
return "Q"; |
||||||
|
if (unicode >= '\uC8BB' && unicode <= '\uC8F5') |
||||||
|
return "R"; |
||||||
|
if (unicode >= '\uC8F6' && unicode <= '\uCBF9') |
||||||
|
return "S"; |
||||||
|
if (unicode >= '\uCBFA' && unicode <= '\uCDD9') |
||||||
|
return "T"; |
||||||
|
if (unicode >= '\uCDDA' && unicode <= '\uCEF3') |
||||||
|
return "W"; |
||||||
|
if (unicode >= '\uCEF4' && unicode <= '\uD188') |
||||||
|
return "X"; |
||||||
|
if (unicode >= '\uD1B9' && unicode <= '\uD4D0') |
||||||
|
return "Y"; |
||||||
|
if (unicode >= '\uD4D1' && unicode <= '\uD7F9') |
||||||
|
return "Z"; |
||||||
|
return string.Empty; |
||||||
|
} |
||||||
|
|
||||||
|
/// <summary> |
||||||
|
/// 从拼音简码文件获取 |
||||||
|
/// </summary> |
||||||
|
/// <param name="text">单个汉字</param> |
||||||
|
public static string ResolvePinYinByFile(string text) |
||||||
|
{ |
||||||
|
int index = Const.ChinesePinYin.IndexOf(text, StringComparison.Ordinal); |
||||||
|
if (index < 0) |
||||||
|
return string.Empty; |
||||||
|
return Const.ChinesePinYin.Substring(index + 1, 1); |
||||||
|
} |
||||||
|
|
||||||
|
#endregion |
||||||
|
|
||||||
|
#region 获取全拼 |
||||||
|
/// <summary> |
||||||
|
/// 把汉字转换成拼音(全拼) |
||||||
|
/// </summary> |
||||||
|
/// <param name="hzString">汉字字符串</param> |
||||||
|
/// <returns>转换后的拼音(全拼)字符串</returns> |
||||||
|
public static string ConvertPinYin(string text) |
||||||
|
{ |
||||||
|
// 匹配中文字符 |
||||||
|
Regex regex = new Regex("^[\u4e00-\u9fa5]$"); |
||||||
|
byte[] array = new byte[2]; |
||||||
|
string pyString = ""; |
||||||
|
int chrAsc = 0; |
||||||
|
int i1 = 0; |
||||||
|
int i2 = 0; |
||||||
|
char[] noWChar = text.ToCharArray(); |
||||||
|
|
||||||
|
for (int j = 0; j < noWChar.Length; j++) |
||||||
|
{ |
||||||
|
// 中文字符 |
||||||
|
if (regex.IsMatch(noWChar[j].ToString())) |
||||||
|
{ |
||||||
|
array = System.Text.Encoding.Default.GetBytes(noWChar[j].ToString()); |
||||||
|
i1 = (short)(array[0]); |
||||||
|
i2 = (short)(array[1]); |
||||||
|
chrAsc = i1 * 256 + i2 - 65536; |
||||||
|
if (chrAsc > 0 && chrAsc < 160) |
||||||
|
{ |
||||||
|
pyString += noWChar[j]; |
||||||
|
} |
||||||
|
else |
||||||
|
{ |
||||||
|
// 修正部分文字 |
||||||
|
if (chrAsc == -9254) // 修正“圳”字 |
||||||
|
pyString += "Zhen"; |
||||||
|
else |
||||||
|
{ |
||||||
|
for (int i = (ChinesePinYinValue.Length - 1); i >= 0; i--) |
||||||
|
{ |
||||||
|
if (ChinesePinYinValue[i] <= chrAsc) |
||||||
|
{ |
||||||
|
pyString += ChinesePinYinName[i]; |
||||||
|
break; |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
// 非中文字符 |
||||||
|
else |
||||||
|
{ |
||||||
|
pyString += noWChar[j].ToString(); |
||||||
|
} |
||||||
|
} |
||||||
|
return pyString; |
||||||
|
} |
||||||
|
|
||||||
|
/// <summary> |
||||||
|
/// 拼音码 |
||||||
|
/// </summary> |
||||||
|
private static int[] ChinesePinYinValue = new int[] |
||||||
|
{ |
||||||
|
-20319,-20317,-20304,-20295,-20292,-20283,-20265,-20257,-20242,-20230,-20051,-20036, |
||||||
|
-20032,-20026,-20002,-19990,-19986,-19982,-19976,-19805,-19784,-19775,-19774,-19763, |
||||||
|
-19756,-19751,-19746,-19741,-19739,-19728,-19725,-19715,-19540,-19531,-19525,-19515, |
||||||
|
-19500,-19484,-19479,-19467,-19289,-19288,-19281,-19275,-19270,-19263,-19261,-19249, |
||||||
|
-19243,-19242,-19238,-19235,-19227,-19224,-19218,-19212,-19038,-19023,-19018,-19006, |
||||||
|
-19003,-18996,-18977,-18961,-18952,-18783,-18774,-18773,-18763,-18756,-18741,-18735, |
||||||
|
-18731,-18722,-18710,-18697,-18696,-18526,-18518,-18501,-18490,-18478,-18463,-18448, |
||||||
|
-18447,-18446,-18239,-18237,-18231,-18220,-18211,-18201,-18184,-18183, -18181,-18012, |
||||||
|
-17997,-17988,-17970,-17964,-17961,-17950,-17947,-17931,-17928,-17922,-17759,-17752, |
||||||
|
-17733,-17730,-17721,-17703,-17701,-17697,-17692,-17683,-17676,-17496,-17487,-17482, |
||||||
|
-17468,-17454,-17433,-17427,-17417,-17202,-17185,-16983,-16970,-16942,-16915,-16733, |
||||||
|
-16708,-16706,-16689,-16664,-16657,-16647,-16474,-16470,-16465,-16459,-16452,-16448, |
||||||
|
-16433,-16429,-16427,-16423,-16419,-16412,-16407,-16403,-16401,-16393,-16220,-16216, |
||||||
|
-16212,-16205,-16202,-16187,-16180,-16171,-16169,-16158,-16155,-15959,-15958,-15944, |
||||||
|
-15933,-15920,-15915,-15903,-15889,-15878,-15707,-15701,-15681,-15667,-15661,-15659, |
||||||
|
-15652,-15640,-15631,-15625,-15454,-15448,-15436,-15435,-15419,-15416,-15408,-15394, |
||||||
|
-15385,-15377,-15375,-15369,-15363,-15362,-15183,-15180,-15165,-15158,-15153,-15150, |
||||||
|
-15149,-15144,-15143,-15141,-15140,-15139,-15128,-15121,-15119,-15117,-15110,-15109, |
||||||
|
-14941,-14937,-14933,-14930,-14929,-14928,-14926,-14922,-14921,-14914,-14908,-14902, |
||||||
|
-14894,-14889,-14882,-14873,-14871,-14857,-14678,-14674,-14670,-14668,-14663,-14654, |
||||||
|
-14645,-14630,-14594,-14429,-14407,-14399,-14384,-14379,-14368,-14355,-14353,-14345, |
||||||
|
-14170,-14159,-14151,-14149,-14145,-14140,-14137,-14135,-14125,-14123,-14122,-14112, |
||||||
|
-14109,-14099,-14097,-14094,-14092,-14090,-14087,-14083,-13917,-13914,-13910,-13907, |
||||||
|
-13906,-13905,-13896,-13894,-13878,-13870,-13859,-13847,-13831,-13658,-13611,-13601, |
||||||
|
-13406,-13404,-13400,-13398,-13395,-13391,-13387,-13383,-13367,-13359,-13356,-13343, |
||||||
|
-13340,-13329,-13326,-13318,-13147,-13138,-13120,-13107,-13096,-13095,-13091,-13076, |
||||||
|
-13068,-13063,-13060,-12888,-12875,-12871,-12860,-12858,-12852,-12849,-12838,-12831, |
||||||
|
-12829,-12812,-12802,-12607,-12597,-12594,-12585,-12556,-12359,-12346,-12320,-12300, |
||||||
|
-12120,-12099,-12089,-12074,-12067,-12058,-12039,-11867,-11861,-11847,-11831,-11798, |
||||||
|
-11781,-11604,-11589,-11536,-11358,-11340,-11339,-11324,-11303,-11097,-11077,-11067, |
||||||
|
-11055,-11052,-11045,-11041,-11038,-11024,-11020,-11019,-11018,-11014,-10838,-10832, |
||||||
|
-10815,-10800,-10790,-10780,-10764,-10587,-10544,-10533,-10519,-10331,-10329,-10328, |
||||||
|
-10322,-10315,-10309,-10307,-10296,-10281,-10274,-10270,-10262,-10260,-10256,-10254 |
||||||
|
}; |
||||||
|
/// <summary> |
||||||
|
/// 拼音码 |
||||||
|
/// </summary> |
||||||
|
private static string[] ChinesePinYinName = new string[] |
||||||
|
{ |
||||||
|
"A","Ai","An","Ang","Ao","Ba","Bai","Ban","Bang","Bao","Bei","Ben", |
||||||
|
"Beng","Bi","Bian","Biao","Bie","Bin","Bing","Bo","Bu","Ba","Cai","Can", |
||||||
|
"Cang","Cao","Ce","Ceng","Cha","Chai","Chan","Chang","Chao","Che","Chen","Cheng", |
||||||
|
"Chi","Chong","Chou","Chu","Chuai","Chuan","Chuang","Chui","Chun","Chuo","Ci","Cong", |
||||||
|
"Cou","Cu","Cuan","Cui","Cun","Cuo","Da","Dai","Dan","Dang","Dao","De", |
||||||
|
"Deng","Di","Dian","Diao","Die","Ding","Diu","Dong","Dou","Du","Duan","Dui", |
||||||
|
"Dun","Duo","E","En","Er","Fa","Fan","Fang","Fei","Fen","Feng","Fo", |
||||||
|
"Fou","Fu","Ga","Gai","Gan","Gang","Gao","Ge","Gei","Gen","Geng","Gong", |
||||||
|
"Gou","Gu","Gua","Guai","Guan","Guang","Gui","Gun","Guo","Ha","Hai","Han", |
||||||
|
"Hang","Hao","He","Hei","Hen","Heng","Hong","Hou","Hu","Hua","Huai","Huan", |
||||||
|
"Huang","Hui","Hun","Huo","Ji","Jia","Jian","Jiang","Jiao","Jie","Jin","Jing", |
||||||
|
"Jiong","Jiu","Ju","Juan","Jue","Jun","Ka","Kai","Kan","Kang","Kao","Ke", |
||||||
|
"Ken","Keng","Kong","Kou","Ku","Kua","Kuai","Kuan","Kuang","Kui","Kun","Kuo", |
||||||
|
"La","Lai","Lan","Lang","Lao","Le","Lei","Leng","Li","Lia","Lian","Liang", |
||||||
|
"Liao","Lie","Lin","Ling","Liu","Long","Lou","Lu","Lv","Luan","Lue","Lun", |
||||||
|
"Luo","Ma","Mai","Man","Mang","Mao","Me","Mei","Men","Meng","Mi","Mian", |
||||||
|
"Miao","Mie","Min","Ming","Miu","Mo","Mou","Mu","Na","Nai","Nan","Nang", |
||||||
|
"Nao","Ne","Nei","Nen","Neng","Ni","Nian","Niang","Niao","Nie","Nin","Ning", |
||||||
|
"Niu","Nong","Nu","Nv","Nuan","Nue","Nuo","O","Ou","Pa","Pai","Pan", |
||||||
|
"Pang","Pao","Pei","Pen","Peng","Pi","Pian","Piao","Pie","Pin","Ping","Po", |
||||||
|
"Pu","Qi","Qia","Qian","Qiang","Qiao","Qie","Qin","Qing","Qiong","Qiu","Qu", |
||||||
|
"Quan","Que","Qun","Ran","Rang","Rao","Re","Ren","Reng","Ri","Rong","Rou", |
||||||
|
"Ru","Ruan","Rui","Run","Ruo","Sa","Sai","San","Sang","Sao","Se","Sen", |
||||||
|
"Seng","Sha","Shai","Shan","Shang","Shao","She","Shen","Sheng","Shi","Shou","Shu", |
||||||
|
"Shua","Shuai","Shuan","Shuang","Shui","Shun","Shuo","Si","Song","Sou","Su","Suan", |
||||||
|
"Sui","Sun","Suo","Ta","Tai","Tan","Tang","Tao","Te","Teng","Ti","Tian", |
||||||
|
"Tiao","Tie","Ting","Tong","Tou","Tu","Tuan","Tui","Tun","Tuo","Wa","Wai", |
||||||
|
"Wan","Wang","Wei","Wen","Weng","Wo","Wu","Xi","Xia","Xian","Xiang","Xiao", |
||||||
|
"Xie","Xin","Xing","Xiong","Xiu","Xu","Xuan","Xue","Xun","Ya","Yan","Yang", |
||||||
|
"Yao","Ye","Yi","Yin","Ying","Yo","Yong","You","Yu","Yuan","Yue","Yun", |
||||||
|
"Za", "Zai","Zan","Zang","Zao","Ze","Zei","Zen","Zeng","Zha","Zhai","Zhan", |
||||||
|
"Zhang","Zhao","Zhe","Zhen","Zheng","Zhi","Zhong","Zhou","Zhu","Zhua","Zhuai","Zhuan", |
||||||
|
"Zhuang","Zhui","Zhun","Zhuo","Zi","Zong","Zou","Zu","Zuan","Zui","Zun","Zuo" |
||||||
|
}; |
||||||
|
#endregion |
||||||
|
|
||||||
|
#region Splice(拼接集合元素) |
||||||
|
|
||||||
|
/// <summary> |
||||||
|
/// 拼接集合元素 |
||||||
|
/// </summary> |
||||||
|
/// <typeparam name="T">集合元素类型</typeparam> |
||||||
|
/// <param name="list">集合</param> |
||||||
|
/// <param name="quotes">引号,默认不带引号,范例:单引号 "'"</param> |
||||||
|
/// <param name="separator">分隔符,默认使用逗号分隔</param> |
||||||
|
public static string Splice<T>(IEnumerable<T> list, string quotes = "", string separator = ",") |
||||||
|
{ |
||||||
|
if (list == null) |
||||||
|
return string.Empty; |
||||||
|
var result = new StringBuilder(); |
||||||
|
foreach (var each in list) |
||||||
|
result.AppendFormat("{0}{1}{0}{2}", quotes, each, separator); |
||||||
|
return result.ToString().TrimEnd(separator.ToCharArray()); |
||||||
|
} |
||||||
|
|
||||||
|
#endregion |
||||||
|
|
||||||
|
#region FirstUpper(将值的首字母大(小)写) |
||||||
|
|
||||||
|
/// <summary> |
||||||
|
/// 将值的首字母大写 |
||||||
|
/// </summary> |
||||||
|
/// <param name="value">值</param> |
||||||
|
public static string FirstUpper(string value) |
||||||
|
{ |
||||||
|
string firstChar = value.Substring(0, 1).ToUpper(); |
||||||
|
return firstChar + value.Substring(1, value.Length - 1); |
||||||
|
} |
||||||
|
/// <summary> |
||||||
|
/// 将值的首字母小写 |
||||||
|
/// </summary> |
||||||
|
/// <param name="value">值</param> |
||||||
|
/// <returns></returns> |
||||||
|
public static string FirstLower(string value) |
||||||
|
{ |
||||||
|
string firstChar = value.Substring(0, 1).ToLower(); |
||||||
|
return firstChar + value.Substring(1, value.Length - 1); |
||||||
|
} |
||||||
|
#endregion |
||||||
|
|
||||||
|
#region ToCamel(将字符串转成驼峰形式) |
||||||
|
|
||||||
|
/// <summary> |
||||||
|
/// 将字符串转成驼峰形式 |
||||||
|
/// </summary> |
||||||
|
/// <param name="value">原始字符串</param> |
||||||
|
public static string ToCamel(string value) |
||||||
|
{ |
||||||
|
return FirstUpper(value.ToLower()); |
||||||
|
} |
||||||
|
|
||||||
|
#endregion |
||||||
|
|
||||||
|
#region ContainsChinese(是否包含中文) |
||||||
|
|
||||||
|
/// <summary> |
||||||
|
/// 是否包含中文 |
||||||
|
/// </summary> |
||||||
|
/// <param name="text">文本</param> |
||||||
|
public static bool ContainsChinese(string text) |
||||||
|
{ |
||||||
|
const string pattern = "[\u4e00-\u9fa5]+"; |
||||||
|
return Regex.IsMatch(text, pattern); |
||||||
|
} |
||||||
|
|
||||||
|
#endregion |
||||||
|
|
||||||
|
#region ContainsNumber(是否包含数字) |
||||||
|
|
||||||
|
/// <summary> |
||||||
|
/// 是否包含数字 |
||||||
|
/// </summary> |
||||||
|
/// <param name="text">文本</param> |
||||||
|
public static bool ContainsNumber(string text) |
||||||
|
{ |
||||||
|
const string pattern = "[0-9]+"; |
||||||
|
return Regex.IsMatch(text, pattern); |
||||||
|
} |
||||||
|
|
||||||
|
#endregion |
||||||
|
|
||||||
|
#region Distinct(去除重复) |
||||||
|
|
||||||
|
/// <summary> |
||||||
|
/// 去除重复 |
||||||
|
/// </summary> |
||||||
|
/// <param name="value">值,范例1:"5555",返回"5",范例2:"4545",返回"45"</param> |
||||||
|
public static string Distinct(string value) |
||||||
|
{ |
||||||
|
var array = value.ToCharArray(); |
||||||
|
return new string(array.Distinct().ToArray()); |
||||||
|
} |
||||||
|
|
||||||
|
#endregion |
||||||
|
|
||||||
|
#region Truncate(截断字符串) |
||||||
|
|
||||||
|
/// <summary> |
||||||
|
/// 截断字符串 |
||||||
|
/// </summary> |
||||||
|
/// <param name="text">文本</param> |
||||||
|
/// <param name="length">返回长度</param> |
||||||
|
/// <param name="endCharCount">添加结束符号的个数,默认0,不添加</param> |
||||||
|
/// <param name="endChar">结束符号,默认为省略号</param> |
||||||
|
public static string Truncate(string text, int length, int endCharCount = 0, string endChar = ".") |
||||||
|
{ |
||||||
|
if (string.IsNullOrWhiteSpace(text)) |
||||||
|
return string.Empty; |
||||||
|
if (text.Length < length) |
||||||
|
return text; |
||||||
|
return text.Substring(0, length) + GetEndString(endCharCount, endChar); |
||||||
|
} |
||||||
|
|
||||||
|
/// <summary> |
||||||
|
/// 获取结束字符串 |
||||||
|
/// </summary> |
||||||
|
private static string GetEndString(int endCharCount, string endChar) |
||||||
|
{ |
||||||
|
StringBuilder result = new StringBuilder(); |
||||||
|
for (int i = 0; i < endCharCount; i++) |
||||||
|
result.Append(endChar); |
||||||
|
return result.ToString(); |
||||||
|
} |
||||||
|
|
||||||
|
#endregion |
||||||
|
|
||||||
|
#region Unique(获取全局唯一值) |
||||||
|
|
||||||
|
/// <summary> |
||||||
|
/// 获取全局唯一值 |
||||||
|
/// </summary> |
||||||
|
public static string Unique() |
||||||
|
{ |
||||||
|
return Guid.NewGuid().ToString().Replace("-", ""); |
||||||
|
} |
||||||
|
|
||||||
|
#endregion |
||||||
|
|
||||||
|
/// <summary> |
||||||
|
/// 取得HTML中所有图片的 URL。 |
||||||
|
/// </summary> |
||||||
|
/// <param name="sHtmlText">HTML代码</param> |
||||||
|
/// <returns>图片的URL列表</returns> |
||||||
|
public static string[] GetHtmlImageUrlList(string sHtmlText) |
||||||
|
{ |
||||||
|
// 定义正则表达式用来匹配 img 标签 |
||||||
|
Regex regImg = new Regex(@"<img\b[^<>]*?\bsrc[\s\t\r\n]*=[\s\t\r\n]*[""']?[\s\t\r\n]*(?<imgUrl>[^\s\t\r\n""'<>]*)[^<>]*?/?[\s\t\r\n]*>", RegexOptions.IgnoreCase); |
||||||
|
// 搜索匹配的字符串 |
||||||
|
MatchCollection matches = regImg.Matches(sHtmlText); |
||||||
|
int i = 0; |
||||||
|
string[] sUrlList = new string[matches.Count]; |
||||||
|
// 取得匹配项列表 |
||||||
|
foreach (Match match in matches) |
||||||
|
sUrlList[i++] = match.Groups["imgUrl"].Value; |
||||||
|
return sUrlList; |
||||||
|
|
||||||
|
} |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,21 @@ |
|||||||
|
|
||||||
|
using System; |
||||||
|
using System.Collections.Generic; |
||||||
|
using System.Net; |
||||||
|
using System.Net.Sockets; |
||||||
|
using System.Text; |
||||||
|
using System.Threading; |
||||||
|
using System.Linq; |
||||||
|
using System.Collections.Concurrent; |
||||||
|
namespace DevicesService.Commen |
||||||
|
{ |
||||||
|
/// <summary> |
||||||
|
/// tcp Socket监听基库 |
||||||
|
/// </summary> |
||||||
|
public class TcpServer |
||||||
|
{ |
||||||
|
|
||||||
|
} |
||||||
|
|
||||||
|
} |
||||||
|
|
@ -0,0 +1,401 @@ |
|||||||
|
using DevicesService.Commen; |
||||||
|
using System; |
||||||
|
using System.Configuration; |
||||||
|
using System.Diagnostics; |
||||||
|
using System.Drawing; |
||||||
|
using System.Drawing.Imaging; |
||||||
|
using System.Drawing.Printing; |
||||||
|
using System.IO; |
||||||
|
using System.IO.Compression; |
||||||
|
using System.Linq; |
||||||
|
using Document = Aspose.Words.Document; |
||||||
|
|
||||||
|
namespace DevicesService.Common |
||||||
|
{ |
||||||
|
public class Util |
||||||
|
{ |
||||||
|
/// <summary> |
||||||
|
/// 文件转Base64码 |
||||||
|
/// </summary> |
||||||
|
/// <param name="imagePath"></param> |
||||||
|
/// <returns></returns> |
||||||
|
public static string ConvertImageToBase64(string imagePath) |
||||||
|
{ |
||||||
|
using (MemoryStream mStream = new MemoryStream()) |
||||||
|
{ |
||||||
|
System.Drawing.Image image = System.Drawing.Image.FromFile(filename: imagePath); |
||||||
|
// 保存图片到MemoryStream流中,并用相应的图片格式 |
||||||
|
image.Save(mStream, image.RawFormat); |
||||||
|
// 转换图片流为字节数组 |
||||||
|
byte[] imageBytes = mStream.ToArray(); |
||||||
|
// 将字节数组转换为Base64字符串 |
||||||
|
string base64String = Convert.ToBase64String(imageBytes); |
||||||
|
image.Dispose(); |
||||||
|
mStream.Close(); |
||||||
|
return base64String; |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
/// <summary> |
||||||
|
/// 文件转Base64码 |
||||||
|
/// </summary> |
||||||
|
/// <param name="fileLocation"></param> |
||||||
|
/// <returns></returns> |
||||||
|
public static string ImgToBase64String(string fileLocation) |
||||||
|
{ |
||||||
|
MemoryStream ms = new MemoryStream(); |
||||||
|
try |
||||||
|
{ |
||||||
|
if (System.IO.File.Exists(fileLocation)) |
||||||
|
{ |
||||||
|
Bitmap bmp = new Bitmap(@fileLocation); |
||||||
|
BitmapFlip(90, ref bmp); |
||||||
|
bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg); |
||||||
|
byte[] arr = new byte[ms.Length]; |
||||||
|
ms.Position = 0; |
||||||
|
ms.Read(arr, 0, (int)ms.Length); |
||||||
|
ms.Close(); |
||||||
|
bmp.Dispose(); |
||||||
|
File.Delete(fileLocation); |
||||||
|
return Convert.ToBase64String(arr); |
||||||
|
} |
||||||
|
return ""; |
||||||
|
} |
||||||
|
catch (Exception ex) |
||||||
|
{ |
||||||
|
return ex.Message; |
||||||
|
} |
||||||
|
finally |
||||||
|
{ |
||||||
|
ms.Close(); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
/// <summary> |
||||||
|
/// 图像翻转,基于图像中心 |
||||||
|
/// </summary> |
||||||
|
public static void BitmapFlip(int iFlip, ref Bitmap btDes) |
||||||
|
{ |
||||||
|
switch (iFlip) |
||||||
|
{ |
||||||
|
case 0: |
||||||
|
break; |
||||||
|
case 90: |
||||||
|
btDes.RotateFlip(RotateFlipType.Rotate90FlipNone); |
||||||
|
break; |
||||||
|
case 180: |
||||||
|
btDes.RotateFlip(RotateFlipType.Rotate180FlipNone); |
||||||
|
break; |
||||||
|
case 270: |
||||||
|
btDes.RotateFlip(RotateFlipType.Rotate270FlipNone); |
||||||
|
break; |
||||||
|
} |
||||||
|
|
||||||
|
} |
||||||
|
|
||||||
|
/// <summary> |
||||||
|
/// 调用打印机打印 |
||||||
|
/// </summary> |
||||||
|
/// <param name="PDFPath">PDF文件路径</param> |
||||||
|
/// <param name="PrinterName">打印机名称</param> |
||||||
|
public static void Print2(string docPath, string PrinterName) |
||||||
|
{ |
||||||
|
Process p = new Process(); |
||||||
|
ProcessStartInfo startInfo = new ProcessStartInfo(); |
||||||
|
startInfo.WindowStyle = ProcessWindowStyle.Hidden; |
||||||
|
startInfo.UseShellExecute = true; |
||||||
|
startInfo.CreateNoWindow = true; |
||||||
|
startInfo.FileName = docPath; |
||||||
|
startInfo.Verb = "print"; |
||||||
|
startInfo.Arguments = @"/p /h \" + docPath + "\"\"" + PrinterName + "\""; |
||||||
|
p.StartInfo = startInfo; |
||||||
|
p.Start(); |
||||||
|
p.WaitForExit(); |
||||||
|
} |
||||||
|
|
||||||
|
/// <summary> |
||||||
|
/// string 转换为 base64 |
||||||
|
/// </summary> |
||||||
|
/// <returns></returns> |
||||||
|
public static string str2Base64(string str) |
||||||
|
{ |
||||||
|
byte[] b = System.Text.Encoding.UTF8.GetBytes(str); |
||||||
|
string result = Convert.ToBase64String(b); |
||||||
|
return result; |
||||||
|
} |
||||||
|
|
||||||
|
/// <summary> |
||||||
|
/// base64 转换为 string |
||||||
|
/// </summary> |
||||||
|
/// <param name="data"></param> |
||||||
|
public static string Base64str2(string data) |
||||||
|
{ |
||||||
|
byte[] c = Convert.FromBase64String(data); |
||||||
|
string result = System.Text.Encoding.UTF8.GetString(c); |
||||||
|
return result; |
||||||
|
} |
||||||
|
|
||||||
|
/// <summary> |
||||||
|
/// 对base64进行压缩 |
||||||
|
/// </summary> |
||||||
|
/// <param name="base64Data"></param> |
||||||
|
/// <returns></returns> |
||||||
|
public static string CompressBase64(string base64Data) |
||||||
|
{ |
||||||
|
byte[] data = Convert.FromBase64String(base64Data); |
||||||
|
byte[] compressedData; |
||||||
|
|
||||||
|
using (var memoryStream = new MemoryStream()) |
||||||
|
{ |
||||||
|
using (var gzipStream = new GZipStream(memoryStream, CompressionMode.Compress, true)) |
||||||
|
{ |
||||||
|
gzipStream.Write(data, 0, data.Length); |
||||||
|
gzipStream.Flush(); |
||||||
|
compressedData = memoryStream.ToArray(); |
||||||
|
} |
||||||
|
} |
||||||
|
return Convert.ToBase64String(compressedData); |
||||||
|
} |
||||||
|
|
||||||
|
/// <summary> |
||||||
|
/// 对base64进行解压 |
||||||
|
/// </summary> |
||||||
|
/// <param name="compressedBase64Data"></param> |
||||||
|
/// <returns></returns> |
||||||
|
public static string DecompressBase64(string compressedBase64Data) |
||||||
|
{ |
||||||
|
byte[] data = Convert.FromBase64String(compressedBase64Data); |
||||||
|
byte[] decompressedData; |
||||||
|
|
||||||
|
using (var memoryStream = new MemoryStream(data)) |
||||||
|
{ |
||||||
|
using (var gzipStream = new GZipStream(memoryStream, CompressionMode.Decompress)) |
||||||
|
{ |
||||||
|
using (var outputStream = new MemoryStream()) |
||||||
|
{ |
||||||
|
gzipStream.CopyTo(outputStream); |
||||||
|
decompressedData = outputStream.ToArray(); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
return Convert.ToBase64String(decompressedData); |
||||||
|
} |
||||||
|
|
||||||
|
/// <summary> |
||||||
|
/// 压缩图片至200 Kb以下 |
||||||
|
/// </summary> |
||||||
|
/// <param name="img">图片</param> |
||||||
|
/// <param name="format">图片格式</param> |
||||||
|
/// <param name="targetLen">压缩后大小</param> |
||||||
|
/// <param name="srcLen">原始大小</param> |
||||||
|
/// <returns>压缩后的图片</returns> |
||||||
|
public static Image ZipImage(Image img, ImageFormat format, long targetLen, long srcLen = 0) |
||||||
|
{ |
||||||
|
//设置大小偏差幅度 10kb |
||||||
|
const long nearlyLen = 10240; |
||||||
|
//内存流 如果参数中原图大小没有传递 则使用内存流读取 |
||||||
|
var ms = new MemoryStream(); |
||||||
|
if (0 == srcLen) |
||||||
|
{ |
||||||
|
img.Save(ms, format); |
||||||
|
srcLen = ms.Length; |
||||||
|
} |
||||||
|
|
||||||
|
//单位 由Kb转为byte 若目标大小高于原图大小,则满足条件退出 |
||||||
|
targetLen *= 1024; |
||||||
|
if (targetLen > srcLen) |
||||||
|
{ |
||||||
|
ms.SetLength(0); |
||||||
|
ms.Position = 0; |
||||||
|
img.Save(ms, format); |
||||||
|
img = Image.FromStream(ms); |
||||||
|
return img; |
||||||
|
} |
||||||
|
|
||||||
|
//获取目标大小最低值 |
||||||
|
var exitLen = targetLen - nearlyLen; |
||||||
|
|
||||||
|
//初始化质量压缩参数 图像 内存流等 |
||||||
|
var quality = (long)Math.Floor(100.00 * targetLen / srcLen); |
||||||
|
var parms = new EncoderParameters(1); |
||||||
|
|
||||||
|
//获取编码器信息 |
||||||
|
ImageCodecInfo formatInfo = null; |
||||||
|
var encoders = ImageCodecInfo.GetImageEncoders(); |
||||||
|
foreach (ImageCodecInfo icf in encoders) |
||||||
|
{ |
||||||
|
if (icf.FormatID == format.Guid) |
||||||
|
{ |
||||||
|
formatInfo = icf; |
||||||
|
break; |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
//使用二分法进行查找 最接近的质量参数 |
||||||
|
long startQuality = quality; |
||||||
|
long endQuality = 100; |
||||||
|
quality = (startQuality + endQuality) / 2; |
||||||
|
|
||||||
|
while (true) |
||||||
|
{ |
||||||
|
//设置质量 |
||||||
|
parms.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, quality); |
||||||
|
|
||||||
|
//清空内存流 然后保存图片 |
||||||
|
ms.SetLength(0); |
||||||
|
ms.Position = 0; |
||||||
|
img.Save(ms, formatInfo, parms); |
||||||
|
|
||||||
|
//若压缩后大小低于目标大小,则满足条件退出 |
||||||
|
if (ms.Length >= exitLen && ms.Length <= targetLen) |
||||||
|
{ |
||||||
|
break; |
||||||
|
} |
||||||
|
else if (startQuality >= endQuality) //区间相等无需再次计算 |
||||||
|
{ |
||||||
|
break; |
||||||
|
} |
||||||
|
else if (ms.Length < exitLen) //压缩过小,起始质量右移 |
||||||
|
{ |
||||||
|
startQuality = quality; |
||||||
|
} |
||||||
|
else //压缩过大 终止质量左移 |
||||||
|
{ |
||||||
|
endQuality = quality; |
||||||
|
} |
||||||
|
|
||||||
|
//重新设置质量参数 如果计算出来的质量没有发生变化,则终止查找。这样是为了避免重复计算情况{start:16,end:18} 和 {start:16,endQuality:17} |
||||||
|
var newQuality = (startQuality + endQuality) / 2; |
||||||
|
if (newQuality == quality) |
||||||
|
{ |
||||||
|
break; |
||||||
|
} |
||||||
|
quality = newQuality; |
||||||
|
//Console.WriteLine("start:{0} end:{1} current:{2}", startQuality, endQuality, quality); |
||||||
|
} |
||||||
|
img = Image.FromStream(ms); |
||||||
|
return img; |
||||||
|
} |
||||||
|
|
||||||
|
/// <summary> |
||||||
|
///获取图片格式 |
||||||
|
/// </summary> |
||||||
|
/// <param name="img">图片</param> |
||||||
|
/// <returns>默认返回JPEG</returns> |
||||||
|
public static ImageFormat GetImageFormat(Image img) |
||||||
|
{ |
||||||
|
if (img.RawFormat.Equals(ImageFormat.Jpeg)) |
||||||
|
{ |
||||||
|
return ImageFormat.Jpeg; |
||||||
|
} |
||||||
|
if (img.RawFormat.Equals(ImageFormat.Gif)) |
||||||
|
{ |
||||||
|
return ImageFormat.Gif; |
||||||
|
} |
||||||
|
if (img.RawFormat.Equals(ImageFormat.Png)) |
||||||
|
{ |
||||||
|
return ImageFormat.Png; |
||||||
|
} |
||||||
|
if (img.RawFormat.Equals(ImageFormat.Bmp)) |
||||||
|
{ |
||||||
|
return ImageFormat.Bmp; |
||||||
|
} |
||||||
|
return ImageFormat.Jpeg;//根据实际情况选择返回指定格式还是null |
||||||
|
} |
||||||
|
|
||||||
|
/// <summary> |
||||||
|
/// 压缩图片 |
||||||
|
/// </summary> |
||||||
|
/// <param name="file"></param> |
||||||
|
/// <returns></returns> |
||||||
|
|
||||||
|
public static string CompressPictures(FileInfo file) |
||||||
|
{ |
||||||
|
Image image = null; |
||||||
|
try |
||||||
|
{ |
||||||
|
string fullpath = String.Empty; |
||||||
|
//获取未压缩的图片进行压缩 |
||||||
|
if (file != null && file.Length > 0) |
||||||
|
{ |
||||||
|
if (!file.Name.Contains("_compress")) |
||||||
|
{ |
||||||
|
//压缩图片 |
||||||
|
image =GetPicThumbnail(file.FullName); |
||||||
|
if (image != null) |
||||||
|
{ |
||||||
|
//压缩后的图片,图片名称增加_compress |
||||||
|
string newName = file.Name.Replace(file.Extension, "") + "_compress" + file.Extension; |
||||||
|
fullpath = file.FullName.Replace(file.Name, newName); |
||||||
|
image.Save(fullpath); |
||||||
|
file.Delete(); |
||||||
|
} |
||||||
|
} |
||||||
|
else |
||||||
|
{ |
||||||
|
fullpath = file.FullName; |
||||||
|
} |
||||||
|
} |
||||||
|
return fullpath; |
||||||
|
} |
||||||
|
catch (Exception ex) |
||||||
|
{ |
||||||
|
Log.Error(file.FullName); |
||||||
|
Log.Error(ex.Message + "MainViewModel.CompressPictures"); |
||||||
|
return ""; |
||||||
|
} |
||||||
|
finally |
||||||
|
{ |
||||||
|
if (image != null) |
||||||
|
image.Dispose(); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
/// <summary> |
||||||
|
/// 图片压缩 |
||||||
|
/// </summary> |
||||||
|
/// <param name="sFile">图片路径</param> |
||||||
|
/// <param name="flag">压缩比1-100</param> |
||||||
|
/// <returns>返回图片对象</returns> |
||||||
|
public static Image GetPicThumbnail(string sFile) |
||||||
|
{ |
||||||
|
int flag = Convert.ToInt32(ConfigurationManager.AppSettings["ysb"].ToString()); |
||||||
|
Image result = null; |
||||||
|
if (sFile != null) |
||||||
|
{ |
||||||
|
Image inputImage = Image.FromFile(sFile); |
||||||
|
try |
||||||
|
{ |
||||||
|
MemoryStream imgms = new MemoryStream(); |
||||||
|
var arrayICI = ImageCodecInfo.GetImageEncoders(); |
||||||
|
ImageCodecInfo jpegICIinfo = arrayICI.Where(w => w.FormatDescription.Contains("JPEG")).FirstOrDefault(); |
||||||
|
if (jpegICIinfo != null) |
||||||
|
{ //以下代码为保存图片时,设置压缩质量 |
||||||
|
var ep = new EncoderParameters() |
||||||
|
{ |
||||||
|
Param = new EncoderParameter[] |
||||||
|
{ |
||||||
|
new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, new long[1]{ flag}) |
||||||
|
} |
||||||
|
}; |
||||||
|
inputImage.Save(imgms, jpegICIinfo, ep); |
||||||
|
result = Image.FromStream(imgms); |
||||||
|
} |
||||||
|
return result; |
||||||
|
} |
||||||
|
catch (Exception ex) |
||||||
|
{ |
||||||
|
throw ex; |
||||||
|
} |
||||||
|
finally |
||||||
|
{ |
||||||
|
inputImage.Dispose(); |
||||||
|
} |
||||||
|
} |
||||||
|
else |
||||||
|
{ |
||||||
|
return result; |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,247 @@ |
|||||||
|
using System; |
||||||
|
using System.Collections.Generic; |
||||||
|
using System.Linq; |
||||||
|
using System.Text; |
||||||
|
using System.Runtime.InteropServices; |
||||||
|
|
||||||
|
namespace DevicesService.Devices |
||||||
|
{ |
||||||
|
using HELOAMIMAGE = IntPtr; |
||||||
|
using HELOAMIMAGELIST = IntPtr; |
||||||
|
using HELOAMFTP = IntPtr; |
||||||
|
using HELOAMHTTP = IntPtr; |
||||||
|
using HELOAMDEVICE = IntPtr; |
||||||
|
using HELOAMVIDEO = IntPtr; |
||||||
|
using HELOAMVIEW = IntPtr; |
||||||
|
using HELOAMMEMORY = IntPtr; |
||||||
|
using HELOAMBASE64 = IntPtr; |
||||||
|
using HELOAMTHUMBNAIL = IntPtr; |
||||||
|
using HELOAMRECT = IntPtr; |
||||||
|
using HELOAMFONT = IntPtr; |
||||||
|
using HELOAMVIDEOCAP = IntPtr; |
||||||
|
using LPVOID = IntPtr; |
||||||
|
using LONG = Int32; |
||||||
|
using BOOL = Int32; |
||||||
|
using HWND = IntPtr; |
||||||
|
using COLORREF = UInt32; |
||||||
|
|
||||||
|
public class EloamDll |
||||||
|
{ |
||||||
|
|
||||||
|
// global |
||||||
|
[DllImport("eloamDll.dll", EntryPoint = "EloamGlobal_DestroyString", |
||||||
|
CharSet = CharSet.Unicode)] |
||||||
|
public static extern BOOL EloamGlobal_DestroyString(IntPtr str); |
||||||
|
[DllImport("eloamDll.dll", EntryPoint = "EloamGlobal_CreateImage")] |
||||||
|
public static extern HELOAMIMAGE EloamGlobal_CreateImage(LONG width, LONG height, LONG channels); |
||||||
|
[DllImport("eloamDll.dll", EntryPoint = "EloamGlobal_CreateRect")] |
||||||
|
public static extern HELOAMRECT EloamGlobal_CreateRect(LONG x, LONG y, LONG width, LONG height); |
||||||
|
[DllImport("eloamDll.dll", EntryPoint = "EloamGlobal_CreateView")] |
||||||
|
public static extern HELOAMVIEW EloamGlobal_CreateView(HWND hWnd, HELOAMRECT rect, LONG flag); |
||||||
|
[DllImport("eloamDll.dll", EntryPoint = "EloamGlobal_CreateThumbnail")] |
||||||
|
public static extern HELOAMTHUMBNAIL EloamGlobal_CreateThumbnail(HWND hWnd, HELOAMRECT rect, LONG flag); |
||||||
|
[DllImport("eloamDll.dll", EntryPoint = "EloamGlobal_InitDevs")] |
||||||
|
public static extern BOOL EloamGlobal_InitDevs(ELOAM_DEVCHANGECALLBACK fun, LPVOID userData); |
||||||
|
[DllImport("eloamDll.dll", EntryPoint = "EloamGlobal_DeinitDevs")] |
||||||
|
public static extern BOOL EloamGlobal_DeinitDevs(); |
||||||
|
[DllImport("eloamDll.dll", EntryPoint = "EloamGlobal_CreateDevice")] |
||||||
|
public static extern HELOAMDEVICE EloamGlobal_CreateDevice(LONG type, LONG idx); |
||||||
|
[DllImport("eloamDll.dll", EntryPoint = "EloamGlobal_InitBarcode")] |
||||||
|
public static extern BOOL EloamGlobal_InitBarcode(); |
||||||
|
[DllImport("eloamDll.dll", EntryPoint = "EloamGlobal_DeinitBarcode")] |
||||||
|
public static extern BOOL EloamGlobal_DeinitBarcode(); |
||||||
|
[DllImport("eloamDll.dll", EntryPoint = "EloamGlobal_DiscernBarcode")] |
||||||
|
public static extern BOOL EloamGlobal_DiscernBarcode(HELOAMIMAGE img); |
||||||
|
[DllImport("eloamDll.dll", EntryPoint = "EloamGlobal_GetBarcodeCount", CallingConvention = CallingConvention.Cdecl)] |
||||||
|
public static extern LONG EloamGlobal_GetBarcodeCount(); |
||||||
|
[DllImport("eloamDll.dll", EntryPoint = "EloamGlobal_GetBarcodeData", |
||||||
|
CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)] |
||||||
|
public static extern string EloamGlobal_GetBarcodeData(LONG idx); |
||||||
|
|
||||||
|
[DllImport("eloamDll.dll", EntryPoint = "EloamGlobal_VideoCapInit", CallingConvention = CallingConvention.Cdecl)] |
||||||
|
public static extern BOOL EloamGlobal_VideoCapInit(); |
||||||
|
|
||||||
|
[DllImport("eloamDll.dll", EntryPoint = "EloamGlobal_VideoCapStop", CallingConvention = CallingConvention.Cdecl)] |
||||||
|
public static extern BOOL EloamGlobal_VideoCapStop(HELOAMVIDEOCAP cap); |
||||||
|
|
||||||
|
[DllImport("eloamDll.dll", EntryPoint = "EloamGlobal_DestroyVideoCap", CallingConvention = CallingConvention.Cdecl)] |
||||||
|
public static extern BOOL EloamGlobal_DestroyVideoCap(HELOAMVIDEOCAP cap); |
||||||
|
|
||||||
|
[DllImport("eloamDll.dll", EntryPoint = "EloamGlobal_CreatVideoCap", CallingConvention = CallingConvention.Cdecl)] |
||||||
|
public static extern HELOAMVIDEOCAP EloamGlobal_CreatVideoCap(); |
||||||
|
|
||||||
|
[DllImport("eloamDll.dll", EntryPoint = "EloamGlobal_VideoCapGetAudioDevNum", CallingConvention = CallingConvention.Cdecl)] |
||||||
|
public static extern LONG EloamGlobal_VideoCapGetAudioDevNum(); |
||||||
|
|
||||||
|
[DllImport("eloamDll.dll", EntryPoint = "EloamGlobal_VideoCapPreCap", |
||||||
|
CharSet = CharSet.Auto, CallingConvention = CallingConvention.Cdecl)] |
||||||
|
public static extern BOOL EloamGlobal_VideoCapPreCap(HELOAMVIDEOCAP m_cap, string fileName, LONG nSelectMic, LONG FrameRate, LONG compressMode, LONG width, LONG Height, BOOL bCapVideo); |
||||||
|
|
||||||
|
[DllImport("eloamDll.dll", EntryPoint = "EloamGlobal_VideoCapStart", CallingConvention = CallingConvention.Cdecl)] |
||||||
|
public static extern BOOL EloamGlobal_VideoCapStart(HELOAMVIDEOCAP cap); |
||||||
|
|
||||||
|
[DllImport("eloamDll.dll", EntryPoint = "EloamGlobal_VideoCapAddVideoSrc", |
||||||
|
CallingConvention = CallingConvention.Cdecl)] |
||||||
|
public static extern BOOL EloamGlobal_VideoCapAddVideoSrc(HELOAMVIDEOCAP cap, HELOAMVIDEO mVideo); |
||||||
|
[DllImport("eloamDll.dll", EntryPoint = "EloamGlobal_InitIdCard", |
||||||
|
CallingConvention = CallingConvention.Cdecl)] |
||||||
|
//[DllImport("eloamDll.dll", EntryPoint = "EloamGlobal_InitIdCard")] |
||||||
|
public static extern BOOL EloamGlobal_InitIdCard(ELOAM_IDCARDCHANGECALLBACK fun, LPVOID userData); |
||||||
|
[DllImport("eloamDll.dll", EntryPoint = "EloamGlobal_DeinitIdCard", |
||||||
|
CallingConvention = CallingConvention.Cdecl)] |
||||||
|
public static extern BOOL EloamGlobal_DeinitIdCard(); |
||||||
|
[DllImport("eloamDll.dll", EntryPoint = "EloamGlobal_ReadIdCard", |
||||||
|
CallingConvention = CallingConvention.Cdecl)] |
||||||
|
public static extern BOOL EloamGlobal_ReadIdCard(); |
||||||
|
[DllImport("eloamDll.dll", EntryPoint = "EloamGlobal_DiscernIdCard", |
||||||
|
CallingConvention = CallingConvention.Cdecl)] |
||||||
|
public static extern BOOL EloamGlobal_DiscernIdCard(ELOAM_IDCARDCHANGECALLBACK fun, LPVOID userData); |
||||||
|
[DllImport("eloamDll.dll", EntryPoint = "EloamGlobal_GetIdCardImage")] |
||||||
|
public static extern HELOAMIMAGE EloamGlobal_GetIdCardImage(LONG flag); |
||||||
|
[DllImport("eloamDll.dll", EntryPoint = "EloamGlobal_GetIdCardData", |
||||||
|
CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)] |
||||||
|
//[DllImport("eloamDll.dll", EntryPoint = "EloamGlobal_GetIdCardData", |
||||||
|
//CharSet = CharSet.Unicode, CallingConvention = CallingConvention.StdCall)] |
||||||
|
public static extern string EloamGlobal_GetIdCardData(LONG flag); |
||||||
|
[DllImport("eloamDll.dll", EntryPoint = "EloamGlobal_StopIdCardDiscern", |
||||||
|
CallingConvention = CallingConvention.Cdecl)] |
||||||
|
public static extern BOOL EloamGlobal_StopIdCardDiscern(); |
||||||
|
|
||||||
|
|
||||||
|
[DllImport("eloamDll.dll", EntryPoint = "EloamGlobal_InitFaceDetect", |
||||||
|
CallingConvention = CallingConvention.Cdecl)] |
||||||
|
public static extern BOOL EloamGlobal_InitFaceDetect(); |
||||||
|
[DllImport("eloamDll.dll", EntryPoint = "EloamGlobal_DiscernFaceDetect")] |
||||||
|
public static extern LONG EloamGlobal_DiscernFaceDetect(HELOAMIMAGE img1, HELOAMIMAGE img2); |
||||||
|
[DllImport("eloamDll.dll", EntryPoint = "EloamGlobal_GetFaceRect")] |
||||||
|
public static extern HELOAMRECT EloamGlobal_GetFaceRect(HELOAMIMAGE img); |
||||||
|
[DllImport("eloamDll.dll", EntryPoint = "EloamGlobal_EnableFaceRectCrop")] |
||||||
|
public static extern HELOAMRECT EloamGlobal_EnableFaceRectCrop(HELOAMVIDEO video, LONG flag); |
||||||
|
[DllImport("eloamDll.dll", EntryPoint = "EloamGlobal_DisableFaceRectCrop")] |
||||||
|
public static extern HELOAMRECT EloamGlobal_DisableFaceRectCrop(HELOAMVIDEO video); |
||||||
|
[DllImport("eloamDll.dll", EntryPoint = "EloamGlobal_DeinitFaceDetect")] |
||||||
|
public static extern HELOAMRECT EloamGlobal_DeinitFaceDetect(); |
||||||
|
|
||||||
|
|
||||||
|
// // image |
||||||
|
[DllImport("eloamDll.dll", EntryPoint = "EloamImage_Release")] |
||||||
|
public static extern LONG EloamImage_Release(HELOAMIMAGE img); |
||||||
|
[DllImport("eloamDll.dll", EntryPoint = "EloamImage_Save", |
||||||
|
CharSet = CharSet.Unicode)] |
||||||
|
public static extern BOOL EloamImage_Save(HELOAMIMAGE img, string fileName, LONG flag); |
||||||
|
[DllImport("eloamDll.dll", EntryPoint = "EloamImage_GetWidth")] |
||||||
|
public static extern LONG EloamImage_GetWidth(HELOAMIMAGE img); |
||||||
|
[DllImport("eloamDll.dll", EntryPoint = "EloamImage_GetHeight")] |
||||||
|
public static extern LONG EloamImage_GetHeight(HELOAMIMAGE img); |
||||||
|
[DllImport("eloamDll.dll", EntryPoint = "EloamImage_Blend")] |
||||||
|
public static extern BOOL EloamImage_Blend(HELOAMIMAGE imgDest, HELOAMRECT rectDest, HELOAMIMAGE imgSrc, HELOAMRECT rectSrc, LONG weight, LONG flag); |
||||||
|
[DllImport("eloamDll.dll", EntryPoint = "EloamMemory_Release")] |
||||||
|
public static extern LONG EloamMemory_Release(HELOAMMEMORY mem); |
||||||
|
|
||||||
|
//EloamImage_DelImageBackColor |
||||||
|
[DllImport("eloamDll.dll", EntryPoint = "EloamImage_DelImageBackColor")] |
||||||
|
public static extern BOOL EloamImage_DelImageBackColor(HELOAMIMAGE image); |
||||||
|
|
||||||
|
// // device |
||||||
|
[DllImport("eloamDll.dll", EntryPoint = "EloamDevice_Release")] |
||||||
|
public static extern LONG EloamDevice_Release(HELOAMDEVICE dev); |
||||||
|
[DllImport("eloamDll.dll", EntryPoint = "EloamDevice_GetFriendlyName")] |
||||||
|
public static extern IntPtr EloamDevice_GetFriendlyName(HELOAMDEVICE dev); |
||||||
|
[DllImport("eloamDll.dll", EntryPoint = "EloamDevice_GetSubtype")] |
||||||
|
public static extern LONG EloamDevice_GetSubtype(HELOAMDEVICE dev); |
||||||
|
[DllImport("eloamDll.dll", EntryPoint = "EloamDevice_GetResolutionCount")] |
||||||
|
public static extern LONG EloamDevice_GetResolutionCount(HELOAMDEVICE dev); |
||||||
|
[DllImport("eloamDll.dll", EntryPoint = "EloamDevice_GetResolutionWidth")] |
||||||
|
public static extern LONG EloamDevice_GetResolutionWidth(HELOAMDEVICE dev, LONG idx); |
||||||
|
[DllImport("eloamDll.dll", EntryPoint = "EloamDevice_GetResolutionHeight")] |
||||||
|
public static extern LONG EloamDevice_GetResolutionHeight(HELOAMDEVICE dev, LONG idx); |
||||||
|
[DllImport("eloamDll.dll", EntryPoint = "EloamDevice_GetResolutionCountEx")] |
||||||
|
public static extern LONG EloamDevice_GetResolutionCountEx(HELOAMDEVICE dev, LONG subtype); |
||||||
|
[DllImport("eloamDll.dll", EntryPoint = "EloamDevice_GetResolutionWidthEx")] |
||||||
|
public static extern LONG EloamDevice_GetResolutionWidthEx(HELOAMDEVICE dev, LONG subtype, LONG idx); |
||||||
|
[DllImport("eloamDll.dll", EntryPoint = "EloamDevice_GetResolutionHeightEx")] |
||||||
|
public static extern LONG EloamDevice_GetResolutionHeightEx(HELOAMDEVICE dev, LONG subtype, LONG idx); |
||||||
|
[DllImport("eloamDll.dll", EntryPoint = "EloamDevice_ShowProperty")] |
||||||
|
public static extern BOOL EloamDevice_ShowProperty(HELOAMDEVICE dev, HWND hWnd); |
||||||
|
[DllImport("eloamDll.dll", EntryPoint = "EloamDevice_CreateVideo")] |
||||||
|
public static extern HELOAMVIDEO EloamDevice_CreateVideo(HELOAMDEVICE dev, LONG resolution, LONG subtype, ELOAM_ARRIVALCALLBACK funArrival, LPVOID userArrival, ELOAM_TOUCHCALLBACK funTouch, LPVOID userTouch, int resolutionStillCap, int subtypeStillCap); |
||||||
|
[DllImport("eloamDll.dll", EntryPoint = "EloamDevice_GetEloamType")] |
||||||
|
public static extern LONG EloamDevice_GetEloamType(HELOAMDEVICE dev); |
||||||
|
[DllImport("eloamDll.dll", EntryPoint = "EloamDevice_GetIndex")] |
||||||
|
public static extern LONG EloamDevice_GetIndex(HELOAMDEVICE dev); |
||||||
|
// |
||||||
|
// // video |
||||||
|
[DllImport("eloamDll.dll", EntryPoint = "EloamVideo_Release")] |
||||||
|
public static extern LONG EloamVideo_Release(HELOAMVIDEO video); |
||||||
|
[DllImport("eloamDll.dll", EntryPoint = "EloamVideo_GetDevice")] |
||||||
|
public static extern HELOAMDEVICE EloamVideo_GetDevice(HELOAMVIDEO video); |
||||||
|
[DllImport("eloamDll.dll", EntryPoint = "EloamVideo_CreateImage")] |
||||||
|
public static extern HELOAMIMAGE EloamVideo_CreateImage(HELOAMVIDEO video, LONG flag, HELOAMVIEW view); |
||||||
|
[DllImport("eloamDll.dll", EntryPoint = "EloamVideo_RotateLeft")] |
||||||
|
public static extern BOOL EloamVideo_RotateLeft(HELOAMVIDEO video); |
||||||
|
[DllImport("eloamDll.dll", EntryPoint = "EloamVideo_RotateRight")] |
||||||
|
public static extern BOOL EloamVideo_RotateRight(HELOAMVIDEO video); |
||||||
|
[DllImport("eloamDll.dll", EntryPoint = "EloamVideo_Flip")] |
||||||
|
public static extern BOOL EloamVideo_Flip(HELOAMVIDEO video); |
||||||
|
[DllImport("eloamDll.dll", EntryPoint = "EloamVideo_Mirror")] |
||||||
|
public static extern BOOL EloamVideo_Mirror(HELOAMVIDEO video); |
||||||
|
[DllImport("eloamDll.dll", EntryPoint = "EloamVideo_EnableDelBkColor")] |
||||||
|
public static extern BOOL EloamVideo_EnableDelBkColor(HELOAMVIDEO video, LONG flag); |
||||||
|
[DllImport("eloamDll.dll", EntryPoint = "EloamVideo_DisableDelBkColor")] |
||||||
|
public static extern BOOL EloamVideo_DisableDelBkColor(HELOAMVIDEO video); |
||||||
|
[DllImport("eloamDll.dll", EntryPoint = "EloamVideo_EnableDeskew")] |
||||||
|
public static extern BOOL EloamVideo_EnableDeskew(HELOAMVIDEO video, LONG flag, int nOffset); |
||||||
|
[DllImport("eloamDll.dll", EntryPoint = "EloamVideo_DisableDeskew")] |
||||||
|
public static extern BOOL EloamVideo_DisableDeskew(HELOAMVIDEO video); |
||||||
|
[DllImport("eloamDll.dll", EntryPoint = "EloamVideo_EnableMoveDetec")] |
||||||
|
public static extern BOOL EloamVideo_EnableMoveDetec(HELOAMVIDEO video, LONG flag, ELOAM_MOVEDETECCALLBACK fun, LPVOID userData); |
||||||
|
[DllImport("eloamDll.dll", EntryPoint = "EloamVideo_DisableMoveDetec")] |
||||||
|
public static extern BOOL EloamVideo_DisableMoveDetec(HELOAMVIDEO video); |
||||||
|
[DllImport("eloamDll.dll", EntryPoint = "EloamVideo_GetWidth")] |
||||||
|
public static extern LONG EloamVideo_GetWidth(HELOAMVIDEO video); |
||||||
|
[DllImport("eloamDll.dll", EntryPoint = "EloamVideo_GetHeight")] |
||||||
|
public static extern LONG EloamVideo_GetHeight(HELOAMVIDEO video); |
||||||
|
|
||||||
|
// view |
||||||
|
[DllImport("eloamDll.dll", EntryPoint = "EloamView_Release")] |
||||||
|
public static extern LONG EloamView_Release(HELOAMVIEW view); |
||||||
|
[DllImport("eloamDll.dll", EntryPoint = "EloamView_SelectVideo")] |
||||||
|
public static extern BOOL EloamView_SelectVideo(HELOAMVIEW view, HELOAMVIDEO video, ELOAM_ATTACHCALLBAK fun, LPVOID userData); |
||||||
|
[DllImport("eloamDll.dll", EntryPoint = "EloamView_SetText", |
||||||
|
CharSet = CharSet.Unicode)] |
||||||
|
public static extern BOOL EloamView_SetText(HELOAMVIEW view, string text, COLORREF clr); |
||||||
|
[DllImport("eloamDll.dll", EntryPoint = "EloamView_PlayCaptureEffect")] |
||||||
|
public static extern BOOL EloamView_PlayCaptureEffect(HELOAMVIEW view); |
||||||
|
|
||||||
|
// rect |
||||||
|
[DllImport("eloamDll.dll", EntryPoint = "EloamRect_Release")] |
||||||
|
public static extern LONG EloamRect_Release(HELOAMRECT rect); |
||||||
|
|
||||||
|
// thumbnail |
||||||
|
[DllImport("eloamDll.dll", EntryPoint = "EloamThumbnail_Release")] |
||||||
|
public static extern LONG EloamThumbnail_Release(HELOAMTHUMBNAIL thumb); |
||||||
|
[DllImport("eloamDll.dll", EntryPoint = "EloamThumbnail_Add", |
||||||
|
CharSet = CharSet.Unicode)] |
||||||
|
public static extern BOOL EloamThumbnail_Add(HELOAMTHUMBNAIL thumb, string imagePath); |
||||||
|
|
||||||
|
//OCR --start |
||||||
|
[DllImport("eloamDll.dll", EntryPoint = "EloamGlobal_InitOcr", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)] |
||||||
|
public static extern BOOL EloamGlobal_InitOcr(); |
||||||
|
|
||||||
|
[DllImport("eloamDll.dll", EntryPoint = "EloamGlobal_DeinitOcr", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)] |
||||||
|
public static extern BOOL EloamGlobal_DeinitOcr(); |
||||||
|
|
||||||
|
[DllImport("eloamDll.dll", EntryPoint = "EloamGlobal_DiscernOcr", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)] |
||||||
|
public static extern BOOL EloamGlobal_DiscernOcr(LONG flag, HELOAMIMAGE img, ELOAM_OCRCALLBACK fun, LPVOID userData); |
||||||
|
|
||||||
|
[DllImport("eloamDll.dll", EntryPoint = "EloamGlobal_WaitOcrDiscern", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)] |
||||||
|
public static extern BOOL EloamGlobal_WaitOcrDiscern(); |
||||||
|
|
||||||
|
[DllImport("eloamDll.dll", EntryPoint = "EloamGlobal_SaveOcr", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)] |
||||||
|
public static extern BOOL EloamGlobal_SaveOcr(string filename, LONG flag); |
||||||
|
|
||||||
|
[DllImport("eloamDll.dll", EntryPoint = "EloamGlobal_StopOcrDiscern", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.StdCall)] |
||||||
|
public static extern BOOL EloamGlobal_StopOcrDiscern(); |
||||||
|
//OCR --end |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,218 @@ |
|||||||
|
|
||||||
|
using DevicesService.Commen; |
||||||
|
using Functions.FileExt; |
||||||
|
using System; |
||||||
|
using System.Collections.Generic; |
||||||
|
using System.Configuration; |
||||||
|
using System.Drawing; |
||||||
|
using System.Globalization; |
||||||
|
using System.IO; |
||||||
|
using System.Linq; |
||||||
|
using System.Net.Http; |
||||||
|
using System.Text; |
||||||
|
using System.Threading.Tasks; |
||||||
|
|
||||||
|
namespace DevicesService.Devices |
||||||
|
{ |
||||||
|
using HELOAMIMAGE = IntPtr; |
||||||
|
using HELOAMIMAGELIST = IntPtr; |
||||||
|
using HELOAMFTP = IntPtr; |
||||||
|
using HELOAMHTTP = IntPtr; |
||||||
|
using HELOAMDEVICE = IntPtr; |
||||||
|
using HELOAMVIDEO = IntPtr; |
||||||
|
using HELOAMVIEW = IntPtr; |
||||||
|
using HELOAMMEMORY = IntPtr; |
||||||
|
using HELOAMBASE64 = IntPtr; |
||||||
|
using HELOAMTHUMBNAIL = IntPtr; |
||||||
|
using HELOAMRECT = IntPtr; |
||||||
|
using HELOAMFONT = IntPtr; |
||||||
|
using HELOAMVIDEOCAP = IntPtr; |
||||||
|
using LPVOID = IntPtr; |
||||||
|
using LONG = Int32; |
||||||
|
using BOOL = Int32; |
||||||
|
using HWND = IntPtr; |
||||||
|
using COLORREF = UInt32; |
||||||
|
|
||||||
|
// callback |
||||||
|
public delegate void ELOAM_DEVCHANGECALLBACK(LONG type, LONG idx, LONG dbt, LPVOID userData); |
||||||
|
public delegate void ELOAM_ARRIVALCALLBACK(HELOAMVIDEO video, LONG id, LPVOID userData); |
||||||
|
public delegate void ELOAM_TOUCHCALLBACK(HELOAMVIDEO video, LPVOID userData); |
||||||
|
public delegate void ELOAM_MOVEDETECCALLBACK(HELOAMVIDEO video, LONG id, LPVOID userData); |
||||||
|
public delegate void ELOAM_ATTACHCALLBAK(HELOAMVIDEO video, LONG videoId, HELOAMVIEW view, LONG viewId, LPVOID userData); |
||||||
|
|
||||||
|
public delegate void ELOAM_IDCARDCHANGECALLBACK(LONG dbt, LPVOID userData); |
||||||
|
//ocr |
||||||
|
public delegate void ELOAM_OCRCALLBACK(LONG flag, LONG value, LPVOID userData); |
||||||
|
public class HScamera |
||||||
|
{ |
||||||
|
HELOAMVIEW m_hView; |
||||||
|
HELOAMTHUMBNAIL m_hThumb; |
||||||
|
|
||||||
|
//设别列表(专指视频) |
||||||
|
List<HELOAMDEVICE> m_vDevice; |
||||||
|
HELOAMVIDEO m_hVideo; |
||||||
|
HELOAMVIDEOCAP m_cap; |
||||||
|
//合成时的临时图片 |
||||||
|
HELOAMIMAGE m_hImageTemp; |
||||||
|
|
||||||
|
//定义系统计时器 |
||||||
|
private System.Timers.Timer timer; |
||||||
|
private int timer_value; |
||||||
|
|
||||||
|
ELOAM_DEVCHANGECALLBACK DevChangeCallBack; |
||||||
|
ELOAM_ATTACHCALLBAK attachCallback; |
||||||
|
private readonly int devIdx; |
||||||
|
private readonly int resIdx; |
||||||
|
private readonly int modeIdx; |
||||||
|
private readonly int turnRight = Convert.ToInt32(ConfigurationManager.AppSettings["xzds"].ToString()); |
||||||
|
public HScamera(int selectDevice, int selectResolution, int selectMode) |
||||||
|
{ |
||||||
|
m_hView = IntPtr.Zero; |
||||||
|
m_hThumb = IntPtr.Zero; |
||||||
|
m_hView = IntPtr.Zero; |
||||||
|
m_hImageTemp = IntPtr.Zero; |
||||||
|
m_vDevice = new List<HELOAMDEVICE>(); |
||||||
|
this.devIdx = selectDevice; |
||||||
|
this.resIdx = selectResolution; |
||||||
|
this.modeIdx = selectMode; |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
/// <summary> |
||||||
|
/// 打开高拍仪 |
||||||
|
/// </summary> |
||||||
|
/// <returns></returns> |
||||||
|
public string openCamera2() |
||||||
|
{ |
||||||
|
try |
||||||
|
{ |
||||||
|
string filename = string.Empty; |
||||||
|
//初始化高拍仪 |
||||||
|
if (m_vDevice.Count == 0) |
||||||
|
{ |
||||||
|
DevChangeCallBack = new ELOAM_DEVCHANGECALLBACK(DEVCHANGECALLBACK); |
||||||
|
EloamDll.EloamGlobal_InitDevs(DevChangeCallBack, IntPtr.Zero); |
||||||
|
EloamDll.EloamGlobal_VideoCapInit(); |
||||||
|
EloamDll.EloamGlobal_InitFaceDetect(); |
||||||
|
} |
||||||
|
//打开高拍仪 |
||||||
|
EloamDll.EloamVideo_Release(m_hVideo); |
||||||
|
HELOAMDEVICE hDev = m_vDevice[devIdx]; |
||||||
|
m_hVideo = EloamDll.EloamDevice_CreateVideo( |
||||||
|
hDev, resIdx, modeIdx, null, (IntPtr)0, null, (IntPtr)0, 0, 2); |
||||||
|
//开始拍照 |
||||||
|
HELOAMIMAGE hImg = EloamDll.EloamVideo_CreateImage( |
||||||
|
m_hVideo, 0, m_hView); |
||||||
|
if (IntPtr.Zero != hImg) |
||||||
|
{ |
||||||
|
DateTime dateTime = DateTime.Now; |
||||||
|
string time = DateTime.Now.ToString( |
||||||
|
"yyyyMMddHHmmss", DateTimeFormatInfo.InvariantInfo); |
||||||
|
var dirpath = Path.Combine(Environment.CurrentDirectory, "wwwroot", "Camera"); |
||||||
|
FileExt.MakeSureDirExist(dirpath); |
||||||
|
var filepath = Path.Combine(dirpath, time); |
||||||
|
filename = dirpath + "\\" + time + ".jpg"; |
||||||
|
if (1 == EloamDll.EloamImage_Save(hImg, filename, 0)) |
||||||
|
{ |
||||||
|
EloamDll.EloamView_PlayCaptureEffect(m_hView); |
||||||
|
} |
||||||
|
EloamDll.EloamImage_Release(hImg); |
||||||
|
//旋转图片 |
||||||
|
//filename = ImgToBase64String(filename); |
||||||
|
} |
||||||
|
return filename; |
||||||
|
} |
||||||
|
catch (Exception ex) |
||||||
|
{ |
||||||
|
Log.Info("扫描图片旋转异常1:" + ex.Message); |
||||||
|
return ""; |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
//加载设备 |
||||||
|
public void DEVCHANGECALLBACK(LONG type, LONG idx, LONG dbt, LPVOID userData) |
||||||
|
{ |
||||||
|
HELOAMDEVICE hDev = EloamDll.EloamGlobal_CreateDevice(1, idx); |
||||||
|
m_vDevice.Add(hDev); |
||||||
|
} |
||||||
|
|
||||||
|
/// <summary> |
||||||
|
/// 文件转Base64码 |
||||||
|
/// </summary> |
||||||
|
/// <param name="fileLocation"></param> |
||||||
|
/// <returns></returns> |
||||||
|
public string ImgToBase64String(string fileLocation) |
||||||
|
{ |
||||||
|
try |
||||||
|
{ |
||||||
|
MemoryStream ms = new MemoryStream(); |
||||||
|
if (System.IO.File.Exists(fileLocation)) |
||||||
|
{ |
||||||
|
Bitmap bmp = new Bitmap(fileLocation); |
||||||
|
BitmapFlip(turnRight, ref bmp); |
||||||
|
bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg); |
||||||
|
byte[] arr = new byte[ms.Length]; |
||||||
|
ms.Position = 0; |
||||||
|
ms.Read(arr, 0, (int)ms.Length); |
||||||
|
string base64 = Convert.ToBase64String(arr); |
||||||
|
DateTime dateTime = DateTime.Now; |
||||||
|
string time = DateTime.Now.ToString( |
||||||
|
"yyyyMMddHHmmss", DateTimeFormatInfo.InvariantInfo); |
||||||
|
var dirpath = Path.Combine(Environment.CurrentDirectory, "wwwroot", "Camera"); |
||||||
|
FileExt.MakeSureDirExist(dirpath); |
||||||
|
var filepath = Path.Combine(dirpath, time); |
||||||
|
string filename = dirpath + "\\" + time + ".jpg"; |
||||||
|
ConvertFromBase64ToImage(base64, filename); |
||||||
|
//ms.Close(); |
||||||
|
//File.Delete(fileLocation); |
||||||
|
return filename; |
||||||
|
} |
||||||
|
return ""; |
||||||
|
} |
||||||
|
catch (Exception ex) |
||||||
|
{ |
||||||
|
Log.Info("扫描图片旋转异常:" + ex.Message); |
||||||
|
return ex.Message; |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
/// <summary> |
||||||
|
/// 图像翻转,基于图像中心 |
||||||
|
/// </summary> |
||||||
|
public void BitmapFlip(int iFlip, ref Bitmap btDes) |
||||||
|
{ |
||||||
|
switch (iFlip) |
||||||
|
{ |
||||||
|
case 0: |
||||||
|
break; |
||||||
|
case 90: |
||||||
|
btDes.RotateFlip(RotateFlipType.Rotate90FlipNone); |
||||||
|
break; |
||||||
|
case 180: |
||||||
|
btDes.RotateFlip(RotateFlipType.Rotate180FlipNone); |
||||||
|
break; |
||||||
|
case 270: |
||||||
|
btDes.RotateFlip(RotateFlipType.Rotate270FlipNone); |
||||||
|
break; |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
public void ConvertFromBase64ToImage(string base64String, string filePath) |
||||||
|
{ |
||||||
|
// 将Base64字符串转换为字节数组 |
||||||
|
byte[] imageBytes = Convert.FromBase64String(base64String); |
||||||
|
|
||||||
|
// 使用MemoryStream将字节数组转换为图片 |
||||||
|
using (var ms = new MemoryStream(imageBytes, 0, imageBytes.Length)) |
||||||
|
{ |
||||||
|
// 使用Bitmap从MemoryStream创建图片 |
||||||
|
using (Bitmap image = new Bitmap(ms)) |
||||||
|
{ |
||||||
|
// 保存图片到文件 |
||||||
|
image.Save(filePath, System.Drawing.Imaging.ImageFormat.Png); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,286 @@ |
|||||||
|
using System; |
||||||
|
using System.Collections.Generic; |
||||||
|
using System.ComponentModel; |
||||||
|
using System.Data; |
||||||
|
using System.Drawing; |
||||||
|
using System.Linq; |
||||||
|
using System.Text; |
||||||
|
using System.Threading.Tasks; |
||||||
|
using System.Runtime.InteropServices; |
||||||
|
using System.Globalization; |
||||||
|
using Functions.FileExt; |
||||||
|
using System.IO; |
||||||
|
//0816加实时报点 |
||||||
|
[StructLayout(LayoutKind.Sequential)] |
||||||
|
public struct TOUCH_INFO |
||||||
|
{ |
||||||
|
public int X; |
||||||
|
public int Y; |
||||||
|
public int Pressure; |
||||||
|
public int SN; |
||||||
|
public int btnID;//5寸 确定 重签 取消按钮 |
||||||
|
} |
||||||
|
namespace DevicesService.Devices |
||||||
|
{ |
||||||
|
public class SignDll |
||||||
|
{ |
||||||
|
public static PointF endPos; |
||||||
|
public static int status = -1; |
||||||
|
public static PointF beginPos; |
||||||
|
public static PointF frontPos; |
||||||
|
private static int xypointcount; |
||||||
|
private static int[] lastpointx = new int[3]; |
||||||
|
private static int[] lastpointy = new int[3]; |
||||||
|
Point mPoint = new Point(-1, -1); |
||||||
|
private System.Drawing.SolidBrush myBrush = new System.Drawing.SolidBrush(System.Drawing.Color.Red); |
||||||
|
private Bitmap bitmap = new Bitmap(594, 392); |
||||||
|
private static Graphics g; |
||||||
|
private static Pen pen = new Pen(Color.Red); |
||||||
|
|
||||||
|
public SignDll() |
||||||
|
{ |
||||||
|
endPos = new PointF(-1F, -1f); |
||||||
|
beginPos = new PointF(-1F, -1f); |
||||||
|
frontPos = new PointF(-1F, -1f); |
||||||
|
lastpointx[0] = -1; |
||||||
|
lastpointy[0] = -1; |
||||||
|
lastpointx[1] = -1; |
||||||
|
lastpointy[1] = -1; |
||||||
|
lastpointx[2] = -1; |
||||||
|
lastpointy[2] = -1; |
||||||
|
xypointcount = 0; |
||||||
|
g = Graphics.FromImage(bitmap); |
||||||
|
|
||||||
|
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias; |
||||||
|
} |
||||||
|
|
||||||
|
public static void GetTouchNumber(int number) |
||||||
|
{ |
||||||
|
if (number == 1) |
||||||
|
{ |
||||||
|
int ret = FiveInchDll.ComSignOK(); |
||||||
|
} |
||||||
|
else |
||||||
|
{ |
||||||
|
g.Clear(System.Drawing.Color.White); |
||||||
|
lastpointx[0] = -1; |
||||||
|
lastpointy[0] = -1; |
||||||
|
lastpointx[1] = -1; |
||||||
|
lastpointy[1] = -1; |
||||||
|
lastpointx[2] = -1; |
||||||
|
lastpointy[2] = -1; |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
//0816加报点 |
||||||
|
public static void GetTouchPoint(TOUCH_INFO[] info1) |
||||||
|
{ |
||||||
|
int x = 0, y = 0; |
||||||
|
int pressurevl; |
||||||
|
int dx = 0, dy = 0; |
||||||
|
|
||||||
|
for (int k = 0; k < 80; k++) |
||||||
|
{ |
||||||
|
x = info1[k].X; |
||||||
|
y = info1[k].Y; |
||||||
|
if (info1[k].Pressure > 0) |
||||||
|
{ |
||||||
|
if (info1[k].Pressure > 0 && info1[k].Pressure < 500) |
||||||
|
{ |
||||||
|
pressurevl = 1; |
||||||
|
pen.Width = 1; |
||||||
|
} |
||||||
|
else if (info1[k].Pressure >= 500 && info1[k].Pressure < 1000) |
||||||
|
{ |
||||||
|
pressurevl = 2; |
||||||
|
pen.Width = 2; |
||||||
|
} |
||||||
|
else if (info1[k].Pressure >= 1000 && info1[k].Pressure < 1500) |
||||||
|
{ |
||||||
|
pressurevl = 3; |
||||||
|
pen.Width = 3; |
||||||
|
} |
||||||
|
else if (info1[k].Pressure >= 1500 && info1[k].Pressure < 2048) |
||||||
|
{ |
||||||
|
pressurevl = 4; |
||||||
|
pen.Width = 4; |
||||||
|
} |
||||||
|
else |
||||||
|
{ |
||||||
|
pressurevl = 0; |
||||||
|
pen.Width = 1; |
||||||
|
} |
||||||
|
} |
||||||
|
else |
||||||
|
{ |
||||||
|
pressurevl = 0; |
||||||
|
|
||||||
|
lastpointx[0] = -1; |
||||||
|
lastpointy[0] = -1; |
||||||
|
lastpointx[1] = -1; |
||||||
|
lastpointy[1] = -1; |
||||||
|
lastpointx[2] = -1; |
||||||
|
lastpointy[2] = -1; |
||||||
|
continue; |
||||||
|
} |
||||||
|
if (info1[k].Pressure > 10) //有画线宽度 |
||||||
|
{ |
||||||
|
lastpointx[2] = x; |
||||||
|
lastpointy[2] = y; |
||||||
|
|
||||||
|
if (lastpointx[2] != -1) |
||||||
|
{ |
||||||
|
if (lastpointx[1] != -1 && lastpointx[0] != -1) |
||||||
|
{ |
||||||
|
//float dx = Math.Abs(lastpointx[2] - beginPos.X); |
||||||
|
//float dy = Math.Abs(endPos.Y - beginPos.Y); |
||||||
|
|
||||||
|
dx = Math.Abs(lastpointx[2] - lastpointx[1]); |
||||||
|
dy = Math.Abs(lastpointy[2] - lastpointy[1]); |
||||||
|
if ((dx != 0) && (dy != 0)) |
||||||
|
{ |
||||||
|
|
||||||
|
if (lastpointy[1] != -1 && lastpointy[2] != -1) //y轴相同的点不画,直接跳过 |
||||||
|
{ |
||||||
|
if (lastpointx[1] != -1 && lastpointx[2] != -1) //第3个点和第二个点比较是否x坐标在同一个位置,不是就执行画第一个点到第二个点的线 |
||||||
|
{ |
||||||
|
//painter->drawLine(frontPos, beginPos); //画线 |
||||||
|
g.DrawLine(pen, lastpointx[0], lastpointy[0], lastpointx[1], lastpointy[1]); |
||||||
|
//painter->drawPoint(beginPos); //画点 |
||||||
|
//frontPos = beginPos; |
||||||
|
//beginPos = endPos; |
||||||
|
lastpointx[0] = lastpointx[1]; |
||||||
|
lastpointy[0] = lastpointy[1]; |
||||||
|
lastpointx[1] = lastpointx[2]; |
||||||
|
lastpointy[1] = lastpointy[2]; |
||||||
|
} |
||||||
|
else |
||||||
|
{ |
||||||
|
//是就执行画第一个点到第三个点的线 |
||||||
|
//painter->drawLine(frontPos, endPos); |
||||||
|
g.DrawLine(pen, lastpointx[0], lastpointy[0], lastpointx[2], lastpointy[2]); |
||||||
|
//frontPos = endPos; //第三个点赋值第一个点 |
||||||
|
//beginPos = QPointF(0, 0); //第二个点置空 |
||||||
|
//beginPos.X = -1; |
||||||
|
//beginPos.Y = -1; |
||||||
|
lastpointx[0] = lastpointx[2]; |
||||||
|
lastpointy[0] = lastpointy[2]; |
||||||
|
lastpointx[1] = -1; |
||||||
|
lastpointy[1] = -1; |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
}// |
||||||
|
else |
||||||
|
{ |
||||||
|
if (lastpointx[1] != -1) //不为空在赋值,防止丢弃点时赋空值 |
||||||
|
{ |
||||||
|
lastpointx[0] = lastpointx[1]; |
||||||
|
lastpointy[0] = lastpointy[1]; |
||||||
|
} |
||||||
|
lastpointx[1] = lastpointx[2]; |
||||||
|
lastpointy[1] = lastpointy[2]; |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
else |
||||||
|
{ |
||||||
|
dx = dy = 0; |
||||||
|
lastpointx[0] = -1; |
||||||
|
lastpointy[0] = -1; |
||||||
|
lastpointx[1] = -1; |
||||||
|
lastpointy[1] = -1; |
||||||
|
lastpointx[2] = -1; |
||||||
|
lastpointy[2] = -1; |
||||||
|
|
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
//打开签字版 |
||||||
|
public static int OpenComDevice() |
||||||
|
{ |
||||||
|
if (status == -1) |
||||||
|
{ |
||||||
|
status = FiveInchDll.OpenComDevice(GetTouchNumber); |
||||||
|
if (status != 0) |
||||||
|
{ |
||||||
|
status = -1; |
||||||
|
} |
||||||
|
} |
||||||
|
return status; |
||||||
|
} |
||||||
|
|
||||||
|
//关闭签字版 |
||||||
|
public static int CloseComDevice() |
||||||
|
{ |
||||||
|
int ret = -1; |
||||||
|
if (status == 0) |
||||||
|
{ |
||||||
|
ret = FiveInchDll.CloseComDevice(); |
||||||
|
if (ret == 0) |
||||||
|
{ |
||||||
|
status = -1; |
||||||
|
} |
||||||
|
} |
||||||
|
return ret; |
||||||
|
} |
||||||
|
|
||||||
|
//保存签字版数据 |
||||||
|
public static string ComSetPictureSavePath() |
||||||
|
{ |
||||||
|
try |
||||||
|
{ |
||||||
|
DateTime dateTime = DateTime.Now; |
||||||
|
string time = DateTime.Now.ToString( |
||||||
|
"yyyyMMddHHmmss", DateTimeFormatInfo.InvariantInfo); |
||||||
|
var dirpath = Path.Combine(Environment.CurrentDirectory, "wwwroot", "Sign"); |
||||||
|
FileExt.MakeSureDirExist(dirpath); |
||||||
|
var filepath = Path.Combine(dirpath, time); |
||||||
|
string SignFile = dirpath + "\\" + time + ".png"; |
||||||
|
string sourcepaht = Environment.CurrentDirectory + "\\fiveInch.png"; |
||||||
|
int ret = FiveInchDll.ComSignOK(); |
||||||
|
if (ret == 0) |
||||||
|
{ |
||||||
|
return SignFile; |
||||||
|
} |
||||||
|
else |
||||||
|
{ |
||||||
|
return ""; |
||||||
|
} |
||||||
|
} |
||||||
|
catch (Exception ex) |
||||||
|
{ |
||||||
|
return ""; |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
class FiveInchDll |
||||||
|
{ |
||||||
|
[System.Runtime.InteropServices.UnmanagedFunctionPointerAttribute(System.Runtime.InteropServices.CallingConvention.StdCall)] |
||||||
|
public delegate void GetTouchNumber(int number); |
||||||
|
|
||||||
|
//0816加报点 |
||||||
|
[System.Runtime.InteropServices.UnmanagedFunctionPointerAttribute(System.Runtime.InteropServices.CallingConvention.StdCall)] |
||||||
|
public delegate void TOUCH_INFO_FUNC([MarshalAs(UnmanagedType.LPArray, SizeConst = 160)] TOUCH_INFO[] info); |
||||||
|
|
||||||
|
[DllImport("XTJZFiveInch.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)] |
||||||
|
public static extern int ComSendPoint(int nState, [MarshalAs(UnmanagedType.FunctionPtr)] TOUCH_INFO_FUNC callback); //0816加报点 |
||||||
|
|
||||||
|
[DllImport("XTJZFiveInch.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)] |
||||||
|
public static extern int OpenComDevice([MarshalAs(UnmanagedType.FunctionPtr)] GetTouchNumber callback); |
||||||
|
|
||||||
|
[DllImport("XTJZFiveInch.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)] |
||||||
|
public static extern int CloseComDevice(); |
||||||
|
|
||||||
|
[DllImport("XTJZFiveInch.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)] |
||||||
|
public static extern int ComSetSignBackgroundImage(string UIFile); |
||||||
|
|
||||||
|
[DllImport("XTJZFiveInch.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)] |
||||||
|
public static extern int ComSetPictureSavePath(string PicturePath, int PicturePathLen); |
||||||
|
|
||||||
|
[DllImport("XTJZFiveInch.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)] |
||||||
|
public static extern int ComSignOK(); |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,82 @@ |
|||||||
|
<Project Sdk="Microsoft.NET.Sdk"> |
||||||
|
|
||||||
|
<PropertyGroup> |
||||||
|
<OutputType>WinExe</OutputType> |
||||||
|
<TargetFramework>net6.0</TargetFramework> |
||||||
|
<ApplicationIcon>favicon.ico</ApplicationIcon> |
||||||
|
<StartupObject /> |
||||||
|
<LangVersion>9.0</LangVersion> |
||||||
|
<AssemblyName>DevicesService</AssemblyName> |
||||||
|
<Platforms>AnyCPU;x86</Platforms> |
||||||
|
<PlatformTarget>x86</PlatformTarget> |
||||||
|
</PropertyGroup> |
||||||
|
|
||||||
|
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'"> |
||||||
|
<AllowUnsafeBlocks>true</AllowUnsafeBlocks> |
||||||
|
<DefineConstants /> |
||||||
|
<PlatformTarget>AnyCPU</PlatformTarget> |
||||||
|
</PropertyGroup> |
||||||
|
|
||||||
|
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x86'"> |
||||||
|
<AllowUnsafeBlocks>true</AllowUnsafeBlocks> |
||||||
|
<DefineConstants /> |
||||||
|
<PlatformTarget>AnyCPU</PlatformTarget> |
||||||
|
</PropertyGroup> |
||||||
|
|
||||||
|
<ItemGroup> |
||||||
|
<None Remove="Commen\COMUtils.cs~RF189d1fe.TMP" /> |
||||||
|
</ItemGroup> |
||||||
|
|
||||||
|
<ItemGroup> |
||||||
|
<Content Include="favicon.ico" /> |
||||||
|
</ItemGroup> |
||||||
|
|
||||||
|
<ItemGroup> |
||||||
|
<PackageReference Include="Aspose.Words" Version="19.11.0" /> |
||||||
|
<PackageReference Include="NAudio" Version="2.2.1" /> |
||||||
|
<PackageReference Include="NAudio.Asio" Version="2.2.1" /> |
||||||
|
<PackageReference Include="NAudio.Core" Version="2.2.1" /> |
||||||
|
<PackageReference Include="NAudio.Midi" Version="2.2.1" /> |
||||||
|
<PackageReference Include="NAudio.Wasapi" Version="2.2.1" /> |
||||||
|
<PackageReference Include="NAudio.WinMM" Version="2.2.1" /> |
||||||
|
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" /> |
||||||
|
<PackageReference Include="System.Configuration.ConfigurationManager" Version="8.0.0" /> |
||||||
|
<PackageReference Include="System.Drawing.Common" Version="8.0.4" /> |
||||||
|
<PackageReference Include="System.IO" Version="4.3.0" /> |
||||||
|
<PackageReference Include="System.IO.Ports" Version="8.0.0" /> |
||||||
|
<PackageReference Include="System.Net.Http" Version="4.3.4" /> |
||||||
|
<PackageReference Include="System.Speech" Version="8.0.0" /> |
||||||
|
<PackageReference Include="System.Text.Encoding.CodePages" Version="8.0.0" /> |
||||||
|
</ItemGroup> |
||||||
|
|
||||||
|
<ItemGroup> |
||||||
|
<Reference Include="Device.ApiCommonModel"> |
||||||
|
<HintPath>C:\Users\admin\source\repos\DevicesService\DevicesService\bin\x86\Debug\Device.ApiCommonModel.dll</HintPath> |
||||||
|
</Reference> |
||||||
|
<Reference Include="Device.ApiModel"> |
||||||
|
<HintPath>C:\Users\admin\source\repos\DevicesService\DevicesService\bin\x86\Debug\Device.ApiModel.dll</HintPath> |
||||||
|
</Reference> |
||||||
|
<Reference Include="Device.Caller"> |
||||||
|
<HintPath>C:\Users\admin\source\repos\DevicesService\DevicesService\bin\x86\Debug\Device.Caller.dll</HintPath> |
||||||
|
</Reference> |
||||||
|
<Reference Include="Device.IICCardReader"> |
||||||
|
<HintPath>C:\Users\admin\source\repos\DevicesService\DevicesService\bin\x86\Debug\Device.IICCardReader.dll</HintPath> |
||||||
|
</Reference> |
||||||
|
<Reference Include="Device.TextMessager"> |
||||||
|
<HintPath>C:\Users\admin\source\repos\DevicesService\DevicesService\bin\x86\Debug\Device.TextMessager.dll</HintPath> |
||||||
|
</Reference> |
||||||
|
<Reference Include="DeviceBase"> |
||||||
|
<HintPath>C:\Users\admin\source\repos\DevicesService\DevicesService\bin\x86\Debug\DeviceBase.dll</HintPath> |
||||||
|
</Reference> |
||||||
|
<Reference Include="Functions"> |
||||||
|
<HintPath>C:\Users\admin\source\repos\DevicesService\DevicesService\bin\x86\Debug\Functions.dll</HintPath> |
||||||
|
</Reference> |
||||||
|
<Reference Include="Functions.PDF"> |
||||||
|
<HintPath>C:\Users\admin\source\repos\DevicesService\DevicesService\bin\x86\Debug\Functions.PDF.dll</HintPath> |
||||||
|
</Reference> |
||||||
|
<Reference Include="IDevice"> |
||||||
|
<HintPath>C:\Users\admin\source\repos\DevicesService\DevicesService\bin\x86\Debug\IDevice.dll</HintPath> |
||||||
|
</Reference> |
||||||
|
</ItemGroup> |
||||||
|
|
||||||
|
</Project> |
@ -0,0 +1,40 @@ |
|||||||
|
using DevicesService.Commen; |
||||||
|
using DevicesService.Common; |
||||||
|
using System; |
||||||
|
using System.Diagnostics; |
||||||
|
using System.Runtime.InteropServices; |
||||||
|
using System.Threading; |
||||||
|
using System.Threading.Tasks; |
||||||
|
|
||||||
|
namespace DevicesService |
||||||
|
{ |
||||||
|
internal class Program |
||||||
|
{ |
||||||
|
/// <summary> |
||||||
|
/// 应用程序的主入口点。 |
||||||
|
/// </summary> |
||||||
|
[STAThread] |
||||||
|
static void Main(string[] args) |
||||||
|
{ |
||||||
|
// 更改为你想检查的进程名称 |
||||||
|
new COMUtils(); |
||||||
|
while (true) |
||||||
|
{ |
||||||
|
Task.Delay(2000).Wait(); |
||||||
|
} |
||||||
|
} |
||||||
|
static bool IsProgramRunning(string programName) |
||||||
|
{ |
||||||
|
Process[] processes = Process.GetProcesses(); |
||||||
|
foreach (Process process in processes) |
||||||
|
{ |
||||||
|
if (process.ProcessName.Equals(programName)) |
||||||
|
{ |
||||||
|
return true; |
||||||
|
} |
||||||
|
} |
||||||
|
return false; |
||||||
|
} |
||||||
|
|
||||||
|
} |
||||||
|
} |
After Width: | Height: | Size: 9.4 KiB |
Loading…
Reference in new issue