// 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);
|
}
|