Path: blob/master/samples/winrt_universal/PhoneTutorial/MainPage.xaml.cpp
16354 views
//1// MainPage.xaml.cpp2// Implementation of the MainPage class.3//45#include "pch.h"6#include "MainPage.xaml.h"78#include <opencv2\imgproc\types_c.h>9#include <opencv2\core.hpp>10#include <opencv2\imgproc.hpp>11#include <Robuffer.h>12#include <ppl.h>13#include <ppltasks.h>1415using namespace PhoneTutorial;16using namespace Platform;17using namespace Windows::Foundation;18using namespace Windows::Foundation::Collections;19using namespace Windows::UI::Xaml;20using namespace Windows::UI::Xaml::Controls;21using namespace Windows::UI::Xaml::Controls::Primitives;22using namespace Windows::UI::Xaml::Data;23using namespace Windows::UI::Xaml::Input;24using namespace Windows::UI::Xaml::Media;25using namespace Windows::UI::Xaml::Navigation;26using namespace Windows::UI::Xaml::Media::Imaging;27using namespace Windows::Storage::Streams;28using namespace Microsoft::WRL;29using namespace Windows::ApplicationModel;3031MainPage::MainPage()32{33InitializeComponent();34}3536/// <summary>37/// Invoked when this page is about to be displayed in a Frame.38/// </summary>39/// <param name="e">Event data that describes how this page was reached. The Parameter40/// property is typically used to configure the page.</param>41void MainPage::OnNavigatedTo(NavigationEventArgs^ e)42{43(void) e; // Unused parameter44LoadImage();45}4647inline void ThrowIfFailed(HRESULT hr)48{49if (FAILED(hr))50{51throw Exception::CreateException(hr);52}53}5455byte* GetPointerToPixelData(IBuffer^ buffer)56{57// Cast to Object^, then to its underlying IInspectable interface.58Object^ obj = buffer;59ComPtr<IInspectable> insp(reinterpret_cast<IInspectable*>(obj));6061// Query the IBufferByteAccess interface.62ComPtr<IBufferByteAccess> bufferByteAccess;63ThrowIfFailed(insp.As(&bufferByteAccess));6465// Retrieve the buffer data.66byte* pixels = nullptr;67ThrowIfFailed(bufferByteAccess->Buffer(&pixels));68return pixels;69}7071void PhoneTutorial::MainPage::Process_Click(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)72{73(void) e; // Unused parameter7475// get the pixels from the WriteableBitmap76byte* pPixels = GetPointerToPixelData(m_bitmap->PixelBuffer);77int height = m_bitmap->PixelHeight;78int width = m_bitmap->PixelWidth;7980// create a matrix the size and type of the image81cv::Mat mat(width, height, CV_8UC4);82memcpy(mat.data, pPixels, 4 * height*width);8384// convert to grayscale85cv::Mat intermediateMat;86cv::cvtColor(mat, intermediateMat, COLOR_RGB2GRAY);8788// convert to BGRA89cv::cvtColor(intermediateMat, mat, COLOR_GRAY2BGRA);9091// copy processed image back to the WriteableBitmap92memcpy(pPixels, mat.data, 4 * height*width);9394// update the WriteableBitmap95m_bitmap->Invalidate();96}9798void PhoneTutorial::MainPage::LoadImage()99{100Concurrency::task<Windows::Storage::StorageFile^> getFileTask(Package::Current->InstalledLocation->GetFileAsync(L"Lena.png"));101102auto getStreamTask = getFileTask.then(103[](Windows::Storage::StorageFile ^storageFile)104{105return storageFile->OpenReadAsync();106});107108getStreamTask.then(109[this](Windows::Storage::Streams::IRandomAccessStreamWithContentType^ stream)110{111m_bitmap = ref new Windows::UI::Xaml::Media::Imaging::WriteableBitmap(1, 1);112m_bitmap->SetSource(stream);113image->Source = m_bitmap;114});115}116117void PhoneTutorial::MainPage::Reset_Click(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)118{119(void) e; // Unused parameter120LoadImage();121}122123124