zoukankan      html  css  js  c++  java
  • windows phone 使用相机并获取图片(3)

    使用相机需要引用如下命名空间
    using Microsoft.Phone.Tasks;

     在这个案例中我们还用到了BitmapImage类,所以我们需要引用命名空间

    using System.Windows.Media.Imaging;

     效果图如下

     

     MainPage.xaml文件在初始化的基础上添加了两个元素

    <TextBlock x:Name="txtName" Text="启动相机" Grid.Row="1" ></TextBlock>
            <Image x:Name="img" Grid.Row="1" Margin="12,10,12,0" ></Image>

     代码隐藏文件

    View Code
    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 System.Windows.Media.Imaging;
    using Microsoft.Phone.Tasks;

    namespace CameraShoot
    {
        public partial class MainPage : PhoneApplicationPage
        {
            //相机捕获任务实例
            CameraCaptureTask cameraCT = new CameraCaptureTask();
            // 构造函数
            public MainPage()
            {
                InitializeComponent();
                //手机拍照功能完成后调用
                cameraCT.Completed += new EventHandler<PhotoResult>(cameraCT_Completed);
            }
            //重写触摸屏事件
            protected override void OnManipulationStarted(ManipulationStartedEventArgs e)
            {
                //知识点①
                if (e.OriginalSource==txtName)
                {
                    //调用相机
                    cameraCT.Show();
                }
                //知识点②
                
    //触摸事件完成 
                e.Complete();
                //知识点③
                
    //不在向父元素传递
                e.Handled = true;
                //知识点④
                base.OnManipulationStarted(e);
            }
            //完成
            void cameraCT_Completed(object sender, PhotoResult e)
            {
                if (e.TaskResult==TaskResult.OK)
                {
                    BitmapImage bmp = new BitmapImage();
                    //知识点⑤
                    
    //获取包含文件流的和Source不同
                    bmp.SetSource(e.ChosenPhoto);
                    //设置图片源
                    img.Source = bmp;
                    txtName.Text = e.OriginalFileName;
                }
            }
        }
    }

    其中CameraCaptureTask,有两个比较实用的方法,也就是我们这里用到的

       Show; 调用相机功能

      Completed ;相机拍摄完成之后可被调用

       知识点①:该事件中的e是指,通过该事件传递过来的参数,所以我们可以从中获取一些信息,属性OriginalSource表示产生该事件的元素(在windows phone 中都称之为元素而非控件);

       知识点②:这里调用了Completed方法,加上该方法之后,系统就不会处理相应的路由事件了,这里是指重写的OnManipulationStarted事件 ;

       知识点③: e.Handled = true; 该属性设置表示该路由事件已经处理完毕,不需要在向可视化树的上一层传递;

       知识点④:调用本方法的基类方法,虽然在这里重写了基类方法,但是不可避免的在基类完成的基础性操作,重写没有完成,这样操作会出现一些错误

       知识点⑤:利用SetSource设置的源一定是Stream流,wp支持png和jpeg格式

     这些都是在模拟器上的的操作,如果在真机上测试,请在测试前关闭ZUNE,也可移步 http://www.cnblogs.com/fwind/archive/2011/11/28/2265890.html

      总结:使用相机是直接调用CameraCaptureTask类的show方法,这样相机就打开了,直到图片生成,再调用Completed方法,在Completed方法中获得 PhotoResult中该图片的stream 并作为位图的源,并设置image的源;当前图片的文件名也就是PhotoResult的属性OriginalFileName,得到的文件名为该图片的完整路径

     

  • 相关阅读:
    Python从菜鸟到高手(18):类与方法的私有化
    小程序云开发实战
    第一行代码:以太坊(3)-使用更多的工具编写和测试智能合约
    第一行代码:以太坊(2)-使用Solidity语言开发和测试智能合约
    第一行代码:以太坊(1)-创建自己的私有区块链
    用量子物理学原理解释为什么振金可以吸收能量(论发散思维的重要性)
    Python网络爬虫实战:根据天猫胸罩销售数据分析中国女性胸部大小分布
    Python从菜鸟到高手(8):print函数、赋值与代码块
    基于ArcPy的ArcGIS python设计与开发实战--GIS二次开发
    tensorflow数据增强
  • 原文地址:https://www.cnblogs.com/shenzhoulong/p/2437804.html
Copyright © 2011-2022 走看看