Windows Phone7在程序中直接开启摄像头(非选择器)

jopen 12年前

很久没有做wp7开发了,最近看到微信在程序中打开摄像头进行二维码,扫描忽然性起,遍做了下研究,然后发现原来是用到了using Microsoft.Devices;命名空间下的

PhotoCamera 对象,把它赋值给VideoBrush 就可以了

但是真机测试发现图像和真实图像是颠倒90度的,后来才发现,原来PhotoCamera 会将图像旋转一个角度,我们只要获取到这个角度做下处理就可以了

 double d= photocamera.Orientation;在这里获取到图像顺时针旋转的角度
            video.RelativeTransform =  在这个把他给前台的VideoBrush 就可以了
                new CompositeTransform() { CenterX = 0.5, CenterY = 0.5, Rotation = d };

 

做了个小demo,在此分享给大家

下面的是前台xaml页面代码

<phone:PhoneApplicationPage       x:Class="MyPocketBook.MainPage"      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"      mc:Ignorable="d" d:DesignWidth="480" d:DesignHeight="768"      FontFamily="{StaticResource PhoneFontFamilyNormal}"      FontSize="{StaticResource PhoneFontSizeNormal}"      Foreground="{StaticResource PhoneForegroundBrush}"      SupportedOrientations="Portrait" Orientation="Portrait"      shell:SystemTray.IsVisible="True">          <!--LayoutRoot 是包含所有页面内容的根网格-->      <Grid x:Name="LayoutRoot" Background="Transparent">          <Grid.RowDefinitions>              <RowDefinition Height="Auto"/>              <RowDefinition Height="*"/>          </Grid.RowDefinitions>              <!--TitlePanel 包含应用程序的名称和页标题-->          <StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">              <TextBlock x:Name="ApplicationTitle" Text="" Style="{StaticResource PhoneTextNormalStyle}"/>              <TextBlock x:Name="PageTitle" Text="页面名称" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>          </StackPanel>              <!--ContentPanel - 在此处放置其他内容-->          <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">              <Grid.RowDefinitions>                  <RowDefinition Height="516"></RowDefinition>                  <RowDefinition Height="91*"></RowDefinition>              </Grid.RowDefinitions>              <Grid x:Name="butlist" Grid.Row="1">                  <Grid.ColumnDefinitions>                      <ColumnDefinition Width="*"></ColumnDefinition>                      <ColumnDefinition Width="*"></ColumnDefinition>                  </Grid.ColumnDefinitions>                  <Button Name="btnstart" Content="开启闪光灯" Grid.Column="0" Width="200"  Click="btnok_Click"></Button>                  <Button Name="btnstop" Content="关闭闪光灯" Grid.Column="1" Width="200"  Click="btnstop_Click"></Button>              </Grid>              <Rectangle Grid.Row="0">                  <Rectangle.Fill>                      <VideoBrush x:Name="video"></VideoBrush>                  </Rectangle.Fill>              </Rectangle>          </Grid>      </Grid>         <!--演示 ApplicationBar 用法的示例代码-->      <!--<phone:PhoneApplicationPage.ApplicationBar>          <shell:ApplicationBar IsVisible="True" IsMenuEnabled="True">              <shell:ApplicationBarIconButton IconUri="/Images/appbar_button1.png" Text="按钮 1"/>              <shell:ApplicationBarIconButton IconUri="/Images/appbar_button2.png" Text="按钮 2"/>              <shell:ApplicationBar.MenuItems>                  <shell:ApplicationBarMenuItem Text="菜单项 1"/>                  <shell:ApplicationBarMenuItem Text="菜单项 2"/>              </shell:ApplicationBar.MenuItems>          </shell:ApplicationBar>      </phone:PhoneApplicationPage.ApplicationBar>-->      </phone:PhoneApplicationPage>

这里是后台代码,都加了注释的

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;      namespace MyPocketBook  {      public partial class MainPage : PhoneApplicationPage      {          PhotoCamera photocamera = new PhotoCamera();                    // 构造函数          public MainPage()          {              InitializeComponent();              //相机初始化事件              photocamera.Initialized += new EventHandler<CameraOperationCompletedEventArgs>(photocamera_Initialized);              //拍摄序列完成时发生              photocamera.CaptureCompleted += new EventHandler<CameraOperationCompletedEventArgs>(photocamera_CaptureCompleted);              //当有缩略图像可用时发生              photocamera.CaptureThumbnailAvailable += new EventHandler<ContentReadyEventArgs>(photocamera_CaptureThumbnailAvailable);              double d= photocamera.Orientation;              video.RelativeTransform =                  new CompositeTransform() { CenterX = 0.5, CenterY = 0.5, Rotation = d };                  video.SetSource(photocamera);          }              void photocamera_CaptureThumbnailAvailable(object sender, ContentReadyEventArgs e)          {              throw new NotImplementedException();          }              void photocamera_CaptureCompleted(object sender, CameraOperationCompletedEventArgs e)          {              throw new NotImplementedException();          }              void photocamera_Initialized(object sender, CameraOperationCompletedEventArgs e)          {                       }          bool i = true;          private void btnok_Click(object sender, RoutedEventArgs e)          {              photocamera.FlashMode = FlashMode.On;              photocamera.Focus();          }              private void btnstop_Click(object sender, RoutedEventArgs e)          {              i = false;              photocamera.FlashMode = FlashMode.Off;          }               }  }