user_ch-ml
2025-06-29 895e234470d25dff5c49ac9b0fdbb1e8faedc28a
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
// 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);
}