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
|
{
|
/// <summary>
|
/// 百度文字识别工具
|
/// </summary>
|
public static class BaiduOcrUtils
|
{
|
/// <summary>
|
/// 营业执照OCR
|
/// </summary>
|
/// <param name="url"></param>
|
/// <param name="scene"></param>
|
/// <param name="isOssUrl"></param>
|
public static async Task<BaiduOcrBusinessLicenseResult> OcrBusinessLicense(this string url, string scene = null, bool isOssUrl = true)
|
{
|
var options = App.GetOptions<BaiduOptions>();
|
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<IHttpRemoteService>().GetAsByteArrayAsync(downloadUrl);
|
value = ocr.BusinessLicense(buffer);
|
}
|
else
|
{
|
var upload = AliyunOSSUtils.Upload(scene, url);
|
url = upload.Url;
|
value = ocr.BusinessLicense(upload.Buffer);
|
}
|
var result = value.ToObject<BaiduOcrBusinessLicenseResult>();
|
result.Url = url;
|
if (result.ErrorCode.IsNotNull())
|
{
|
return result;
|
}
|
result.Model = new BaiduOcrBusinessLicenseResultModel
|
{
|
EnterpriseName = result.WordsResult["单位名称"].Words,
|
SocietyCreditCode = result.WordsResult["社会信用代码"].Words,
|
LegalPerson = result.WordsResult["法人"].Words,
|
EnterpriseType = result.WordsResult["类型"].Words,
|
RegisteredCapital = result.WordsResult["注册资本"].Words,
|
EstablishmentDate = result.WordsResult["成立日期"].Words,
|
Address = result.WordsResult["地址"].Words,
|
MainBusiness = result.WordsResult["经营范围"].Words,
|
};
|
return result;
|
}
|
|
/// <summary>
|
/// 身份证正面OCR
|
/// </summary>
|
/// <param name="url"></param>
|
/// <param name="scene"></param>
|
/// <param name="isOssUrl"></param>
|
public static async Task<BaiduOcrIdentityFrontResult> OcrIdentityFront(this string url, string scene = null, bool isOssUrl = true)
|
{
|
var options = App.GetOptions<BaiduOptions>();
|
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<IHttpRemoteService>().GetAsByteArrayAsync(downloadUrl);
|
value = ocr.Idcard(buffer, "front");
|
}
|
else
|
{
|
var upload = AliyunOSSUtils.Upload(scene, url);
|
url = upload.Url;
|
value = ocr.Idcard(upload.Buffer, "front");
|
}
|
var result = value.ToObject<BaiduOcrIdentityFrontResult>();
|
result.Url = url;
|
if (result.ErrorCode.IsNotNull())
|
{
|
return result;
|
}
|
|
result.Model = new BaiduOcrIdentityFrontResultModel
|
{
|
Address = result.WordsResult["住址"].Words,
|
Identity = result.WordsResult["公民身份号码"].Words,
|
BirthdayText = result.WordsResult["出生"].Words,
|
Name = result.WordsResult["姓名"].Words,
|
GenderText = result.WordsResult["性别"].Words,
|
Nation = result.WordsResult["民族"].Words,
|
};
|
return result;
|
}
|
|
/// <summary>
|
/// 身份证背面OCR
|
/// </summary>
|
/// <param name="url"></param>
|
/// <param name="scene"></param>
|
/// <param name="isOssUrl"></param>
|
public static async Task<BaiduOcrIdentityBackResult> OcrIdentityBack(this string url, string scene = null, bool isOssUrl = true)
|
{
|
var options = App.GetOptions<BaiduOptions>();
|
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<IHttpRemoteService>().GetAsByteArrayAsync(downloadUrl);
|
value = ocr.Idcard(buffer, "back");
|
}
|
else
|
{
|
var upload = AliyunOSSUtils.Upload(scene, url);
|
url = upload.Url;
|
value = ocr.Idcard(upload.Buffer, "back");
|
}
|
var result = value.ToObject<BaiduOcrIdentityBackResult>();
|
result.Url = url;
|
if (result.ErrorCode.IsNotNull())
|
{
|
return result;
|
}
|
|
result.Model = new BaiduOcrIdentityBackResultModel
|
{
|
ExpiryDateText = result.WordsResult["失效日期"].Words,
|
IssueAuthority = result.WordsResult["签发机关"].Words,
|
IssueDateText = result.WordsResult["签发日期"].Words,
|
};
|
return result;
|
}
|
}
|
}
|