2012-11-14 13 views
0

鑑於以下保存char數組爲JPG爲C++視窗Store應用

  • 位圖的原始圖像中的字符陣列中的std :: wstring的使用如下產生的數據
  • 圖像的寬度和高度
  • 路徑wzAppDataDirectory代碼

// Get a good path. 
wchar_t wzAppDataDirectory[MAX_PATH]; 
wcscpy_s(wzAppDataDirectory, MAX_PATH, Windows::Storage::ApplicationData::Current->LocalFolder->Path->Data()); 
wcscat_s(wzAppDataDirectory, MAX_PATH, (std::wstring(L"\\") + fileName).c_str()); 

我們怎樣才能將圖像另存爲JPG? (包括編碼以及char數組是原始位圖形式)

代碼例如是非常讚賞。

+1

保存是什麼意思?只需保存或編碼? –

+0

@Let_Me_Be我將我的問題改進爲「原始圖像數據」。因此,它也包含編碼。 –

回答

0

這是我從我的朋友處獲得的示例代碼。

它使用了OpenCV的墊數據結構。請注意,您需要確保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(); 
    }); 
}