標籤:style class blog code http tar
原文: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;}
更多的使用方法可到諾基亞協助中心查看:連結