Windows Phone开发(23):启动器与选择器之CameraCaptureTask和PhotoChooserTask .

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

 

 

一、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>  
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="*"/>
            <RowDefinition Height="auto"/>
        </Grid.RowDefinitions>
        <Image x:Name="img" Grid.Row="0" Stretch="Uniform"
               HorizontalAlignment="Stretch"
               VerticalAlignment="Stretch"/>
        <Button x:Name="btnCamera" Grid.Row="1"
                Content="启动相机程序" Click="btnCamera_Click"/>
    </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. }  
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.Phone.Tasks;
using System.Windows.Media.Imaging;

namespace PhoneApp1
{
    public partial class MainPage : PhoneApplicationPage
    {
        // 第一步,声明类级别的局部变量,并实例化。
        CameraCaptureTask MyCamera = new CameraCaptureTask();

        // 构造函数
        public MainPage()
        {
            InitializeComponent();

            // 第二步,在页面构造函数中注册完成回调事件
            MyCamera.Completed += new EventHandler<PhotoResult>(MyCamera_Completed);
        }

        private void btnCamera_Click(object sender, RoutedEventArgs e)
        {
            // 第三步,显示组件
            MyCamera.Show();
        }

        // 第四步,处理事返回结果
        void MyCamera_Completed(object sender, PhotoResult e)
        {
            // 确定用户确认了还是取消了操作。
            if (e.TaskResult == TaskResult.OK)
            {
                // 从返回的流中创建图象
                BitmapImage bmp = new BitmapImage();
                try
                {
                    bmp.SetSource(e.ChosenPhoto);
                    // 把图象作为Image控件的源。
                    // 防止异步回调直接访问UI元素,故应使用BeginInvoke方法。
                    Dispatcher.BeginInvoke(() =>
                    {
                        this.img.Source = bmp;
                    });
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }

            }
        }
    }
}


 

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

 

 

 

 

二、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>  
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="*"/>
            <RowDefinition Height="auto"/>
        </Grid.RowDefinitions>
        <Image x:Name="img" Stretch="Uniform" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Grid.Row="0"/>
        <Grid Grid.Row="1">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="auto"/>
                <ColumnDefinition Width="auto"/>
                <ColumnDefinition Width="auto"/>
                <ColumnDefinition Width="auto"/>
            </Grid.ColumnDefinitions>
            <Grid.RowDefinitions>
                <RowDefinition Height="auto"/>
                <RowDefinition Height="auto"/>
            </Grid.RowDefinitions>
            <TextBlock Grid.Column="0" Grid.Row="0" Text="高度:"/>
            <TextBlock Grid.Column="2" Grid.Row="0" Text="宽度:"/>
            <TextBox x:Name="txtHeight" Grid.Column="1"
                     Grid.Row="0" Width="160" Height="auto" FontSize="20">
                <TextBox.InputScope>
                    <InputScope>
                        <InputScopeName NameValue="Number"/>
                    </InputScope>
                </TextBox.InputScope>
            </TextBox>
            <TextBox x:Name="txtWidth" Grid.Column="3"
                     Grid.Row="0" Width="160" Height="auto" FontSize="20">
                <TextBox.InputScope>
                    <InputScope>
                        <InputScopeName NameValue="Number"/>
                    </InputScope>
                </TextBox.InputScope>
            </TextBox>
            <CheckBox x:Name="chkShowCamera"
                      Grid.Row="1" Grid.ColumnSpan="2"
                      Content="显示启动相机"/>
            <Button x:Name="btnShow"
                    Grid.Column="2" Grid.Row="1"
                    Grid.ColumnSpan="2"
                    Content="选择图片..."
                    Margin="5"
                    Click="btnShow_Click"/>
        </Grid>
    </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. }  
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.Phone.Tasks;
using System.Windows.Media.Imaging;

namespace PhoneApp1
{
    public partial class Page1 : PhoneApplicationPage
    {
        PhotoChooserTask ptc = new PhotoChooserTask();

        public Page1()
        {
            InitializeComponent();

            ptc.Completed += new EventHandler<PhotoResult>(ptc_Completed);
        }

        void ptc_Completed(object sender, PhotoResult e)
        {
            if (e.TaskResult == TaskResult.OK)
            {
                BitmapImage bmp = new BitmapImage();
                try
                {
                    bmp.SetSource(e.ChosenPhoto);
                    Dispatcher.BeginInvoke(() => {
                        this.img.Source = bmp;
                    });
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
        }

        private void btnShow_Click(object sender, RoutedEventArgs e)
        {
            // 设置相关属性
            ptc.PixelHeight = int.Parse(txtHeight.Text);
            ptc.PixelWidth=int.Parse(txtWidth.Text);
            ptc.ShowCamera = this.chkShowCamera.IsChecked.HasValue ? chkShowCamera.IsChecked.Value : false;

            ptc.Show();
        }
    }
}

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值