相机功能是手机区别于PC的一大功能,在作手机应用时,若是合理的利用了拍照功能,可能会给本身的应用增色不少。使用Windows Phone的相机功能,有两种方法,一种是使用PhotoCamera类来构建本身的相机UI,另一种是经过CameraCaptureTask选择器来实现该功能。windows
他们的区别是:app
一. CameraCaptureTask选择器。ide
using Microsoft.Phone.Tasks;
CameraCaptureTask cameraCaptureTask;
在构造函数中实例化CameraCaptureTask对象,而且注册回调方法。函数
cameraCaptureTask = new CameraCaptureTask(); cameraCaptureTask.Completed += new EventHandler<PhotoResult>(cameraCaptureTask_Completed);
在应用程序中的所需位置添加如下代码,例如按键点击事件中this
cameraCaptureTask.Show();
在页面中添加已完成事件处理程序的代码。此代码在用户完成任务后运行。结果是一个 PhotoResult对象,该对象公开包含图像数据的流。spa
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/1pJ0Poltcode
二. PhotoCameraorm
PhotoCamera是在windows phone os 7.1开始加入的,在使用以前须要给应用添加访问相机的权限,在xml
WMAppManifest.xml中添加ID_CAP_ISV_CAMERA对象
在建立取景器时,通常会使用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>
固然你还要考虑页面上的其余元素,好比点击取景器对焦,快门、闪光灯按钮等,这些可随我的洗好自定义。
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; }
// 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;
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。
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,前面的方法是保存到照片中心“保存的照片”中,后一种方法是保存到“本机拍照”中。
这个例子的demo是微软提供的,比较详细,源码以下:http://pan.baidu.com/s/1c0rIqSK
参考文章:Windows Phone 的相机和照片