温馨提示×

c++ bmp转jpg怎么实现

c++
小亿
131
2024-08-23 01:39:29
栏目: 编程语言

要实现将BMP格式的图片转换为JPG格式,可以使用OpenCV库来实现。

以下是使用OpenCV库实现BMP转JPG的示例代码:

#include <opencv2/opencv.hpp>

int main() {
    // 读取BMP图片
    cv::Mat image = cv::imread("input.bmp", cv::IMREAD_UNCHANGED);

    if (!image.data) {
        std::cerr << "Error: Unable to read BMP image." << std::endl;
        return -1;
    }

    // 将BMP图片保存为JPG格式
    cv::imwrite("output.jpg", image);

    std::cout << "BMP image converted to JPG successfully." << std::endl;

    return 0;
}

在上面的示例代码中,首先使用cv::imread函数读取BMP图片,然后使用cv::imwrite函数将读取的图片保存为JPG格式。

请确保你的编译环境已经配置好OpenCV库,以便成功编译上面的示例代码。

0