Commit a1028fa1 王海洋

添加AIocr打印接口

1 个父辈 cfa214df
......@@ -106,32 +106,39 @@ namespace BLL
// 比较当前 key 和上次的 key
bool isSame = false;
if (extensions != null && !extensions[0].Control.InvokeRequired)
try
{
//第一次刷新界面
for (int i = 0; i < extensions.Count; i++)
if (extensions != null && !extensions[0].Control.InvokeRequired)
{
extensions[i].Control.ForeColor = System.Drawing.Color.White;
if (key.ContainsKey(extensions[i].Key))
extensions[i].Control.Text = key[extensions[i].Key].Replace("<OCR>", "");
if (extensions[i].LinkName== "ischeckresult" && lastKeys != null)
//第一次刷新界面
for (int i = 0; i < extensions.Count; i++)
{
//if (BLLCommon.extension.labelText=="NG")
//{
// extensions[i].Control.Text = BLLCommon.extension.labelText;
// extensions[i].Control.ForeColor = System.Drawing.Color.Red;
//}
//else
//{
// extensions[i].Control.Text = BLLCommon.extension.labelText;
// extensions[i].Control.ForeColor = System.Drawing.Color.Green;
//}
extensions[i].Control.ForeColor = System.Drawing.Color.White;
if (key.ContainsKey(extensions[i].Key))
extensions[i].Control.Text = key[extensions[i].Key].Replace("<OCR>", "");
if (extensions[i].LinkName == "ischeckresult" && lastKeys != null)
{
//if (BLLCommon.extension.labelText=="NG")
//{
// extensions[i].Control.Text = BLLCommon.extension.labelText;
// extensions[i].Control.ForeColor = System.Drawing.Color.Red;
//}
//else
//{
// extensions[i].Control.Text = BLLCommon.extension.labelText;
// extensions[i].Control.ForeColor = System.Drawing.Color.Green;
//}
}
}
}
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
Application.DoEvents();
#region 判断指定关键字是否存在内容
......
......@@ -6,6 +6,7 @@ using System.Collections.Generic;
using CameraVisionLib.Model;
using System.Drawing;
using System.Reflection;
using RestSharp;
namespace Model
{
......@@ -25,6 +26,14 @@ namespace Model
WebResultCamera GetCamera();
[OperationContract]
[WebGet(UriTemplate = "NeoScan/GetmacroKey", BodyStyle = WebMessageBodyStyle.Bare, RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
WebResultCamera GetmacroKey();
[OperationContract]
[WebInvoke(Method = "POST", UriTemplate = "NeoScan/PrintAIing", BodyStyle = WebMessageBodyStyle.Bare, RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
WebResultCamera GetPrintAiing(string a);
[OperationContract]
[WebInvoke(Method = "POST", UriTemplate = "NeoScan/WorkWithCode", BodyStyle = WebMessageBodyStyle.Bare, RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
WebResultCode WorkWithCode(Stream json);
[OperationContract]
......@@ -53,6 +62,8 @@ namespace Model
public WebCodeAll[] Data { get; set; }
[DataMember]
public string Text { get; set; }
[DataMember]
public List<string> MacroKey { get; set; }
public WebResultCamera()
{
......
......@@ -302,6 +302,7 @@ namespace SmartScan
FrmRetrospect.Print += Extension_Printing;
UsrWorkMode.Printing += Extension_Printing;
WebCallWork.PrintAiing += Extension_Printing;
//扩展面板
PnlExtension.Width = BLLCommon.config.ExtensionWidth;
PnlExtension.Left = Width - PnlExtension.Width - 12;
......
......@@ -19,9 +19,9 @@ namespace SmartScan
private readonly FacePictureBox picShow;
private readonly FaceButton btnMatchedName;
private List<CameraVisionLib.Model.BarcodeInfo> workCodeInfo;
public List<CameraVisionLib.Model.BarcodeInfo> workCodeInfo;
public Dictionary<string, string> workCodeKeyword;
private string[] originalCodeText;
public string[] originalCodeText;
private bool[] originalCodeIsUsed;
public ScanWork()
......@@ -300,7 +300,7 @@ namespace SmartScan
Application.DoEvents();
}
private bool MatchingTemplate()
public bool MatchingTemplate()
{
LogNet.log.Info("Work MatchingTemplate");
//没有条码也继续进行模板匹配
......@@ -336,7 +336,7 @@ namespace SmartScan
return rtn;
}
private void SetKey(bool hasMatch)
public void SetKey(bool hasMatch)
{
LogNet.log.Info("Work SetKey hasMatch:" + hasMatch);
//if (workCodeKeyword.Count == 0){ return;}
......
using Asa.FaceControl;
using BLL;
using DocumentFormat.OpenXml;
using DocumentFormat.OpenXml.Spreadsheet;
using Model;
using Newtonsoft.Json;
using SmartScan.Form;
using SmartScan.PlusSettingFrm;
using System;
using System.Collections.Generic;
......@@ -13,6 +15,7 @@ using System.Reflection;
using System.Runtime.Serialization.Formatters.Binary;
using System.ServiceModel;
using System.ServiceModel.Activation;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.UI.WebControls;
......@@ -39,10 +42,18 @@ namespace SmartScan
Common.frmMain.Invoke(new Action(() => { msg = Common.frmMain.WebTouchWork(); }));
return new WebResultCamera() { Data = msg };
}
public delegate void GetCameraAi(Dictionary<string, string> content);
public static event GetCameraAi PrintAiing;
public WebResultCamera GetmacroKey()
{
var aa = BLLCommon.macroKey;
return new WebResultCamera { MacroKey = aa };
}
public WebResultCamera GetCamera()
{
//开灯
BLLCommon.lightSource.TurnOn();
Bitmap[] result = Camera.Capture();
Bitmap bmp;
var content = "";
......@@ -50,7 +61,7 @@ namespace SmartScan
if (result.Length == 0)
{
// 当没有捕获到图像时,从本地加载默认图像
string defaultImagePath = "C:\\Users\\HY\\Desktop\\77F1EA37-A9B5-46cb-BFFA-5F201EE0FA41.png"; // 替换为你的默认图像路径
string defaultImagePath = "C:\\Users\\HY\\Desktop\\ocrt.jpg"; // 替换为你的默认图像路径
content = PaddleOCRHelper.StartCplusOcr(defaultImagePath);
//string res = PaddleOCRHelper.StartPythonOcr(defaultImagePath);
......@@ -75,9 +86,10 @@ namespace SmartScan
string location = assembly.Location;
string directory = System.IO.Path.GetDirectoryName(location);
string filePath = directory+"\\ocr.png"; // 替换为实际路径
// 保存图像到文件
SaveBitmapToFile(bmp, filePath);
//关灯
//BLLCommon.lightSource.TurnOff();
// 保存图像到文件
SaveBitmapToFile(bmp, filePath);
LogNet.log.Info("ocr存图成功");
content = PaddleOCRHelper.StartCplusOcr(filePath);
LogNet.log.Info("content ocr识别结果:"+content);
......@@ -90,11 +102,183 @@ namespace SmartScan
// string res = PaddleOCRHelper.StartPythonOcr(filePath);
}
scanWork.workCodeInfo= Camera.GetBarCode(bmp);
string base64String = ConvertBitmapToBase64(bmp);
return new WebResultCamera { Msg = base64String,Text= content };
}
private ScanWork scanWork=new();
public WebResultCamera GetPrintAiing(string a)
{
BLLCommon.extension.Clear();
scanWork.workCodeInfo = new();
//string[] parts = a.Split(';');
//if (a.StartsWith("[") && a.EndsWith("]"))
//{
// a = a.Substring(1, a.Length - 2);
// Console.WriteLine(a);
//}
var aa = BLLCommon.macroKey;
// 2. 初始化目标字典
Dictionary<string, string> aaa = new Dictionary<string, string>();// { { "LOT",null} };
foreach (var key in aa)
{
aaa[key] = ""; // or aaa.Add(key, null);
}
try
{
// 解析 JSON 字符串
aaa = JsonConvert.DeserializeObject<Dictionary<string, string>>(a);
// 将解析结果赋值给 aaa 字典
//foreach (var key in aaa.Keys)
//{
// if (jsonObject.ContainsKey(key))
// {
// aaa[key] = jsonObject[key];
// }
//}
}
catch (JsonException ex)
{
Console.WriteLine($"JSON 解析出错: {ex.Message}");
}
//string output = Regex.Replace(a, @"^-\s*|\n", "");
//string trimmed = a.Trim('[', ']').Replace("\n", "").Replace(" ", "");
//// 按逗号分隔字符串
//string[] keyValuePairs = trimmed.Split(new[] { '\"', ',' }, StringSplitOptions.RemoveEmptyEntries);
//foreach (var line in keyValuePairs)
//{
// string[] part = line.Split(new[] { ':' }, 2); // Split on first colon only
// if (part.Length == 2)
// {
// var key = part[0].Trim();
// var value = part[1].Trim();
// aaa[key] = value;
// }
//}
//var dataDict = new Dictionary<string, string>();
//foreach (var part in parts)
//{
// if (part.Contains(":"))
// {
// var keyValue = part.Split(new[] { ':' }, 2);
// dataDict[keyValue[0].Trim()] = keyValue[1].Trim();
// }
//}
//// Extract specific values with fallback to empty string if not found
//string article = parts[0]; // "ArticleRTIRES09221"
//string cleanedArticle = article.StartsWith("Article") ? article.Substring(7) : article; // 去掉 "Article"
//string vpn = dataDict.TryGetValue("VP/N", out var v) ? v : "";
//string lot = dataDict.TryGetValue("LOT", out var l) ? l : "";
//string qty = dataDict.TryGetValue("QTY", out var q) ? q : "";
//string coo = dataDict.TryGetValue("COO", out var c) ? c : "";
//string articleNumber = parts[0].Replace("Article", "PR"); // 将Article替换为PR
//string vpn = "";
//string lot = "";
//string qty = "";
//string coo = "AUSTIN"; // 根据要求固定为AUSTIN
//foreach (string segment in parts)
//{
// if (segment.StartsWith("VP/N:"))
// {
// vpn = segment.Substring(5); // 获取VP/N的值
// }
// else if (segment.StartsWith("LOT:"))
// {
// lot = segment.Substring(4); // 获取LOT的值
// }
// else if (segment == "5000") // QTY是单独的分段
// {
// qty = "5000";
// }
//}
//string text1 = articleNumber; // PRTIRES09221
//string text2 = $"{vpn};{lot};{qty};{coo}"; // CR0603-10W-2001FT;330NO057040;5000;AUSTIN
//// 4. 添加到workCodeInfo
//scanWork.workCodeInfo.Add(new CameraVisionLib.Model.BarcodeInfo { Text = text1 });
//scanWork.workCodeInfo.Add(new CameraVisionLib.Model.BarcodeInfo { Text = text2 });
// 2. Create the required text entries
//var text1 = $"{cleanedArticle}";
//var text2 = $"{vpn};{lot};{qty};{coo.Split(' ').FirstOrDefault()}";
// 3. Add to workCodeInfo
//scanWork.workCodeInfo.Add(new CameraVisionLib.Model.BarcodeInfo { Text = text1 });
//scanWork.workCodeInfo.Add(new CameraVisionLib.Model.BarcodeInfo { Text = text2 });
// aaa.Add(aa);
//{
// { "LOT", null }, // 默认值为 null
// { "Sample_2", null }, // 默认值为 null
// { "RID", null }, // 默认值为 null
// { "QTY", null }, // 默认值为 null
// { "VPN", null }, // 默认值为 null
// { "VPN_2", null }, // 默认值为 null
// { "FC", null }, // 默认值为 null
// // 默认值为 null
//};
// 3. 遍历所有字段,提取 LOT 和 RID
// 遍历所有字段,提取目标值
//foreach (string part in parts)
//{
// 检查字段是否包含目标键(如 "LOT:"、"QTY:"、"VP/N:")
// foreach (string key in aaa.Keys.ToList()) // 遍历字典的键
// {
// 特殊处理 VP/ N → VPN(替换 "/" 为 "_")
// string prefix = (key == "VPN") ? "VP/N:" : key + ":";
// if (part.StartsWith(prefix))
// {
// aaa[key] = part.Substring(prefix.Length); // 提取冒号后的值
// break; // 找到后跳出当前循环
// }
// if (part.StartsWith(prefix))
// {
// if (key == "LOT")
// {
// aaa[key] = $"<OCR>{part.Substring(prefix.Length)}";
// }
// else
// {
// aaa[key] = part.Substring(prefix.Length);
// aaa[key] = $"<OCR>{part.Substring(prefix.Length)}";
// }
// break;
// }
// }
//}
try
{
bool hasMatch = scanWork.MatchingTemplate();
BLLCommon.extension.SetKey(scanWork.originalCodeText, aaa, hasMatch, out _);
BLLCommon.extension.Print(hasMatch, aaa);
}
catch (Exception ex)
{
LogNet.log.Info("AI打印异常"+ex.ToString());
return new WebResultCamera { Text = "NG" };
}
//Common.frmMain.Invoke(delegate ()
//{
//scanWork.SetKey(hasMatch);
//});
//PrintAiing?.Invoke(aaa);
return new WebResultCamera { Text = "OK" };
}
public static void SaveBitmapToFile(Bitmap bmp, string filePath)
{
try
......@@ -422,6 +606,7 @@ namespace SmartScan
#endregion
}
}
}
......@@ -26,6 +26,7 @@ namespace SmartScan
_serviceHost = new(service, u);
_serviceHost.Open();
Registry.SetValue("HKEY_LOCAL_MACHINE\\SOFTWARE\\NS100\\", "port", u.Port);
Registry.SetValue("HKEY_CURRENT_USER\\SOFTWARE\\NS100\\", "port", u.Port);
Model.LogNet.log.Info("Web服务已开启,URL=" + url);
IsOpen = true;
}
......
支持 Markdown 格式
你添加了 0 到此讨论。请谨慎行事。
Finish editing this message first!