【WP7】关于相机应用程序

在WP OS7.1中,允许我们通过编程的方式访问设备相机,可以通过访问部分API通过相机获取图像

1、把相机获取图像显示在屏幕上

  新建一个画布,用于显示相机获取到的图像数据 

<Canvas x:Name="cameraViewCanvas" Width="400" Margin="28,4,28,284">
    <Canvas.Background>
        <VideoBrush x:Name="cameraVideoBrush" />
    </Canvas.Background>
</Canvas>

 

2、新建一个 相机设备 的实例,通过该实例访问相机设备,新建一个MediaLibrary 用于访问系统的媒体资源

首先需要包含命名空间(解析MediaLibrary时要先引用Microsoft.Xna.Framework命名空间)

  using using Microsoft.Devices;
  using Microsoft.Xna.Framework.Media;

  PhotoCamera cam;
       MediaLibrary library = new MediaLibrary();

 

3、初始化相机设备(后置摄像头)和画布画刷,一般放在页面的初始化或 OnNavigatedTo 函数里 

if (PhotoCamera.IsCameraTypeSupported(CameraType.Primary))
{
    cam = new PhotoCamera(CameraType.Primary);
    cam.Initialized += new EventHandler<CameraOperationCompletedEventArgs>(cam_Initialized);
    cam.CaptureCompleted += new EventHandler<CameraOperationCompletedEventArgs>(cam_CaptureCompleted);
    cam.CaptureImageAvailable += new EventHandler<ContentReadyEventArgs>(cam_CaptureImageAvailable);
    cam.CaptureThumbnailAvailable += new EventHandler<ContentReadyEventArgs>(cam_CaptureThumbnailAvailable);
    cameraVideoBrush.SetSource(cam);
}

   CameraType.FrontFacing表示前置摄像头(使用前判断是否支持)

注意:应该在页面退出时释放掉相机设备资源,注销事件,一般放在 OnNavigatedFrom 函数里面

if (cam != null)
{
    cam.Dispose();
    cam.Initialized -= cam_Initialized;
    cam.CaptureCompleted -= cam_CaptureCompleted;
    cam.CaptureImageAvailable -= cam_CaptureImageAvailable;
    cam.CaptureThumbnailAvailable -= cam_CaptureThumbnailAvailable;
}

 

4、接下来是事件函数

  cam_Initialized           相机初始化完成后执行

  cam_CaptureImageAvailable    当有图像可用时发生

  cam_CaptureThumbnailAvailable  当有缩略图图像可用时发生

  cam_CaptureCompleted      拍摄完成时发生 

View Code
        void cam_Initialized(object sender, CameraOperationCompletedEventArgs e)
        {
        }

        void cam_CaptureThumbnailAvailable(object sender, ContentReadyEventArgs e)
        {
        }

        void cam_CaptureImageAvailable(object sender, ContentReadyEventArgs e)
        {
            string fileName = "image.jpg";
            //保存图像到CameraRoll里
            library.SavePictureToCameraRoll(fileName, e.ImageStream);

            //保存图像到独立存储空间
            using (IsolatedStorageFile isStore = IsolatedStorageFile.GetUserStoreForApplication())
            {
                using (IsolatedStorageFileStream targetStream = isStore.OpenFile(fileName, FileMode.Create, FileAccess.Write))
                {
                    byte[] readBuffer = new byte[4096];
                    int bytesRead = -1;

                    while ((bytesRead = e.ImageStream.Read(readBuffer, 0, readBuffer.Length)) > 0)
                    {
                        targetStream.Write(readBuffer, 0, bytesRead);
                    }
                }
            }
            //关闭流
            e.ImageStream.Close();
        }

        void cam_CaptureCompleted(object sender, CameraOperationCompletedEventArgs e)
        {
        }

     到这里,相机设备初始化完成了,接下来是对相机资源的操作

 

1、拍摄图片:

  cam.CaptureImage();

2、闪光灯设置

  cam.FlashMode = FlashMode.On;

3、缩略图分辨率设置(分辨率大小必须是相机设备支持的分辨率)

    //获取设备支持的分辨率
    IEnumerable<Size> resList = cam.AvailableResolutions;
    int resCount = resList.Count<Size>();
    Size res;

    if(resCount >= 1)
    {
        //根据需要选择不同的分辨率,这里选择第一个
        res = resList.ElementAt<Size>(0);
    }

    //设置分辨率
    cam.Resolution = res;

4、对焦

  if (cam.IsFocusSupported)
  {
    cam.Focus();

    //cam.FocusAtPoint(0.5, 0.5);
  }

5、访问快门按钮

快门按钮的访问是通过 CameraButtons 类中的一系列事件管理的

CameraButtons 类的三个事件

  ShutterKeyHalfPressed    半按快门且持续800ms以上

  ShutterKeyPressed      全按快门

  ShutterKeyReleased      松开快门

在相机设备初始化时注册这三个事件,不用生存CameraButtons类的实例,它是一个static类 

    CameraButtons.ShutterKeyHalfPressed += new EventHandler(CameraButtons_ShutterKeyHalfPressed);
    CameraButtons.ShutterKeyPressed += new EventHandler(CameraButtons_ShutterKeyPressed);
    CameraButtons.ShutterKeyReleased += new EventHandler(CameraButtons_ShutterKeyReleased);

同样的,在离开页面的时候,注销这三个事件

    CameraButtons.ShutterKeyHalfPressed -= CameraButtons_ShutterKeyHalfPressed;
    CameraButtons.ShutterKeyPressed -= CameraButtons_ShutterKeyPressed;
    CameraButtons.ShutterKeyReleased -= CameraButtons_ShutterKeyReleased;

实现三个事件函数

View Code
    void CameraButtons_ShutterKeyReleased(object sender, EventArgs e)
    {
        if (cam != null)
        {
            cam.CancelFocus();
        }
    }

    void CameraButtons_ShutterKeyPressed(object sender, EventArgs e)
    {
        if (cam != null)
        {
            cam.CaptureImage();
        }
    }

    void CameraButtons_ShutterKeyHalfPressed(object sender, EventArgs e)
    {
        if (cam != null)
        {
            cam.Focus();
        }
    }

 

posted @ 2012-10-31 14:25  bomo  阅读(1045)  评论(0编辑  收藏  举报