private static PhotoCaptureDevice d = null;
public static async Task<bool> InitializeCamera(CameraSensorLocation sensorLocation)
{
bool catchValue = true;
try
{
Windows.Foundation.Size initialResolution = new Windows.Foundation.Size(640, 480);
Windows.Foundation.Size previewResolution = new Windows.Foundation.Size(640, 480);
Windows.Foundation.Size captureResolution = new Windows.Foundation.Size(640, 480);
d = await PhotoCaptureDevice.OpenAsync(sensorLocation, initialResolution);
await d.SetPreviewResolutionAsync(previewResolution);
await d.SetCaptureResolutionAsync(captureResolution);
d.SetProperty(KnownCameraGeneralProperties.PlayShutterSoundOnCapture, true);
MemoryStream stream = new MemoryStream();
CameraCaptureSequence sequence = d.CreateCaptureSequence(1);
sequence.Frames[0].CaptureStream = stream.AsOutputStream();
await d.PrepareCaptureSequenceAsync(sequence);
await sequence.StartCaptureAsync();
if (stream.Length > 0)
{
d.Dispose();
d = null;
stream.Dispose();
stream = null;
return true;
}
else
{
d.Dispose();
d = null;
stream.Dispose();
stream = null;
}
return false;
}
catch (Exception ex)
{
catchValue = false;
}
if (!catchValue)
{
Windows.Foundation.Size initialResolution = new Windows.Foundation.Size(640, 480);
Windows.Foundation.Size previewResolution = new Windows.Foundation.Size(640, 480);
Windows.Foundation.Size captureResolution = new Windows.Foundation.Size(640, 480);
d = await PhotoCaptureDevice.OpenAsync(sensorLocation,initialResolution);
await d.SetPreviewResolutionAsync(previewResolution);
await d.SetCaptureResolutionAsync(captureResolution);
d.SetProperty(KnownCameraGeneralProperties.PlayShutterSoundOnCapture, true);
MemoryStream stream = new MemoryStream();
CameraCaptureSequence sequence = d.CreateCaptureSequence(1);
sequence.Frames[0].CaptureStream = stream.AsOutputStream();
await d.PrepareCaptureSequenceAsync(sequence);
await sequence.StartCaptureAsync();
if (stream.Length > 0)
{
d.Dispose();
d = null;
stream.Dispose();
stream = null;
return true;
}
else
{
d.Dispose();
d = null;
stream.Dispose();
stream = null;
}
return false;
}
return false;
}
是的,我試過了,但沒有use.after,也引發異常。 – user3146084
然後你將需要顯示更多的代碼(或者我應該說 - 至少是一些代碼),以顯示如何初始化PhotoCaptureDevice等。 –
我已經添加了上面的代碼。 – user3146084