Compare commits

..

No commits in common. "main" and "0.0.2.20240927_alpha" have entirely different histories.

7 changed files with 168 additions and 352 deletions

View File

@ -4,13 +4,13 @@ cmake_minimum_required(VERSION 3.20)
# C++OFF使 # C++OFF使
set(CMAKE_CXX_STANDARD_REQUIRED ON) set(CMAKE_CXX_STANDARD_REQUIRED ON)
# C++23 # C++20
set(CMAKE_CXX_STANDARD 23) set(CMAKE_CXX_STANDARD 20)
set(PROJ_NAME "SEMS-data-collection") set(PROJ_NAME "SEMS-data-collection")
set(PROJ_VERSION_MAJOR 0) set(PROJ_VERSION_MAJOR 0)
set(PROJ_VERSION_MINOR 0) set(PROJ_VERSION_MINOR 0)
set(PROJ_VERSION_PATCH 4.20241011_alpha) set(PROJ_VERSION_PATCH 2.20240927_alpha)
#HelloWorld #HelloWorld
project(${PROJ_NAME}) project(${PROJ_NAME})
@ -25,29 +25,27 @@ include_directories(${PROJECT_SOURCE_DIR}/include)
link_directories (${PROJECT_SOURCE_DIR}/lib) link_directories (${PROJECT_SOURCE_DIR}/lib)
add_library(SpectralCameraLib "${PROJECT_SOURCE_DIR}/src/spectral_camera.cpp") add_library(SpectralCameraLib "${PROJECT_SOURCE_DIR}/src/spectral_camera.cpp")
add_library(DisplayLib "${PROJECT_SOURCE_DIR}/src/display.cpp") add_library(DisplayLib "${PROJECT_SOURCE_DIR}/src/display.cpp")
# add_library(DataPreprocessLib "${PROJECT_SOURCE_DIR}/src/data_preprocess.cpp") add_library(DataPreprocessLib "${PROJECT_SOURCE_DIR}/src/data_preprocess.cpp")
# add_library(VisibleLightCameraLIB "${PROJECT_SOURCE_DIR}/src/visible_light_camera.cpp") add_library(VisibleLightCameraLIB "${PROJECT_SOURCE_DIR}/src/visible_light_camera.cpp")
add_library(UtilsLIB "${PROJECT_SOURCE_DIR}/src/utils.cpp")
# link_directories (${PROJECT_SOURCE_DIR}/../CompliteEnv/Python37) link_directories (${PROJECT_SOURCE_DIR}/../CompliteEnv/Python37)
#${PROJECT_SOURCE_DIR}/src/main.cppcppHelloWorld #${PROJECT_SOURCE_DIR}/src/main.cppcppHelloWorld
add_executable(${PROJ_NAME} "${PROJECT_SOURCE_DIR}/src/main.cpp") add_executable(${PROJ_NAME} "${PROJECT_SOURCE_DIR}/src/main.cpp")
# #
# target_link_libraries(${PROJ_NAME} PUBLIC yaml-cpp) target_link_libraries(${PROJ_NAME} PUBLIC yaml-cpp)
target_link_libraries(${PROJ_NAME} PUBLIC SpectralCameraLib) target_link_libraries(${PROJ_NAME} PUBLIC SpectralCameraLib)
target_link_libraries(${PROJ_NAME} PUBLIC SpecSensor) target_link_libraries(${PROJ_NAME} PUBLIC SpecSensor)
# target_link_libraries(${PROJ_NAME} PUBLIC VisibleLightCameraLIB) target_link_libraries(${PROJ_NAME} PUBLIC VisibleLightCameraLIB)
target_link_libraries(${PROJ_NAME} PUBLIC DisplayLib) target_link_libraries(${PROJ_NAME} PUBLIC DisplayLib)
target_link_libraries(${PROJ_NAME} PUBLIC opencv_core490 opencv_highgui490 opencv_imgproc490) target_link_libraries(${PROJ_NAME} PUBLIC opencv_core490 opencv_highgui490 opencv_imgproc490 opencv_photo490 opencv_imgcodecs490 opencv_video490 opencv_videoio490)
# target_link_libraries(${PROJ_NAME} PUBLIC DataPreprocessLib) target_link_libraries(${PROJ_NAME} PUBLIC DataPreprocessLib)
target_link_libraries(${PROJ_NAME} PUBLIC UtilsLIB)

View File

@ -1,20 +1,4 @@
#ifndef _UYILS_H #ifndef _SPECTRAL_CAMERA_H
#define _UYILS_H #define _SPECTRAL_CAMERA_H
struct Configs
{
bool isEmpty = false;
int SPECTRUM_deviceTypeIndex;
double SPECTRUM_FrameRate;
double SPECTRUM_ExposureTime;
int SPECTRUM_BinningSpatial;
int SPECTRUM_BinningSpectral;
bool SPECTRUM_BinningAverage;
long long SPECTRUM_RingBufferSize;
std::string RAWDATA_Folder;
unsigned long long RAWDATA_MaxSize;
};
Configs getConfigFromEnvVar();
bool AddExePathtoPATH();
#endif #endif

39
run.ps1
View File

@ -1,28 +1,25 @@
Param($cmd) Param($cmd)
#在 PowerShell 中将字符集修改为 UTF-8 $Env:PATH="C:\Program Files\Common Files\Pleora\eBUS SDK;" #开发光谱相机所必需的路径
$OutputEncoding = [System.Console]::OutputEncoding = [System.Console]::InputEncoding = [System.Text.Encoding]::UTF8 $Env:PATH+="C:\Program Files\Git\cmd;C:\Windows\System32\OpenSSH;" #开发所需的git/ssh命令的路径
$Env:PATH+=(Resolve-Path ../CompliteEnv/mingw64/bin).path+";" #g++所在路径
$Env:PATH+=(Resolve-Path ../CompliteEnv/CMake/bin).path+";" #cmake所在路径
$Env:PATH = "C:\Program Files\Common Files\Pleora\eBUS SDK;" #开发光谱相机所必需的路径 $Env:PATH+=(Resolve-Path ./lib).path+";" #本项目所需的动态链接库所在路径
$Env:PATH += "C:\Program Files\Git\cmd;C:\Windows\System32\OpenSSH;" #开发所需的git/ssh命令的路径
$Env:PATH += (Resolve-Path ../CompliteEnv/mingw64/bin).path + ";" #g++所在路径
$Env:PATH += (Resolve-Path ../CompliteEnv/CMake/bin).path + ";" #cmake所在路径
$Env:PATH += (Resolve-Path ./lib).path + ";" #本项目所需的动态链接库所在路径
Write-Host "PATH is set to $Env:PATH" Write-Host "PATH is set to $Env:PATH"
function Remove-Build-Dir { function Remove-Build-Dir
If (Test-Path "build") { {
#如果当前目录下build文件夹存在 If(Test-Path "build"){ #如果当前目录下build文件夹存在
Remove-Item -Path "build" -Recurse #递归阐述build文件夹 Remove-Item -Path "build" -Recurse #递归阐述build文件夹
} }
} }
function ReBuild-Target { function ReBuild-Target
{
# 编译生成Makefile # 编译生成Makefile
cmake -B build -G "MinGW Makefiles" cmake -B build -G "MinGW Makefiles"
# 编译生成可执行程序 # 编译生成可执行程序
@ -30,13 +27,14 @@ function ReBuild-Target {
} }
function Run-All-Exe { function Run-All-Exe
$exeFiles = Get-ChildItem -Path "build" -Filter *.exe {
$exeFiles= Get-ChildItem -Path "build" -Filter *.exe
Write-Host "Exe Files: $exeFiles" Write-Host "Exe Files: $exeFiles"
ForEach ($exeFile in $exeFiles) { ForEach($exeFile in $exeFiles){
Write-Host "Processing file: $($exeFile.FullName)" Write-Host "Processing file: $($exeFile.FullName)"
Start-Process -Wait -FilePath $exeFile.FullName -NoNewWindow -PassThru Start-Process -Wait -FilePath $exeFile.FullName -PassThru
} }
} }
@ -44,7 +42,7 @@ function Run-All-Exe {
If ($cmd -eq "b") { If($cmd -eq "b"){
Write-Host "Rebuilding all targets..." Write-Host "Rebuilding all targets..."
Remove-Build-Dir Remove-Build-Dir
@ -55,13 +53,12 @@ If ($cmd -eq "b") {
} }
If ($cmd -eq "p") { If($cmd -eq "p"){
Write-Host "Packing all targets..." Write-Host "Packing all targets..."
cmake --build build --target package cmake --build build --target package
} }
If ([String]::IsNullOrEmpty($cmd)) { If([String]::IsNullOrEmpty($cmd)){ #如果没有任何参数就运行所有exe
#如果没有任何参数就运行所有exe
Run-All-Exe Run-All-Exe
} }

View File

@ -10,55 +10,50 @@
#include <string> #include <string>
// #include <vector> // #include <vector>
int64_t last_t = std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now().time_since_epoch()).count();
void display_spectral_image(unsigned char *raw_data, long long width, long long height, std::string window_name)
void display_spectral_image(unsigned char *raw_data, long long width, long long height,std::string window_name)
{ {
cv::Mat img = cv::Mat(height, width, CV_16U, raw_data); cv::Mat img = cv::Mat(height, width,CV_16U, raw_data);
img.convertTo(img, CV_16F); img.convertTo(img, CV_16F);
img = img / 4095 * 255; img = img / 4095 * 255;
img.convertTo(img, CV_8U); img.convertTo(img, CV_8U);
cv::applyColorMap(img, img, cv::COLORMAP_TURBO); cv::applyColorMap(img, img, cv::COLORMAP_TURBO);
cv::namedWindow("Spectral Image", 0); cv::namedWindow("Spectral Image", 0);
cv::resizeWindow("Spectral Image", width, height); cv::resizeWindow("Spectral Image", width , height );
uint16_t *p = (uint16_t *)raw_data; uint16_t *p = (uint16_t *)raw_data;
cv::Vec3b white = img.at<cv::Vec3b>(0, 0); cv::Vec3b white= img.at<cv::Vec3b>(0, 0);
white[0] = 0; white[0]=0;
white[1] = 0; white[1]=0;
white[2] = 0; white[2]=0;
int over_exposure_num = 0; int over_exposure_num=0;
int max_intensity = 0; int max_intensity=0;
for (int i = 0; i < width; i++) for(int i=0;i<width;i++){
{ for(int j=0;j<height;j++){
for (int j = 0; j < height; j++) if(*(p+(j*width+i))==4095){
{ img.at<cv::Vec3b>(j,i)=white;
if (*(p + (j * width + i)) == 4095)
{
img.at<cv::Vec3b>(j, i) = white;
over_exposure_num++; over_exposure_num++;
} }
if (*(p + (j * width + i)) > max_intensity) if (*(p+(j*width+i))>max_intensity){
{ max_intensity=*(p+(j*width+i));
max_intensity = *(p + (j * width + i));
} }
} }
} }
std::string add_text=std::string("Over Exposure:")+std::to_string(over_exposure_num)+std::string(" Max Intensity:")+std::to_string(max_intensity);
int64_t t = std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now().time_since_epoch()).count(); cv::putText(img,add_text.c_str(),cv::Point(30,400),cv::FONT_HERSHEY_SIMPLEX,1,CV_RGB(255,255,255),1,8);
std::string add_text = std::string("Over Exposure:") + std::to_string(over_exposure_num) + std::string(" Max Intensity:") + std::to_string(max_intensity) + " FPS:" + std::to_string(1000 / (double)(t - last_t));
last_t = t;
cv::putText(img, add_text.c_str(), cv::Point(10, 20), cv::FONT_HERSHEY_SIMPLEX, 0.5, CV_RGB(255, 255, 255), 1, 2);
cv::imshow(window_name, img); cv::imshow(window_name, img);
cv::waitKey(1); cv::waitKey(1);
} }
void display_img(cv::Mat img, std::string window_name) void display_img(cv::Mat img, std::string window_name)
{ {
cv::imshow(window_name, img); cv::imshow(window_name, img);
cv::waitKey(1); // Wait for a keystroke in the window cv::waitKey(1); // Wait for a keystroke in the window
} }

View File

@ -1,31 +1,28 @@
#include <windows.h>
#include <iostream> #include <iostream>
#include <format> #include <cstdio>
#include <utils.h>
// #include <windows.h>
// #include <cstdio>
#include <conio.h> #include <conio.h>
#include "spectral_camera.h" #include "spectral_camera.h"
// #include "yaml-cpp/yaml.h" #include "yaml-cpp/yaml.h"
#include "display.h" #include "display.h"
// #include "data_preprocess.h" #include "data_preprocess.h"
// #include <chrono> #include <chrono>
// #include <vector> #include <vector>
// #include <opencv2/core.hpp> #include <opencv2/core.hpp>
// #include <opencv2/highgui.hpp> #include <opencv2/highgui.hpp>
#include <fstream> #include <fstream>
// #include "visible_light_camera.h" #include "visible_light_camera.h"
// #include <windows.h> #include<windows.h>
#include <chrono> #include <chrono>
extern unsigned long long int last_file_time = 0;
extern unsigned long long int frame_num = 0;
int check_update_file_handle(std::string save_dir, std::ofstream &h_raw_spectal_file, std::ofstream &h_header_file, unsigned long long max_file_size) extern unsigned long long int last_file_time=0;
extern unsigned long long int frame_num=0;
int check_update_file_handle(std::string save_dir, std::ofstream &h_raw_spectal_file, std::ofstream &h_header_file, unsigned long long max_file_size, std::ofstream &h_visible_light_file, bool enable_visible_light_camera)
{ {
if (h_raw_spectal_file.is_open()) if (h_raw_spectal_file.is_open())
{ {
@ -38,72 +35,110 @@ int check_update_file_handle(std::string save_dir, std::ofstream &h_raw_spectal_
h_raw_spectal_file.open((raw_data_file_name + std::string(".bin").data()), std::ios::out | std::ios::binary | std::ios::app); h_raw_spectal_file.open((raw_data_file_name + std::string(".bin").data()), std::ios::out | std::ios::binary | std::ios::app);
h_header_file.open((raw_data_file_name + std::string(".csv").data()), std::ios::out | std::ios::app); h_header_file.open((raw_data_file_name + std::string(".csv").data()), std::ios::out | std::ios::app);
unsigned long long int now = (unsigned long long int)now_t; if (enable_visible_light_camera)
if (last_file_time != 0)
{ {
float t = (float)(now - last_file_time) / 1000; if (h_visible_light_file.is_open())
// std::cout<<now<<last_file_time<<t<<frame_num<<std::endl; {
float fps = frame_num / t; h_visible_light_file.close();
printf("文件%lld的帧率为 %.2f\n", last_file_time, fps);
frame_num = 0;
} }
last_file_time = now; h_visible_light_file.open((raw_data_file_name + std::string(".img").data()), std::ios::out | std::ios::binary | std::ios::app);
}
unsigned long long int now=(unsigned long long int)now_t;
if (last_file_time!=0){
float t=(float)(now-last_file_time)/1000;
// std::cout<<now<<last_file_time<<t<<frame_num<<std::endl;
float fps= frame_num/t;
printf("FPS %.2f\n",fps);
frame_num=0;
}
last_file_time=now;
return 0; return 0;
} }
int main() int main()
{ {
std::cout << "Hello World" << std::endl; // std::cout << "Hello" << std::endl;
Configs configs = getConfigFromEnvVar();
if (configs.isEmpty) char szPath[512] = {0};
{ GetModuleFileName(NULL, szPath, sizeof(szPath)-1);
return -1; std::string exe_dir(szPath);
};
if (!AddExePathtoPATH()) //这里返回的是exe文件的完整路径包括文件名所以下面执行两边就是先取出exe文件名再去除exe所在文件夹
{ //即实现找到exe所在文件夹的上层文件夹的绝对路径
return -1; size_t lastSlashIndex = exe_dir.find_last_of('\\');
} exe_dir=exe_dir.substr(0,lastSlashIndex); //+1是为了也包含最后一个反斜杠
lastSlashIndex = exe_dir.find_last_of('\\');
exe_dir=exe_dir.substr(0,lastSlashIndex+1); //+1是为了也包含最后一个反斜杠
std::cout << "Configs file path is " <<exe_dir+"configs\\data_collection_configs.yaml"<< std::endl;
YAML::Node configs = YAML::LoadFile(exe_dir+"configs\\data_collection_configs.yaml");
SpectralCamera *spec_camera = new SpectralCamera(); SpectralCamera *spec_camera = new SpectralCamera();
spec_camera->listDevices(); spec_camera->listDevices();
spec_camera->init(configs.SPECTRUM_deviceTypeIndex, spec_camera->init(configs["spectral_camera_configs"]["deviceTypeIndex"].as<int>(),
configs.SPECTRUM_FrameRate, configs["spectral_camera_configs"]["FrameRate"].as<double>(),
configs.SPECTRUM_ExposureTime, configs["spectral_camera_configs"]["ExposureTime"].as<double>(),
configs.SPECTRUM_BinningSpatial, configs["spectral_camera_configs"]["BinningSpatial"].as<int>(),
configs.SPECTRUM_BinningSpectral, configs["spectral_camera_configs"]["BinningSpectral"].as<int>(),
configs.SPECTRUM_BinningAverage, configs["spectral_camera_configs"]["BinningAverage"].as<bool>(),
configs.SPECTRUM_RingBufferSize); configs["spectral_camera_configs"]["RingBufferSize"].as<long long>());
std::ofstream h_raw_spectal_file, h_header_file; bool enable_visible_light_camera = configs["visible_light_camera_configs"]["enable"].as<bool>();
check_update_file_handle(configs.RAWDATA_Folder, h_raw_spectal_file, h_header_file, configs.RAWDATA_MaxSize); std::string save_dir = configs["save_configs"]["save_dir"].as<std::string>();
unsigned long long max_file_size = configs["save_configs"]["max_file_size"].as<unsigned long long>();
VisibleLightCamera *visible_light_camera = NULL;
if (enable_visible_light_camera)
{
visible_light_camera = new VisibleLightCamera();
visible_light_camera->init(configs["visible_light_camera_configs"]["video_capture_index"].as<int>());
}
std::ofstream h_raw_spectal_file,h_header_file, h_visible_light_file;
check_update_file_handle(save_dir, h_raw_spectal_file, h_header_file, max_file_size, h_visible_light_file, enable_visible_light_camera);
bool flag_run = true; bool flag_run = true;
while (flag_run) while (flag_run)
{ {
spec_camera->read(); spec_camera->read();
spec_camera->save(h_raw_spectal_file, h_header_file); spec_camera->save(h_raw_spectal_file, h_header_file);
frame_num++; frame_num++;
display_spectral_image(spec_camera->pFrameBuffer, spec_camera->nWidth, spec_camera->nHeight, "Spectral Image");
display_spectral_image(spec_camera->pFrameBuffer, spec_camera->nWidth, spec_camera->nHeight,"Spectral Image");
// 如果启动了可见光相机
if (enable_visible_light_camera)
{
visible_light_camera->read();
visible_light_camera->save(h_visible_light_file, h_header_file);
display_img(visible_light_camera->image_frame, "Camera");
}
h_header_file << std::endl; h_header_file << std::endl;
if (h_raw_spectal_file.tellp() > configs.RAWDATA_MaxSize)
{ if (h_raw_spectal_file.tellp()>max_file_size){
check_update_file_handle(configs.RAWDATA_Folder, h_raw_spectal_file, h_header_file, configs.RAWDATA_MaxSize); check_update_file_handle(save_dir, h_raw_spectal_file, h_header_file, max_file_size, h_visible_light_file, enable_visible_light_camera);
} }
if (_kbhit()) if (_kbhit())
{ {
char key = _getch(); char key = _getch();
if ((key == 27) | (key == 'q')) if ( (key == 27)|(key == 'q'))
{ {
flag_run = false; flag_run=false;
} }
} }
} }
@ -118,5 +153,8 @@ int main()
{ {
h_header_file.close(); h_header_file.close();
} }
return 0; if (h_visible_light_file.is_open())
{
h_visible_light_file.close();
}
} }

View File

@ -46,20 +46,6 @@ Error:
int SpectralCamera::init(int deviceTypeIndex, double FrameRate, double ExposureTime, int BinningSpatial, int BinningSpectral, bool BinningAverage, long long RingBufferSize) int SpectralCamera::init(int deviceTypeIndex, double FrameRate, double ExposureTime, int BinningSpatial, int BinningSpectral, bool BinningAverage, long long RingBufferSize)
{ {
// char szPath[512] = {0};
// GetModuleFileName(NULL, szPath, sizeof(szPath) - 1);
// std::string exe_dir(szPath);
// // 这里返回的是exe文件的完整路径包括文件名所以下面执行两边就是先取出exe文件名再去除exe所在文件夹
// // 即实现找到exe所在文件夹的上层文件夹的绝对路径
// size_t lastSlashIndex = exe_dir.find_last_of('\\');
// std::string autoclick_path = exe_dir.substr(0, lastSlashIndex + 1) + "autoClick.exe"; //+1是为了也包含最后一个反斜杠
// wchar_t *wstr = new wchar_t[autoclick_path.length() + 1];
// mbstowcs(wstr, autoclick_path.c_str(), autoclick_path.length() + 1);
// wprintf(L"Started autoClick.exe in %ls\n", wstr);
HINSTANCE state = ShellExecuteW(NULL, L"open", L"autoClick.exe", NULL, NULL, SW_SHOW); HINSTANCE state = ShellExecuteW(NULL, L"open", L"autoClick.exe", NULL, NULL, SW_SHOW);
// autoClick.exe以及依赖的msvcp140d.dllucrtbased.dllvcruntime140d.dll在lib文件夹中 // autoClick.exe以及依赖的msvcp140d.dllucrtbased.dllvcruntime140d.dll在lib文件夹中
wprintf(L"Started autoClick.exe. The state code is %d\n", state); wprintf(L"Started autoClick.exe. The state code is %d\n", state);
@ -80,7 +66,7 @@ int SpectralCamera::init(int deviceTypeIndex, double FrameRate, double ExposureT
this->ExposureTime, this->ExposureTime,
this->BinningSpatial, this->BinningSpatial,
this->BinningSpectral, this->BinningSpectral,
this->BinningAverage, this->RingBufferSize); this->BinningAverage,this->RingBufferSize);
wprintf(L"******************************************************\n"); wprintf(L"******************************************************\n");
SI_CHK(SI_Open(this->deviceTypeIndex, &this->deviceHandle)); SI_CHK(SI_Open(this->deviceTypeIndex, &this->deviceHandle));
@ -137,6 +123,7 @@ Error:
return -1; return -1;
} }
int SpectralCamera::stop(void) int SpectralCamera::stop(void)
{ {
SI_CHK(SI_Command(this->deviceHandle, L"Acquisition.Stop")); SI_CHK(SI_Command(this->deviceHandle, L"Acquisition.Stop"));
@ -149,6 +136,7 @@ Error:
return -1; return -1;
} }
int SpectralCamera::start(void) int SpectralCamera::start(void)
{ {
SI_CHK(SI_Command(this->deviceHandle, L"Acquisition.Start")); SI_CHK(SI_Command(this->deviceHandle, L"Acquisition.Start"));
@ -161,7 +149,7 @@ Error:
return -1; return -1;
} }
int SpectralCamera::reset_FrameRate(double Value) int SpectralCamera::reset_FrameRate( double Value)
{ {
SI_CHK(SI_SetFloat(this->deviceHandle, L"Camera.FrameRate", Value)); SI_CHK(SI_SetFloat(this->deviceHandle, L"Camera.FrameRate", Value));
return 0; return 0;
@ -173,7 +161,7 @@ Error:
return -1; return -1;
} }
int SpectralCamera::reset_ExposureTime(double Value) int SpectralCamera::reset_ExposureTime( double Value)
{ {
SI_CHK(SI_SetFloat(this->deviceHandle, L"Camera.ExposureTime", Value)); SI_CHK(SI_SetFloat(this->deviceHandle, L"Camera.ExposureTime", Value));
return 0; return 0;
@ -194,6 +182,7 @@ void SpectralCamera::save(std::ofstream &h_raw_spectral_file, std::ofstream &h_h
h_header_file << now_t << "," << raw_spectral_file_size << "," << this->nFrameSize; h_header_file << now_t << "," << raw_spectral_file_size << "," << this->nFrameSize;
} }
int SpectralCamera::read(void) int SpectralCamera::read(void)
{ {
SI_CHK(SI_Wait(this->deviceHandle, this->pFrameBuffer, &this->nFrameSize, &this->nFrameNumber, 10000)); SI_CHK(SI_Wait(this->deviceHandle, this->pFrameBuffer, &this->nFrameSize, &this->nFrameNumber, 10000));

View File

@ -1,188 +1,3 @@
// #include <chrono> #include <chrono>
// const auto t = std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now().time_since_epoch()).count(); const auto t = std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now().time_since_epoch()).count();
#include <iostream>
#include <windows.h>
#include <utils.h>
bool AddExePathtoPATH()
{
/*将当前可执行文件所在的路径添加到环境变量中*/
wchar_t szPath[512] = {0};
if (GetModuleFileNameW(NULL, szPath, sizeof(szPath) - 1) == 0)
{
std::cerr << "获取exe文件路径失败." << std::endl;
return false;
}
std::wstring exe_dir(szPath);
size_t lastSlashIndex = exe_dir.find_last_of('\\');
if (lastSlashIndex == std::wstring::npos)
{
std::cerr << "获取exe文件路径中的反斜杠失败." << std::endl;
return false;
}
exe_dir = exe_dir.substr(0, lastSlashIndex);
wchar_t currentPath[32767];
if (GetEnvironmentVariableW(L"PATH", currentPath, 32767) == 0)
{
std::cerr << "获取当前环境变量PATH失败" << std::endl;
return false;
}
std::wstring newPath = exe_dir + L";" + std::wstring(currentPath);
if (SetEnvironmentVariableW(L"PATH", newPath.c_str()) == 0)
{
std::cerr << "添加当前路径到环境变量PATH失败" << std::endl;
return false;
}
return true;
}
Configs getConfigFromEnvVar()
{
/*检查*/
Configs configs;
std::cout << "***********正在检查此程序所需的环境变量************" << std::endl;
// char buffer[1024];
// DWORD ret = GetEnvironmentVariableW(L"TEST", buffer, 1024);
wchar_t buffer[32767];
DWORD result;
try
{
result = GetEnvironmentVariableW(L"SPECTRUM_deviceTypeIndex", buffer, 32767);
if (result > 0 && result < 32767)
{
configs.SPECTRUM_deviceTypeIndex = std::stoi(std::wstring(buffer));
std::cout << "光谱相机设备型号索引(SPECTRUM_deviceTypeIndex)为\t" << configs.SPECTRUM_deviceTypeIndex << std::endl;
}
else
{
std::cout << "未获取到光谱相机设备型号索引(SPECTRUM_deviceTypeIndex)" << std::endl;
configs.isEmpty = true;
}
result = GetEnvironmentVariableW(L"SPECTRUM_FrameRate", buffer, 32767);
if (result > 0 && result < 32767)
{
configs.SPECTRUM_FrameRate = std::stod(std::wstring(buffer));
std::cout << "光谱相机帧率(SPECTRUM_FrameRate)为\t" << configs.SPECTRUM_FrameRate << std::endl;
}
else
{
std::cout << "未获取到光谱相机帧率(SPECTRUM_FrameRate)" << std::endl;
configs.isEmpty = true;
}
result = GetEnvironmentVariableW(L"SPECTRUM_ExposureTime", buffer, 32767);
if (result > 0 && result < 32767)
{
configs.SPECTRUM_ExposureTime = std::stod(std::wstring(buffer));
std::cout << "光谱相机曝光时间(SPECTRUM_ExposureTime)为\t" << configs.SPECTRUM_ExposureTime << std::endl;
}
else
{
std::cout << "未获取到光谱相机曝光时间(SPECTRUM_ExposureTime)" << std::endl;
configs.isEmpty = true;
}
result = GetEnvironmentVariableW(L"SPECTRUM_BinningSpatial", buffer, 32767);
if (result > 0 && result < 32767)
{
configs.SPECTRUM_BinningSpatial = std::stoi(std::wstring(buffer));
std::cout << "光谱相机空间像素组合(SPECTRUM_BinningSpatial)为\t" << configs.SPECTRUM_BinningSpatial << std::endl;
}
else
{
std::cout << "未获取到光谱相机空间像素组合(SPECTRUM_BinningSpatial)" << std::endl;
configs.isEmpty = true;
}
result = GetEnvironmentVariableW(L"SPECTRUM_BinningSpectral", buffer, 32767);
if (result > 0 && result < 32767)
{
configs.SPECTRUM_BinningSpectral = std::stoi(std::wstring(buffer));
std::cout << "光谱相机光谱像素组合(SPECTRUM_BinningSpectral)为\t" << configs.SPECTRUM_BinningSpectral << std::endl;
}
else
{
std::cout << "未获取到光谱相机光谱像素组合(SPECTRUM_BinningSpectral)" << std::endl;
configs.isEmpty = true;
}
result = GetEnvironmentVariableW(L"SPECTRUM_BinningAverage", buffer, 32767);
if (result > 0 && result < 32767)
{
int tmp = std::stoi(std::wstring(buffer));
configs.SPECTRUM_BinningAverage = tmp;
std::cout << "光谱相机是否组合平均(SPECTRUM_BinningAverage)为\t" << configs.SPECTRUM_BinningAverage << std::endl;
}
else
{
std::cout << "未获取到光谱相机是否组合平均(SPECTRUM_BinningAverage)" << std::endl;
configs.isEmpty = true;
}
result = GetEnvironmentVariableW(L"SPECTRUM_RingBufferSize", buffer, 32767);
if (result > 0 && result < 32767)
{
configs.SPECTRUM_RingBufferSize = std::stoll(std::wstring(buffer));
std::cout << "光谱相机循环缓存大小(SPECTRUM_RingBufferSize)为\t" << configs.SPECTRUM_RingBufferSize << std::endl;
}
else
{
std::cout << "未获取到光谱相机循环缓存大小(SPECTRUM_RingBufferSize)" << std::endl;
configs.isEmpty = true;
}
result = GetEnvironmentVariableW(L"RAWDATA_Folder", buffer, 32767);
if (result > 0 && result < 32767)
{
std::wstring tmp = std::wstring(buffer);
configs.RAWDATA_Folder = std::string(tmp.begin(), tmp.end());
// wstring转成string
std::cout << "原始光谱数据保存路径(RAWDATA_Folder)为\t" << configs.RAWDATA_Folder << std::endl;
}
else
{
std::cout << "未获取到原始数据保存路径(RAWDATA_Folder)" << std::endl;
configs.isEmpty = true;
}
result = GetEnvironmentVariableW(L"RAWDATA_MaxSize", buffer, 32767);
if (result > 0 && result < 32767)
{
configs.RAWDATA_MaxSize = std::stoull(std::wstring(buffer));
std::cout << "原始数据文件最大大小(RAWDATA_MaxSize)为\t" << configs.RAWDATA_MaxSize << " Byte" << std::endl;
}
else
{
std::cout << "未获取到原始数据文件最大大小(RAWDATA_MaxSize)" << std::endl;
configs.isEmpty = true;
}
}
catch (const std::invalid_argument &e)
{
std::cerr << "Invalid argument: " << e.what() << std::endl;
configs.isEmpty = true;
}
catch (const std::out_of_range &e)
{
std::cerr << "Out of range: " << e.what() << std::endl;
configs.isEmpty = true;
}
return configs;
}