我正在使用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。
答案 0 :(得分: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