Commit c5ee0a04 刘韬

NS200贴标计算更新

1 个父辈 01437829
......@@ -146,7 +146,7 @@ namespace BLL
keyword = null;
code.ForEach((c) =>
{
LogNet.log.Info($"扫描到 {c.CodeType},{c.Text}");
LogNet.log.Info($"扫描到 {c.Angle}, {c.CodeType},{c.Text}");
});
MatchAnalysis.StartNewAnalysis(code);
if (Config.Func_EnabledOCR)
......@@ -158,7 +158,7 @@ namespace BLL
string currentDirectory = System.Windows.Forms.Application.StartupPath;
string filePath = System.IO.Path.Combine(currentDirectory, "ocr.jpg");
//CurrntBitmap?.Save(filePath, System.Drawing.Imaging.ImageFormat.Jpeg);
LogNet.log.Info($"开始保存图片");
LogNet.log.Info($"开始保存图片:{filePath}");
CurrntBitmap?.Save(filePath, System.Drawing.Imaging.ImageFormat.Jpeg);
LogNet.log.Info($"保存图片");
......@@ -821,6 +821,12 @@ namespace BLL
var y2 = p1.Y + distance * Math.Sin(angle / 180 * Math.PI);
return new System.Drawing.Point((int)x2, (int)y2);
}
public static System.Drawing.Point PointWithAngle(System.Drawing.Point p1, double angle, double distance)
{
var x2 = p1.X + distance * Math.Cos(angle / 180 * Math.PI);
var y2 = p1.Y + distance * Math.Sin(angle / 180 * Math.PI);
return new System.Drawing.Point((int)x2, (int)y2);
}
//public Dictionary<string, string> ExtractCode(List<MaterialCode> code, int mateIndex)
//{
// Dictionary<string, string> keyword = new Dictionary<string, string>();
......
......@@ -80,7 +80,7 @@ namespace SmartScan
this.BtnOK.BorderWidth = 2;
this.BtnOK.ForeColor = System.Drawing.Color.FromArgb(((int)(((byte)(230)))), ((int)(((byte)(230)))), ((int)(((byte)(230)))));
this.BtnOK.HoldPress = false;
this.BtnOK.Location = new System.Drawing.Point(910, 737);
this.BtnOK.Location = new System.Drawing.Point(641, 689);
this.BtnOK.Name = "BtnOK";
this.BtnOK.Padding = new System.Windows.Forms.Padding(3);
this.BtnOK.Size = new System.Drawing.Size(120, 40);
......@@ -96,7 +96,7 @@ namespace SmartScan
this.BtnCancel.BorderWidth = 2;
this.BtnCancel.ForeColor = System.Drawing.Color.FromArgb(((int)(((byte)(230)))), ((int)(((byte)(230)))), ((int)(((byte)(230)))));
this.BtnCancel.HoldPress = false;
this.BtnCancel.Location = new System.Drawing.Point(1036, 737);
this.BtnCancel.Location = new System.Drawing.Point(767, 689);
this.BtnCancel.Name = "BtnCancel";
this.BtnCancel.Padding = new System.Windows.Forms.Padding(3);
this.BtnCancel.Size = new System.Drawing.Size(120, 40);
......@@ -116,7 +116,7 @@ namespace SmartScan
this.PnlTemp.Location = new System.Drawing.Point(142, 55);
this.PnlTemp.Name = "PnlTemp";
this.PnlTemp.Padding = new System.Windows.Forms.Padding(3);
this.PnlTemp.Size = new System.Drawing.Size(1140, 676);
this.PnlTemp.Size = new System.Drawing.Size(871, 628);
this.PnlTemp.TabIndex = 11;
this.PnlTemp.Text = "facePanel1";
this.PnlTemp.TitleFont = new System.Drawing.Font("宋体", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
......@@ -130,7 +130,7 @@ namespace SmartScan
this.BtnApply.BorderWidth = 2;
this.BtnApply.ForeColor = System.Drawing.Color.FromArgb(((int)(((byte)(230)))), ((int)(((byte)(230)))), ((int)(((byte)(230)))));
this.BtnApply.HoldPress = false;
this.BtnApply.Location = new System.Drawing.Point(1162, 737);
this.BtnApply.Location = new System.Drawing.Point(893, 689);
this.BtnApply.Name = "BtnApply";
this.BtnApply.Padding = new System.Windows.Forms.Padding(3);
this.BtnApply.Size = new System.Drawing.Size(120, 40);
......@@ -145,7 +145,7 @@ namespace SmartScan
this.CboLanguage.BorderStyle = Asa.FaceControl.ControlShape.Rectangle;
this.CboLanguage.BorderWidth = 2;
this.CboLanguage.ForeColor = System.Drawing.Color.FromArgb(((int)(((byte)(230)))), ((int)(((byte)(230)))), ((int)(((byte)(230)))));
this.CboLanguage.Location = new System.Drawing.Point(1002, 11);
this.CboLanguage.Location = new System.Drawing.Point(733, 11);
this.CboLanguage.Name = "CboLanguage";
this.CboLanguage.Padding = new System.Windows.Forms.Padding(3);
this.CboLanguage.ReadOnly = true;
......@@ -230,7 +230,7 @@ namespace SmartScan
//
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.None;
this.AutoSize = true;
this.ClientSize = new System.Drawing.Size(1293, 788);
this.ClientSize = new System.Drawing.Size(1024, 740);
this.Controls.Add(this.but_Labeling);
this.Controls.Add(this.Keywordlabeling);
this.Controls.Add(this.BtnWorkMode);
......
......@@ -199,144 +199,6 @@ namespace SmartScan.PlusSettingFrm
labelings.Add(labeling);
}
public static System.Drawing.Point LabelingAngle(string name, AMatch aMatch, System.Drawing.Point centrality, bool isvaor, int PlateW, out int angle)
{
System.Drawing.Point lable = aMatch.Points;
var data = labelings.Where(a => a.Name == name).FirstOrDefault();
LogNet.log.Info($"{name}:原标签角度x={lable.X};y={lable.Y};中心点x={centrality.X};y={centrality.Y}");
if (data != null)
{
if (data.IsOverlaylabel)
{
#region 覆盖原标签
//LogNet.log.Info($"{name}:覆盖原标签;是否ID相机={isvaor};");
//aMatch.Points = LabelingPosition.ClockwiseRotation(lable, centrality, 0);
//if (isvaor)
//{
// angle = ScanningCameraAngle(aMatch.Angle);
//}
//else
//{
// angle = OldAngleAlgo(centrality, lable, PlateW);
//}
//LogNet.log.Info($"{name}:覆盖原标签!新标签位置:{aMatch.Points};角度:{angle}");
//return aMatch.Points;
#endregion
return Overwritelabel(centrality, aMatch.Points, isvaor, PlateW, aMatch.Angle, out angle);
}
System.Drawing.Point point = new Point(0, 0);
if (data.IscIockwise)
{
point = LabelingPosition.ClockwiseRotation(lable, centrality, data.RotationAngle);
}
if (data.IsCounterclockwise)
{
point = LabelingPosition.CounterclockwiseRotation(lable, centrality, data.RotationAngle);
}
if (!data.IscIockwise && !data.IsCounterclockwise)
{
return Overwritelabel(centrality, aMatch.Points, isvaor, PlateW, aMatch.Angle, out angle);
}
System.Drawing.Point Newpoint = new System.Drawing.Point(point.X + data.Xoffset, point.Y + data.Yoffset);
angle = data.LabelRotationAngle;
LogNet.log.Info($"{name}:xy轴偏移后位置,x={Newpoint.X},y={Newpoint.Y};角度为:{angle}");
return Newpoint;
}
else
{
LogNet.log.Info($"未找到<{name}>模板:默认覆盖原标签!");
return Overwritelabel(centrality, aMatch.Points, isvaor, PlateW, aMatch.Angle, out angle);
#region 覆盖原标签
//aMatch.Points = LabelingPosition.ClockwiseRotation(lable, centrality, 0);
//if (isvaor)
//{
// angle = ScanningCameraAngle(aMatch.Angle);
//}
//else
//{
// angle = OldAngleAlgo(centrality, lable, PlateW);
//}
//LogNet.log.Info($"未找到<{name}>模板:默认覆盖原标签!新标签位置:{aMatch.Points};角度:{angle}");
//return aMatch.Points;
#endregion
}
}
public static System.Drawing.Point LabelingAngle_New(string name, AMatch aMatch, System.Drawing.Point centrality, bool isvaor, int PlateW, out int angle)
{
System.Drawing.Point label = aMatch.Points;
var data = labelings.FirstOrDefault(a => a.Name == name);
LogNet.log.Info($"{name}:原标签角度x={label.X};y={label.Y};中心点x={centrality.X};y={centrality.Y}");
if (data == null)
{
LogNet.log.Info("未查询到贴标数据,使用覆盖原标签方式;");
return Overwritelabel(centrality, aMatch.Points, isvaor, PlateW, aMatch.Angle, out angle);
}
else
{
if (data.IsOverlaylabel || (!data.IscIockwise && !data.IsCounterclockwise))
{
var position = Overwritelabel(centrality, aMatch.Points, isvaor, PlateW, aMatch.Angle, out angle);
return new Point(position.X+data.Xoffset,position.Y+data.Yoffset);
}
System.Drawing.Point point = new Point(0, 0);
if (data.IscIockwise)
{
point = LabelingPosition.ClockwiseRotation(label, centrality, data.RotationAngle);
}
else if (data.IsCounterclockwise)
{
point = LabelingPosition.CounterclockwiseRotation(label, centrality, data.RotationAngle);
}
System.Drawing.Point newPoint = new System.Drawing.Point(point.X + data.Xoffset, point.Y + data.Yoffset);
angle = data.LabelRotationAngle;
LogNet.log.Info($"{name}:xy轴偏移后位置,x={newPoint.X},y={newPoint.Y};角度为:{angle}");
return newPoint;
}
}
public static Point Overwritelabel(Point centrality, Point lable, bool isvaor, int PlateW, int Angle, out int angle)
{
Point Points = LabelingPosition.ClockwiseRotation(lable, centrality, 0);
if (isvaor)
{
angle = ScanningCameraAngle(Angle);
}
else
{
angle = OldAngleAlgo(centrality, lable, PlateW);
}
LogNet.log.Info($"覆盖原标签!是否id相机={isvaor};新标签位置:{Points};角度:{angle}");
return Points;
}
public static int ScanningCameraAngle(int angles)
{
int angle = 0;
double rotationAngle = 83;
if (angles < 0 && angles != 0)
{
angle = (int)(Math.Abs(angles) + rotationAngle);
}
else
{
if (angles <= 83)
{
angle = (int)Math.Abs(angles - rotationAngle);
}
else
{
angle = 350 - (int)(angles - rotationAngle);
}
}
angle += 90;
if (angle > 344)
{
angle -= 344;
}
return angle;
}
/// <summary>
/// 获取以点为中心的角度,-180~180
/// </summary>
......
using Asa.FaceControl;
using BLL;
using DeviceLibrary.AutoScanAndLabel;
using DocumentFormat.OpenXml.Drawing;
using DocumentFormat.OpenXml.EMMA;
......@@ -258,13 +259,13 @@ namespace SmartScan.PlusSettingFrm
item.key = "";
}
}
public static System.Drawing.Point LabelingAngle(string keyvalue, AMatch aMatch, System.Drawing.Point centrality, bool isvaor, int PlateW, out int angle)
public static Point LabelingAngle(string keyvalue, AMatch aMatch, System.Drawing.Point centrality, bool isvaor, int PlateW, out int angle)
{
Point lable = aMatch.Points;
Point newpoint = aMatch.Points;
var Data = labels.FirstOrDefault(a => a.value == keyvalue);
int RotateAngle = 0;
int X = 0, Y = 0;
//int X = 0, Y = 0;
if (Data==null&&labels.Count>0)
{
Data = labels.FirstOrDefault(a=>a.Primarykey&&a.PlateW==PlateW);
......@@ -272,53 +273,59 @@ namespace SmartScan.PlusSettingFrm
if (Data!=null)
{
RotateAngle = Data.ClockwiseAngle;
X += Data.X; Y += Data.Y;
//X += Data.X; Y += Data.Y;
LogUtil.info($"关键字{Data.key}={Data.value};顺时针旋转角度={Data.ClockwiseAngle};xy偏移{Data.X};{Data.Y};标签角度:{Data.LabelAngle}");
}
newpoint = Overwritelabel(centrality, aMatch.Points, isvaor, PlateW, aMatch.Angle, RotateAngle, out angle);
if (Data != null&&Data.LabelAngle > 0)
//if (Data != null&&Data.LabelAngle > 0)
//{
// angle = Data.LabelAngle;
//}
if (Data.X != 0) {
newpoint= MaterialEdit.PointWithAngle(newpoint, angle - 90, Data.X);
}
if (Data.Y != 0)
{
angle = Data.LabelAngle;
newpoint = MaterialEdit.PointWithAngle(newpoint, angle, Data.Y);
}
return new Point(newpoint.X + X, newpoint.Y + Y);
LogUtil.info($"处理后角度={angle};处理后贴标位置:{newpoint};标签变换角度:{Data.LabelAngle}");
return new Point(newpoint.X, newpoint.Y);
}
public static Point Overwritelabel(Point centrality, Point lable, bool isvaor, int PlateW, int Angle,int RotateAngle, out int angle)
{
Point Points = LabelingPosition.ClockwiseRotation(lable, centrality, RotateAngle);
if (isvaor)
{
angle = ScanningCameraAngle(Angle);
angle = ScanningCameraAngle(Angle, RotateAngle);
}
else
{
angle = OldAngleAlgo(centrality, lable, PlateW);
}
LogNet.log.Info($"是否id相机={isvaor};新标签位置:{Points};角度:{angle}");
LogNet.log.Info($"是否id相机={isvaor};新标签位置:{Points};输入角度:{Angle},设定偏转:{RotateAngle},转换后角度:{angle}");
return Points;
}
public static int ScanningCameraAngle(int angles)
public static int ScanningCameraAngle(int angles, int RotateAngle)
{
int angle = 0;
double rotationAngle = 83;
if (angles < 0 && angles != 0)
//ID MVS相机, 右侧为正角度, 左侧为负角度
int angle;
int rotationAngle = RotateAngle+83;// 83;
if (angles < 0)
{
angle = (int)(Math.Abs(angles) + rotationAngle);
angle = angles * -1 + rotationAngle;
}
else
{
if (angles <= 83)
{
angle = (int)Math.Abs(angles - rotationAngle);
angle = 360 - angles + rotationAngle;
}
else
angle += 90;
if (angle >= 360)
{
angle = 350 - (int)(angles - rotationAngle);
angle -= 360;
}
}
angle += 90;
if (angle > 344)
else if (angle < 0)
{
angle -= 344;
angle = 360 + angle;
}
return angle;
}
......
using BLL;
using HalconDotNet;
using Model;
using System;
using System.Collections.Generic;
......
......@@ -233,10 +233,11 @@ namespace SmartScan
System.Threading.Thread.Sleep(100); //光源打开有一个由暗变亮的过程
}
List<Bitmap> image = new List<Bitmap>(Camera.CaptureAndGetCode(out workCodeInfo));
if (image.Count > 0 && !BLL.Config.Backgrounder) picShow.Image = image[0];
if (image.Count > 0)
{
BLLCommon.mateEdit.CurrntBitmap = image[0];
BLLCommon.mateEdit.CurrntBitmap = WebCallWork.DeepClone(image[0]);
if (!BLL.Config.Backgrounder)
picShow.Image = image[0];
if (BLLCommon.mateEdit.CurrntBitmap!=null)
_ = UnifiedDataHandler.PostSmfImageAsync(BLLCommon.mateEdit.CurrntBitmap, new Dictionary<string, string> { { "cid", BLLCommon.config.CID + "_1" } }, BLLCommon.mateEdit.CurrntBitmap.Width, BLLCommon.mateEdit.CurrntBitmap.Height);
}
......
......@@ -69,16 +69,16 @@ namespace SmartScan
if (workCodeInfo.Count == 0)
return new WebResultCode() { ErrorCode = -1, Msg = "扫码失败" };
BLLCommon.mateEdit.CurrntBitmap = bitmap;
BLLCommon.mateEdit.CurrntBitmap = DeepClone(bitmap);
bool rtn = BLLCommon.mateEdit.MatchingTemplate(workCodeInfo, BLLCommon.config.DefaultMaterialName, false, out string mateName, out workCodeKeyword, out AMatch aMatch);
LogNet.log.Info("模板匹配结果:" + rtn);
WebResultCode webResultCode = null;
Dictionary<string,string> keys=new Dictionary<string, string> ();
Dictionary<string, string> keys = new Dictionary<string, string>();
foreach (var item in workCodeKeyword)
{
// workCodeKeyword[item.Key]= item.Value.Replace("<OCR>", "");
keys.Add(item.Key,item.Value.Replace("<OCR>",""));
keys.Add(item.Key, item.Value.Replace("<OCR>", ""));
}
if (!BLLCommon.extension.SetKey(null, keys, rtn, out string errmsg))
{
......@@ -138,10 +138,35 @@ namespace SmartScan
LogNet.log.Error("ProcessBitmap", ex);
return new WebResultCode() { ErrorCode = -1, Msg = "扫码失败:" + ex };
}
finally {
bitmap?.Dispose();
}
}
public static T DeepClone<T>(T _object)
{
try
{
T dstobject;
using (MemoryStream mStream = new MemoryStream())
{
BinaryFormatter bf = new BinaryFormatter();
bf.Serialize(mStream, _object);
mStream.Seek(0, SeekOrigin.Begin);//指定当前流的位置为流的开头。
dstobject = (T)bf.Deserialize(mStream);
mStream.Close();
}
return dstobject;
}
catch (Exception e)
{
LogNet.log.Error("DeepClone" , e);
return default;
}
}
public WebResultCode ProcessBitmaps(BitmapData bitmapData)
{
BLLCommon.mateEdit.CurrntBitmap?.Dispose();
Bitmap bitmap = null;
try
{
if (bitmapData == null)
......@@ -154,18 +179,18 @@ namespace SmartScan
}
byte[] imageData = Convert.FromBase64String(bitmapData.ImageData);
Bitmap bitmap = null;
using(MemoryStream stream = new MemoryStream(imageData))
using (MemoryStream stream = new MemoryStream(imageData))
{
bitmap = new Bitmap(stream);
}
if (bitmap==null)
if (bitmap == null)
{
return new WebResultCode() { ErrorCode = -1, Msg = "图片解析完成为空;" };
}
List<CameraVisionLib.Model.BarcodeInfo> workCodeInfo = new();
if (bitmapData.BarCodeList == null&&bitmapData.IsIDCamera)
if (bitmapData.BarCodeList == null && bitmapData.IsIDCamera)
{
return new WebResultCode() { ErrorCode = -1, Msg = "扫码相机未传来条码数据;" };
}
......@@ -191,19 +216,19 @@ namespace SmartScan
if (workCodeInfo.Count == 0)
return new WebResultCode() { ErrorCode = -1, Msg = "扫码失败" };
}
BLLCommon.mateEdit.CurrntBitmap = bitmap;
BLLCommon.mateEdit.CurrntBitmap = DeepClone(bitmap);
Dictionary<string, string> workCodeKeyword;
bool rtn = BLLCommon.mateEdit.MatchingTemplate(workCodeInfo, BLLCommon.config.DefaultMaterialName, false, out string mateName, out workCodeKeyword, out AMatch aMatch);
var point=new Point(-1,-1);
var point = new Point(-1, -1);
int angle = 0;
LogNet.log.Info("模板匹配结果:" + rtn);
if (rtn)
{
string key = ConfigHelper.Config.Get("Label_Key", "");
workCodeKeyword.TryGetValue(key, out String Value);
LogNet.log.Info("模板匹配结果:" + rtn + $";关键字{key}:{Value};匹配成功条码xy:X={aMatch.Points.X};Y={aMatch.Points.Y},中心点x={bitmapData.X};y={bitmapData.Y},角度={aMatch.Angle},料盘尺寸={bitmapData.PlateW};匹配成功条码:{string.Join(", ", aMatch.IsCodeUsed)}" );
point=UsrKeywordlabeling.LabelingAngle(Value, aMatch, new Point(bitmapData.X, bitmapData.Y), bitmapData.IsIDCamera, bitmapData.PlateW, out angle);
LogNet.log.Info("模板匹配结果:" + rtn + $";关键字{key}:{Value};匹配成功条码xy:X={aMatch.Points.X};Y={aMatch.Points.Y},中心点x={bitmapData.X};y={bitmapData.Y},角度={aMatch.Angle},料盘尺寸={bitmapData.PlateW};匹配成功条码:{string.Join(", ", aMatch.IsCodeUsed)}");
point = UsrKeywordlabeling.LabelingAngle(Value, aMatch, new Point(bitmapData.X, bitmapData.Y), bitmapData.IsIDCamera, bitmapData.PlateW, out angle);
//point =UsrCustomlabeling.LabelingAngle_New(mateName, aMatch, new Point(bitmapData.X, bitmapData.Y), bitmapData.IsIDCamera, bitmapData.PlateW, out angle);
}
Dictionary<string, string> keys = new Dictionary<string, string>();
......@@ -216,23 +241,29 @@ namespace SmartScan
return new WebResultCode() { ErrorCode = -2, Msg = errmsg };
}
List<KeyValuePair<string, string>> result = new List<KeyValuePair<string, string>>();
workCodeKeyword=WebserverReplaceData(keys);
workCodeKeyword = WebserverReplaceData(keys);
foreach (var wc in workCodeKeyword)
{
result.Add(new KeyValuePair<string, string>(wc.Key, wc.Value));
}
NewPositionAngle newPosition = new NewPositionAngle
{
X= point.X, Y= point.Y,Angle= angle,IsCodeUsed=aMatch.IsCodeUsed
X = point.X,
Y = point.Y,
Angle = angle,
IsCodeUsed = aMatch.IsCodeUsed
};
LogNet.log.Info($"返回坐标及角度X={newPosition.X};Y={newPosition.Y};角度={newPosition.Angle}");
return new WebResultCode() { workCodeKeyword = result, workCodeInfo = workCodeInfo, PositionAngle= newPosition };
return new WebResultCode() { workCodeKeyword = result, workCodeInfo = workCodeInfo, PositionAngle = newPosition };
}
catch (Exception ex)
{
LogNet.log.Error("ProcessBitmaps", ex);
return new WebResultCode() { ErrorCode = -1, Msg = "扫码失败:" + ex };
}
finally {
bitmap?.Dispose();
}
}
public Dictionary<string, string> WebserverReplaceData(Dictionary<string, string> workCodeKeyword)
......
支持 Markdown 格式
你添加了 0 到此讨论。请谨慎行事。
Finish editing this message first!