37 lines
1.6 KiB
C#
37 lines
1.6 KiB
C#
namespace AgriculturalPlatform.Api.Services;
|
|
|
|
/// <summary>身份证 OCR 识别结果(未识别成功时 Success=false 并给出原因)</summary>
|
|
public record IdCardOcrResult(
|
|
string Name = "",
|
|
string IdCard = "",
|
|
string Gender = "",
|
|
string Address = "",
|
|
string BankName = "",
|
|
string BankAccount = "",
|
|
bool Success = false,
|
|
string Message = "");
|
|
|
|
/// <summary>
|
|
/// OCR 识别服务抽象。当前默认实现为 DisabledOcrService(仅占位);
|
|
/// 后续接入阿里云/百度 OCR 时,实现本接口并在 Program.cs 替换注册即可,调用方无需改动。
|
|
/// </summary>
|
|
public interface IOcrService
|
|
{
|
|
/// <summary>
|
|
/// 识别身份证图片,side: front(正面)/ back(反面)。
|
|
/// storageKey 为已保存文件的存储 key(OSS 模式下可据此生成临时访问 URL 传给识别服务)。
|
|
/// </summary>
|
|
Task<IdCardOcrResult> RecognizeAsync(Stream image, string side, string fileName = "idcard.jpg", string? storageKey = null);
|
|
}
|
|
|
|
/// <summary>默认 OCR 实现:未配置凭证时给出友好提示,图片仍会正常保存为附件</summary>
|
|
public sealed class DisabledOcrService(IConfiguration cfg) : IOcrService
|
|
{
|
|
public Task<IdCardOcrResult> RecognizeAsync(Stream image, string side, string fileName = "idcard.jpg", string? storageKey = null)
|
|
{
|
|
var message = cfg["Ocr:DisabledMessage"]
|
|
?? "未配置 OCR 识别服务(待接入阿里云/百度 OCR 凭证),图片已保存为附件,识别字段请手动填写";
|
|
return Task.FromResult(new IdCardOcrResult(Success: false, Message: message));
|
|
}
|
|
}
|