捕获高像素照片(updated)

    可以使用Windows.Phone.Media.Capture.PhotoCaptureDevice 进行高像素图片的捕获(需要自己画视图界面,因为该平台的PhotoChooserTask API 不支持捕获高像素的功能),并且需要使用预定义像素分辨率手动设置捕获的像素。需要注意的额外的像素是没有从 PhotoCaptureDevice 类的获取手机支持分辨率的静态方法 IReadOnlyList<Size> GetAvailabeCaptureResolutions() 方法中检索到的。还有就是为了让 PhotoCaptureDevice 正常工作ID_CAP_ISV_CAMERA功能声明需要添加到清单文件WMAppManifest.xml

  

下面是可以手动获取的支持高分辨率的设备型号

 手机型号变体 手动配置高像素的选项
 Lumia 1020 RM-875, RM-876, RM-877 7712x4352 (16:9), 7136x5360 (4:3)
 Lumia 1520 RM-937, RM-938, RM-939 5376x3024 (16:9), 4992x3744 (4:3)


为高像素拍摄初始化相机

    下面的示例代码演示你如何在应用中创建简单的取景器,并且初始化高像素 photo capture。下面

的代码是使用 VideoBrush 作为 canvas 的背景:

<!-- Simple viewfinder page -->
<phone:PhoneApplicationPage x:Class="ViewfinderPage" ... >
    <Grid x:Name="LayoutRoot" Background="Transparent">
        <Grid x:Name="ContentPanel">
            <Canvas x:Name="Canvas" ... >
                <Canvas.Background>
                    <VideoBrush x:Name="ViewfinderVideoBrush" Stretch="Uniform"/>
                </Canvas.Background>
            </Canvas>
        </Grid>
    </Grid>
</phone:PhoneApplicationPage>  

    在相应的 C# 页面,向以往相同,先初始化 PhotoCaptureDevice,这里需要添加额外的代码检测

设备的型号,从而根据条件进行选择,并且在 IAsyncOperation<PhotoCaptureDevice> OpenAsync(CameraSensorLocation senor, Size initialRsolution) 方法中初始化高分辨率的尺寸。需要注意的是你只能每次使用一种分辨率捕获一张照片,所以如果你需要高像素的版本和低像素

的版本的图片,你可以先捕获一张高像素照片,然后对它进行操作,比如使用 Nokia Imaging SDK 对它进行压缩

尺寸来获得另一个版本的图片。

 

using Microsoft.Devices;
using Windows.Phone.Media.Capture;

...

public partial class ViewfinderPage : PhoneApplicationPage
{
    private const CameraSensorLocation SENSOR_LOCATION = CameraSensorLocation.Back;

    private PhotoCaptureDevice _device = null;
    private bool _focusing = false;
    private bool _capturing = false;

    ...

    ~ViewfinderPage()
    {
        if (_device != null)
        {
            UninitializeCamera();
        }
    }

    protected override void OnNavigatedTo(NavigationEventArgs e)
    {
        base.OnNavigatedTo(e);

        if (_device == null)
        {
            InitializeCamera();
        }
    }

    protected override void OnNavigatingFrom(NavigatingCancelEventArgs e)
    {
        if ((_focusing || _capturing) && e.IsCancelable)
        {
            e.Cancel = true;
        }

        base.OnNavigatingFrom(e);
    }

    protected override void OnNavigatedFrom(NavigationEventArgs e)
    {
        base.OnNavigatedFrom(e);

        if (_device != null)
        {
            UninitializeCamera();
        }
    }

    /// <summary>
    /// Synchronously initialises the photo capture device for a high resolution photo capture.
    /// Viewfinder video stream is sent to a VideoBrush element called ViewfinderVideoBrush, and
    /// device hardware capture key is wired to the CameraButtons_ShutterKeyHalfPressed and
    /// CameraButtons_ShutterKeyPressed methods.
    /// </summary>
    private void InitializeCamera()
    {
        Windows.Foundation.Size captureResolution;

        var deviceName = DeviceStatus.DeviceName;

        if (deviceName.Contains("RM-875") || deviceName.Contains("RM-876") || deviceName.Contains("RM-877"))
        {
            captureResolution = new Windows.Foundation.Size(7712, 4352); // 16:9 ratio
            //captureResolution = new Windows.Foundation.Size(7136, 5360); // 4:3 ratio
        }
        else if (deviceName.Contains("RM-937") || deviceName.Contains("RM-938") || deviceName.Contains("RM-939"))
        {
            captureResolution = new Windows.Foundation.Size(5376, 3024); // 16:9 ratio
            //captureResolution = new Windows.Foundation.Size(4992, 3744); // 4:3 ratio
        }
        else
        {
            captureResolution = PhotoCaptureDevice.GetAvailableCaptureResolutions(SENSOR_LOCATION).First();
        }

        var task = PhotoCaptureDevice.OpenAsync(SENSOR_LOCATION, captureResolution).AsTask();

        task.Wait();

        _device = task.Result;

        ViewfinderVideoBrush.SetSource(_device);

        if (PhotoCaptureDevice.IsFocusSupported(SENSOR_LOCATION))
        {
            Microsoft.Devices.CameraButtons.ShutterKeyHalfPressed += CameraButtons_ShutterKeyHalfPressed;
        }

        Microsoft.Devices.CameraButtons.ShutterKeyPressed += CameraButtons_ShutterKeyPressed;

        ...
    }

    /// <summary>
    /// Uninitialises the photo capture device and unwires the device hardware capture keys.
    /// </summary>
    private void UninitializeCamera()
    {
        if (PhotoCaptureDevice.IsFocusSupported(SENSOR_LOCATION))
        {
            Microsoft.Devices.CameraButtons.ShutterKeyHalfPressed -= CameraButtons_ShutterKeyHalfPressed;
        }

        Microsoft.Devices.CameraButtons.ShutterKeyPressed -= CameraButtons_ShutterKeyPressed;

        _device.Dispose();
        _device = null;
    }

    ...
}  

 

 


拍摄

现在设备的相机已经初始化完成了,设备的拍照按键可以是相机进行自动对焦,并且捕获一张高像素照片

...

public partial class ViewfinderPage : PhoneApplicationPage
{
    ...

    /// <summary>
    /// Asynchronously autofocuses the photo capture device.
    /// </summary>
    private async void CameraButtons_ShutterKeyHalfPressed(object sender, EventArgs e)
    {
        if (!_focusing && !_capturing)
        {
            _focusing = true;

            await _device.FocusAsync();

            _focusing = false;
        }
    }

    /// <summary>
    /// Asynchronously captures a frame for further usage.
    /// </summary>
    private async void CameraButtons_ShutterKeyPressed(object sender, EventArgs e)
    {
        if (!_focusing && !_capturing)
        {
            _capturing = true;

            var stream = new MemoryStream();

            try
            {
                var sequence = _device.CreateCaptureSequence(1);
                sequence.Frames[0].CaptureStream = stream.AsOutputStream();

                await _device.PrepareCaptureSequenceAsync(sequence);
                await sequence.StartCaptureAsync();
            }
            catch (Exception ex)
            {
                stream.Close();
            }

            _capturing = false;

            if (stream.CanRead)
            {
                // We now have the captured image JPEG data in variable "stream" for further usage
            }
        }
    }

    ...
}  


获得一个全功能的“单击画面对焦”、“在捕获时冻结预览”,可以参考示例代码  Photo Inspector

 

 

Nokia WiKi 原文链接:http://developer.nokia.com/Resources/Library/Lumia/#!imaging/working-with-high-resolution-photos/capturing-high-resolution-photos.html



 

posted @ 2013-09-11 17:19  博琼  阅读(491)  评论(0编辑  收藏  举报