与众不同 windows phone (22) - Device(设备)之摄像头(硬件快门, 自动对焦, 实时修改捕获视频)...
生活随笔
收集整理的這篇文章主要介紹了
与众不同 windows phone (22) - Device(设备)之摄像头(硬件快门, 自动对焦, 实时修改捕获视频)...
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
原文:與眾不同 windows phone (22) - Device(設(shè)備)之?dāng)z像頭(硬件快門, 自動(dòng)對(duì)焦, 實(shí)時(shí)修改捕獲視頻)
[索引頁]
[源碼下載]
作者:webabcd
介紹
與眾不同 windows phone 7.5 (sdk 7.1) 之設(shè)備
- 硬件快門
- 自動(dòng)對(duì)焦、自動(dòng)對(duì)焦到指定的點(diǎn)
- 實(shí)時(shí)修改捕獲到的視頻幀
示例
1、演示如何響應(yīng)硬件快門
HardwareShutter.xaml
HardwareShutter.xaml.cs
/** 演示如何捕獲相機(jī)的硬件快門的相關(guān)事件* * CameraButtons.ShutterKeyHalfPressed - 硬件快門半按壓時(shí)所觸發(fā)的事件* CameraButtons.ShutterKeyPressed - 硬件快門全按壓時(shí)所觸發(fā)的事件* CameraButtons.ShutterKeyReleased - 硬件快門被釋放時(shí)所觸發(fā)的事件* * * 注:無論是拍照模式還是攝像模式,只有在攝像頭工作起來的時(shí)候,系統(tǒng)才能響應(yīng)硬件快門的相關(guān)事件*/using System; using System.Collections.Generic; using System.Linq; using System.Net; using System.Windows; using System.Windows.Controls; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Animation; using System.Windows.Shapes; using Microsoft.Phone.Controls;using Microsoft.Devices; using System.Windows.Navigation;namespace Demo.Device.Camera {public partial class HardwareShutter : PhoneApplicationPage{private PhotoCamera _camera;public HardwareShutter(){InitializeComponent();}protected override void OnNavigatedTo(NavigationEventArgs e){if (PhotoCamera.IsCameraTypeSupported(CameraType.Primary)){_camera = new PhotoCamera(CameraType.Primary);// 注冊(cè)硬件快門的相關(guān)事件CameraButtons.ShutterKeyHalfPressed += CameraButtons_ShutterKeyHalfPressed;CameraButtons.ShutterKeyPressed += CameraButtons_ShutterKeyPressed;CameraButtons.ShutterKeyReleased += CameraButtons_ShutterKeyReleased;// 相機(jī)模式下,必須將捕獲到的信息輸出到 UI 上,系統(tǒng)才能響應(yīng)硬件快門的事件(同理,攝像模式下,必須調(diào)用了 CaptureSource.Start() 之后系統(tǒng)才能響應(yīng)硬件快門的事件) videoBrush.SetSource(_camera);}}protected override void OnNavigatingFrom(NavigatingCancelEventArgs e){// 清理相關(guān)資源CameraButtons.ShutterKeyHalfPressed -= CameraButtons_ShutterKeyHalfPressed;CameraButtons.ShutterKeyPressed -= CameraButtons_ShutterKeyPressed;CameraButtons.ShutterKeyReleased -= CameraButtons_ShutterKeyReleased;}void CameraButtons_ShutterKeyHalfPressed(object sender, EventArgs e){lblMsg.Text = "快門半按壓";}void CameraButtons_ShutterKeyPressed(object sender, EventArgs e){lblMsg.Text = "快門全按壓";}void CameraButtons_ShutterKeyReleased(object sender, EventArgs e){lblMsg.Text = "快門被釋放";}} }
2、演示如何自動(dòng)對(duì)焦,以及如何自動(dòng)對(duì)焦到指定的點(diǎn)
Focus.xaml
Focus.xaml.cs
/** 演示如何自動(dòng)對(duì)焦,以及如何自動(dòng)對(duì)焦到指定的點(diǎn)* * PhotoCamera - 用于提供相機(jī)功能* Focus() - 讓相機(jī)自動(dòng)對(duì)焦* FocusAtPoint(double x, double y) - 自動(dòng)對(duì)焦到取景器上指定的點(diǎn)* x, y - 取景器上需要對(duì)焦的點(diǎn)的坐標(biāo),取景器左上角坐標(biāo)為 0,0,取景器右下角坐標(biāo)為 1,1* AutoFocusCompleted - 自動(dòng)對(duì)焦完成后所觸發(fā)的事件(事件參數(shù)為 CameraOperationCompletedEventArgs 類型)* * * CameraOperationCompletedEventArgs* Succeeded - 操作是否成功* Exception - 異常信息*/using System; using System.Collections.Generic; using System.Linq; using System.Net; using System.Windows; using System.Windows.Controls; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Animation; using System.Windows.Shapes; using Microsoft.Phone.Controls;using Microsoft.Devices; using System.Windows.Navigation;namespace Demo.Device.Camera {public partial class Focus : PhoneApplicationPage{private PhotoCamera _camera;public Focus(){InitializeComponent();}protected override void OnNavigatedTo(NavigationEventArgs e){if (PhotoCamera.IsCameraTypeSupported(CameraType.Primary)){// 實(shí)例化 PhotoCamera,注冊(cè)相關(guān)事件_camera = new PhotoCamera(CameraType.Primary);_camera.AutoFocusCompleted += _camera_AutoFocusCompleted;// 在 VideoBrush 上顯示攝像頭捕獲到的實(shí)時(shí)信息 videoBrush.SetSource(_camera);}}protected override void OnNavigatingFrom(NavigatingCancelEventArgs e){// 清理相關(guān)資源_camera.AutoFocusCompleted -= _camera_AutoFocusCompleted;}void _camera_AutoFocusCompleted(object sender, CameraOperationCompletedEventArgs e){if (e.Succeeded){Deployment.Current.Dispatcher.BeginInvoke(delegate(){lblMsg.Text = "自動(dòng)對(duì)焦完成";});}else{Deployment.Current.Dispatcher.BeginInvoke(delegate(){lblMsg.Text = "自動(dòng)對(duì)焦失敗";});}}private void btnFocus_Click(object sender, RoutedEventArgs e){if (_camera.IsFocusSupported == true){try{// 開始自動(dòng)對(duì)焦 _camera.Focus();lblMsg.Text = "開始自動(dòng)對(duì)焦";}catch (Exception ex){this.Dispatcher.BeginInvoke(delegate(){lblMsg.Text = "自動(dòng)對(duì)焦失敗:" + ex.ToString();});}}else{this.Dispatcher.BeginInvoke(delegate(){lblMsg.Text = "相機(jī)不支持自動(dòng)對(duì)焦";});}}private void canvas_Tap(object sender, System.Windows.Input.GestureEventArgs e){if (_camera != null){if (_camera.IsFocusAtPointSupported == true){try{// 獲取用戶觸摸的點(diǎn)相對(duì)于 canvas 的坐標(biāo)Point tapLocation = e.GetPosition(canvas);// 計(jì)算觸摸點(diǎn)映射于取景器上的坐標(biāo)(取景器左上角為0,0,右下角為1,1)double focusXPercent = tapLocation.X / canvas.Width;double focusYPercent = tapLocation.Y / canvas.Height;// 自動(dòng)對(duì)焦到指定的點(diǎn) _camera.FocusAtPoint(focusXPercent, focusYPercent);this.Dispatcher.BeginInvoke(delegate(){lblMsg.Text = String.Format("自動(dòng)對(duì)焦到指定的點(diǎn){0}X:{1:N2}{2}Y:{3:N2}", System.Environment.NewLine, focusXPercent, System.Environment.NewLine, focusYPercent);});}catch (Exception ex){this.Dispatcher.BeginInvoke(delegate(){lblMsg.Text = "自動(dòng)對(duì)焦到指定的點(diǎn)失敗:" + ex.ToString();});}}else{this.Dispatcher.BeginInvoke(delegate(){lblMsg.Text = "相機(jī)不支持自動(dòng)對(duì)焦到指定的點(diǎn)";});}}}} }
3、演示如何實(shí)時(shí)修改捕獲到的視頻幀
LiveAlter.xaml
LiveAlter.xaml.cs
/** 演示如何實(shí)時(shí)處理攝像頭捕獲到的圖像* * PhotoCamera - 用于提供相機(jī)功能* PreviewResolution - 捕獲到的圖像的當(dāng)前的分辨率(返回 System.Windows.Size 類型的結(jié)構(gòu)體,其包含 Width 和 Height 字段)* GetPreviewBufferArgb32(int[] pixelData) - 將當(dāng)前捕獲到的圖像的 ARGB 數(shù)據(jù)復(fù)制到指定的緩沖區(qū)中* * * 注:* Resolution 指的是相機(jī)設(shè)置的分辨率* PreviewResolution 指的是系統(tǒng)針對(duì)顯示設(shè)備縮放后的真實(shí)分辨率* 因?yàn)橥ǔO鄼C(jī)能夠拍攝大于設(shè)備顯示器的分辨率的圖像,所以實(shí)時(shí)顯示攝像頭捕獲到的圖像時(shí),系統(tǒng)會(huì)對(duì)其分辨率進(jìn)行優(yōu)化,PreviewResolution 就是優(yōu)化后的數(shù)據(jù)*/using System; using System.Collections.Generic; using System.Linq; using System.Net; using System.Windows; using System.Windows.Controls; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Animation; using System.Windows.Shapes; using Microsoft.Phone.Controls;using Microsoft.Devices; using System.Threading; using System.Windows.Media.Imaging; using System.Windows.Navigation;namespace Demo.Device.Camera {public partial class LiveAlter : PhoneApplicationPage{private PhotoCamera _camera = new PhotoCamera();// 用于顯示處理后的圖像private WriteableBitmap _writeableBitmap;// 有信號(hào)private static ManualResetEvent _manualReset = new ManualResetEvent(true);public LiveAlter(){InitializeComponent();}protected override void OnNavigatedTo(NavigationEventArgs e){if (PhotoCamera.IsCameraTypeSupported(CameraType.Primary)){// 實(shí)例化 PhotoCamera,并注冊(cè)相關(guān)事件_camera = new PhotoCamera(CameraType.Primary);_camera.Initialized += _camera_Initialized;videoBrush.SetSource(_camera);}else{this.Dispatcher.BeginInvoke(delegate(){lblMsg.Text = "設(shè)備不支持主攝像頭";});}}protected override void OnNavigatingFrom(NavigatingCancelEventArgs e){// 清理資源if (_camera != null){_camera.Dispose();_camera.Initialized -= _camera_Initialized;}}void _camera_Initialized(object sender, CameraOperationCompletedEventArgs e){// 新開線程去執(zhí)行實(shí)時(shí)處理圖片的任務(wù)Thread thread = new Thread(CameraToGray);thread.Start();this.Dispatcher.BeginInvoke(delegate(){// 讓 Image 顯示 WriteableBitmap 中的內(nèi)容_writeableBitmap = new WriteableBitmap((int)_camera.PreviewResolution.Width, (int)_camera.PreviewResolution.Height);imgEffect.Source = _writeableBitmap;});}private void CameraToGray(){// 初始化緩沖區(qū)大小:圖像寬和高的乘積int[] buffer = new int[(int)_camera.PreviewResolution.Width * (int)_camera.PreviewResolution.Height];try{while (true){// 實(shí)例化 ManualResetEvent 的時(shí)候,指定了其是有信號(hào)的_manualReset.WaitOne(); // 有信號(hào)則不阻塞,無信號(hào)則阻塞// 將當(dāng)前捕獲到的圖像以 ARGB 的方式寫入到緩沖區(qū) _camera.GetPreviewBufferArgb32(buffer);// 將緩沖區(qū)中的每一個(gè)像素的顏色都轉(zhuǎn)換為灰色系for (int i = 0; i < buffer.Length; i++){buffer[i] = ColorToGray(buffer[i]);}_manualReset.Reset(); // 設(shè)置為無信號(hào) Deployment.Current.Dispatcher.BeginInvoke(delegate(){// 將處理后的圖像數(shù)據(jù)保存到 WriteableBitmap 對(duì)象buffer.CopyTo(_writeableBitmap.Pixels, 0);// 重新繪制整個(gè) WriteableBitmap 對(duì)象 _writeableBitmap.Invalidate();lblMsg.Text = "圖像實(shí)時(shí)處理中";_manualReset.Set(); // 設(shè)置為有信號(hào) });}}catch (Exception ex){this.Dispatcher.BeginInvoke(delegate(){lblMsg.Text = "圖像處理失敗:" + ex.ToString();});}}// 將指定的顏色轉(zhuǎn)換成灰色系的顏色private int ColorToGray(int color){int gray = 0;int a = color >> 24;int r = (color & 0x00ff0000) >> 16;int g = (color & 0x0000ff00) >> 8;int b = (color & 0x000000ff);if ((r == g) && (g == b)){gray = color;}else{int i = (7 * r + 38 * g + 19 * b + 32) >> 6;gray = ((a & 0xFF) << 24) | ((i & 0xFF) << 16) | ((i & 0xFF) << 8) | (i & 0xFF);}return gray;}} }
OK
[源碼下載]
總結(jié)
以上是生活随笔為你收集整理的与众不同 windows phone (22) - Device(设备)之摄像头(硬件快门, 自动对焦, 实时修改捕获视频)...的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: iOS 7开发快速入门
- 下一篇: Guava关于JAVA中系统组件之间交互