public FaceChannel StartFaceChannel( string strUrl, out string strError) { strError = ""; FaceChannel result = new FaceChannel(); result.Channel = new IpcClientChannel(Guid.NewGuid().ToString(), // 随机的名字,令多个 Channel 对象可以并存 new BinaryClientFormatterSinkProvider()); ChannelServices.RegisterChannel(result.Channel, true); bool bDone = false; try { result.Object = (IBioRecognition)Activator.GetObject(typeof(IBioRecognition), strUrl); if (result.Object == null) { strError = "无法连接到服务器 " + strUrl; return(null); } bDone = true; return(result); } finally { if (bDone == false) { EndFaceChannel(result); } } }
public void EndFaceChannel(FaceChannel channel) { if (channel != null && channel.Channel != null) { ChannelServices.UnregisterChannel(channel.Channel); channel.Channel = null; } }
public async Task <NormalResult> FaceGetState(string strStyle) { if (string.IsNullOrEmpty(Program.MainForm.FaceReaderUrl) == true) { return(new NormalResult { Value = -1, ErrorInfo = "尚未配置 人脸识别接口URL 系统参数,无法读取人脸信息" }); } FaceChannel channel = StartFaceChannel( Program.MainForm.FaceReaderUrl, out string strError); if (channel == null) { return new NormalResult { Value = -1, ErrorInfo = strError } } ; try { return(await Task.Factory.StartNew <NormalResult>( () => { return channel.Object.GetState(strStyle); })); } catch (Exception ex) { strError = "针对 " + Program.MainForm.FaceReaderUrl + " 的 GetState() 操作失败: " + ex.Message; return(new NormalResult { Value = -1, ErrorInfo = strError }); } finally { EndFaceChannel(channel); } }
// 人脸识别 API RecognitionFaceResult Recognition(string url, string style) { FaceChannel channel = StartFaceChannel( url, out string strError); if (channel == null) { return new RecognitionFaceResult { Value = -1, ErrorInfo = strError } } ; try { return(channel.Object.RecognitionFace(style)); } catch (Exception ex) { strError = $"针对 {url} 的 RecongitionFace() 操作失败: { ex.Message}"; return(new RecognitionFaceResult { Value = -1, ErrorInfo = strError }); } finally { EndFaceChannel(channel); } } // 取消人脸识别任务 API NormalResult CancelRecognition(string url) { FaceChannel channel = StartFaceChannel( url, out string strError); if (channel == null) { return new NormalResult { Value = -1, ErrorInfo = strError } } ; try { return(channel.Object.CancelRecognitionFace()); } catch (Exception ex) { strError = $"针对 {url} 的 CancelRecognitionFace() 操作失败: { ex.Message}"; return(new RecognitionFaceResult { Value = -1, ErrorInfo = strError }); } finally { EndFaceChannel(channel); } } NormalResult DisplayVideo(string url, CancellationToken token) { FaceChannel channel = StartFaceChannel( url, out string strError); if (channel == null) { return new NormalResult { Value = -1, ErrorInfo = strError } } ; try { while (token.IsCancellationRequested == false) { var result = channel.Object.GetImage(""); if (result.Value == -1) { return(result); } using (MemoryStream stream = new MemoryStream(result.ImageData)) { this.pictureBox1.Image = Image.FromStream(stream); } } return(new NormalResult()); } catch (Exception ex) { strError = $"针对 {url} 的 GetImage() 请求失败: { ex.Message}"; return(new RecognitionFaceResult { Value = -1, ErrorInfo = strError }); } finally { EndFaceChannel(channel); } } #region 人脸识别有关功能 public class FaceChannel