標籤:style blog http color os 使用 io strong ar
昨天,我非常馬虎地給大家說了有關處理物理網路攝影機翻轉的話題,今天,還是這個話題,而且內容不差,只是為了完整性,順便也提供了運行時API的版本,其實實現起來與SL架構版本差不多,畢竟這兩個架構都有不少API是共用的。
首先,開啟資訊清單檔,在“應用程式”選項卡上,把“支援的旋轉”右面的橫向選上,其他的不要選,只選橫向。
然後切換到“功能”選項卡,把 網路攝像機 和 圖片庫 勾上,因為我們要用到它們。
同樣,使用MediaCapture類時要注意,在應用程式掛起時把它釋放掉,而在應用程式啟動或繼續運行時,對其進行初始化。
在App類中加入以下代碼:
/// <summary> /// 視頻捕捉對象 /// </summary> public MediaCapture TheCapture { get; private set; } /// <summary> /// 初始化網路攝影機 /// </summary> private async Task InitializeCapture () { TheCapture = new MediaCapture(); // 尋找後置網路攝影機 var deviceCollection = await DeviceInformation.FindAllAsync(DeviceClass.VideoCapture); DeviceInformation backCamera = deviceCollection.FirstOrDefault(d => d.EnclosureLocation.Panel == Windows.Devices.Enumeration.Panel.Back); if (backCamera != null) { MediaCaptureInitializationSettings setting = new MediaCaptureInitializationSettings(); setting.AudioDeviceId = ""; setting.VideoDeviceId = backCamera.Id; await TheCapture.InitializeAsync(setting); } else { await TheCapture.InitializeAsync(); } } /// <summary> /// 清理網路攝影機相關資源 /// </summary> private void CleanupCapture () { if (TheCapture != null) { TheCapture.Dispose(); TheCapture = null; } }
InitializeCapture方法用來初始化捕捉組件,CleanupCapture方法則用來清理。InitializeCapture方法使用了Task,表示它可以非同步等待,因為稍後要在Launch中調用,在導航到首頁前調用,如果不進行非同步等待的話,應用程式會在MediaCapture未初始化之前就進入了首頁,而在首頁中開啟預覽就會發生異常,因此,通過非同步等待,可以確保在進入首頁前完成MediaCapture對象的初始化。
在OnLaunched方法中加入以下代碼來初始化捕捉組件。
protected async override void OnLaunched(LaunchActivatedEventArgs e) { // 隱藏狀態列 Windows.UI.ViewManagement.StatusBar statusbar = Windows.UI.ViewManagement.StatusBar.GetForCurrentView(); await statusbar.HideAsync();#if DEBUG if (System.Diagnostics.Debugger.IsAttached) { this.DebugSettings.EnableFrameRateCounter = true; }#endif await this.InitializeCapture(); ……
使用StatusBar類是為了把系統欄隱藏起來,系統表徵圖欄就是手機頂部那條表徵圖欄,顯示訊號、時間等資訊的地方。
在應用程式掛起時,要釋放MediaCapture對象,故要處理Suspending事件。
private async void OnSuspending(object sender, SuspendingEventArgs e) { var deferral = e.SuspendingOperation.GetDeferral(); // TODO: 儲存應用程式狀態並停止任何後台活動 // 停止拍攝預覽 await TheCapture.StopPreviewAsync(); this.CleanupCapture(); deferral.Complete(); }
當應用程式從掛起(如切換到其他應用,或回到開始畫面)狀態中恢複時(回到應用程式),會引發Resuming事件,處理該事件並重新初始化MediaCapture對象。
async void OnResuming ( object sender, object e ) { await this.InitializeCapture(); Frame root = Window.Current.Content as Frame; if (root != null) { MainPage page = root.Content as MainPage; if (page != null) await page.SetCaptureSourceAsync(); } }
SetCaptureSourceAsync方法是在MainPage頁面類中定義的一個方法,作用是擷取MediaCapture對象的引用,並開始拍攝預覽。方法的定義如下:
public async System.Threading.Tasks.Task SetCaptureSourceAsync () { capture = (App.Current as App).TheCapture; ce.Source = capture; await capture.StartPreviewAsync(); }
在Silverlight架構中,是通過VideoBrush來顯示網路攝影機的預覽畫面的,而在運行時API中,從RT應用程式中移植了CaptureElement類,該類有一個Source屬性,用於設定關聯的MediaCapture執行個體,這樣就可以在CaptureElement可視化元素中看到網路攝影機的預覽效果了,接著調用StartPreviewAsync方法開始預覽。
拍照和儲存照片的方法和前面的樣本差不多,不過,運行時API可以使用Windows.Storage中的類來進行檔案處理。
通過以下代碼獲得圖片庫檔案夾的引用:
StorageFolder picDir = KnownFolders.PicturesLibrary;
其他流程和前面的樣本一樣,先把照片捕捉到流中,再通過解碼/編碼的方法來調整圖片的旋轉方向。不過,這裡還要提及一個細節:
下面是樣本的:http://files.cnblogs.com/tcjiaan/CameraRTTestApp.zip
【WP 8.1開發】解決網路攝影機翻轉問題(RuntimeApp篇)