Пример #1
0
        public Task <IDCardFrontOCRResult> RecognizeIDCardFront(Stream idCardFrontImageData)
        {
            var result = new IDCardFrontOCRResult
            {
                Name         = "Name",
                SexString    = "男",
                Nationality  = "Nationality",
                DateOfBirth  = new DateTime(1985, 1, 15),
                Address      = "Address",
                IDCardNumber = "530302198501150314",
            };

            return(Task.FromResult(result));
        }
Пример #2
0
        /// <summary>
        /// 识别身份证正面(个人信息面)。
        /// </summary>
        /// <param name="idCardFrontImageData"></param>
        /// <returns></returns>
        public async Task <IDCardFrontOCRResult> RecognizeIDCardFront(Stream idCardFrontImageData)
        {
            string imgBase64;

            using (var ms = new MemoryStream())
            {
                idCardFrontImageData.CopyTo(ms);
                imgBase64 = Convert.ToBase64String(ms.ToArray());
            }
            var requestData = new
            {
                image     = imgBase64,
                configure = new
                {
                    side = "face",
                },
            };
            var request = WebRequest.Create(svcUrl);

            request.Method = "POST";
            request.Headers.Add("Authorization", "APPCODE " + appCode);
            request.ContentType = "application/json; charset=UTF-8";
            var requestStream = await request.GetRequestStreamAsync();

            var writer     = new StreamWriter(requestStream);
            var jsonWriter = new JsonTextWriter(writer);

            this.serializer.Serialize(jsonWriter, requestData);
            jsonWriter.Flush();

            WebResponse response;

            try
            {
                response = await request.GetResponseAsync();
            }
            catch (Exception ex)
            {
                Trace.TraceError(ex.Message);
                throw;
            }
            var responseStream = response.GetResponseStream();
            var result         = this.serializer.Deserialize <dynamic>(new JsonTextReader(new StreamReader(responseStream)));

            response.Close();

            if (!(bool)result.success)
            {
                throw new IDCardRecognizeException("Can not recognize");
            }
            var returnResult = new IDCardFrontOCRResult
            {
                Name         = (string)result.name,
                SexString    = (string)result.sex,
                Nationality  = (string)result.nationality,
                DateOfBirth  = this.parseDate((string)result.birth),
                Address      = (string)result.address,
                IDCardNumber = (string)result.num,
            };

            return(returnResult);
        }