199 lines
6.1 KiB
C#
199 lines
6.1 KiB
C#
using Aurora.Api;
|
|
using MilCommon;
|
|
using VisionCommon;
|
|
using Hermes;
|
|
using Matrox.MatroxImagingLibrary;
|
|
using System;
|
|
using System.IO;
|
|
using System.Drawing;
|
|
using VisionCommonUI;
|
|
|
|
namespace MilPattern {
|
|
public class BarCodeReader : PatternBase, IPatternFinder, IDisposable {
|
|
public string FILE_EXT_PATTERN { get; set; } = ".mco";
|
|
private BarCodeParamExt _paramExt = null;
|
|
|
|
private Logger _logger;
|
|
private PatternInfo _patternInfo;
|
|
private MIL_ID _contextId = MIL.M_NULL;
|
|
private MIL_ID _resultContextId = MIL.M_NULL;
|
|
|
|
public BarCodeReader(PatternInfo patternInfo) {
|
|
_patternInfo = patternInfo;
|
|
_logger = new Logger(
|
|
$"ImageModelFinder_{patternInfo.GetPatternIndex()}");
|
|
InitContext();
|
|
}
|
|
|
|
private void InitContext() {
|
|
if (E1.OK != Load()) {
|
|
Create();
|
|
}
|
|
}
|
|
|
|
#region IPatternFinder
|
|
public string GetPatternFilePath() {
|
|
return Path.Combine(new string[] {
|
|
PatternConstants.GetSubPatternDirPath(_patternInfo),
|
|
PatternConstants.FILE_NAME_PATTERN + FILE_EXT_PATTERN
|
|
});
|
|
}
|
|
|
|
public int Load() {
|
|
var patternFilePath = GetPatternFilePath();
|
|
|
|
if (!File.Exists(patternFilePath)) {
|
|
_logger.I($"Pattern File does NOT exist : {patternFilePath}");
|
|
return E2.NOT_FOUND;
|
|
}
|
|
|
|
FreeContexts();
|
|
_contextId = MilCode.LoadContext(_patternInfo.MilSystemId, patternFilePath);
|
|
_resultContextId = MilCode.CreateResultContext(_contextId);
|
|
_paramExt = LoadParamExt();
|
|
|
|
return E1.OK;
|
|
}
|
|
|
|
public int Save() {
|
|
var patternFilePath = GetPatternFilePath();
|
|
var patternDirPath = Path.GetDirectoryName(patternFilePath);
|
|
if (!Directory.Exists(patternDirPath)) {
|
|
Directory.CreateDirectory(patternDirPath);
|
|
}
|
|
|
|
SaveParamExt(_paramExt);
|
|
return MilCode.SaveContext(_contextId, patternFilePath);
|
|
}
|
|
|
|
public int Create() {
|
|
FreeContexts();
|
|
_contextId = MilCode.CreateContext(_patternInfo.MilSystemId);
|
|
_resultContextId = MilCode.CreateResultContext(_contextId);
|
|
_paramExt = LoadParamExt();
|
|
|
|
return E1.OK;
|
|
}
|
|
|
|
public int Remove() {
|
|
FreeContexts();
|
|
|
|
var patternFilePath = GetPatternFilePath();
|
|
var patternDirPath = Path.GetDirectoryName(patternFilePath);
|
|
if (Directory.Exists(patternDirPath)) {
|
|
Directory.Delete(patternDirPath, recursive: true);
|
|
}
|
|
|
|
return E1.OK;
|
|
}
|
|
|
|
public ROI GetFindROI() {
|
|
return _paramExt.FindROI;
|
|
}
|
|
|
|
public int SetFindROI(ROI findRoi) {
|
|
if (ROI.IsNullOrEmpty(findRoi)) {
|
|
return E1.GENERIC;
|
|
}
|
|
|
|
_paramExt.FindROI = findRoi.Clone() as ROI;
|
|
|
|
return E1.OK;
|
|
}
|
|
|
|
public IRunParam GetRunParam() {
|
|
var runParam = MilCode.GetRunParam(_contextId);
|
|
runParam.BarCodeParamExt = _paramExt.Clone() as BarCodeParamExt;
|
|
|
|
return runParam;
|
|
}
|
|
|
|
public int SetRunParam(IRunParam runParam) {
|
|
if (MilCode.HasModel(_contextId)) {
|
|
FreeContexts();
|
|
Create();
|
|
}
|
|
|
|
_paramExt = ((CodeRunParam)runParam).Clone() as BarCodeParamExt;
|
|
return MilCode.SetRunParam(_contextId, runParam);
|
|
}
|
|
|
|
public PatternResult Find(object image) {
|
|
return Find((MIL_ID)image, _paramExt.FindROI);
|
|
}
|
|
|
|
public PatternResult Find(object image, ROI findRoi) {
|
|
MIL_ID cropImage = MIL.M_NULL;
|
|
var patternResult = new PatternResult() { Status = eFoundStatus.NotFound };
|
|
|
|
try {
|
|
if (!ROI.IsNullOrEmpty(findRoi)) {
|
|
cropImage = MilEnvironment.CropImage((MIL_ID)image, findRoi);
|
|
patternResult = MilCode.Find(_contextId, _resultContextId, cropImage);
|
|
|
|
if (patternResult.Status != eFoundStatus.NotFound) {
|
|
var imageCenter = MilEnvironment.GetImageCenter((MIL_ID)image);
|
|
|
|
foreach (CodeResult result in patternResult.Results) {
|
|
result.ImagePosition = result.ImagePosition + findRoi.LeftTop as ImagePoint;
|
|
result.Offset = result.ImagePosition - imageCenter as ImagePoint;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
catch (Exception e) {
|
|
_logger.E(e.Message);
|
|
}
|
|
finally {
|
|
MilEnvironment.FreeImage(cropImage);
|
|
}
|
|
|
|
return patternResult;
|
|
}
|
|
|
|
public PatternResult GetNotFoundPatternResult() {
|
|
return MilCode.GetNotFoundPatternResult();
|
|
}
|
|
|
|
public object GetTool() {
|
|
return _contextId;
|
|
}
|
|
|
|
public void Draw(
|
|
PatternResult patternResult, ICameraDisplay cameraDisplay, bool isFixturePattern = false) {
|
|
MilCode.Draw(patternResult, cameraDisplay as IMilCameraDisplay);
|
|
}
|
|
|
|
public Bitmap GetThumbnail() {
|
|
return ThumbnailImage.BarCodeReader();
|
|
}
|
|
#endregion
|
|
|
|
#region IDisposible
|
|
public void Dispose() {
|
|
FreeContexts();
|
|
}
|
|
|
|
private void FreeContexts() {
|
|
MilCode.FreeContext(_contextId);
|
|
MilCode.FreeContext(_resultContextId);
|
|
|
|
_contextId = MIL.M_NULL;
|
|
_resultContextId = MIL.M_NULL;
|
|
}
|
|
#endregion
|
|
|
|
#region CodeReader Specific
|
|
internal BarCodeParamExt LoadParamExt() {
|
|
return BarCodeParamExt.Load(PatternConstants.GetSubPatternDirPath(_patternInfo));
|
|
}
|
|
|
|
internal int SaveParamExt(BarCodeParamExt codeParamExt) {
|
|
return BarCodeParamExt.Save(
|
|
PatternConstants.GetSubPatternDirPath(_patternInfo),
|
|
codeParamExt);
|
|
}
|
|
#endregion
|
|
}
|
|
}
|