This commit is contained in:
梁骏愉
2025-02-10 09:24:44 +08:00
parent 3d15fd4b7e
commit 17bb7e4131
56 changed files with 15484 additions and 502 deletions

View File

@@ -1,6 +1,7 @@
using Kingdee.BOS.Core.DynamicForm;
using Kingdee.BOS.Core.DynamicForm.PlugIn.Args;
using Kingdee.BOS.Core.List.PlugIn;
using Kingdee.BOS.Core.Permission;
using Kingdee.BOS.Util;
using System;
using System.Collections.Generic;
@@ -20,15 +21,12 @@ namespace GZ.LJY000.PiLot.SAL_OUTSTOCK
if (e.BarItemKey.ToUpper().Equals("TBSCANQRCODEBYFILE"))
{
//"F_YCKE_Attachments_qtr"
var billShowParameter = new DynamicFormShowParameter();
if (this.Context.DBId.Equals("6735f10547df64"))
billShowParameter.FormId = "k689f2ef87e584d78afb120939e794fd3";
else
billShowParameter.FormId = "VRYF_ShippingIdentificationUpload";
billShowParameter.ParentPageId = this.View.PageId;
this.View.ShowForm(billShowParameter, (result) =>
{

View File

@@ -24,6 +24,7 @@ using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Kingdee.BOS.ServiceHelper;
using OpenCvSharp.Aruco;
using Kingdee.BOS.Core.DynamicForm.PlugIn.ControlModel;
namespace GZ.LJY000.PiLot.SAL_OUTSTOCK.UploadExtension
{
@@ -34,10 +35,18 @@ namespace GZ.LJY000.PiLot.SAL_OUTSTOCK.UploadExtension
private string FormId = "";
private bool IsDoUploaded = false;
private const string detector_prototxt_path = "detect.prototxt";
private const string detector_caffe_model_path = "detect.caffemodel";
private const string prototxt_path = "sr.prototxt";
private const string caffe_model_path = "sr.caffemodel";
private WeChatQRCode _wechatQrcode;
public override void OnInitialize(InitializeEventArgs e)
{
base.OnInitialize(e);
//FormId = e.Paramter.FormId;
_wechatQrcode = weChatQRCodeInit();
}
public override void ButtonClick(ButtonClickEventArgs e)
@@ -52,153 +61,220 @@ namespace GZ.LJY000.PiLot.SAL_OUTSTOCK.UploadExtension
if (e.Key.EndsWith("FDoUpLoad", StringComparison.OrdinalIgnoreCase))
{
var entity = this.View.Model.BillBusinessInfo.GetEntryEntity("FEntity");
var rows = this.View.Model.GetEntityDataObject(entity);
if (rows != null && rows.Any())
DoUpLoad();
}
}
private WeChatQRCode weChatQRCodeInit()
{
var modelPath = HttpContext.Current.Request.PhysicalApplicationPath + @"/Bin/opencv_3rdparty-wechat_qrcode/";
return WeChatQRCode.Create(modelPath + detector_prototxt_path, modelPath + detector_caffe_model_path, modelPath + prototxt_path, modelPath + caffe_model_path);
}
/// <summary>
/// 预设图片切割区域,提高识别精度
/// </summary>
/// <param name="srcWidth"></param>
/// <param name="srcHeight"></param>
/// <returns></returns>
private List<Rect> GetRects(int srcWidth, int srcHeight)
{
List<Rect> rects = new List<Rect>();
#region 1/4
var width = srcWidth / 2;
var height = srcHeight / 2;
var x = width * 1;
var y = 0;
rects.Add(new Rect(x, y, width, height));
#endregion
#region 1/9
width = srcWidth / 3;
height = srcHeight / 3;
x = width * 2;
y = 0;
rects.Add(new Rect(x, y, width, height));
#endregion
#region 1/60()
width = srcWidth / 10;
height = srcHeight / 6;
x = width * 7;
y = 0;
width = width * 2;
rects.Add(new Rect(x, y, width, height));
#endregion
return rects;
}
/// <summary>
/// 使用微信扫码开源识别
/// </summary>
/// <param name="src">需要扫描的图片</param>
/// <returns></returns>
private AnalyzeResult WeChatQRCodeRectRegion(Mat src)
{
_wechatQrcode.DetectAndDecode(src, out var rects, out var texts);
if (texts != null && texts.Length > 0)
{
return new AnalyzeResult
{
//var path = HttpContext.Current.Request.PhysicalApplicationPath + @"\FileUpLoadServices\UploadFiles\";
var path = HttpContext.Current.Request.PhysicalApplicationPath + @"/FileUpLoadServices/UploadFiles/";
name = texts[0]
};
}
//if (!this.Context.DBId.Equals("6735f10547df64"))
// path = "D:/Program Files (x86)/Kingdee/K3Cloud/WebSite/FileUpLoadServices/UploadFiles/";
foreach (var row in rows)
var rescts = GetRects(src.Width, src.Height);
foreach (var rect in rescts)
{
using (Mat roi = new Mat(src, rect))
{
_wechatQrcode.DetectAndDecode(roi, out rects, out texts);
if (texts != null && texts.Length > 0)
{
var isSucceed = row["FisSucceed"].ToString() == "1";
if (isSucceed)
continue;
var fileName = row["FFileName"].ToString();
var serverFileName = row["FServerFileName"].ToString();
var fileSuffix = row["FFileSuffix"].ToString();
var fileBytesLength = row["FFileBytesLength"].Long2Int();
AnalyzeResult resData = null;
//文件地址
var fullName = path + serverFileName;
if (fileSuffix.ToUpper().Equals(".PDF"))
return new AnalyzeResult
{
// 创建 PdfDocument 类的实例
using (PdfDocument document = new PdfDocument())
name = texts[0]
};
}
}
}
return null;
}
/// <summary>
///
/// </summary>
private void DoUpLoad()
{
var entity = this.View.Model.BillBusinessInfo.GetEntryEntity("FEntity");
var rows = this.View.Model.GetEntityDataObject(entity);
if (rows != null && rows.Any())
{
var path = HttpContext.Current.Request.PhysicalApplicationPath + @"/FileUpLoadServices/UploadFiles/";
var grid = this.View.GetControl<EntryGrid>("FEntity");
var index = 0;
foreach (var row in rows)
{
var isSucceed = bool.Parse(row["FisSucceed"].ToString());
if (isSucceed)
{
index++;
continue;
}
var fileName = row["FFileName"].ToString();
var serverFileName = row["FServerFileName"].ToString();
var fileSuffix = row["FFileSuffix"].ToString();
var fileBytesLength = row["FFileBytesLength"].Long2Int();
AnalyzeResult resData = null;
//文件地址
var fullName = path + serverFileName;
if (fileSuffix.ToUpper().Equals(".PDF"))
{
// 创建 PdfDocument 类的实例
using (PdfDocument document = new PdfDocument())
{
// 加载 PDF 文档
document.LoadFromFile(fullName);
// 将页面转换为图片
using (Image image = document.SaveAsImage(0, 200, 200))
{
// 加载 PDF 文档
document.LoadFromFile(fullName);
// 将页面转换为图片
using (Image image = document.SaveAsImage(0, 200, 200))
// 创建流来保存图片数据
using (MemoryStream stream = new MemoryStream())
{
// 创建流来保存图片数据
using (MemoryStream stream = new MemoryStream())
// 将图片以 PNG 格式保存到流中
image.Save(stream, ImageFormat.Png);
stream.Position = 0;
using (Mat src = BitmapConverter.ToMat(new Bitmap(image)))
{
// 将图片以 PNG 格式保存到流中
image.Save(stream, ImageFormat.Png);
stream.Position = 0;
using (Mat src = BitmapConverter.ToMat(new Bitmap(image)))
{
var srcWidth = src.Width;
var srcHeight = src.Height;
int x = srcWidth / 2;
int y = 0;
int width = srcWidth / 2;
int height = srcHeight / 2;
Rect region = new Rect(x, y, width, height);
resData = DecodeQRCodes(src, region, true);
}
resData = WeChatQRCodeRectRegion(src);
}
}
}
}
else if (fileSuffix.ToUpper().Equals(".JPG"))
}
else if (fileSuffix.ToUpper().Equals(".JPG"))
{
using (Mat src = new Mat(fullName))
{
Mat src = new Mat(fullName);
var srcWidth = src.Width;
var srcHeight = src.Height;
int x = srcWidth / 2;
int y = 0;
int width = srcWidth / 2;
int height = srcHeight / 2;
Rect region = new Rect(x, y, width, height);
resData = DecodeQRCodes(src, region, true);
resData = WeChatQRCodeRectRegion(src);
}
}
if (resData != null)
if (resData != null)
{
var isSuccess = false;
var resMsg = string.Empty;
row["FBillNo"] = resData.name;
if (!resData.name.IsNullOrEmptyOrWhiteSpace())
{
var isSuccess = false;
var resMsg = string.Empty;
row["FBillNo"] = resData.name;
var base64 = FileToBase64String(fullName);
var resultJson = DoAttachmentUpload("SAL_OUTSTOCK", fileName, resData.name, fileBytesLength, fileSuffix, base64);
if (!resData.name.IsNullOrEmptyOrWhiteSpace())
if (resultJson != null)
{
var base64 = FileToBase64String(fullName);
var resultJson = DoAttachmentUpload("SAL_OUTSTOCK", fileName, resData.name, fileBytesLength, fileSuffix, base64);
var result = JsonConvert.DeserializeObject<JToken>(resultJson);
//var result = resultJson as JObject;
var responseData = result["Result"]["ResponseStatus"];
isSuccess = responseData["IsSuccess"].Value<bool>();
if (resultJson != null)
{
var result = JsonConvert.DeserializeObject<JToken>(resultJson);
//var result = resultJson as JObject;
var responseData = result["Result"]["ResponseStatus"];
isSuccess = responseData["IsSuccess"].Value<bool>();
resMsg = JsonConvert.SerializeObject(responseData);
}
else
{
resMsg = "识别单号找不到对应销售出库单!";
}
}
if (isSuccess)
{
row["FisSucceed"] = true;
row["FResultMsg"] = "上传完毕";
this.View.GetControl("FFileName").SetCustomPropertyValue("ForeColor", "#00994C");
resMsg = JsonConvert.SerializeObject(responseData);
}
else
{
if (resMsg.IsNullOrEmpty())
resMsg = "识别失败!";
row["FisSucceed"] = false;
row["FResultMsg"] = resMsg;
this.View.GetControl("FFileName").SetCustomPropertyValue("ForeColor", "#FF3300");
resMsg = "识别单号找不到对应销售出库单!";
}
}
if (isSuccess)
{
row["FisSucceed"] = true;
row["FResultMsg"] = "上传完毕";
grid.SetForecolor("FFileName", "#00994C", index);
}
else
{
row["FisSucceed"] = false;
row["FResultMsg"] = "未知错误导致识别失败";
this.View.GetControl("FFileName").SetCustomPropertyValue("ForeColor", "#FF3300");
}
if (resMsg.IsNullOrEmpty())
resMsg = "识别失败!";
row["FisSucceed"] = false;
row["FResultMsg"] = resMsg;
grid.SetForecolor("FFileName", "#FF3300", index);
}
}
//fileUploaded = false;
this.View.UpdateView("FEntity");
else
{
row["FisSucceed"] = false;
row["FResultMsg"] = "未知错误导致识别失败!";
grid.SetForecolor("FFileName", "#FF3300", index);
}
index++;
}
//fileUploaded = false;
this.View.UpdateView("FEntity");
}
}
/// <summary>
/// 文件转base64
/// </summary>
/// <returns>base64字符串</returns>
public string FileToBase64String(string fullName)
{
//FileStream fsForRead = new FileStream(fullName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);//文件路径
string base64Str = "";
try
{
////读写指针移到距开头10个字节处
//fsForRead.Seek(0, SeekOrigin.Begin);
//byte[] bs = new byte[fsForRead.Length];
//int log = Convert.ToInt32(fsForRead.Length);
////从文件中读取10个字节放到数组bs中
//fsForRead.Read(bs, 0, log);
//base64Str = Convert.ToBase64String(bs);
//return base64Str;
var fileBytes = File.ReadAllBytes(fullName);
return Convert.ToBase64String(fileBytes);
@@ -362,7 +438,7 @@ ORDER BY FENABLE DESC";
""FBillType"": ""{formId}"",
""FInterID"": ""{InterId}"",
""FBillNo"": ""{formBillNo}"",
""FAttachmentSize"": {fileBytesLength},
""FAttachmentSize"": {(Math.Round((decimal)fileBytesLength / 1024, 2))},
""FAttachment"":""{base64}"",
""FFILESTORAGE"":0,
""FExtName"": ""{fileSuffix}"",
@@ -425,29 +501,7 @@ ORDER BY FENABLE DESC";
}
}
//if (e.Key.ToUpper() == "FFileUpdate".ToUpper())
//{ //触发事件是上传文件有变化
// if (e.EventName.ToUpper() == "FILECHANGED")
// {
// JSONObject jSONObject = KDObjectConverter.DeserializeObject<JSONObject>(e.EventArgs);
// if (jSONObject != null)
// {
// JSONArray jSONArray = new JSONArray(jSONObject["NewValue"].ToString()); if (jSONArray.Count > 0)
// {
// //获取上传的文件名
// string text = (jSONArray[0] as Dictionary<string, object>)["ServerFileName"].ToString();
// //文件上传到服务端的临时目录
// string directory = "FileUpLoadServices\\UploadFiles";
// //文件的完整路径
// string fileFullPath = PathUtils.GetPhysicalPath(directory, text);
// }
// }
// }
//}
base.CustomEvents(e);
return;
}
}