Development of New Fashion Windows 8 (29): multimedia capturing (advanced)

Source: Internet
Author: User
Tags blank page

This so-called advanced article is relatively more flexible than the content mentioned in the previous section, but I think when we are doing an application, there are very few such advanced jobs. If you want to develop professional shooting programs, you don't need to do it at all. People's equipment providers have already developed them, just like the Dell products I bought, people have already provided a very powerful shooting program.

 

However, it makes sense to study it. This "advanced" content is related to the mediacapture class. It is mainly about it. You can see that it has a poor experience in life. There are n methods. Of course, in general, we may use three methods: preview, photograph, and video recording. Yes, you just want to use video recording. Some people may say, what about video chat? Video chats do not support dynamic recording of videos.

 

Therefore, we do not have to make a lot of things too complicated. Just like today's example, it is very simple, but it may prove its application value.

So what are we playing today? Can I get a scheduled shot? This is also worth noting.

 

1. If you create a new project, you will not go home to review it.

2. There is no need for many items on the XAML page. If you want to take a picture, you must be able to preview the content in the camera so that shooting is convenient. So, first get a captureelement, this is used to display the camera preview on the UI. In addition, we want to display the photos captured at regular intervals. Therefore, we need a listview control. To perform related operations, we need to throw several buttons.

<Page X: class = "app1.mainpage" xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns: x = "http://schemas.microsoft.com/winfx/2006/xaml" xmlns: Local = "using: app1" xmlns: D = "http://schemas.microsoft.com/expression/blend/2008" xmlns: MC = "http://schemas.openxmlformats.org/markup-compatibility/2006" MC: ignorable = "D"> <grid background = "{staticresource applicationpagebackgroundthemebrush}"> <grid. rowdefinitions> <rowdefinition Height = "*"/> <rowdefinition Height = "Auto"/> </grid. rowdefinitions> <captureelement X: Name = "CE" grid. row = "0" margin = "13"/> <grid. row = "1"> <grid. columndefinitions> <columndefinition width = "*"/> <columndefinition width = "Auto"/> </grid. columndefinitions> <listview X: Name = "LV" grid. column = "0" margin = "150," selectionmode = "NONE" Height = ""> <listview. itemtemplate> <datatemplate> <image width = "120" Height = "120" stretch = "Uniform" Source = "{binding}"/> </datatemplate> </listview. itemtemplate> <listview. itemspanel> <itemspaneltemplate> <virtualizingstackpanel orientation = "horizontal"/> </itemspaneltemplate> </listview. itemspanel> </listview> <stackpanel grid. column = "1" margin = "10"> <button content = "start" Click = "onstart" X: name = "btnstart"/> <button content = "stop" Click = "onstop" X: name = "btnstop" isenabled = "false"/> </stackpanel> </GRID> </Page>

3. For timed shooting, a timer is required. In the windows. system. Threading namespace, the timer name is threadpooltimer. We will use it for timing.

First introduce the following namespace:

using Windows.System.Threading;using System.Collections.ObjectModel;using Windows.UI.Xaml.Media.Imaging;using Windows.Media.Capture;using Windows.Media.MediaProperties;using Windows.Storage.Streams;

Define the following members in the mainpage class:

    public sealed partial class MainPage : Page    {        ObservableCollection<BitmapImage> images = null;        MediaCapture myCapture = null;        ThreadPoolTimer myTimer = null;

In the mainpage constructor, make up for them.

        public  MainPage()        {            this.InitializeComponent();            this.myCapture = new MediaCapture();            this.images = new ObservableCollection<BitmapImage>();            this.lv.ItemsSource = images;        }

We want to enable the camera and start previewing when navigation to the page. Therefore, we need to do something in the onnavigatedto method.

        protected override async void OnNavigatedTo(NavigationEventArgs e)        {            await this.myCapture.InitializeAsync();            this.ce.Source = myCapture;            await this.myCapture.StartPreviewAsync();        }

The threadpooltimer class creates a timer by calling a static method createperiodictimer. We can see that in the two overloading of this method, each parameter needs a delegate, and this delegate needs to bind a method, when the timer reaches the task execution time, the delegate is called to call the specified method.

Therefore, we need to define the processing method first:

Private async void timerhandler (threadpooltimer t) {await dispatcher. runasync (windows. UI. core. coredispatcherpriority. normal, async () =>{ irandomaccessstream stream = new inmemoryrandomaccessstream (); await mycapture. capturephototostreamasync (imageencodingproperties. createpng (), stream); // when writing data to a stream, the pointer has been moved to the end. // Therefore, when initializing an image, you must first move the pointer back to the starting position stream. seek (0); bitmapimage bmp img = new bitmapimage (); bmp img. setsource (Stream); this. images. add (bmp img );});}

Note: The timer starts immediately after the createperiodictimer method is successfully returned. to cancel the timer, call the cancel method.

 

4. At this time, the component code we need is basically enough. Next we will handle the click events of the two buttons that control the start shooting and stop shooting.

Private void onstop (Object sender, routedeventargs e) {If (this. mytimer! = NULL) {This. mytimer. cancel (); // cancel} btnstop. isenabled = false; btnstart. isenabled = true;} private void onstart (Object sender, routedeventargs e) {This. images. clear (); // start timing this. mytimer = threadpooltimer. createperiodictimer (timerhandler, timespan. fromseconds (5); btnstart. isenabled = false; btnstop. isenabled = true ;}

 

The complete code is as follows:

Using system; using system. collections. generic; using system. io; using system. LINQ; using Windows. foundation; using Windows. foundation. collections; using Windows. UI. XAML; using Windows. UI. XAML. controls; using Windows. UI. XAML. controls. primitives; using Windows. UI. XAML. data; using Windows. UI. XAML. input; using Windows. UI. XAML. media; using Windows. UI. XAML. navigation; using Windows. system. threading; using system. coll Ections. objectmodel; using Windows. UI. XAML. media. imaging; using Windows. media. capture; using Windows. media. mediaproperties; using Windows. storage. streams; // "blank page" item template in http://go.microsoft.com/fwlink? On linkid = 234238, the introduction of namespace app1 {/// <summary> /// can be used for itself or navigation to a blank page inside the frame. /// </Summary> Public sealed partial class mainpage: Page {observablecollection <bitmapimage> images = NULL; mediacapture mycapture = NULL; threadpooltimer mytimer = NULL; Public mainpage () {This. initializecomponent (); this. mycapture = new mediacapture (); this. images = new observablecollection <bitmapimage> (); this. LV. itemssource = images;} /// <summary> /// call when this page is to be displayed in the frame. /// </Summary> /// <Param name = "E"> describes how to access event data on this page. Parameter // properties are usually used on the configuration page. </Param> protected override async void onnavigatedto (navigationeventargs e) {await this. mycapture. initializeasync (); this. CE. source = mycapture; await this. mycapture. startpreviewasync ();} private async void timerhandler (threadpooltimer t) {await dispatcher. runasync (windows. UI. core. coredispatcherpriority. normal, async () =>{ irandomaccessstream stream = new inmemoryrandomaccessstream (); await my Capture. capturephototostreamasync (imageencodingproperties. createpng (), stream); // when writing data to a stream, the pointer has been moved to the end. // Therefore, when initializing an image, you must first move the pointer back to the starting position stream. seek (0); bitmapimage bmp img = new bitmapimage (); bmp img. setsource (Stream); this. images. add (bmp img) ;}) ;}private void onstop (Object sender, routedeventargs e) {If (this. mytimer! = NULL) {This. mytimer. cancel (); // cancel} btnstop. isenabled = false; btnstart. isenabled = true;} private void onstart (Object sender, routedeventargs e) {This. images. clear (); // start timing this. mytimer = threadpooltimer. createperiodictimer (timerhandler, timespan. fromseconds (5); btnstart. isenabled = false; btnstop. isenabled = true ;}}}

5. Open the configuration file, switch to the "functions" tab, select "Microphone" and "Network Camera", and save.

 

6. Run the application. After the preview starts, click the start button and the program will take a photo every five seconds (because the Code specifies the second, and displayed in listview.

 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.