/*
* 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 {
///
/// 이미지 로드 및 저장
///
public class ImageManager {
private Logger _Logger = new Logger("ImageManager");
///
/// 생성자
///
public ImageManager() {
}
///
/// 이미지 로드
///
/// 파일 경로
/// 카메라 컨피그
/// 이미지
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;
}
///
/// 자동 생성된 경로에 이미지 저장 (비동기식)
///
/// FOV 정보
/// 이미지
/// 공정 결과 상태
/// 자재 정보
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);
}
}
///
/// 공정중 Grab한 이미지를 저장
///
/// 서브 시스템 키 정보
/// 데이터 저장 타입
/// 이미지
public void SaveImageInternal(SubSysInfo subSysInfo, eSaveDataType saveDataType, object image) {
try {
if (image == null) {
_Logger.FuncE($"image is null.");
return;
}
List 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);
}
}
///
/// 공정 중 Grab한 이미지를 저장
///
/// FOV 키 정보
/// 데이터 저장 타입
/// 이미지
public void SaveImageInternal(FovInfo fovInfo, eSaveDataType saveDataType, object image) {
try {
if (image == null) {
_Logger.FuncE($"image is null.");
return;
}
List 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);
}
}
///
/// 지정된 경로에 이미지 저장 (비동기식)
///
/// 파일 경로
/// 파일 이름
/// 이미지
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 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);
}
}
///
/// 오버레이 이미지 저장
///
/// 마지막 FOV 정보
/// 공정 결과 상태
/// 자재 정보
/// 디스플레이 타입
public void SaveScreenShot(
FovInfo lastFovInfo, string status, MaterialInfo materialInfo, eDisplayType displayType, string timeStamp = null, object image = null) {
try {
Task.Run(() => {
var fovInfos = new List();
for (int i = (int)eFovId.Fov1; i <= (int)lastFovInfo.FovId; i++) {
fovInfos.Add(VisionRecipeManager.GetFovInfo(lastFovInfo.UnitId, lastFovInfo.SubSysId, (eFovId)i));
}
List 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);
}
}
}
}