1 /*
2 * Copyright 2023 Unionman Technology Co., Ltd.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *
16 */
17 #include <cstdio>
18 #include <cstring>
19 #include <opencv2/opencv.hpp>
resizeImage(const std::string & imagePath,int newWidth,int newHeight)20 static std::string resizeImage(const std::string& imagePath, int newWidth, int newHeight)
21 {
22 // 读取原始图像
23 cv::Mat originalImage = cv::imread(imagePath);
24 // 检查图像是否成功加载
25 if (originalImage.empty()) {
26 std::cerr << "Failed to load the image: " << imagePath << std::endl;
27 return "erro";
28 }
29
30 // 调整图像大小
31 cv::Mat resizedImage;
32 cv::resize(originalImage, resizedImage, cv::Size(newWidth, newHeight));
33
34 // 保存结果图像,使用不同的文件名
35 cv::imwrite(imagePath, resizedImage);
36
37 return "succ";
38 }
39
main(int argc,char * argv[])40 int main(int argc, char* argv[])
41 {
42 if (argc != 4L) {
43 std::cerr << "Usage: " << argv[0] << " <image_path> <new_width> <new_height>" << std::endl;
44 return 1;
45 }
46
47 std::string imagePath = argv[1];
48 int newWidth = std::stoi(argv[2]);
49 int newHeight = std::stoi(argv[3]);
50
51 std::string result = resizeImage(imagePath, newWidth, newHeight);
52
53 std::cout << "Result: " << result << std::endl;
54
55 return 0;
56 }