zoukankan      html  css  js  c++  java
  • Windows Phone 8.1 多媒体(1):相片

    Windows Phone 8.1 多媒体(1):相片

    Windows Phone 8.1 多媒体(2):视频

    Windows Phone 8.1 多媒体(3):音乐


    (1)拍摄相片

    1)CaptureElement

    CaptureElement 是放在应用界面上预览拍照的控件:

    <Grid>
        <CaptureElement x:Name="capturePhotoElement"/>
    </Grid>
    
    <Page.BottomAppBar>
        <CommandBar>
            <AppBarButton x:Name="btnCapturePhoto"
                          Icon="Camera" Label="Capture"
                          Click="btnCapturePhoto_Click"/>
        </CommandBar>
    </Page.BottomAppBar>

     

    2)MediaCapture

    MediaCapture 是控制拍摄的重要类。

    首先初始化 MediaCapture,并将 CaptureElement 的 Source 设为 该 MediaCapture:

    MediaCapture photoCapture;
    ImageEncodingProperties imgEncodingProperties;
    
    protected override async void OnNavigatedTo(NavigationEventArgs e)
    {
        capturePhotoElement.Source = await Initialize();
    
        await photoCapture.StartPreviewAsync();
    }
    
    private async Task<MediaCapture> Initialize()
    {
        photoCapture = new MediaCapture();
        await photoCapture.InitializeAsync();
    
        photoCapture.VideoDeviceController.PrimaryUse = CaptureUse.Photo;
    
        imgEncodingProperties = ImageEncodingProperties.CreateJpeg();
        imgEncodingProperties.Width = 640;
        imgEncodingProperties.Height = 480;
    
        return photoCapture;
    }

    然后在按下某个按钮的时候完成拍摄:

    private async void btnCapturePhoto_Click(object sender, RoutedEventArgs e)
    {
        var photo = await KnownFolders.PicturesLibrary.CreateFileAsync("photo.jpg", CreationCollisionOption.GenerateUniqueName);
    
        await photoCapture.CapturePhotoToStorageFileAsync(imgEncodingProperties, photo);
    }

    也可以添加手机实体按键的事件:

    HardwareButtons.CameraHalfPressed += HardwareButtons_CameraHalfPressed;
    
    async void HardwareButtons_CameraHalfPressed(object sender, CameraEventArgs e)
    {
        await photoCapture.VideoDeviceController.FocusControl.FocusAsync();
    }

    最后记得在离开页面时释放 MediaCapture 资源:

    protected override void OnNavigatedFrom(NavigationEventArgs e)
    {
        if( photoCapture != null )
        {
            photoCapture.Dispose();
            photoCapture = null;
        }
    }

    (2)编辑相片

    我在这里使用了 Nokia Imaging SDK 和 WritableBitmapEx 库,可在 Nuget 中搜索并安装。

    注意要将配置管理器中的 CPU 改成 ARM,否则 Nokia Imaging SDK 将不可用。

    使用方法非常简单,比如以下为一张图片添加滤镜:

    WriteableBitmap originBitmap;
    WriteableBitmap editedBitmap;
    
    private async void editButton_Click(object sender, RoutedEventArgs e)
    {
        var imageSource = new BitmapImageSource(originBitmap.AsBitmap());
    
        using( var effect = new FilterEffect(imageSource) )
        {
            var filter = new AntiqueFilter();
    
            effect.Filters = new[] { filter };
    
            var renderer = new WriteableBitmapRenderer(effect, originBitmap);
            editedBitmap = await renderer.RenderAsync();
    
            editedBitmap.Invalidate();
        }
    
        myImage.Source = editedBitmap;
    }

    更多的使用方法可到诺基亚帮助中心查看:链接

  • 相关阅读:
    程序员的7中武器
    需要强化的知识
    微软中国联合小i推出MSN群Beta 不需任何插件
    XML Notepad 2006 v2.0
    Sandcastle August 2006 Community Technology Preview
    [推荐] TechNet 广播 SQL Server 2000完结篇
    《太空帝国 4》(Space Empires IV)以及 xxMod 英文版 中文版 TDM Mod 英文版 中文版
    IronPython 1.0 RC2 更新 1.0.60816
    Microsoft .NET Framework 3.0 RC1
    《Oracle Developer Suite 10g》(Oracle Developer Suite 10g)V10.1.2.0.2
  • 原文地址:https://www.cnblogs.com/xiaoshi3003/p/3784030.html
Copyright © 2011-2022 走看看