1

我正在使用 PhotoCaptureDevice 类,我可以捕获相机帧,但是当我将 CameraCaptureSequence 的 CameraCaptureFrame.CaptureSequence 中的图像数据复制到 MemoryStream 中然后将其保存在相机胶卷中时出现错误。这是我正在尝试做的代码片段。

PhotoCaptureDevice cam;    
cam = await PhotoCaptureDevice.OpenAsync(<front/rear depending on user input>,<resolution depends on user input>);

CameraCaptureSequence seq;
seq = cam.CreateCaptureSequence(1);

cam.SetProperty(KnownCameraGeneralProperties.PlayShutterSoundOnCapture, true);

MemoryStream captureStream1 = new MemoryStream();
seq.Frames[0].CaptureStream = captureStream1.AsOutputStream();//This stream is for saving the image data to camera roll

await cam.PrepareCaptureSequenceAsync(seq);
await seq.StartCaptureAsync();

bool a = seq.Frames[0].CaptureStream.Equals(0);//This value is false during debugging
if(capturestream1.Length>0)//This condition evaluates to false
{
MediaLibrary library = new MediaLibrary();
Picture picture1 = library.SavePictureToCameraRoll("image1", captureStream1);
}
else
{
//Logic to handle condition
}

正如我在评论中添加的那样,该变量的bool a计算结果为 false,我通过调试代码进行了检查。但由于某种原因,该capturestream1.Length属性为 0。

4

1 回答 1

3

下面是有关如何使用单个图像捕获序列并将该图像保存到 MediaLibrary 的代码片段。显然,对于这个 API,这是一个微不足道的例子,因为序列非常适合捕获多个图像并将它们与后处理组合在一起。

private async void MainPage_Loaded(object sender, RoutedEventArgs e)
{
    using (MemoryStream stream = new MemoryStream())
    using (var camera = await PhotoCaptureDevice.OpenAsync(CameraSensorLocation.Back,
            PhotoCaptureDevice.GetAvailableCaptureResolutions(CameraSensorLocation.Back).First()))
    {
        var sequence = camera.CreateCaptureSequence(1);
        sequence.Frames[0].CaptureStream = stream.AsOutputStream();
        camera.PrepareCaptureSequenceAsync(sequence);
        await sequence.StartCaptureAsync();

        stream.Seek(0, SeekOrigin.Begin);

        using (var library = new MediaLibrary())
        {
            library.SavePictureToCameraRoll("currentImage.jpg", stream);
        }
    }
}

当您运行此代码片段时,您可以看到存储在设备上的图像:

带有新图像的图片中心

您可以在诺基亚 Camera Explorer 应用程序中找到完整的工作示例,该应用程序演示了 WP8 相机 API 的端到端用例:http ://projects.developer.nokia.com/cameraexplorer

于 2013-01-24T00:56:09.383 回答