zoukankan      html  css  js  c++  java
  • Windows Phone开发(23):启动器与选择器之CameraCaptureTask和PhotoChooserTask 转:http://blog.csdn.net/tcjiaan/article/details/7396621

    这两个组件都属于选择器,而且它们也有很多相似的地方,最明显的上一点,它们都是用来选择图片。

    一、CameraCaptureTask选择器。

    它用于启动照相机,当你拍下照片后,自动把照的字节流返回给调用方应用程序。前文说过,启动器和选择的使用方法和步骤都是一样的。对于CameraCaptureTask组件也如此,不过注意的一点是,处理Completed事件时一定要记住,尽可能的使用页面类的Dispatcher.BeginInvoke方法,因为异步回调直接访问UI元素是不安全的,极有可能会引发异常,但我不是说绝对。

    1. <Grid>  
    2.     <Grid.RowDefinitions>  
    3.         <RowDefinition Height="*"/>  
    4.         <RowDefinition Height="auto"/>  
    5.     </Grid.RowDefinitions>  
    6.     <Image x:Name="img" Grid.Row="0" Stretch="Uniform"  
    7.            HorizontalAlignment="Stretch"  
    8.            VerticalAlignment="Stretch"/>  
    9.     <Button x:Name="btnCamera" Grid.Row="1"  
    10.             Content="启动相机程序" Click="btnCamera_Click"/>  
    11. </Grid>  


     

    1. using System;  
    2. using System.Collections.Generic;  
    3. using System.Linq;  
    4. using System.Net;  
    5. using System.Windows;  
    6. using System.Windows.Controls;  
    7. using System.Windows.Documents;  
    8. using System.Windows.Input;  
    9. using System.Windows.Media;  
    10. using System.Windows.Media.Animation;  
    11. using System.Windows.Shapes;  
    12. using Microsoft.Phone.Controls;  
    13. // 引入以下命名空间。  
    14. using Microsoft.Phone.Tasks;  
    15. using System.Windows.Media.Imaging;  
    16.   
    17. namespace PhoneApp1  
    18. {  
    19.     public partial class MainPage : PhoneApplicationPage  
    20.     {  
    21.         // 第一步,声明类级别的局部变量,并实例化。  
    22.         CameraCaptureTask MyCamera = new CameraCaptureTask();  
    23.   
    24.         // 构造函数  
    25.         public MainPage()  
    26.         {  
    27.             InitializeComponent();  
    28.   
    29.             // 第二步,在页面构造函数中注册完成回调事件  
    30.             MyCamera.Completed += new EventHandler<PhotoResult>(MyCamera_Completed);  
    31.         }  
    32.   
    33.         private void btnCamera_Click(object sender, RoutedEventArgs e)  
    34.         {  
    35.             // 第三步,显示组件  
    36.             MyCamera.Show();  
    37.         }  
    38.   
    39.         // 第四步,处理事返回结果  
    40.         void MyCamera_Completed(object sender, PhotoResult e)  
    41.         {  
    42.             // 确定用户确认了还是取消了操作。  
    43.             if (e.TaskResult == TaskResult.OK)  
    44.             {  
    45.                 // 从返回的流中创建图象  
    46.                 BitmapImage bmp = new BitmapImage();  
    47.                 try  
    48.                 {  
    49.                     bmp.SetSource(e.ChosenPhoto);  
    50.                     // 把图象作为Image控件的源。  
    51.                     // 防止异步回调直接访问UI元素,故应使用BeginInvoke方法。  
    52.                     Dispatcher.BeginInvoke(() =>  
    53.                     {  
    54.                         this.img.Source = bmp;  
    55.                     });  
    56.                 }  
    57.                 catch (Exception ex)  
    58.                 {  
    59.                     MessageBox.Show(ex.Message);  
    60.                 }  
    61.   
    62.             }  
    63.         }  
    64.     }  
    65. }  


     

    当然,在模拟器中你是不能进行拍摄的,但可以进行模拟操作,也就是说无论你拍的什么,最后都是返回同一张照片。

    二、PhotoChooserTask选择器。

    这个选择器已经包含CameraCaptureTask的功能,当然,它主要是为了选择图片。

    1、ShowCamera属性设置是否显示可以让用户启动相机的按钮;

    2、PixelHeight:选择图片后将其裁剪的高度;

    3、PixelWidth属性与上面相同,裁剪宽度。

    照片被选择后,以流的形式返回,驼过Completed事件的参数PhotoResult的ChosenPhoto属性获取。

    1. <Grid>  
    2.     <Grid.RowDefinitions>  
    3.         <RowDefinition Height="*"/>  
    4.         <RowDefinition Height="auto"/>  
    5.     </Grid.RowDefinitions>  
    6.     <Image x:Name="img" Stretch="Uniform" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Grid.Row="0"/>  
    7.     <Grid Grid.Row="1">  
    8.         <Grid.ColumnDefinitions>  
    9.             <ColumnDefinition Width="auto"/>  
    10.             <ColumnDefinition Width="auto"/>  
    11.             <ColumnDefinition Width="auto"/>  
    12.             <ColumnDefinition Width="auto"/>  
    13.         </Grid.ColumnDefinitions>  
    14.         <Grid.RowDefinitions>  
    15.             <RowDefinition Height="auto"/>  
    16.             <RowDefinition Height="auto"/>  
    17.         </Grid.RowDefinitions>  
    18.         <TextBlock Grid.Column="0" Grid.Row="0" Text="高度:"/>  
    19.         <TextBlock Grid.Column="2" Grid.Row="0" Text="宽度:"/>  
    20.         <TextBox x:Name="txtHeight" Grid.Column="1"  
    21.                  Grid.Row="0" Width="160" Height="auto" FontSize="20">  
    22.             <TextBox.InputScope>  
    23.                 <InputScope>  
    24.                     <InputScopeName NameValue="Number"/>  
    25.                 </InputScope>  
    26.             </TextBox.InputScope>  
    27.         </TextBox>  
    28.         <TextBox x:Name="txtWidth" Grid.Column="3"  
    29.                  Grid.Row="0" Width="160" Height="auto" FontSize="20">  
    30.             <TextBox.InputScope>  
    31.                 <InputScope>  
    32.                     <InputScopeName NameValue="Number"/>  
    33.                 </InputScope>  
    34.             </TextBox.InputScope>  
    35.         </TextBox>  
    36.         <CheckBox x:Name="chkShowCamera"  
    37.                   Grid.Row="1" Grid.ColumnSpan="2"  
    38.                   Content="显示启动相机"/>  
    39.         <Button x:Name="btnShow"  
    40.                 Grid.Column="2" Grid.Row="1"  
    41.                 Grid.ColumnSpan="2"  
    42.                 Content="选择图片..."  
    43.                 Margin="5"  
    44.                 Click="btnShow_Click"/>  
    45.     </Grid>  
    46. </Grid>  
    1. using System;  
    2. using System.Collections.Generic;  
    3. using System.Linq;  
    4. using System.Net;  
    5. using System.Windows;  
    6. using System.Windows.Controls;  
    7. using System.Windows.Documents;  
    8. using System.Windows.Input;  
    9. using System.Windows.Media;  
    10. using System.Windows.Media.Animation;  
    11. using System.Windows.Shapes;  
    12. using Microsoft.Phone.Controls;  
    13. //  
    14. using Microsoft.Phone.Tasks;  
    15. using System.Windows.Media.Imaging;  
    16.   
    17. namespace PhoneApp1  
    18. {  
    19.     public partial class Page1 : PhoneApplicationPage  
    20.     {  
    21.         PhotoChooserTask ptc = new PhotoChooserTask();  
    22.   
    23.         public Page1()  
    24.         {  
    25.             InitializeComponent();  
    26.   
    27.             ptc.Completed += new EventHandler<PhotoResult>(ptc_Completed);  
    28.         }  
    29.   
    30.         void ptc_Completed(object sender, PhotoResult e)  
    31.         {  
    32.             if (e.TaskResult == TaskResult.OK)  
    33.             {  
    34.                 BitmapImage bmp = new BitmapImage();  
    35.                 try  
    36.                 {  
    37.                     bmp.SetSource(e.ChosenPhoto);  
    38.                     Dispatcher.BeginInvoke(() => {  
    39.                         this.img.Source = bmp;  
    40.                     });  
    41.                 }  
    42.                 catch (Exception ex)  
    43.                 {  
    44.                     MessageBox.Show(ex.Message);  
    45.                 }  
    46.             }  
    47.         }  
    48.   
    49.         private void btnShow_Click(object sender, RoutedEventArgs e)  
    50.         {  
    51.             // 设置相关属性  
    52.             ptc.PixelHeight = int.Parse(txtHeight.Text);  
    53.             ptc.PixelWidth=int.Parse(txtWidth.Text);  
    54.             ptc.ShowCamera = this.chkShowCamera.IsChecked.HasValue ? chkShowCamera.IsChecked.Value : false;  
    55.   
    56.             ptc.Show();  
    57.         }  
    58.     }  
    59. }  

  • 相关阅读:
    找工过程中碰到的笔试面试题整理(1)
    windows核心编程学习笔记(五.续)堆
    windows核心编程学习笔记(二)Wait For Kernel Object(s)
    windows核心编程学习笔记(四)windows内存结构/虚拟内存/线程的堆栈
    [转]筛选法求素数
    windows核心编程学习笔记(五)内存映射文件
    windows核心编程学习笔记(八)结构化异常处理(Structured Exception Handling)
    [转]亲密接触VC6.0编译器
    windows核心编程学习笔记(三)线程池(Thread Pooling)
    windows核心编程学习笔记(七)DLL Injection and API Hooking
  • 原文地址:https://www.cnblogs.com/songtzu/p/2607138.html
Copyright © 2011-2022 走看看