New file |
| | |
| | | // ImageStorage.cpp - ç
§çå卿¨¡åå®ç° |
| | | #include "stdafx.h" |
| | | #include "ImageStorage.h" |
| | | #include <fstream> |
| | | #include <opencv2/opencv.hpp> |
| | | |
| | | ImageStorage::ImageStorage() {} |
| | | |
| | | ImageStorage::~ImageStorage() {} |
| | | |
| | | bool ImageStorage::SaveImage(const std::string& filePath, const unsigned char* imageData, |
| | | int width, int height) { |
| | | try { |
| | | // å建OpenCVå¾å |
| | | cv::Mat image(height, width, CV_8UC3, const_cast<unsigned char*>(imageData)); |
| | | |
| | | // ä¿åå¾å |
| | | return cv::imwrite(filePath, image); |
| | | } |
| | | catch (const std::exception& e) { |
| | | std::cerr << "ä¿åå¾å失败: " << e.what() << std::endl; |
| | | return false; |
| | | } |
| | | } |
| | | |
| | | bool ImageStorage::LoadImage(const std::string& filePath, unsigned char*& imageData, |
| | | int& width, int& height) const { |
| | | try { |
| | | // å è½½å¾å |
| | | cv::Mat image = cv::imread(filePath, cv::IMREAD_COLOR); |
| | | |
| | | if (image.empty()) { |
| | | return false; |
| | | } |
| | | |
| | | // åé
å
åå¹¶å¤å¶æ°æ® |
| | | width = image.cols; |
| | | height = image.rows; |
| | | size_t dataSize = width * height * 3; |
| | | |
| | | imageData = new unsigned char[dataSize]; |
| | | memcpy(imageData, image.data, dataSize); |
| | | |
| | | return true; |
| | | } |
| | | catch (const std::exception& e) { |
| | | std::cerr << "å è½½å¾å失败: " << e.what() << std::endl; |
| | | return false; |
| | | } |
| | | } |
| | | |
| | | bool ImageStorage::DeleteImage(const std::string& filePath) { |
| | | return (remove(filePath.c_str()) == 0); |
| | | } |