コード例 #1
0
        internal static PassportDocumentInfo CreateFromDocumentUrl(string documentUrl)
        {
            var document = new PassportDocumentInfo();

            document.DocumentUrl = documentUrl;

            var xDoc = XDocument.Load(documentUrl);
            var ns   = xDoc.Root.GetDefaultNamespace();

            var fieldElements = xDoc.Descendants(ns + "field").ToDictionary(xe => xe.Attribute("type").Value);
            var propertyInfos = typeof(PassportDocumentInfo).GetProperties().Where(pi => pi.Name != nameof(DocumentUrl)).ToDictionary(pi => pi.Name);

            foreach (var field in fieldElements)
            {
                var fieldName = field.Key;
                if (propertyInfos.TryGetValue(fieldName, out var prop))
                {
                    var valueElement = field.Value.Element(ns + "value");
                    var fieldValue   = prop.PropertyType == typeof(bool)
                        ? (object)(bool)valueElement
                        : (string)valueElement;

                    prop.SetValue(document, fieldValue);
                }
            }

            return(document);
        }
コード例 #2
0
        public async Task <PassportParseResult> ParsePassport(Stream passportImage)
        {
            var result = new PassportParseResult();

            try
            {
                // TODO: Reusing last task's result (if exists) to spare executions during dev -to be removed in deployed
                var task = ocrClient.ListTasks().LastOrDefault() ?? ocrClient.ProcessMrz(passportImage);

                result.TaskId = task.Id.ToString();
                while (task.IsTaskActive())
                {
                    await ThreadTask.Delay(1000);

                    task = ocrClient.GetTaskStatus(task.Id);
                }

                // Sample response file content: /SampleResponse/OcrMrzTaskDocument.xml
                var documentUrl = task.DownloadUrls.Single();
                result.PassportDocument = PassportDocumentInfo.CreateFromDocumentUrl(documentUrl);
            }
            catch (Exception ex)
            {
                result.SetError(ex);
            }

            return(result);
        }