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);
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["单位名称"].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;
}
///
/// 营业执照OCR
///
///
///
///
public static async Task OcrIdentity(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, "1");
}
else
{
var upload = AliyunOSSUtils.Upload(scene, url);
url = upload.Url;
//value = ocr.Idcard(upload.Buffer);
}
//var result = value.ToObject();
//result.Url = url;
//if (result.ErrorCode.IsNotNull())
//{
// return result;
//}
//result.Model = new BaiduOcrIdentityResultModel
//{
// 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;
throw new Exception();
}
}
}