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

时间:2014-06-13 17:33:12   收藏:0   阅读:367

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

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

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

 


 

(1)拍摄相片

1)CaptureElement

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

bubuko.com,布布扣
<Grid>
    <CaptureElement x:Name="capturePhotoElement"/>
</Grid>

<Page.BottomAppBar>
    <CommandBar>
        <AppBarButton x:Name="btnCapturePhoto"
                      Icon="Camera" Label="Capture"
                      Click="btnCapturePhoto_Click"/>
    </CommandBar>
</Page.BottomAppBar>
bubuko.com,布布扣

 

2)MediaCapture

MediaCapture 是控制拍摄的重要类。

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

bubuko.com,布布扣
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;
}
bubuko.com,布布扣

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

bubuko.com,布布扣
private async void btnCapturePhoto_Click(object sender, RoutedEventArgs e)
{
    var photo = await KnownFolders.PicturesLibrary.CreateFileAsync("photo.jpg", CreationCollisionOption.GenerateUniqueName);

    await photoCapture.CapturePhotoToStorageFileAsync(imgEncodingProperties, photo);
}
bubuko.com,布布扣

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

bubuko.com,布布扣
HardwareButtons.CameraHalfPressed += HardwareButtons_CameraHalfPressed;

async void HardwareButtons_CameraHalfPressed(object sender, CameraEventArgs e)
{
    await photoCapture.VideoDeviceController.FocusControl.FocusAsync();
}
bubuko.com,布布扣

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

bubuko.com,布布扣
protected override void OnNavigatedFrom(NavigationEventArgs e)
{
    if( photoCapture != null )
    {
        photoCapture.Dispose();
        photoCapture = null;
    }
}
bubuko.com,布布扣

 

(2)编辑相片

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

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

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

bubuko.com,布布扣
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;
}
bubuko.com,布布扣

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

Windows Phone 8.1 多媒体(1):相片,布布扣,bubuko.com

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!