增加上相机扫码功能

master
lhiven 1 year ago
parent 93b56ceaa3
commit 19d42e5722

@ -0,0 +1,25 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Rs.MotionPlat.Commom
{
/// <summary>
/// 虚拟码
/// </summary>
public static class VirtualBarCode
{
private static int barCode = 1;
public static string Code
{
get
{
barCode++;
return barCode.ToString().PadLeft(18,'0');
}
}
}
}

@ -166,21 +166,33 @@ namespace Rs.MotionPlat.Commom
mr.Score = score.DArr[0];
mr.Himage = hImage;
mr.LocationOk = true;
FindCode(hImage, ref mr);
if(GlobalVar.EnableScanBarCodeByDownCamera)
{
FindCode(hImage, ref mr);
}
string dirname = $"d://images/{DateTime.Now.ToString("yyyyMMdd")}";
if(!Directory.Exists(dirname))
{
Directory.CreateDirectory(dirname);
}
if(mr.ScanOK)
if (!GlobalVar.EnableScanBarCodeByDownCamera)
{
mr.IsOK= true;
HOperatorSet.WriteImage(hImage, "bmp", 0, $"{dirname}//{mr.SN.Replace("\\", "").Replace("\"", "")+"_"+ DateTime.Now.ToString("yyyyMMddHHmmssfff")}");
mr.IsOK = true;
}
else
{
HOperatorSet.WriteImage(hImage, "bmp", 0, $"{dirname}//{DateTime.Now.ToString("yyyyMMddHHmmssfff")}");
if (mr.ScanOK)
{
mr.IsOK = true;
HOperatorSet.WriteImage(hImage, "bmp", 0, $"{dirname}//{mr.SN.Replace("\\", "").Replace("\"", "") + "_" + DateTime.Now.ToString("yyyyMMddHHmmssfff")}");
}
else
{
HOperatorSet.WriteImage(hImage, "bmp", 0, $"{dirname}//{DateTime.Now.ToString("yyyyMMddHHmmssfff")}");
}
}
matches.Add(mr);
}
else
@ -189,7 +201,6 @@ namespace Rs.MotionPlat.Commom
mr.LocationOk = false;
mr.IsOK = false;
mr.Row = 0;
mr.Col = 0;
mr.Angle = 0;
mr.Score = 0;

@ -23,6 +23,11 @@ namespace Rs.MotionPlat.Flow.Camera
/// </summary>
public HObject SearchRegion { get; set; }
/// <summary>
/// 二维码区域
/// </summary>
public HObject BarCodeRegion { get; set; }
/// <summary>
/// 起始角度
/// </summary>
@ -62,6 +67,9 @@ namespace Rs.MotionPlat.Flow.Camera
string searchReg = Path.Combine(modelDir, "search.reg");
HOperatorSet.ReadRegion(out HObject searchRegion, searchReg);
SearchRegion = searchRegion;
string barCodeReg = Path.Combine(modelDir, "barcode.reg");
HOperatorSet.ReadRegion(out HObject barCodeRegion, searchReg);
BarCodeRegion = barCodeRegion;
}
}

@ -225,7 +225,24 @@ namespace Rs.MotionPlat.Flow.Camera
vr.OffsetY =-1 * (row.D- upCameraScanBarCodeModel.ModelMatchResult[0].D) * GlobalVar.UpCameraMmPerPixel;
vr.OffsetR = AngleTool.Rad2Deg((angle.D - upCameraScanBarCodeModel.ModelMatchResult[2].D));
vr.SourceImage = image;
vr.SearchModelOK = true;
if(vr.OffsetX<GlobalVar.TurnoverTrayLocateXRange && vr.OffsetY<GlobalVar.TurnoverTrayLocateYRange && vr.OffsetR<GlobalVar.TurnoverTrayLocateRRange)
{
vr.SearchModelOK = true;
//只有定位成功的才去扫二维码
string sn = FindCode(image, upCameraScanBarCodeModel.BarCodeRegion);
if(string.IsNullOrEmpty(sn))
{
vr.ScanBarCodeOK = false;
}
else
{
vr.SN = sn;
}
}
else
{
vr.SearchModelOK = false;
}
}
return vr;
}
@ -240,5 +257,67 @@ namespace Rs.MotionPlat.Flow.Camera
}
return vr;
}
public static string FindCode(HObject images,HObject _barCodeRegion)
{
try
{
// HOperatorSet.GenRectangle1(out HObject searchRegion, new HTuple(1533), new HTuple(731), new HTuple(1751), new HTuple(1071));
//HOperatorSet.GenRectangle1(out HObject searchRegion,row1, col1, row2, col2);
HOperatorSet.ReduceDomain(images, _barCodeRegion, out HObject imgReduaced);
//HOperatorSet.CropDomain(imgReduaced, out HObject imagePart);
//HOperatorSet.WriteImage(imagePart, "bmp", 0, "d://images/11");
//HOperatorSet.Threshold(imagePart, out HObject region, new HTuple(200), new HTuple(255));
//HOperatorSet.Connection(region, out HObject connectedRegions);
//HOperatorSet.FillUp(connectedRegions, out HObject regionFillUp);
//HOperatorSet.ShapeTrans(regionFillUp, out HObject regionTrans, "rectangle1");
//HOperatorSet.SelectShape(regionTrans, out HObject selectedRegions, "area", "and", new HTuple(7000), new HTuple(9000));
//HOperatorSet.SelectShape(selectedRegions, out HObject searchRegions, "outer_radius", "and", new HTuple(50), new HTuple(70));
//if (searchRegions.IsInitialized() && searchRegions.CountObj() == 1)
{
//找到了二维码区域
//HOperatorSet.DilationRectangle1(searchRegions, out HObject regionDilation, new HTuple(40), new HTuple(40));
//HOperatorSet.ReduceDomain(imagePart, regionDilation, out HObject imageReduacedQrcode);
//HOperatorSet.InvertImage(imageReduacedQrcode, out HObject imageInvert);
//HOperatorSet.ScaleImage(imageInvert, out HObject imageScaled, 2, 0);
//HOperatorSet.WriteImage(imageScaled, "bmp", 0, "d://images/22");
//HOperatorSet.ZoomImageFactor(imageScaled, out HObject imageZoomed, new HTuple(10), new HTuple(10), "constant");
HTuple decodedDataStrings = "";
HObject xlds = new HObject();
string[] modes = new string[] { "standard_recognition", "enhanced_recognition", "maximum_recognition" };
foreach (string mode in modes)
{
HOperatorSet.CreateDataCode2dModel("Data Matrix ECC 200", "default_parameters", mode, out HTuple dataCodeHandle);
HOperatorSet.FindDataCode2d(imgReduaced, out xlds, dataCodeHandle, new HTuple(), new HTuple(), out HTuple resultHandles, out decodedDataStrings);
if (decodedDataStrings.Length > 0)
{
return decodedDataStrings.ToString().Replace("\"", "");
}
else
{
if (mode == "maximum_recognition")
{
HOperatorSet.WriteImage(images, "bmp", 0, $"d://images/scanfail/{DateTime.Now.ToString("yyyy-MM-dd HHmmss ffff")}");
HOperatorSet.ClearDataCode2dModel(dataCodeHandle);
}
}
}
return "";
}
//else
//{
// HOperatorSet.WriteImage(images, "bmp", 0, $"d://images/scanfail/{DateTime.Now.ToString("yyyy-MM-dd HHmmss ffff")}");
// mr.ScanOK = false;
//}
}
catch (Exception ex)
{
LogHelper.Error(ex.Message, ex);
return "";
}
}
}
}

@ -708,11 +708,14 @@ namespace Rs.MotionPlat.Flow
}
if (GlobalVar.RunSpace && GlobalVar.EnableScanBarCodeByDownCamera)
{
foreach (Nozzle nl in NozzleManager.GetNozzlesByStatus(ENozzleStatus.ToUnload))
if(GlobalVar.EnableScanBarCodeByDownCamera)
{
if (string.IsNullOrEmpty(nl.SN))
foreach (Nozzle nl in NozzleManager.GetNozzlesByStatus(ENozzleStatus.ToUnload))
{
nl.SN = (nl.FromIndex + 1).ToString().PadLeft(18, '0');
if (string.IsNullOrEmpty(nl.SN))
{
nl.SN = VirtualBarCode.Code;// (nl.FromIndex + 1).ToString().PadLeft(18, '0');
}
}
}
mrs = new List<MatchResult>();
@ -743,11 +746,15 @@ namespace Rs.MotionPlat.Flow
logInfo = GetClassName() + $"图片处理完成,准备放料";
MessageQueue.Instance.Insert(logInfo);
int count = 0;
foreach (Nozzle nozzle in NozzleManager.GetNozzlesByStatus( ENozzleStatus.ToUnload))
if(GlobalVar.EnableScanBarCodeByDownCamera)
{
nozzle.SN = mrs[count].SN;
count++;
foreach (Nozzle nozzle in NozzleManager.GetNozzlesByStatus(ENozzleStatus.ToUnload))
{
nozzle.SN = mrs[count].SN;
count++;
}
}
//int ni = 1;
//foreach (MatchResult item in mrs)
//{
@ -764,7 +771,7 @@ namespace Rs.MotionPlat.Flow
List<int> errCodeList = new List<int>();
foreach (MatchResult item in mrs)
{
if(item.IsOK)
if(item.IsOK&&GlobalVar.EnableScanBarCodeByDownCamera)
{
NozzleManager.GetNozzle(ni).SN = item.SN;
}
@ -1449,16 +1456,34 @@ namespace Rs.MotionPlat.Flow
{
if(TurnoverTrayManager.Instance.Slot(vr.SlotIndex).IsHasProduct)
{
if (vr.SearchModelOK && vr.OffsetX < 0.5 && vr.OffsetY < 0.5 && vr.OffsetR < 5)//定位成功
if (vr.SearchModelOK)//定位成功
{
if(vr.ScanBarCodeOK)
if(!GlobalVar.EnableScanBarCodeByDownCamera)
{
//复制二维码
LoadAndUnloadTask.Instance.CopyBarcodeToTask(vr.SlotIndex, vr.SN);
if(GlobalVar.RunSpace)
{
LoadAndUnloadTask.Instance.CopyBarcodeToTask(vr.SlotIndex, VirtualBarCode.Code);
}
else
{
if(vr.ScanBarCodeOK)
{
//复制二维码
LoadAndUnloadTask.Instance.CopyBarcodeToTask(vr.SlotIndex, vr.SN);
}
else
{
//复制二维码
LoadAndUnloadTask.Instance.CopyBarcodeToTask(vr.SlotIndex, "");
}
}
}
}
else
{
//定位失败,或者超出指定范围
}
}
}
OnUpCameraGrabResult?.Invoke(result);
}

@ -133,6 +133,7 @@
<Compile Include="Commom\Scheduling.cs" />
<Compile Include="Commom\SlotPoint.cs" />
<Compile Include="Commom\TestCenterMessageBox.cs" />
<Compile Include="Commom\VirtualBarCode.cs" />
<Compile Include="Entitys\TransitNozzleManager.cs" />
<Compile Include="Commom\TrayPointManager.cs" />
<Compile Include="Commom\TurnoverSlotOffset.cs" />

Loading…
Cancel
Save