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; }
更多的使用方法可到诺基亚帮助中心查看:链接