using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using CvBase;
using CWindowTool;
using HalconDotNet;
using System.IO;
namespace CvImageTool
{
public class OCR:BaseImageOpt
{
private string cProcessSettingFilePath = null;
private string cImageOptSettingFilePath = null;
private string cTrainFilePath = null;
private OCRForm ocrForm = null;
private OCRTool ocrTool = null;
private int CProcessIndex;
public HObject ROI = null;
public HObject resultRegion = null;
public HTuple OCRStr = null;
public HTuple OCRTypes = null;
public List<BaseProcess> processes;
public CWindowTool.CWindows[] cCWindows;
public OCR(int processIndex, int imageOptIndex, string description, EnumOrStruct.RunProcessOrign runProcessOrign)
{
switch (runProcessOrign)
{
case EnumOrStruct.RunProcessOrign.MainProcess:
processes = Refrence.Instance.baseProcesses.ToList();
cCWindows = Refrence.Instance.CWindows;
//获取流程配置文件和算法参数文件路径
cProcessSettingFilePath = Path.Combine(AppSetting.Instance.SubAppSettingRootPath, processes[processIndex].Name);
if (!Directory.Exists(cProcessSettingFilePath)) Directory.CreateDirectory(cProcessSettingFilePath);
cTrainFilePath = Path.Combine(cProcessSettingFilePath, "ocr.trf");
cImageOptSettingFilePath = Path.Combine(cProcessSettingFilePath, description) + ".json";
cProcessSettingFilePath = Path.Combine(cProcessSettingFilePath, AppSetting.Instance.ProcessFormSettingName);
break;
//case EnumOrStruct.RunProcessOrign.CalibrationProcess:
// processes = Refrence.Instance.CalibrationProcesses;
// cCWindows = Refrence.Instance.CalibrationHWindows;
// cProcessSettingFilePath = Path.Combine(AppSetting.Instance.CalProcessSettingDir, processes[processIndex].Name);
// if (!Directory.Exists(cProcessSettingFilePath)) Directory.CreateDirectory(cProcessSettingFilePath);
// cImageOptSettingFilePath = Path.Combine(cProcessSettingFilePath, description) + ".json";
// cModelHandleFilePatH = Path.Comby(cProcessSettingFilePath, description) + ".tiff";
// cProcessSettingFilePath = Path.Combine(cProcessSettingFilePath, AppSetting.Instance.ProcessSettingFileName);
// break;
}
this.Description = description;
this.CProcessIndex = processIndex;
this.ImageOptIndex = imageOptIndex;
//将当前算法的输入输出参数添加到公共资源类的输入输出列表
processes[processIndex].BaseImageOutParams.Add(new BarCodeOutput());
//创建算法抓圆图像处理工具
ocrTool = new OCRTool(CProcessIndex, ImageOptIndex, cProcessSettingFilePath, cImageOptSettingFilePath, cTrainFilePath, processes, cCWindows);
//创建算法抓圆窗体
ocrForm = new OCRForm(CProcessIndex, ImageOptIndex, cProcessSettingFilePath, cImageOptSettingFilePath, cTrainFilePath, description, processes, cCWindows);
}
/// </summary>
public override string Name { get; set; }
/// <summary>
/// 算法描述(参数文件的文件名/流程算法显示名称)
/// </summary>
public override string Description { get; set; }
/// <summary>
/// 英文名称,用于配置参数的读写和区分
/// </summary>
public override string EnglishName { get; set; }
/// <summary>
/// 算法所属流程名称
/// </summary>
public override string ParentProcessName { get; set; }
/// <summary>
/// 在所属流程中其索引
/// </summary>
public override int ImageOptIndex { get; set; }
/// <summary>
/// 算法运行
/// </summary>
public override void Running(bool isTest = false)
{
ReadFile();
#region 处理图像参数赋值
string[] imageSelects = ocrTool.imageSelect.Split('_');
if (imageSelects.Length != 2) return;
if (imageSelects[0] == "相机")
{
if (imageSelects[1] == "灰度图")
{
ocrTool.Image = processes[CProcessIndex].ImageGray;
}
else
{
ocrTool.Image = processes[CProcessIndex].ImageHeight;
}
}
else
{
ProcessTool.Instance.FindImageOpt(processes[CProcessIndex], imageSelects[0], out BaseImageOpt paramByImageOpt1, out int paramByImageOptIndex1);
if (paramByImageOptIndex1 >= 0)
{
processes[CProcessIndex].BaseImageOutParams[paramByImageOptIndex1].GetHObjectValue(imageSelects[1], out HObject value);
ocrTool.Image = value;
}
else
{
return;
}
}
#endregion
#region 仿射矩阵参数赋值
string[] matrixSelect;
if (ocrTool.ocrParams.isAffin)
matrixSelect = ocrTool.affinMatrixSelect.Split('_');
else
matrixSelect = new string[0];
if (matrixSelect.Length != 2 && ocrTool.ocrParams.isAffin) return;
ProcessTool.Instance.FindImageOpt(processes[CProcessIndex], matrixSelect[0], out BaseImageOpt paramByImageOpt2, out int paramByImageOptIndex2);
processes[CProcessIndex].BaseImageOutParams[paramByImageOptIndex2].GetHTupleValue(matrixSelect[1], out HTuple matrix);
ocrTool.AffinMatrix = matrix;
#endregion
//if (CProcessIndex == 3)
// ;
//运行算法
ocrTool.Running(out OCRStr, out OCRTypes, out ROI, out resultRegion);
#region 输出参数赋值
processes[CProcessIndex].BaseImageOutParams[ImageOptIndex].Dispose();
processes[CProcessIndex].BaseImageOutParams[ImageOptIndex].MeasureContours = ROI;
processes[CProcessIndex].BaseImageOutParams[ImageOptIndex].ResultRegions = resultRegion;
if (OCRStr.Length > 0)
{
processes[CProcessIndex].BaseImageOutParams[ImageOptIndex].ResultContent = OCRStr;
}
#endregion
#region 添加显示
if (ocrTool.isShowCharRegion)
{
CObject cObject1 = new CObject();
cObject1._Object = resultRegion;
cObject1._Color = "green";
cObject1._Draw = false;
cCWindows[CProcessIndex].AObject.Add(cObject1);
}
if (ocrTool.isShowROI)
{
CObject cObject1 = new CObject();
cObject1._Object = ROI;
cObject1._Color = "blue";
cObject1._Draw = false;
cCWindows[CProcessIndex].AObject.Add(cObject1);
}
if (ocrTool.isShowChar && OCRStr.Length > 0)
{
CText cText01 = new CText();
string content = "";
for (int i = 0; i < OCRStr.Length;i++)
{
content += OCRStr[i];
}
cText01._Text = "字符:" + content.ToString();
cText01._X = 5;
cText01._Y = 5;
cText01._Color = "blue";
cCWindows[CProcessIndex].AText.Add(cText01);
}
#endregion
}
/// <summary>
/// 显示算法相关界面
/// </summary>
public override void show(int processIndex, int imageIndex)
{
ocrForm.ShowDialog();
}
/// <summary>
/// 读取配置文件
/// </summary>
/// <param name="cProcessSettingFilePath"></param>
/// <param name="cImgeOptSettingFilePath"></param>
/// <returns></returns>
public override bool ReadFile()
{
return ocrTool.ReadFile(Description);
}
}
}