当我尝试在我的uwp应用程序中拍照时,我有时会出错。 这个错误很难在桌面平台上几分钟前重现和发生。我使用hovercam来检索图像缓冲区。
public async Task<IBuffer> TakePhotoAsync()
{
Debug.WriteLine("taking picture...");
using (var stream = new InMemoryRandomAccessStream())
{
try
{
await mediaCapture.CapturePhotoToStreamAsync(ImageEncodingProperties.CreateJpeg(), stream);
Debug.WriteLine("captured to stream");
}
catch (Exception e)
{
ExceptionHandler.Instance.HandleException(e);
}
//rotate img
return await GetRotatedIBuffer(stream);
}
}
public async Task<IBuffer> GetRotatedIBuffer(IRandomAccessStream stream)
{
//rotate img
using (var imageStream = new InMemoryRandomAccessStream())
{
BitmapDecoder dec = await BitmapDecoder.CreateAsync(stream);
BitmapEncoder enc = await BitmapEncoder.CreateForTranscodingAsync(stream, dec);
BitmapRotation rotation = BitmapRotation.None;
switch (App.settings.DefaultCamera.Rotation)
{
case VideoRotation.Clockwise180Degrees:
rotation = BitmapRotation.Clockwise180Degrees;
break;
case VideoRotation.Clockwise270Degrees:
rotation = BitmapRotation.Clockwise270Degrees;
break;
case VideoRotation.Clockwise90Degrees:
rotation = BitmapRotation.Clockwise90Degrees;
break;
}
enc.BitmapTransform.Rotation = rotation;
await enc.FlushAsync();
try
{
await RandomAccessStream.CopyAsync(stream,imageStream);
}
catch (Exception ex)
{
ExceptionHandler.Instance.HandleException(ex);
}
return await StreamHelpers.StreamToIBuffer(imageStream);
}
}
输出:
taking picture...
Exception thrown: 'System.Runtime.InteropServices.COMException' in mscorlib.ni.dll
WinRT information: Started
System.Runtime.InteropServices.COMException (0xC00D36B2): The request is not valid in current state
Started
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.GetResult()
at KNFB_Reader_Windows10.KNFBCamera.<TakePhotoAsync>d__25.MoveNext()
Exception thrown: 'System.Exception' in mscorlib.ni.dll
Exception thrown: 'System.Exception' in mscorlib.ni.dll
Exception thrown: 'System.Exception' in mscorlib.ni.dll
Exception thrown: 'System.Exception' in mscorlib.ni.dll
The component cannot be found. (Exception from HRESULT: 0x88982F50)