1 // Video support for Windows Runtime 2 3 // Copyright (c) Microsoft Open Technologies, Inc. 4 // All rights reserved. 5 // 6 // (3 - clause BSD License) 7 // 8 // Redistribution and use in source and binary forms, with or without modification, are permitted provided that 9 // the following conditions are met: 10 // 11 // 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the 12 // following disclaimer. 13 // 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the 14 // following disclaimer in the documentation and/or other materials provided with the distribution. 15 // 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or 16 // promote products derived from this software without specific prior written permission. 17 // 18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED 19 // WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A 20 // PARTICULAR PURPOSE ARE DISCLAIMED.IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY 21 // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES(INCLUDING, BUT NOT LIMITED TO, 22 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23 // HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT(INCLUDING 24 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 25 // POSSIBILITY OF SUCH DAMAGE. 26 27 #include <ppl.h> 28 #include <functional> 29 #include <concrt.h> 30 #include <agile.h> 31 32 using namespace Windows::UI::Xaml::Controls; 33 34 namespace cv 35 { 36 37 //! @addtogroup videoio_winrt 38 //! @{ 39 40 enum { 41 OPEN_CAMERA = 300, 42 CLOSE_CAMERA, 43 UPDATE_IMAGE_ELEMENT, 44 SHOW_TRACKBAR 45 }; 46 47 /********************************** WinRT API ************************************************/ 48 49 template <typename ...Args> 50 CV_EXPORTS void winrt_startMessageLoop(std::function<void(Args...)>&& callback, Args... args); 51 52 template <typename ...Args> 53 CV_EXPORTS void winrt_startMessageLoop(void callback(Args...), Args... args); 54 55 /** @brief 56 @note 57 Starts (1) frame-grabbing loop and (2) message loop 58 1. Function passed as an argument must implement common OCV reading frames 59 pattern (see cv::VideoCapture documentation) AND call cv::winrt_imgshow(). 60 2. Message processing loop required to overcome WinRT container and type 61 conversion restrictions. OCV provides default implementation 62 Here is how the class can be used: 63 @code 64 void cvMain() 65 { 66 Mat frame; 67 VideoCapture cam; 68 cam.open(0); 69 70 while (1) 71 { 72 cam >> frame; 73 74 // don't reprocess the same frame again 75 if (!cam.grab()) continue; 76 77 // your processing logic goes here 78 79 // obligatory step to get XAML image component updated 80 winrt_imshow(); 81 } 82 } 83 84 MainPage::MainPage() 85 { 86 InitializeComponent(); 87 88 cv::winrt_setFrameContainer(cvImage); 89 cv::winrt_startMessageLoop(cvMain); 90 } 91 @endcode 92 */ 93 template 94 CV_EXPORTS void winrt_startMessageLoop(void callback(void)); 95 96 /** @brief 97 @note 98 Must be called from WinRT specific callback to handle image grabber state. 99 Here is how the class can be used: 100 @code 101 MainPage::MainPage() 102 { 103 // ... 104 Window::Current->VisibilityChanged += ref new Windows::UI::Xaml::WindowVisibilityChangedEventHandler(this, &Application::MainPage::OnVisibilityChanged); 105 // ... 106 } 107 108 void Application::MainPage::OnVisibilityChanged(Platform::Object ^sender, 109 Windows::UI::Core::VisibilityChangedEventArgs ^e) 110 { 111 cv::winrt_onVisibilityChanged(e->Visible); 112 } 113 @endcode 114 */ 115 CV_EXPORTS void winrt_onVisibilityChanged(bool visible); 116 117 /** @brief 118 @note 119 Must be called to assign WinRT control holding image you're working with. 120 Code sample is available for winrt_startMessageLoop(). 121 */ 122 CV_EXPORTS void winrt_setFrameContainer(::Windows::UI::Xaml::Controls::Image^ image); 123 124 /** @brief 125 @note 126 Must be called to update attached image source. 127 Code sample is available for winrt_startMessageLoop(). 128 */ 129 CV_EXPORTS void winrt_imshow(); 130 131 //! @} videoio_winrt 132 133 } // cv