using Baidu.Aip.Ocr;
using Furion;
using Furion.DistributedIDGenerator;
using Furion.FriendlyException;
using Furion.HttpRemote;
using Microsoft.AspNetCore.DataProtection;
using Microsoft.Extensions.Options;
using NetTopologySuite.Operation.Buffer;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
namespace FlexJobApi.Core
{
///
/// 百度文字识别工具
///
public static class BaiduOcrUtils
{
///
/// 营业执照OCR
///
///
///
///
public static async Task OcrBusinessLicense(this string url, string scene = null, bool isOssUrl = true)
{
var options = App.GetOptions();
var ocr = new Ocr(options.Ocr.Key, options.Ocr.Secret);
ocr.Timeout = 60000;
JObject value;
if (isOssUrl)
{
var downloadUrl = AliyunOSSUtils.GetUrl(url);
var buffer = await App.GetRequiredService().GetAsByteArrayAsync(downloadUrl);
value = ocr.BusinessLicense(buffer);
}
else
{
var upload = AliyunOSSUtils.Upload(scene, url, null);
url = upload.Url;
value = ocr.BusinessLicense(upload.Buffer);
}
var result = value.ToObject();
result.Url = url;
if (result.ErrorCode.IsNotNull())
{
return result;
}
result.Model = new BaiduOcrBusinessLicenseResultModel
{
EnterpriseName = result.WordsResult.GetWords("单位名称"),
SocietyCreditCode = result.WordsResult.GetWords("社会信用代码"),
LegalPerson = result.WordsResult.GetWords("法人"),
EnterpriseType = result.WordsResult.GetWords("类型"),
RegisteredCapital = result.WordsResult.GetWords("注册资本"),
EstablishmentDate = result.WordsResult.GetWords("成立日期"),
Address = result.WordsResult.GetWords("地址"),
MainBusiness = result.WordsResult.GetWords("经营范围"),
};
return result;
}
///
/// 身份证正面OCR
///
///
///
///
public static async Task OcrIdentityFront(this string url, string scene = null, bool isOssUrl = true)
{
var options = App.GetOptions();
var ocr = new Ocr(options.Ocr.Key, options.Ocr.Secret);
ocr.Timeout = 60000;
JObject value;
if (isOssUrl)
{
var downloadUrl = AliyunOSSUtils.GetUrl(url);
var buffer = await App.GetRequiredService().GetAsByteArrayAsync(downloadUrl);
value = ocr.Idcard(buffer, "front");
}
else
{
var upload = AliyunOSSUtils.Upload(scene, url, null);
url = upload.Url;
value = ocr.Idcard(upload.Buffer, "front");
}
var result = value.ToObject();
result.Url = url;
if (result.ErrorCode.IsNotNull())
{
return result;
}
result.Model = new BaiduOcrIdentityFrontResultModel
{
Address = result.WordsResult.GetWords("住址"),
Identity = result.WordsResult.GetWords("公民身份号码"),
BirthdayText = result.WordsResult.GetWords("出生"),
Name = result.WordsResult.GetWords("姓名"),
GenderText = result.WordsResult.GetWords("性别"),
Nation = result.WordsResult.GetWords("民族"),
};
return result;
}
///
/// 身份证背面OCR
///
///
///
///
public static async Task OcrIdentityBack(this string url, string scene = null, bool isOssUrl = true)
{
var options = App.GetOptions();
var ocr = new Ocr(options.Ocr.Key, options.Ocr.Secret);
ocr.Timeout = 60000;
JObject value;
if (isOssUrl)
{
var downloadUrl = AliyunOSSUtils.GetUrl(url);
var buffer = await App.GetRequiredService().GetAsByteArrayAsync(downloadUrl);
value = ocr.Idcard(buffer, "back");
}
else
{
var upload = AliyunOSSUtils.Upload(scene, url, null);
url = upload.Url;
value = ocr.Idcard(upload.Buffer, "back");
}
var result = value.ToObject();
result.Url = url;
if (result.ErrorCode.IsNotNull())
{
return result;
}
result.Model = new BaiduOcrIdentityBackResultModel
{
ExpiryDateText = result.WordsResult.GetWords("失效日期"),
IssueAuthority = result.WordsResult.GetWords("签发机关"),
IssueDateText = result.WordsResult.GetWords("签发日期"),
};
return result;
}
public static string GetWords(this Dictionary result, string name)
{
return result.ContainsKey(name) ? result[name].Words : null;
}
}
}