鸿 网 互 联 www.68idc.cn

Device(设备)之摄像头(拍摄照片, 录制视频)

来源:互联网 作者:佚名 时间:2012-11-12 15:00
与众不同 windows phone (21) - Device(设备)之摄像头(拍摄照片, 录制视频)Posted on [索引页] [源码下载] 与众不同 windows phone (21) - Device(设备)之摄像头(拍摄照片, 录制视频) 作者:webabcd 介绍 与众不同 windows phone 7.5 (sdk 7.1) 之设

与众不同 windows phone (21) - Device(设备)之摄像头(拍摄照片, 录制视频) Posted on

[索引页]
[源码下载]


与众不同 windows phone (21) - Device(设备)之摄像头(拍摄照片, 录制视频



作者:webabcd


介绍
与众不同 windows phone 7.5 (sdk 7.1) 之设备

  • 用摄像头拍摄照片
  • 用摄像头录制视频


  • 示例
    1、演示如何使用摄像头拍摄照片
    ImageDemo.xaml

    <phone:PhoneApplicationPage x:Class="Demo.Device.Camera.ImageDemo" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone" xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" FontFamily FontSize Foreground SupportedOrientations mc:Ignorable shell:SystemTray.IsVisible

    ImageDemo.xaml.cs

    /* * 演示如何使用摄像头拍摄照片 * * PhotoCamera - 用于提供相机功能 * CameraType - 摄像头的类型(Microsoft.Devices.CameraType 枚举) * Primary - 主摄像头 * FrontFacing - 前置摄像头 * AvailableResolutions - 获取摄像头的可用分辨率集合(返回一个 IEnumerable<Size> 类型的对象) * FlashMode - 相机的闪光灯模式(Microsoft.Devices.FlashMode 枚举) * On - 启用闪光灯 * Off - 禁用闪光灯 * Auto - 自动闪光灯 * RedEyeReduction - 消除红眼闪光灯 * Resolution - 相机的分辨率 * Orientation - 取景器与相机传感器对齐所需要顺时针旋转的度数(只读) * * CaptureImage() - 拍摄相机当前捕获到的照片 * IsFlashModeSupported(FlashMode mode) - 相机是否支持指定的闪光灯模式 * * Initialized - 相机初始化时触发的事件(事件参数为 CameraOperationCompletedEventArgs 类型) * CaptureStarted - 相机开始捕获图片时触发的事件 * CaptureCompleted - 相机捕获图片完成时触发的事件(事件参数为 CameraOperationCompletedEventArgs 类型) * CaptureImageAvailable - 获得了捕获到的图片时触发的事件(事件参数为 ContentReadyEventArgs 类型) * CaptureThumbnailAvailable - 获得了捕获到的图片的缩略图时触发的事件(事件参数为 ContentReadyEventArgs 类型) * * * CameraOperationCompletedEventArgs * Succeeded - 操作是否成功 * Exception - 异常信息 * * * ContentReadyEventArgs * ImageStream - 获取到的图像流 * * * 注:如果要求只使用前置摄像头的话,则在 manifest 中添加 <Capability/> */ 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.IO; using System.IO.IsolatedStorage; using Microsoft.Xna.Framework.Media; using System.Windows.Navigation; using System.Windows.Media.Imaging; namespace Demo.Device.Camera { ImageDemo : PhoneApplicationPage { private PhotoCamera _camera; _currentResolutionIndex = 0; public ImageDemo() { InitializeComponent(); } OnNavigatedTo(NavigationEventArgs e) { (PhotoCamera.IsCameraTypeSupported(CameraType.Primary)) { // 获取主摄像头,并注册相关事件 _camera = new PhotoCamera(CameraType.Primary); _camera.Initialized += _camera_Initialized; _camera.CaptureStarted += _camera_CaptureStarted; _camera.CaptureCompleted += _camera_CaptureCompleted; _camera.CaptureImageAvailable += _camera_CaptureImageAvailable; _camera.CaptureThumbnailAvailable += _camera_CaptureThumbnailAvailable; // 在 VideoBrush 上显示摄像头捕获到的实时信息 videoBrush.SetSource(_camera); lblMsg.Text = ; } else { lblMsg.Text = ; btnShutter.IsEnabled = false; btnFlash.IsEnabled = false; btnResolution.IsEnabled = false; } } OnNavigatingFrom(NavigatingCancelEventArgs e) { (_camera != null) { _camera.Dispose(); _camera.Initialized -= _camera_Initialized; _camera.CaptureStarted -= _camera_CaptureStarted; _camera.CaptureCompleted -= _camera_CaptureCompleted; _camera.CaptureImageAvailable -= _camera_CaptureImageAvailable; _camera.CaptureThumbnailAvailable -= _camera_CaptureThumbnailAvailable; } } void _camera_Initialized(object sender, CameraOperationCompletedEventArgs e) { if (e.Succeeded) { this.Dispatcher.BeginInvoke(delegate() { // 初始化闪光灯模式 _camera.FlashMode = FlashMode.Off; btnFlash.Content = ; // 初始化分辨率设置 _camera.Resolution = _camera.AvailableResolutions.ElementAt<Size>(_currentResolutionIndex); btnResolution.Content = + _camera.AvailableResolutions.ElementAt<Size>(_currentResolutionIndex); lblMsg.Text = ; }); } } void _camera_CaptureStarted(object sender, EventArgs e) { .Dispatcher.BeginInvoke(delegate() { lblMsg.Text = ; }); } void _camera_CaptureCompleted(object sender, CameraOperationCompletedEventArgs e) { .Dispatcher.BeginInvoke(delegate() { lblMsg.Text = ; }); } void _camera_CaptureImageAvailable(object sender, ContentReadyEventArgs e) { .Dispatcher.BeginInvoke(delegate() { var bitmapImage = new BitmapImage(); bitmapImage.SetSource(e.ImageStream); imgCapture.Source = bitmapImage; }); } public void _camera_CaptureThumbnailAvailable(object sender, ContentReadyEventArgs e) { .Dispatcher.BeginInvoke(delegate() { var bitmapImage = new BitmapImage(); bitmapImage.SetSource(e.ImageStream); imgCaptureThumbnail.Source = bitmapImage; }); } private void btnShutter_Click(object sender, RoutedEventArgs e) { if (_camera != null) { try { // 拍摄摄像头当前捕获的图片 _camera.CaptureImage(); } catch (Exception ex) { lblMsg.Text = + ex.Message; } } } btnResolution_Click(object sender, RoutedEventArgs e) { if (++_currentResolutionIndex >= _camera.AvailableResolutions.Count()) _currentResolutionIndex = 0; _camera.Resolution = _camera.AvailableResolutions.ElementAt<Size>(_currentResolutionIndex); btnResolution.Content = + _camera.AvailableResolutions.ElementAt<Size>(_currentResolutionIndex); } btnFlash_Click(object sender, RoutedEventArgs e) { switch (_camera.FlashMode) { case FlashMode.Off: if (_camera.IsFlashModeSupported(FlashMode.On)) { _camera.FlashMode = FlashMode.On; btnFlash.Content = ; } break; case FlashMode.On: if (_camera.IsFlashModeSupported(FlashMode.RedEyeReduction)) { _camera.FlashMode = FlashMode.RedEyeReduction; btnFlash.Content = ; } else if (_camera.IsFlashModeSupported(FlashMode.Auto)) { _camera.FlashMode = FlashMode.Auto; btnFlash.Content = ; } else { _camera.FlashMode = FlashMode.Off; btnFlash.Content = ; } break; case FlashMode.RedEyeReduction: if (_camera.IsFlashModeSupported(FlashMode.Auto)) { _camera.FlashMode = FlashMode.Auto; btnFlash.Content = ; } else { _camera.FlashMode = FlashMode.Off; btnFlash.Content = ; } break; case FlashMode.Auto: if (_camera.IsFlashModeSupported(FlashMode.Off)) { _camera.FlashMode = FlashMode.Off; btnFlash.Content = ; } break; } } } }


    2、演示如何使用摄像头录制视频
    VideoDemo.xaml

    网友评论
    <