Path: blob/master/modules/videoio/src/cap_winrt_bridge.cpp
16354 views
// videoio to XAML bridge for OpenCV12// Copyright (c) Microsoft Open Technologies, Inc.3// All rights reserved.4//5// (3 - clause BSD License)6//7// Redistribution and use in source and binary forms, with or without modification, are permitted provided that8// the following conditions are met:9//10// 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the11// following disclaimer.12// 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the13// following disclaimer in the documentation and/or other materials provided with the distribution.14// 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or15// promote products derived from this software without specific prior written permission.16//17// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED18// WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A19// PARTICULAR PURPOSE ARE DISCLAIMED.IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY20// DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES(INCLUDING, BUT NOT LIMITED TO,21// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)22// HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT(INCLUDING23// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE24// POSSIBILITY OF SUCH DAMAGE.2526#include "opencv2\videoio\cap_winrt.hpp"27#include "cap_winrt_capture.hpp"28#include "cap_winrt_bridge.hpp"29#include "cap_winrt_video.hpp"3031using namespace Windows::Foundation;32using namespace Windows::Media::Capture;33using namespace Windows::Media::MediaProperties;34using namespace Windows::Devices::Enumeration;3536using namespace Windows::UI::Xaml::Media::Imaging;37using namespace Microsoft::WRL;3839using namespace Platform;40using namespace ::Concurrency;4142using namespace ::std;4344/***************************** VideoioBridge class ******************************/4546// non-blocking47void VideoioBridge::requestForUIthreadAsync(int action)48{49reporter.report(action);50}5152VideoioBridge& VideoioBridge::getInstance()53{54static VideoioBridge instance;55return instance;56}5758void VideoioBridge::swapInputBuffers()59{60// TODO: already locked, check validity61// lock_guard<mutex> lock(inputBufferMutex);62swap(backInputPtr, frontInputPtr);63//if (currentFrame != frameCounter)64//{65// currentFrame = frameCounter;66// swap(backInputPtr, frontInputPtr);67//}68}6970void VideoioBridge::swapOutputBuffers()71{72lock_guard<mutex> lock(outputBufferMutex);73swap(frontOutputBuffer, backOutputBuffer);74}7576void VideoioBridge::allocateOutputBuffers()77{78frontOutputBuffer = ref new WriteableBitmap(width, height);79backOutputBuffer = ref new WriteableBitmap(width, height);80}8182// performed on UI thread83void VideoioBridge::allocateBuffers(int width, int height)84{85// allocate input Mats (bgra8 = CV_8UC4, RGB24 = CV_8UC3)86frontInputMat.create(height, width, CV_8UC3);87backInputMat.create(height, width, CV_8UC3);8889frontInputPtr = frontInputMat.ptr(0);90backInputPtr = backInputMat.ptr(0);9192allocateOutputBuffers();93}9495// performed on UI thread96bool VideoioBridge::openCamera()97{98// buffers must alloc'd on UI thread99allocateBuffers(width, height);100101// nb. video capture device init must be done on UI thread;102if (!Video::getInstance().isStarted())103{104Video::getInstance().initGrabber(deviceIndex, width, height);105return true;106}107108return false;109}110111// nb on UI thread112void VideoioBridge::updateFrameContainer()113{114// copy output Mat to WBM115Video::getInstance().CopyOutput();116117// set XAML image element with image WBM118cvImage->Source = backOutputBuffer;119}120121void VideoioBridge::imshow()122{123swapOutputBuffers();124requestForUIthreadAsync(cv::UPDATE_IMAGE_ELEMENT);125}126127int VideoioBridge::getDeviceIndex()128{129return deviceIndex;130}131132void VideoioBridge::setDeviceIndex(int index)133{134deviceIndex = index;135}136137int VideoioBridge::getWidth()138{139return width;140}141142int VideoioBridge::getHeight()143{144return height;145}146147void VideoioBridge::setWidth(int _width)148{149width = _width;150}151152void VideoioBridge::setHeight(int _height)153{154height = _height;155}156157// end158159