zoukankan      html  css  js  c++  java
  • 【WP开发】记录屏幕操作

    在某些应用中,比如游戏,有时候需要将用户的操作记录下来。ScreenCapture类提供了这个功能。但必须注意的是:此屏幕记录功能只对当前应用程序的屏幕有效,即只有当前应用程序在前台运行时才有效。

    与使用手机相机捕捉媒体一样,捕捉屏幕也要用到MediaCapture类,大家知道,在使用MediaCapture前,需要调用InitializeAsync方法进行初始化,其中一个重载版本是这样的:

    InitializeAsync(Windows.Media.Capture.MediaCaptureInitializationSettings)

    使用MediaCaptureInitializationSettings实例的时候,将ScreenCapture对象的VideoSource属性赋给MediaCaptureInitializationSettings的VideoSource属性;将ScreenCapture的AudioSource属性赋给MediaCaptureInitializationSettings的AudioSource属性。总之,赋值时要对应上就行。

    然后就可以使用MediaCapture如同用相机录视频一样录制屏幕了。

    下面给大家Show一个示例,示例也比较简单,可能会有1000个Bug,但主要是给大家做演示之用,就不管那么多了。本例就是让ScreenCapture和MediaCapture亲密合作,实现当前应用程序的屏幕记录功能。本示例中的屏幕记录功能包括两方面:1、记录为单张照片;2、录制视频。

    首先,在页面上随便放一些控件,并在底部工具栏中定义几个操作按钮。

        <StackPanel>
            <TextBox Header="姓名:"/>
            <DatePicker Header="日生:"/>
            <ComboBox Header="工种:">
                <ComboBoxItem>车工</ComboBoxItem>
                <ComboBoxItem>钳工</ComboBoxItem>
                <ComboBoxItem>铁工</ComboBoxItem>
            </ComboBox>
            <CheckBox Margin="0,20,0,0">临时</CheckBox>
            <CheckBox >调岗</CheckBox>
            <CheckBox>新入职</CheckBox>
        </StackPanel>
        
        <Page.BottomAppBar>
            <CommandBar>
                <AppBarButton Icon="Camera" Label="截取" Click="OnCapSingle"/>
                <AppBarButton Icon="Video" Label="开始" Click="OnStartRec"/>
                <AppBarButton Icon="Stop" Label="停止" Click="OnStopRec"/>
            </CommandBar>
        </Page.BottomAppBar>

    页面上的控件大家随便弄就行了,不必过于讲究,只不过拿来演示用的。

    记得曾几何时跟大家讲过,MediaCapture对象一旦初始化之后,会占用系统的资源,在应用程序挂起(就是应用不在前台运行时)时一定要将MediaCapture实例干掉,不然当其他程序访问相同资源时会导致死机,这可能是API没完全优化的原因吧,不过也有可能是应用程序生命周期机制引起的。不管哪种情况,我们只需记住在挂起时进行清理就行了。

    这里为了方便,我把MediaCapture对象的初始化和清理代码都写到App类中,大家在下载我的示例后记得到App类中找就是了。

            #region 方法
            /// <summary>
            /// 清理MediaCapture组件
            /// </summary>
            public static async Task CleanupCaptureAsync()
            {
                if (CurrentCapture != null)
                {
                    if (IsRecording)
                    {
                        await CurrentCapture.StopRecordAsync();
                        IsRecording = false;
                    }
                    CurrentCapture.Dispose();
                    CurrentCapture = null;
                }
            }
    
            /// <summary>
            /// 初始化Capture组件
            /// </summary>
            public static async Task InitailizeCapureAsync()
            {
                CurrentCapture = new MediaCapture();
                ScreenCapture screenCap = ScreenCapture.GetForCurrentView();
    
                MediaCaptureInitializationSettings settings = new MediaCaptureInitializationSettings();
                // 设置源
                //settings.AudioSource = screenCap.AudioSource;
                settings.VideoSource = screenCap.VideoSource;
                // 只捕捉视频
                settings.StreamingCaptureMode = StreamingCaptureMode.Video;
                await CurrentCapture.InitializeAsync(settings);
            }
            #endregion

    CurrentCapture是定义在App类中的静态属性,另外再加一个IsRecording静态属性,这个表示是否正在录制视频。

            #region 属性
            /// <summary>
            /// 引用正在使用的MediaCapture对象
            /// </summary>
            public static MediaCapture CurrentCapture { get; set; }
            /// <summary>
            /// 标识是否处于录制状态
            /// </summary>
            public static bool IsRecording { get; set; }
            #endregion

    声明为静态属性和静态方法是为了便于访问。

    在程序挂起时对MediaCapture实例进行清理。

            private async void OnSuspending(object sender, SuspendingEventArgs e)
            {
                var deferral = e.SuspendingOperation.GetDeferral();
    
                // TODO: 保存应用程序状态并停止任何后台活动
                await CleanupCaptureAsync();
    
                deferral.Complete();
            }

    好,以下代码是截图功能,即记录屏幕内容到一张图片中。

            private async void OnCapSingle(object sender, RoutedEventArgs e)
            {
                Button btn = sender as Button;
                btn.IsEnabled = false;
                if (App.CurrentCapture == null)
                {
                    await App.InitailizeCapureAsync();
                }
                StorageFolder cam = KnownFolders.CameraRoll;
                StorageFile newFile = await cam.CreateFileAsync(DateTime.Now.ToString("yyyyMMddHHmmss") + ".png", CreationCollisionOption.ReplaceExisting);
                await App.CurrentCapture.CapturePhotoToStorageFileAsync(ImageEncodingProperties.CreatePng(), newFile);
                await App.CleanupCaptureAsync(); //用完后清理
                btn.IsEnabled = true;
            }


    下面代码分别处理开始录制与停止录制。

            private async void OnStartRec(object sender, RoutedEventArgs e)
            {
                if (!App.IsRecording)
                {
                    if (App.CurrentCapture == null)
                    {
                        await App.InitailizeCapureAsync();
                    }
                    StorageFolder vdlib = KnownFolders.VideosLibrary;
                    StorageFile newfile = await vdlib.CreateFileAsync(DateTime.Now.ToString("yyyyMMddHHmmss") + ".mp4", CreationCollisionOption.ReplaceExisting);
                    // 开始录制视频
                    await App.CurrentCapture.StartRecordToStorageFileAsync(MediaEncodingProfile.CreateMp4(VideoEncodingQuality.Auto), newfile);
                    // 标识已经开始录制
                    App.IsRecording = true;
                }
            }
    
            private async void OnStopRec(object sender, RoutedEventArgs e)
            {
                if (App.IsRecording)
                {
                    // 停止录制并释放
                    await App.CleanupCaptureAsync();
                }
            }

    虽然与ScreenCapture结合用MediaCapture类记录屏幕不需要相机,但是由于MediaCapture类对摄像头有要求,因此需要打开清单文件,在“功能”选项卡页勾选“网络摄像机”,本例截取的图片存到相册目录中,录好的视频放在视频库中,故还要勾上“图片库”和“视频库”。

    运行程序后,就可以测试了。

        

    OK,完工,正好开饭。

    源码下载:http://files.cnblogs.com/files/tcjiaan/ScreenCaptureApp.zip

  • 相关阅读:
    Python3 input() 函数
    Python3 enumerate() 函数
    Python3 ascii() 函数
    Python3 sorted() 函数
    css sprite
    Java 理论与实践: 并发集合类
    关于 Java Collections API 您不知道的 5 件事,第 1 部分
    Treasure! Free Mobile Design Resources
    Indigo Studio
    ionic:Build mobile apps faster with the web technologies you know and love
  • 原文地址:https://www.cnblogs.com/tcjiaan/p/4292649.html
Copyright © 2011-2022 走看看