昨天,我非常馬虎地給大家說了有關處理物理攝像頭翻轉的話題,今天,還是這個話題,而且內容不差,只是為了完整性,順便也提供了運行時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
