216 lines
8.6 KiB
C#
216 lines
8.6 KiB
C#
/*
|
|
* Copyright (c) 2025 LG Electronics Inc.
|
|
* SPDX-License-Identifier: LicenseRef-LGE-Proprietary
|
|
*/
|
|
using System;
|
|
using System.IO;
|
|
using System.Collections.Generic;
|
|
using Aurora.Api;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace VisionCommon {
|
|
/// <summary>
|
|
/// 이미지 로드 및 저장
|
|
/// </summary>
|
|
public class ImageManager {
|
|
private Logger _Logger = new Logger("ImageManager");
|
|
|
|
/// <summary>
|
|
/// 생성자
|
|
/// </summary>
|
|
public ImageManager() {
|
|
}
|
|
|
|
/// <summary>
|
|
/// 이미지 로드
|
|
/// </summary>
|
|
/// <param name="filePath">파일 경로</param>
|
|
/// <param name="camConfig">카메라 컨피그</param>
|
|
/// <returns>이미지</returns>
|
|
public object LoadImage(string filePath, CamConfig camConfig) {
|
|
if (!File.Exists(filePath)) {
|
|
return null;
|
|
}
|
|
|
|
try {
|
|
return IpAdapter.Instance.IpEnvironment.LoadImage(filePath, camConfig);
|
|
}
|
|
catch (Exception e) {
|
|
_Logger.FuncE(e.Message);
|
|
}
|
|
return null;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 자동 생성된 경로에 이미지 저장 (비동기식)
|
|
/// </summary>
|
|
/// <param name="fovInfo">FOV 정보</param>
|
|
/// <param name="image">이미지</param>
|
|
/// <param name="status">공정 결과 상태</param>
|
|
/// <param name="materialInfo">자재 정보</param>
|
|
public void SaveImage(FovInfo fovInfo, object image, string status, MaterialInfo materialInfo) {
|
|
try {
|
|
Task.Run(() => {
|
|
SaveImageInternal(fovInfo, image, status.ToString(), materialInfo);
|
|
});
|
|
}
|
|
catch (Exception e) {
|
|
_Logger.FuncE(e.Message);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 공정중 Grab한 이미지를 저장
|
|
/// </summary>
|
|
/// <param name="subSysInfo">서브 시스템 키 정보</param>
|
|
/// <param name="saveDataType">데이터 저장 타입</param>
|
|
/// <param name="image">이미지</param>
|
|
public void SaveImageInternal(SubSysInfo subSysInfo, eSaveDataType saveDataType, object image) {
|
|
try {
|
|
if (image == null) {
|
|
_Logger.FuncE($"image is null.");
|
|
return;
|
|
}
|
|
|
|
List<string> subDirNames = ImagePathManager.Instance.GetSubDirNames(subSysInfo, saveDataType);
|
|
string dirPath = ImagePathManager.Instance.GetDirPath(subDirNames);
|
|
string imageName = subSysInfo.SubSysName + "_" + saveDataType.ToString();
|
|
string fileName = ImagePathManager.Instance.GetFileNameWithDataTime(imageName);
|
|
string filePath = dirPath + "\\" + fileName;
|
|
|
|
if (!File.Exists(filePath)) {
|
|
if (!Directory.Exists(dirPath)) {
|
|
Directory.CreateDirectory(dirPath);
|
|
}
|
|
}
|
|
SaveImage(dirPath, fileName, image);
|
|
}
|
|
catch (Exception e) {
|
|
_Logger.FuncE(e.Message);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 공정 중 Grab한 이미지를 저장
|
|
/// </summary>
|
|
/// <param name="fovInfo">FOV 키 정보</param>
|
|
/// <param name="saveDataType">데이터 저장 타입</param>
|
|
/// <param name="image">이미지</param>
|
|
public void SaveImageInternal(FovInfo fovInfo, eSaveDataType saveDataType, object image) {
|
|
try {
|
|
if (image == null) {
|
|
_Logger.FuncE($"image is null.");
|
|
return;
|
|
}
|
|
|
|
List<string> subDirNames = ImagePathManager.Instance.GetSubDirNames(fovInfo.ToSubSysInfo(), saveDataType);
|
|
string dirPath = ImagePathManager.Instance.GetDirPath(subDirNames);
|
|
string imageName = fovInfo.SubSysName + "_" + fovInfo.FovName + "_" + saveDataType.ToString();
|
|
string fileName = ImagePathManager.Instance.GetFileNameWithDataTime(imageName);
|
|
string filePath = dirPath + "\\" + fileName;
|
|
|
|
if (!File.Exists(filePath)) {
|
|
if (!Directory.Exists(dirPath)) {
|
|
Directory.CreateDirectory(dirPath);
|
|
}
|
|
}
|
|
SaveImage(dirPath, fileName, image);
|
|
}
|
|
catch (Exception e) {
|
|
_Logger.FuncE(e.Message);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 지정된 경로에 이미지 저장 (비동기식)
|
|
/// </summary>
|
|
/// <param name="dirPath">파일 경로</param>
|
|
/// <param name="fileName">파일 이름</param>
|
|
/// <param name="image">이미지</param>
|
|
public void SaveImage(string dirPath, string fileName, object image) {
|
|
try {
|
|
Task.Run(() => {
|
|
SaveImageInternal(dirPath, fileName, image);
|
|
});
|
|
}
|
|
catch (Exception e) {
|
|
_Logger.FuncE(e.Message);
|
|
}
|
|
}
|
|
|
|
private void SaveImageInternal(FovInfo fovInfo, object image, string status, MaterialInfo materialInfo) {
|
|
try {
|
|
if (image == null) {
|
|
_Logger.FuncE("image is null");
|
|
return;
|
|
}
|
|
|
|
var fovRecipe = VisionRecipeManager.GetFovRecipe(fovInfo);
|
|
eImageFormat imgFormat = VisionSettingManager.VisionSetting.ImageFormat;
|
|
|
|
List<string> subDirNames = ImagePathManager.Instance.GetSubDirNames(fovInfo, materialInfo);
|
|
subDirNames.Add(status); // 패턴을 찾은 정보에 따라 폴더를 구분한다.
|
|
string dirPath = ImagePathManager.Instance.GetDirPath(subDirNames);
|
|
string fovName = fovRecipe.FovName;
|
|
string fileName = ImagePathManager.Instance.GetFileNameWithMaterialInfo(fovInfo.SubSysName, fovName, status, materialInfo);
|
|
string fileFullName = ImagePathManager.Instance.GetFileNameWithDataTime(fileName, imgFormat);
|
|
string fullPath = Path.Combine(dirPath, fileFullName);
|
|
IpAdapter.Instance.IpEnvironment.SaveImage(fullPath, image);
|
|
}
|
|
catch (Exception e) {
|
|
_Logger.FuncE(e.Message);
|
|
}
|
|
}
|
|
|
|
private void SaveImageInternal(string dirPath, string fileName, object image) {
|
|
try {
|
|
if (image == null) {
|
|
_Logger.FuncE("image is null");
|
|
return;
|
|
}
|
|
|
|
string fullPath = Path.Combine(dirPath, fileName);
|
|
IpAdapter.Instance.IpEnvironment.SaveImage(fullPath, image);
|
|
}
|
|
catch (Exception e) {
|
|
_Logger.FuncE(e.Message);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 오버레이 이미지 저장
|
|
/// </summary>
|
|
/// <param name="lastFovInfo">마지막 FOV 정보</param>
|
|
/// <param name="status">공정 결과 상태</param>
|
|
/// <param name="materialInfo">자재 정보</param>
|
|
/// <param name="displayType">디스플레이 타입</param>
|
|
public void SaveScreenShot(
|
|
FovInfo lastFovInfo, string status, MaterialInfo materialInfo, eDisplayType displayType, string timeStamp = null, object image = null) {
|
|
try {
|
|
Task.Run(() => {
|
|
var fovInfos = new List<FovInfo>();
|
|
for (int i = (int)eFovId.Fov1; i <= (int)lastFovInfo.FovId; i++) {
|
|
fovInfos.Add(VisionRecipeManager.GetFovInfo(lastFovInfo.UnitId, lastFovInfo.SubSysId, (eFovId)i));
|
|
}
|
|
|
|
List<string> subDirNames = ImagePathManager.Instance.GetSubDirNames(fovInfos[0], materialInfo);
|
|
|
|
string dirPath = ImagePathManager.Instance.GetDirPath(subDirNames);
|
|
if (!Directory.Exists(dirPath)) {
|
|
new DirectoryInfo(dirPath).Create();
|
|
}
|
|
|
|
string fileName = fovInfos[0].SubSysName + "_" + status;
|
|
string fileFullName = ImagePathManager.Instance.GetFileNameWithDataTime(fileName, eImageFormat.JPG, timeStamp);
|
|
string fullPath = Path.Combine(dirPath, fileFullName);
|
|
|
|
IpAdapter.Instance.IpEnvironment.SaveScreenShot(fullPath, fovInfos, displayType, image);
|
|
});
|
|
}
|
|
catch (Exception e) {
|
|
_Logger.FuncE(e.Message);
|
|
}
|
|
}
|
|
}
|
|
}
|