原文:Windows Phone 开发——相机功能开发

  相机功能是手机区别于PC的一大功能,在做手机应用时,如果合理的利用了拍照功能,可能会给自己的应用增色很多。使用Windows Phone的相机功能,有两种方法,一种是使用PhotoCamera类来构建自己的相机UI,另外一种是通过CameraCaptureTask选择器来实现该功能。

他们的区别是:

  • PhotoCamera类允许应用控制照片属性,如 ISO、曝光补偿和手动对焦位置,应用可以对照片有更多的控制,当然也会麻烦很多。需要实现闪光灯、对焦、分辨率、快门按钮等操作。
  • CameraCaptureTask拍照会调用系统的相机功能,返回一个有照片数据的返回值,同时一旦拍照,就会进入手机相册。

一. CameraCaptureTask选择器。

  1. 首先需要引用
using Microsoft.Phone.Tasks;

  1. 声明任务对象,需要在页面的构造函数之前声明
CameraCaptureTask cameraCaptureTask;

在构造函数中实例化CameraCaptureTask对象,并且注册回调方法。

cameraCaptureTask = new CameraCaptureTask();
cameraCaptureTask.Completed += new EventHandler<PhotoResult>(cameraCaptureTask_Completed);

在应用程序中的所需位置添加以下代码,例如按键点击事件中

cameraCaptureTask.Show();

在页面中添加已完成事件处理程序的代码。此代码在用户完成任务后运行。结果是一个 PhotoResult对象,该对象公开包含图像数据的流。

void cameraCaptureTask_Completed(object sender, PhotoResult e)
{if (e.TaskResult == TaskResult.OK){MessageBox.Show(e.ChosenPhoto.Length.ToString());//Code to display the photo on the page in an image control named myImage.//System.Windows.Media.Imaging.BitmapImage bmp = new System.Windows.Media.Imaging.BitmapImage();//bmp.SetSource(e.ChosenPhoto);//myImage.Source = bmp;
    }
}

这部分比较简单,就不多讲了,给个demo吧:http://pan.baidu.com/s/1pJ0Polt

二. PhotoCamera

  PhotoCamera是在windows phone os 7.1开始加入的,在使用之前需要给应用添加访问相机的权限,在

WMAppManifest.xml中添加ID_CAP_ISV_CAMERA

  1. 创建UI

在创建取景器时,一般会使用VideoBrush,如果需要支持横竖屏的切换,则需要加入RelativeTransform,如下代码是一个典型的相机UI

<!-- 相机取景器 --><Canvas x:Name="VideoCanvas"><Canvas.Background><VideoBrush x:Name="BackgroundVideoBrush" ><VideoBrush.RelativeTransform><CompositeTransform x:Name="VideoBrushTransform" CenterY="0.5" CenterX="0.5"/></VideoBrush.RelativeTransform></VideoBrush></Canvas.Background></Canvas>

当然你还要考虑页面上的其他元素,比如点击取景器对焦,快门、闪光灯按钮等,这些可随个人洗好自定义。

  1. 实现取景器和相关相机事件。

    1. 首先实现取景器,先判断手机有没有相关的硬件设备(背部相机(主)或者前部相机)
if ((PhotoCamera.IsCameraTypeSupported(CameraType.Primary) == true) ||(PhotoCamera.IsCameraTypeSupported(CameraType.FrontFacing) == true)){// Initialize the camera, when available.if (PhotoCamera.IsCameraTypeSupported(CameraType.FrontFacing)){// Use front-facing camera if available.
cam = new Microsoft.Devices.PhotoCamera(CameraType.FrontFacing);}else{// Otherwise, use standard camera on back of device.
cam = new Microsoft.Devices.PhotoCamera(CameraType.Primary);}//Set the VideoBrush source to the camera.
viewfinderBrush.SetSource(cam);}else{// The camera is not supported on the device.this.Dispatcher.BeginInvoke(delegate(){// Write message.
txtDebug.Text = "A Camera is not available on this device.";});// Disable UI.
ShutterButton.IsEnabled = false;FlashButton.IsEnabled = false;AFButton.IsEnabled = false;ResButton.IsEnabled = false;}

  1. 在加载时也需要实现各种操作事件
// Event is fired when the PhotoCamera object has been initialized.
cam.Initialized += new EventHandler<Microsoft.Devices.CameraOperationCompletedEventArgs>(cam_Initialized);// Event is fired when the capture sequence is complete.
cam.CaptureCompleted += new EventHandler<CameraOperationCompletedEventArgs>(cam_CaptureCompleted);// Event is fired when the capture sequence is complete and an image is available.
cam.CaptureImageAvailable += new EventHandler<Microsoft.Devices.ContentReadyEventArgs>(cam_CaptureImageAvailable);// Event is fired when the capture sequence is complete and a thumbnail image is available.
cam.CaptureThumbnailAvailable += new EventHandler<ContentReadyEventArgs>(cam_CaptureThumbnailAvailable);// The event is fired when auto-focus is complete.
cam.AutoFocusCompleted += new EventHandler<CameraOperationCompletedEventArgs>(cam_AutoFocusCompleted);// The event is fired when the viewfinder is tapped (for focus).
viewfinderCanvas.Tap += new EventHandler<GestureEventArgs>(focus_Tapped);// The event is fired when the shutter button receives a half press.
CameraButtons.ShutterKeyHalfPressed += OnButtonHalfPress;// The event is fired when the shutter button receives a full press.
CameraButtons.ShutterKeyPressed += OnButtonFullPress;// The event is fired when the shutter button is released.
CameraButtons.ShutterKeyReleased += OnButtonRelease;

  1. 上面加载了这么多事件,需要在离开此页面时释放:
protected override void OnNavigatingFrom(System.Windows.Navigation.NavigatingCancelEventArgs e){if (cam != null){// Dispose camera to minimize power consumption and to expedite shutdown.
cam.Dispose();// Release memory, ensure garbage collection.
cam.Initialized -= cam_Initialized;cam.CaptureCompleted -= cam_CaptureCompleted;cam.CaptureImageAvailable -= cam_CaptureImageAvailable;cam.CaptureThumbnailAvailable -= cam_CaptureThumbnailAvailable;cam.AutoFocusCompleted -= cam_AutoFocusCompleted;CameraButtons.ShutterKeyHalfPressed -= OnButtonHalfPress;CameraButtons.ShutterKeyPressed -= OnButtonFullPress;CameraButtons.ShutterKeyReleased -= OnButtonRelease;}}

上面这些事件,看看名字估计也就懂了是干啥的了,这里说明下他们的执行顺序,CaptureThumbnailAvailable —>CaptureImageAvailable —>CaptureCompleted。

  1. 拍出了照片后需要保存,可以保存到相机中,使用的是SavePictureToCameraRoll方法,同时可以保存到独立存储空间中,方便以后读取(如果仅仅保存在相册中,下次读取时必须使用照片选择器让用户去选择照片):
public void cam_CaptureThumbnailAvailable(object sender, ContentReadyEventArgs e){string fileName = savedCounter + "_th.jpg";try{// Write message to UI thread.
Deployment.Current.Dispatcher.BeginInvoke(delegate(){txtDebug.Text = "Captured image available, saving thumbnail.";});// Save thumbnail as JPEG to isolated storage.using (IsolatedStorageFile isStore = IsolatedStorageFile.GetUserStoreForApplication()){using (IsolatedStorageFileStream targetStream = isStore.OpenFile(fileName, FileMode.Create, FileAccess.Write)){// Initialize the buffer for 4KB disk pages.byte[] readBuffer = new byte[4096];int bytesRead = -1;// Copy the thumbnail to isolated storage.while ((bytesRead = e.ImageStream.Read(readBuffer, 0, readBuffer.Length)) > 0){targetStream.Write(readBuffer, 0, bytesRead);}}}// Write message to UI thread.
Deployment.Current.Dispatcher.BeginInvoke(delegate(){txtDebug.Text = "Thumbnail has been saved to isolated storage.";});}finally{// Close image stream
e.ImageStream.Close();}}

保存照片有两个方法:SavePicture和SavePictureToCameraRoll,前面的方法是保存到照片中心“保存的照片”中,后一种方法是保存到“本机拍照”中。

  1. 对于闪光灯、对焦、分辨率以及快门都有相应的方法,从上面的代码中也可以看到快门有半按、全按、释放等事件,这里不再赘述,可以从源代码中看到相关的事件。

这个例子的demo是微软提供的,比较详细,源码如下:http://pan.baidu.com/s/1c0rIqSK

参考文章:Windows Phone 的相机和照片

Windows Phone 开发——相机功能开发相关推荐

  1. 从零开始安卓端相机功能开发(一)了解用什么去开发以及流程

    目前已有章节大家可以去学习也可以参考一下 1.从零开始安卓端相机功能开发(一)了解用什么去开发以及流程 2.从零开始安卓端相机功能开发(二)让我们来开发一个相机 3.从零开始开发Android相机ap ...

  2. 从零开始安卓端相机功能开发(二)让我们来开发一个相机

    目前已有章节大家可以去学习也可以参考一下 1.从零开始安卓端相机功能开发(一)了解用什么去开发以及流程 2.从零开始安卓端相机功能开发(二)让我们来开发一个相机 3.从零开始开发Android相机ap ...

  3. ios开发 架子鼓功能开发_适用于iOS开发人员的功能标志

    ios开发 架子鼓功能开发 When building modern applications, things tend to move quite quickly. When you add mul ...

  4. 指南-Luat二次开发教程-功能开发教程-SOCKET

    目录 socket 简介 API说明 实现流程 同步与异步 示例 开机与连接网络 连接服务器 socket发送与接收消息 常见问题 连接服务器失败 最多同时支持多少个连接 socket异常的情况排查 ...

  5. Luat模块应用手册-指南-luat二次开发教程-功能开发教程-HTTP

    关键词:HTTP,POST,GET 目录 http 简介 API说明 实现流程 示例 执行HTTP请求的回调函数 HTTP请求函数 常见问题 是否支持HTTPS 为什么频繁请求会失败 为什么POST ...

  6. 织梦php开发tags功能开发,织梦全网最新联动筛选功能的实现(单选和多选)可显示分类的文章...

    之前写过这篇文章<>下面二次开发织梦联动筛选是之前升级版,修复手机端无法加载_m.htm列表模板BUG: 首先需要说明的是这个功能需要在模板里面用到php标签,所以需要在后台模板引擎禁用标 ...

  7. 织梦php开发tags功能开发,织梦二次开发模板新建PHP页面支持系统标签

    这篇文章主要为大家详细介绍了织梦二次开发模板新建PHP页面支持系统标签,具有一定的参考价值,感兴趣的小伙伴们可以参考一下,有需要的朋友可以收藏方便以后借鉴. 有的时候我们在用织梦系统做二次开发建设织梦 ...

  8. 织梦php开发tags功能开发,织梦dedecms二次开发中几个标签的应用

    在织梦dedecms 里面对于数组进行循环的标签有好几个,在前台模板即使用静态模板引擎有,{dede:foreach array='数组名称'}[field:key/] [field:value/]{ ...

  9. 织梦php开发tags功能开发,DedeCms织梦二次开发批量添加tag标签 批量修改TAG标签功能的方法...

    需求背景: 之前发布了不少文章,后来发现之前的这些文章当时发布时都没有添加tag标签,然而现在需要都添加上tag标签,然而这个在默认的织梦后台是没办法批量实现的,只能一篇篇文章去修改. 思考: 织梦原 ...

最新文章

  1. a*算法的优缺点_轻松理解机器学习算法-朴素贝叶斯
  2. solr快速入门的地址,spring-data/solr
  3. Android_安卓为按钮控件绑定事件的五种方式
  4. 基于阿里云MaxCompute实现游戏数据运营
  5. matlab 多 带阻,matlab程序之——滤波器(带通-带阻
  6. bootstap-水果节案例-编辑中
  7. Python Chemistry
  8. python c++ socket 通信一个示例
  9. python绘制聚类分析树状图
  10. 云服务器被攻击了怎么办
  11. Tesseract-OCR样本训练方法
  12. 群体Polymorphism information content(PIC)、 Nei diversity index (Nei s) 和 Shannon index (I)计算方法
  13. sqlplus之runstats
  14. Eclipse汉化教程详细
  15. 随手科技累计用户超3亿 领跑互金App
  16. 开放api接口签名验证
  17. SPF 记录是什么以及它如何工作:SPF 记录全解释
  18. Hive 实战(2)--hive分区分桶实战
  19. 韩立刚网络视频—学习记录
  20. 12306最新买票攻略

热门文章

  1. SQL函数设计——临时表的使用
  2. Android Q:新系统名称和新特性整理
  3. Django REST framework API 指南(12):验证器
  4. 在Linux下安装RabbitMQ
  5. ERP系统管理员的工具箱 推荐几款优秀的数据比较同步工具 Data Compare and Sync tool...
  6. 阎崇年:《袁崇焕传》自序
  7. 队列学习笔记 顺序队列
  8. C#函数式编程之可选值
  9. 创建Cocos2d-x 3.x项目以及项目结构简要说明
  10. 汉语是世界上唯一一种面向对象的高级语言