C++ 将字符数组另存为C++;Windows应用商店应用程序

C++ 将字符数组另存为C++;Windows应用商店应用程序,c++,windows-8,microsoft-metro,C++,Windows 8,Microsoft Metro,鉴于以下情况 字符数组中的位图原始图像数据 图像宽度和高度 std::wstring中的路径wzAppDataDirectory,使用以下代码生成 如何将图像保存为JPG?(包括编码以及字符数组是原始位图形式) 非常感谢您的代码示例。您需要使用库对JPEG进行编码。一些可能性是,或。这是我从朋友那里获得的示例代码 它使用OpenCV的Mat数据结构。注意,您需要确保cv::Mat中的无符号字符数据数组是连续的cv::cvtColor会起作用(或者,cv::Mat.clone) 请注意,不

鉴于以下情况

  • 字符数组中的位图原始图像数据
  • 图像宽度和高度
  • std::wstring中的路径
    wzAppDataDirectory
    ,使用以下代码生成


如何将图像保存为JPG?(包括编码以及字符数组是原始位图形式)


非常感谢您的代码示例。

您需要使用库对JPEG进行编码。一些可能性是,或。

这是我从朋友那里获得的示例代码

它使用OpenCV的Mat数据结构。注意,您需要确保
cv::Mat
中的无符号字符数据数组是连续的
cv::cvtColor
会起作用(或者,
cv::Mat.clone

请注意,不要使用OpenCV的
imwrite
因为在撰写本文时,
imwrite
未通过Windows应用商店认证测试。它使用多个API,这在WinRT中是被禁止的

void SaveMatAsJPG(const cv::Mat& mat, const std::wstring fileName)
{
    cv::Mat tempMat;
    cv::cvtColor(mat, tempMat, CV_BGR2BGRA);

    Platform::String^ pathName = ref new Platform::String(fileName.c_str());

    task<StorageFile^>(ApplicationData::Current->LocalFolder->CreateFileAsync(pathName, CreationCollisionOption::ReplaceExisting)).
    then([=](StorageFile^ file)
    {
        return file->OpenAsync(FileAccessMode::ReadWrite);
    }).
    then([=](IRandomAccessStream^ stream)
    {
        return BitmapEncoder::CreateAsync(BitmapEncoder::JpegEncoderId, stream);
    }).
    then([=](BitmapEncoder^ encoder)
    {
        const Platform::Array<unsigned char>^ pixels = ref new Platform::Array<unsigned char>(tempMat.data, tempMat.total() * tempMat.channels());
        encoder->SetPixelData(BitmapPixelFormat::Bgra8, BitmapAlphaMode::Ignore, tempMat.cols , tempMat.rows, 96.0, 96.0, pixels);
        encoder->FlushAsync();
    });
}
void SaveMatAsJPG(const cv::Mat&Mat,const std::wstring文件名)
{
cv::Mat tempMat;
cv::CVT颜色(mat、tempMat、cv_BGR2BGRA);
平台::字符串^pathName=ref新平台::字符串(fileName.c_str());
任务(ApplicationData::Current->LocalFolder->CreateFileAsync(路径名,CreationCollisionOption::ReplaceExisting))。
然后([=](存储文件^file)
{
返回文件->OpenAsync(FileAccessMode::ReadWrite);
}).
然后([=](irandomaccesstream^stream)
{
返回BitmapEncoder::CreateAsync(BitmapEncoder::JpegEncoderId,流);
}).
然后([=](位图编码器^encoder)
{
常量平台::数组^pixels=ref新平台::数组(tempMat.data,tempMat.total()*tempMat.channels());
编码器->设置像素数据(BitmapPixelFormat::Bgra8,BitmapAlphaMode::Ignore,tempMat.cols,tempMat.rows,96.0,96.0,像素);
编码器->FlushAsync();
});
}

保存是什么意思?简单地保存,或者也进行编码?@Let_Me_Be I将我的问题细化为
原始图像数据
。因此,它还包括编码。
void SaveMatAsJPG(const cv::Mat& mat, const std::wstring fileName)
{
    cv::Mat tempMat;
    cv::cvtColor(mat, tempMat, CV_BGR2BGRA);

    Platform::String^ pathName = ref new Platform::String(fileName.c_str());

    task<StorageFile^>(ApplicationData::Current->LocalFolder->CreateFileAsync(pathName, CreationCollisionOption::ReplaceExisting)).
    then([=](StorageFile^ file)
    {
        return file->OpenAsync(FileAccessMode::ReadWrite);
    }).
    then([=](IRandomAccessStream^ stream)
    {
        return BitmapEncoder::CreateAsync(BitmapEncoder::JpegEncoderId, stream);
    }).
    then([=](BitmapEncoder^ encoder)
    {
        const Platform::Array<unsigned char>^ pixels = ref new Platform::Array<unsigned char>(tempMat.data, tempMat.total() * tempMat.channels());
        encoder->SetPixelData(BitmapPixelFormat::Bgra8, BitmapAlphaMode::Ignore, tempMat.cols , tempMat.rows, 96.0, 96.0, pixels);
        encoder->FlushAsync();
    });
}