From b20d38f0c9969c4bf11f7fd106e3abff31aa4fd3 Mon Sep 17 00:00:00 2001 From: wwj <3427896184@qq.com> Date: 星期四, 26 六月 2025 15:30:30 +0800 Subject: [PATCH] 日志4 --- Server/汪卫军/code/ImageStorage.h | 17 ++++++++ /dev/null | 15 ------- Server/汪卫军/code/ImageStorage.cpp | 54 +++++++++++++++++++++++++++ 3 files changed, 71 insertions(+), 15 deletions(-) diff --git "a/Server/\346\261\252\345\215\253\345\206\233/code/ImageProcessor.cpp" "b/Server/\346\261\252\345\215\253\345\206\233/code/ImageProcessor.cpp" deleted file mode 100644 index 4148d4e..0000000 --- "a/Server/\346\261\252\345\215\253\345\206\233/code/ImageProcessor.cpp" +++ /dev/null @@ -1,93 +0,0 @@ -// ImageProcessor.cpp - 照片处理模块实现 -#include "stdafx.h" -#include "ImageProcessor.h" -#include <opencv2/opencv.hpp> - -ImageProcessor::ImageProcessor() {} - -ImageProcessor::~ImageProcessor() {} - -bool ImageProcessor::CropFace(const unsigned char* imageData, int width, int height, - unsigned char*& faceData, int& faceWidth, int& faceHeight) const { - try { - // 创建OpenCV图像 - cv::Mat image(height, width, CV_8UC3, const_cast<unsigned char*>(imageData)); - - // 加载人脸检测器 - cv::CascadeClassifier face_cascade; - if (!face_cascade.load(cv::samples::findFile("haarcascade_frontalface_alt.xml"))) { - std::cerr << "无法加载人脸检测器" << std::endl; - return false; - } - - // 转换为灰度图 - cv::Mat gray; - cv::cvtColor(image, gray, cv::COLOR_BGR2GRAY); - cv::equalizeHist(gray, gray); - - // 检测人脸 - std::vector<cv::Rect> faces; - face_cascade.detectMultiScale(gray, faces, 1.1, 2, 0 | cv::CASCADE_SCALE_IMAGE, cv::Size(30, 30)); - - if (faces.empty()) { - return false; - } - - // 获取第一个检测到的人脸 - cv::Rect face_rect = faces[0]; - cv::Mat face = image(face_rect); - - // 保存人脸数据 - faceWidth = face.cols; - faceHeight = face.rows; - size_t dataSize = faceWidth * faceHeight * 3; - - faceData = new unsigned char[dataSize]; - memcpy(faceData, face.data, dataSize); - - return true; - } - catch (const std::exception& e) { - std::cerr << "裁剪人脸失败: " << e.what() << std::endl; - return false; - } -} - -bool ImageProcessor::EnhanceQuality(unsigned char* imageData, int width, int height) const { - try { - // 创建OpenCV图像 - cv::Mat image(height, width, CV_8UC3, imageData); - - // 图像增强处理 - cv::Mat enhanced; - cv::GaussianBlur(image, enhanced, cv::Size(0, 0), 3); - cv::addWeighted(image, 1.5, enhanced, -0.5, 0, enhanced); - - // 复制回原始数据 - memcpy(imageData, enhanced.data, width * height * 3); - - return true; - } - catch (const std::exception& e) { - std::cerr << "增强图像质量失败: " << e.what() << std::endl; - return false; - } -} - -bool ImageProcessor::ValidateImage(const unsigned char* imageData, int width, int height) const { - try { - // 简单验证:检查图像尺寸是否合理 - if (width < 10 || height < 10) { - return false; - } - - // 这里可以添加更复杂的图像验证逻辑 - // 例如:检查图像是否模糊、是否包含人脸等 - - return true; - } - catch (const std::exception& e) { - std::cerr << "验证图像失败: " << e.what() << std::endl; - return false; - } -} \ No newline at end of file diff --git "a/Server/\346\261\252\345\215\253\345\206\233/code/ImageProcessor.h" "b/Server/\346\261\252\345\215\253\345\206\233/code/ImageProcessor.h" deleted file mode 100644 index 2a5727b..0000000 --- "a/Server/\346\261\252\345\215\253\345\206\233/code/ImageProcessor.h" +++ /dev/null @@ -1,15 +0,0 @@ -// ImageProcessor.h - 照片处理模块 -#pragma once -#include <string> -#include "FaceImageManager.h" - -class ImageProcessor { -public: - ImageProcessor(); - ~ImageProcessor(); - - bool CropFace(const unsigned char* imageData, int width, int height, - unsigned char*& faceData, int& faceWidth, int& faceHeight) const; - bool EnhanceQuality(unsigned char* imageData, int width, int height) const; - bool ValidateImage(const unsigned char* imageData, int width, int height) const; -}; \ No newline at end of file diff --git "a/Server/\346\261\252\345\215\253\345\206\233/code/ImageStorage.cpp" "b/Server/\346\261\252\345\215\253\345\206\233/code/ImageStorage.cpp" new file mode 100644 index 0000000..b43bc2e --- /dev/null +++ "b/Server/\346\261\252\345\215\253\345\206\233/code/ImageStorage.cpp" @@ -0,0 +1,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); +} \ No newline at end of file diff --git "a/Server/\346\261\252\345\215\253\345\206\233/code/ImageStorage.h" "b/Server/\346\261\252\345\215\253\345\206\233/code/ImageStorage.h" new file mode 100644 index 0000000..3b754d6 --- /dev/null +++ "b/Server/\346\261\252\345\215\253\345\206\233/code/ImageStorage.h" @@ -0,0 +1,17 @@ +// ImageStorage.h - 照片存储模块 +#pragma once +#include <string> +#include <vector> +#include "FaceImageManager.h" + +class ImageStorage { +public: + ImageStorage(); + ~ImageStorage(); + + bool SaveImage(const std::string& filePath, const unsigned char* imageData, + int width, int height); + bool LoadImage(const std::string& filePath, unsigned char*& imageData, + int& width, int& height) const; + bool DeleteImage(const std::string& filePath); +}; \ No newline at end of file -- Gitblit v1.8.0