ocr功能

booking_auth_dev
wanghaomei 2 years ago
parent c267b4eaa6
commit 672fe0665a

@ -4905,6 +4905,33 @@
<param name="carrierId">船公司ID</param>
<returns>返回适用的路由枚举</returns>
</member>
<member name="M:Myshipping.Application.BookingOrderService.OcrUpFile(Microsoft.AspNetCore.Http.IFormFile)">
<summary>
上传ocr文件
</summary>
<param name="file"></param>
<returns></returns>
</member>
<member name="M:Myshipping.Application.BookingOrderService.OcrGetImg(System.String,System.Single)">
<summary>
获取图片
</summary>
<param name="fileName">文件名称</param>
<param name="scale">缩放比例默认为1.5</param>
<returns></returns>
</member>
<member name="M:Myshipping.Application.BookingOrderService.OcrGetText(System.String,System.Single,System.Int32,System.Int32,System.Int32,System.Int32)">
<summary>
获取文字
</summary>
<param name="fileName">文件名称</param>
<param name="scale">缩放比例</param>
<param name="x">x坐标</param>
<param name="y">y坐标</param>
<param name="w">宽度</param>
<param name="h">高度</param>
<returns></returns>
</member>
<member name="M:Myshipping.Application.BookingOrderService.GenReportJson(System.Int64)">
<summary>
获取用户报表的json

@ -51,6 +51,7 @@ using System.Threading;
using Furion.JsonSerialization;
using System.Xml.Linq;
using Myshipping.Application.Helper;
using System.Net;
namespace Myshipping.Application
{
@ -784,7 +785,8 @@ namespace Myshipping.Application
{
_logger.LogInformation("提单号:" + MBLNO + " 调用运踪接口");
var key = _repWebAcc.FirstOrDefault(x => x.TenantId == Convert.ToInt64(UserManager.TENANT_ID) && x.TypeCode == "seae_billtraceurl");
if (key==null) {
if (key == null)
{
throw Oops.Bah("调用运踪接口相关账号未维护!");
}
@ -2196,6 +2198,122 @@ namespace Myshipping.Application
}
#endregion
#region ocr
/// <summary>
/// 上传ocr文件
/// </summary>
/// <param name="file"></param>
/// <returns></returns>
[HttpPost("/BookingOrder/OcrUpFile")]
public async Task<string> OcrUpFile(IFormFile file)
{
//未上传文件
if (file == null || file.Length == 0)
{
throw Oops.Bah("未上传文件");
}
var originalFilename = file.FileName; // 文件原始名称
var fileSuffix = Path.GetExtension(file.FileName).ToLower(); // 文件后缀
var serverUrl = (await _cache.GetAllDictData()).FirstOrDefault(x => x.TypeCode == "url_set" && x.Code == "ocr_api_url").Value;
if (!serverUrl.EndsWith("/"))
{
serverUrl += "/";
}
var ms = new MemoryStream();
await file.CopyToAsync(ms);
_logger.LogInformation($"调用ocr处理文件{originalFilename}");
//Furion框架上传文件名称带有双引号导致上传失败
//var strRtn = await $"{serverUrl}pdf/upload"
// .SetContentType("multipart/form-data")
// .SetContentEncoding(Encoding.UTF8)
// .SetBodyBytes(("file", ms.GetBuffer(), "test.pdf"))
// .PostAsStringAsync();
//var jobj = strRtn.ToJObject();
//if (jobj.GetIntValue("code") == 0)
//{
// var fn = jobj.GetStringValue("data");
// return fn;
//}
//else
//{
// throw Oops.Bah(jobj.GetStringValue("message"));
//}
//使用HttpClient方式上传文件
ms.Position = 0;
var formData = new MultipartFormDataContent();
formData.Add(new StreamContent(ms, (int)ms.Length), "file", originalFilename);
var _httpclient = new HttpClient();
var response = await _httpclient.PostAsync($"{serverUrl}pdf/upload", formData);
if (response.IsSuccessStatusCode)
{
var strRtn = response.Content.ReadAsStringAsync().Result;
var jobj = strRtn.ToJObject();
if (jobj.GetIntValue("code") == 0)
{
var fn = jobj.GetStringValue("data");
return fn;
}
else
{
throw Oops.Bah(jobj.GetStringValue("message"));
}
}
throw Oops.Bah("文件上传失败");
}
/// <summary>
/// 获取图片
/// </summary>
/// <param name="fileName">文件名称</param>
/// <param name="scale">缩放比例默认为1.5</param>
/// <returns></returns>
[HttpPost("/BookingOrder/OcrGetImg")]
public async Task<IActionResult> OcrGetImg(string fileName, float scale = 1.5f)
{
var serverUrl = (await _cache.GetAllDictData()).FirstOrDefault(x => x.TypeCode == "url_set" && x.Code == "ocr_api_url").Value;
if (!serverUrl.EndsWith("/"))
{
serverUrl += "/";
}
var bs = await $"{serverUrl}pdf/getCanvasImage?fileName={fileName}&scale={scale}"
.GetAsByteArrayAsync();
var result = new FileContentResult(bs, "application/octet-stream") { FileDownloadName = "ocr.jpg" };
return result;
}
/// <summary>
/// 获取文字
/// </summary>
/// <param name="fileName">文件名称</param>
/// <param name="scale">缩放比例</param>
/// <param name="x">x坐标</param>
/// <param name="y">y坐标</param>
/// <param name="w">宽度</param>
/// <param name="h">高度</param>
/// <returns></returns>
[HttpPost("/BookingOrder/OcrGetText")]
public async Task<string> OcrGetText(string fileName, float scale, int x, int y, int w, int h)
{
var serverUrl = (await _cache.GetAllDictData()).FirstOrDefault(x => x.TypeCode == "url_set" && x.Code == "ocr_api_url").Value;
if (!serverUrl.EndsWith("/"))
{
serverUrl += "/";
}
var str = await $"{serverUrl}pdf/getRegionText?fileName={fileName}&scale={scale}&x={x}&y={y}&w={w}&h={h}"
.PostAsStringAsync();
return str;
}
#endregion
#region 其他
/// <summary>
/// 获取用户报表的json

@ -64,7 +64,7 @@ namespace Myshipping.Application
//未上传打印模板文件
if (file == null || file.Length == 0)
{
throw Oops.Oh(BookingErrorCode.BOOK113);
throw Oops.Bah(BookingErrorCode.BOOK113);
}
var opt = App.GetOptions<PrintTemplateOptions>();
@ -72,7 +72,7 @@ namespace Myshipping.Application
var fileSuffix = Path.GetExtension(file.FileName).ToLower(); // 文件后缀
if (!opt.fileType.Contains(fileSuffix))
{
throw Oops.Oh(BookingErrorCode.BOOK114);
throw Oops.Bah(BookingErrorCode.BOOK114);
}
var dirAbs = string.Empty;
@ -124,7 +124,7 @@ namespace Myshipping.Application
//未上传打印模板文件
if (file == null || file.Length == 0)
{
throw Oops.Oh(BookingErrorCode.BOOK113);
throw Oops.Bah(BookingErrorCode.BOOK113);
}
var opt = App.GetOptions<PrintTemplateOptions>();
@ -132,14 +132,14 @@ namespace Myshipping.Application
var fileSuffix = Path.GetExtension(file.FileName).ToLower(); // 文件后缀
if (!opt.fileType.Contains(fileSuffix))
{
throw Oops.Oh(BookingErrorCode.BOOK114);
throw Oops.Bah(BookingErrorCode.BOOK114);
}
_rep.Context.QueryFilter.Clear();
var entity = _rep.FirstOrDefault(x => x.Id == input.Id);
if (entity == null)
{
throw Oops.Oh(BookingErrorCode.BOOK112);
throw Oops.Bah(BookingErrorCode.BOOK112);
}
entity = input.Adapt(entity);
@ -219,7 +219,7 @@ namespace Myshipping.Application
var printFile = await _rep.FirstOrDefaultAsync(u => u.Id == id);
if (printFile == null)
{
throw Oops.Oh(BookingErrorCode.BOOK115);
throw Oops.Bah(BookingErrorCode.BOOK115);
}
@ -233,7 +233,7 @@ namespace Myshipping.Application
var fileFullPath = Path.Combine(dirAbs, printFile.FilePath);
if (!File.Exists(fileFullPath))
{
throw Oops.Oh(BookingErrorCode.BOOK115);
throw Oops.Bah(BookingErrorCode.BOOK115);
}
var fileName = HttpUtility.UrlEncode(printFile.FileName, Encoding.GetEncoding("UTF-8"));

Loading…
Cancel
Save