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;
using Newtonsoft.Json;
namespace CvImageTool
{
public class BarCodeTool
{
public FileINI INI = new FileINI();
public BarCodeParam barCodeParam; //匹配参数
private string cProcessSettingFilePath; //当前流程配置文件的路径
private string cImageOptSettingFilePath; //当前算法参数文件路径
public string imageSelect = ""; //图像输入参数选择项
public string affinMatrixSelect = ""; //仿射矩阵输入参数选择项
private int cProcessIndex = -1; //当前归属流程的索引
private int cImageOptIndex = -1; //当前归属算法的索引
public bool isShowBarCodeRegion = false;
public bool isShowROI = false;
public bool isShowBarCode = false;
public HObject Image;
public HTuple AffinMatrix;
private List<BaseProcess> process;
private CWindows[] cCWindows;
public BarCodeTool(int cProcessIndex, int cImageOptIndex,
string cProcessSettingFilePath, string cImageOptSettingFilePath,
List<BaseProcess> processes, CWindows[] cWindows)
{
this.cProcessSettingFilePath = cProcessSettingFilePath;
this.cImageOptSettingFilePath = cImageOptSettingFilePath;
this.cProcessIndex = cProcessIndex;
this.cImageOptIndex = cImageOptIndex;
this.process = processes;
this.cCWindows = cWindows;
barCodeParam = new BarCodeParam();
Image = new HObject();
}
/// <summary>
/// 读取配置文件
/// </summary>
/// <param name="cProcessSettingFilePath"></param>
/// <param name="cImgeOptSettingFilePath"></param>
/// <returns></returns>
public bool ReadFile(string cProcessDescription)
{
imageSelect = ""; affinMatrixSelect = "";
if (!File.Exists(cProcessSettingFilePath) || !File.Exists(cImageOptSettingFilePath)) return false;
imageSelect = FileINI.ReadValueFromIniFile(cProcessDescription, "Image", "", cProcessSettingFilePath);
affinMatrixSelect = FileINI.ReadValueFromIniFile(cProcessDescription, "Matrix", "", cProcessSettingFilePath);
isShowBarCodeRegion = Convert.ToBoolean(FileINI.ReadValueFromIniFile(cProcessDescription, "isShowBarCodeRegion", "", cProcessSettingFilePath));
isShowBarCode = Convert.ToBoolean(FileINI.ReadValueFromIniFile(cProcessDescription, "isShowBarCode", "", cProcessSettingFilePath));
isShowROI = Convert.ToBoolean(FileINI.ReadValueFromIniFile(cProcessDescription, "ShowROI", "", cProcessSettingFilePath));
barCodeParam = JsonConvert.DeserializeObject<BarCodeParam>(File.ReadAllText(cImageOptSettingFilePath));
return true;
}
/// <summary>
/// 写配置文件
/// </summary>
/// <param name="cProcessSettingFilePath"></param>
/// <param name="cImgeOptSettingFilePath"></param>
/// <returns></returns>
public bool WriteFile(string cProcessDescription)
{
FileINI.WriteValueFromIniFile(cProcessDescription, "Image", imageSelect, cProcessSettingFilePath);
FileINI.WriteValueFromIniFile(cProcessDescription, "Matrix", affinMatrixSelect, cProcessSettingFilePath);
FileINI.WriteValueFromIniFile(cProcessDescription, "isShowBarCodeRegion", isShowBarCodeRegion.ToString(), cProcessSettingFilePath);
FileINI.WriteValueFromIniFile(cProcessDescription, "isShowBarCode", isShowBarCode.ToString(), cProcessSettingFilePath);
FileINI.WriteValueFromIniFile(cProcessDescription, "ShowROI", isShowROI.ToString(), cProcessSettingFilePath);
var json = JsonConvert.SerializeObject(barCodeParam, Formatting.Indented);
File.WriteAllText(cImageOptSettingFilePath, json);
return true;
}
public string[] barCodeTypeStr = { "auto", "Codabar", "Code 39", "Code 93", "Code 128", "EAN-13", "EAN-8", "UPC-A", "UPC-E",
"MSI", "PharmaCode" , "GS1 DataBar Omnidi", "GS1 DataBar Truncated", "GS1 DataBar Stacked", "GS1 DataBar Stacked Omnidir",
"GS1 DataBar Limited", "GS1 DataBar Expanded", "GS1 DataBar Expanded Stacked", "GS1-128"};
public void Running(out HObject ROI, out HObject barCodeRegion, out HTuple barCodeStr, out HTuple resultCodeType)
{
ROI = new HObject(); barCodeRegion = new HObject(); barCodeStr = new HTuple(); resultCodeType = new HTuple();
if (Image == null) return;
HOperatorSet.SetSystem("clip_region", "false");
HOperatorSet.GenRectangle1(out ROI, barCodeParam.ROIStartRow, barCodeParam.ROIStartColumn,
barCodeParam.ROIEndRow, barCodeParam.ROIEndColumn);
string[] imageOptAndParamName = affinMatrixSelect.Split('_');
ProcessTool.Instance.FindImageOpt(process[cProcessIndex], imageOptAndParamName[0], out BaseImageOpt byImageOpt, out int byIndex);
if (byIndex >= 0)
process[cProcessIndex].BaseImageOutParams[byIndex].GetHTupleValue(imageOptAndParamName[1], out AffinMatrix);
else
AffinMatrix = null;
if (barCodeParam.isAffin && AffinMatrix != null)
{
HOperatorSet.AffineTransRegion(ROI, out HObject NROI, AffinMatrix, "nearest_neighbor");
ROI.Dispose();
ROI = NROI;
}
HOperatorSet.ReduceDomain(Image, ROI, out HObject ReduceImage);
HTuple names = "element_size_min";
names.Append("element_size_max");
names.Append("element_height_min");
HTuple values = barCodeParam.barWidthMin;
values.Append(barCodeParam.barWidthMax);
values.Append(barCodeParam.barHeightMin);
HOperatorSet.CreateBarCodeModel(names, values, out HTuple barCodeHandle);
HOperatorSet.Rgb1ToGray(ReduceImage, out ReduceImage);
HOperatorSet.FindBarCode(ReduceImage, out barCodeRegion, barCodeHandle, barCodeTypeStr[(int)barCodeParam.barcodeType], out barCodeStr);
if (barCodeParam.barcodeType == EnumOrStruct.BarCodeType.auto)
HOperatorSet.GetBarCodeResult(barCodeHandle, "all", "decoded_types", out resultCodeType);
else
resultCodeType = barCodeTypeStr[(int)barCodeParam.barcodeType];
if (barCodeHandle != null) HOperatorSet.ClearBarCodeModel(barCodeHandle);
}
}
}