当前位置:首页 > 操作系统 > Windows

Windows Phone 开发——相机功能开发

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

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

他们的区别是:

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

 

. CameraCaptureTask选择器。

  1. 首先需要引用
<span>using</span> Microsoft.Phone.Tasks;

 

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

 

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

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

 

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

cameraCaptureTask.Show();

 

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

<span>void</span> cameraCaptureTask_Completed(<span>object</span><span> sender, PhotoResult e)
{
    </span><span>if</span> (e.TaskResult ==<span> TaskResult.OK)
    {
        MessageBox.Show(e.ChosenPhoto.Length.ToString());

</span><span>//</span><span>Code to display the photo on the page in an image control named myImage.
        </span><span>//</span><span>System.Windows.Media.Imaging.BitmapImage bmp = new System.Windows.Media.Imaging.BitmapImage();
        </span><span>//</span><span>bmp.SetSource(e.ChosenPhoto);
        </span><span>//</span><span>myImage.Source = bmp;</span><span>    }
}</span>

 

这部分比较简单,就不多讲了,给个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

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

 

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

 

  1. 实现取景器和相关相机事件。
    1. 首先实现取景器,先判断手机有没有相关的硬件设备(背部相机()或者前部相机)
<span>if</span> ((PhotoCamera.IsCameraTypeSupported(CameraType.Primary) == <span>true</span>) ||<span>

                 (PhotoCamera.IsCameraTypeSupported(CameraType.FrontFacing) </span>== <span>true</span><span>))

            {

                </span><span>//</span><span> Initialize the camera, when available.</span><span>if</span><span> (PhotoCamera.IsCameraTypeSupported(CameraType.FrontFacing))

                {

                    </span><span>//</span><span> Use front-facing camera if available.</span><span>
                    cam </span>= <span>new</span><span> Microsoft.Devices.PhotoCamera(CameraType.FrontFacing);

                }

                </span><span>else</span><span>

                {

                    </span><span>//</span><span> Otherwise, use standard camera on back of device.</span><span>
                    cam </span>= <span>new</span><span> Microsoft.Devices.PhotoCamera(CameraType.Primary);

                }

    </span><span>//</span><span>Set the VideoBrush source to the camera.</span><span>
                viewfinderBrush.SetSource(cam);

            }

            </span><span>else</span><span>

            {

                </span><span>//</span><span> The camera is not supported on the device.</span><span>this</span>.Dispatcher.BeginInvoke(<span>delegate</span><span>()

                {

                    </span><span>//</span><span> Write message.</span><span>
                    txtDebug.Text </span>= <span>"</span><span>A Camera is not available on this device.</span><span>"</span><span>;

                });

 

                </span><span>//</span><span> Disable UI.</span><span>
                ShutterButton.IsEnabled </span>= <span>false</span><span>;

                FlashButton.IsEnabled </span>= <span>false</span><span>;

                AFButton.IsEnabled </span>= <span>false</span><span>;

                ResButton.IsEnabled </span>= <span>false</span><span>;

            }</span>

 

  1. 在加载时也需要实现各种操作事件
<span>//</span><span> Event is fired when the PhotoCamera object has been initialized.</span><span>
                cam.Initialized </span>+= <span>new</span> EventHandler<Microsoft.Devices.CameraOperationCompletedEventArgs><span>(cam_Initialized);

 

                </span><span>//</span><span> Event is fired when the capture sequence is complete.</span><span>
                cam.CaptureCompleted </span>+= <span>new</span> EventHandler<CameraOperationCompletedEventArgs><span>(cam_CaptureCompleted);

 

                </span><span>//</span><span> Event is fired when the capture sequence is complete and an image is available.</span><span>
                cam.CaptureImageAvailable </span>+= <span>new</span> EventHandler<Microsoft.Devices.ContentReadyEventArgs><span>(cam_CaptureImageAvailable);

 

                </span><span>//</span><span> Event is fired when the capture sequence is complete and a thumbnail image is available.</span><span>
                cam.CaptureThumbnailAvailable </span>+= <span>new</span> EventHandler<ContentReadyEventArgs><span>(cam_CaptureThumbnailAvailable);

 

                </span><span>//</span><span> The event is fired when auto-focus is complete.</span><span>
                cam.AutoFocusCompleted </span>+= <span>new</span> EventHandler<CameraOperationCompletedEventArgs><span>(cam_AutoFocusCompleted);

 

                </span><span>//</span><span> The event is fired when the viewfinder is tapped (for focus).</span><span>
                viewfinderCanvas.Tap </span>+= <span>new</span> EventHandler<GestureEventArgs><span>(focus_Tapped);

 

                </span><span>//</span><span> The event is fired when the shutter button receives a half press.</span><span>
                CameraButtons.ShutterKeyHalfPressed </span>+=<span> OnButtonHalfPress;

 

                </span><span>//</span><span> The event is fired when the shutter button receives a full press.</span><span>
                CameraButtons.ShutterKeyPressed </span>+=<span> OnButtonFullPress;

 

                </span><span>//</span><span> The event is fired when the shutter button is released.</span><span>
                CameraButtons.ShutterKeyReleased </span>+= OnButtonRelease;

 

  1. 上面加载了这么多事件,需要在离开此页面时释放:
<span>protected</span><span>override</span><span>void</span><span> OnNavigatingFrom(System.Windows.Navigation.NavigatingCancelEventArgs e)

        {

            </span><span>if</span> (cam != <span>null</span><span>)

            {

                </span><span>//</span><span> Dispose camera to minimize power consumption and to expedite shutdown.</span><span>
                cam.Dispose();

 

                </span><span>//</span><span> Release memory, ensure garbage collection.</span><span>
                cam.Initialized </span>-=<span> cam_Initialized;

                cam.CaptureCompleted </span>-=<span> cam_CaptureCompleted;

                cam.CaptureImageAvailable </span>-=<span> cam_CaptureImageAvailable;

                cam.CaptureThumbnailAvailable </span>-=<span> cam_CaptureThumbnailAvailable;

                cam.AutoFocusCompleted </span>-=<span> cam_AutoFocusCompleted;

                CameraButtons.ShutterKeyHalfPressed </span>-=<span> OnButtonHalfPress;

                CameraButtons.ShutterKeyPressed </span>-=<span> OnButtonFullPress;

                CameraButtons.ShutterKeyReleased </span>-=<span> OnButtonRelease;

            }

        }</span>

 

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

 

  1. 拍出了照片后需要保存,可以保存到相机中,使用的是SavePictureToCameraRoll方法,同时可以保存到独立存储空间中,方便以后读取(如果仅仅保存在相册中,下次读取时必须使用照片选择器让用户去选择照片)
<span>public</span><span>void</span> cam_CaptureThumbnailAvailable(<span>object</span><span> sender, ContentReadyEventArgs e)

        {

            </span><span>string</span> fileName = savedCounter + <span>"</span><span>_th.jpg</span><span>"</span><span>;

 

            </span><span>try</span><span>

            {

                </span><span>//</span><span> Write message to UI thread.</span><span>
                Deployment.Current.Dispatcher.BeginInvoke(</span><span>delegate</span><span>()

                {

                    txtDebug.Text </span>= <span>"</span><span>Captured image available, saving thumbnail.</span><span>"</span><span>;

                });

 

                </span><span>//</span><span> Save thumbnail as JPEG to isolated storage.</span><span>using</span> (IsolatedStorageFile isStore =<span> IsolatedStorageFile.GetUserStoreForApplication())

                {

                    </span><span>using</span> (IsolatedStorageFileStream targetStream =<span> isStore.OpenFile(fileName, FileMode.Create, FileAccess.Write))

                    {

                        </span><span>//</span><span> Initialize the buffer for 4KB disk pages.</span><span>byte</span>[] readBuffer = <span>new</span><span>byte</span>[<span>4096</span><span>];

                        </span><span>int</span> bytesRead = -<span>1</span><span>;

 

                        </span><span>//</span><span> Copy the thumbnail to isolated storage.</span><span>while</span> ((bytesRead = e.ImageStream.Read(readBuffer, <span>0</span>, readBuffer.Length)) > <span>0</span><span>)

                        {

                            targetStream.Write(readBuffer, </span><span>0</span><span>, bytesRead);

                        }

                    }

                }

 

                </span><span>//</span><span> Write message to UI thread.</span><span>
                Deployment.Current.Dispatcher.BeginInvoke(</span><span>delegate</span><span>()

                {

                    txtDebug.Text </span>= <span>"</span><span>Thumbnail has been saved to isolated storage.</span><span>"</span><span>;

 

                });

            }

            </span><span>finally</span><span>

            {

                </span><span>//</span><span> Close image stream</span><span>
                e.ImageStream.Close();

            }

        }</span>

 

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

 

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

 

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

 

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

原文:http://www.cnblogs.com/lonelyxmas/p/3812865.html


【说明】本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:254677821)!

相关教程推荐

其他课程推荐