我拍摄照片时遇到问题。我试图解决这个问题3天,希望你能帮我解决这个问题。
我的xaml:
<CaptureElement x:Name="capturePreview" Stretch="Uniform" Grid.Column="0" Height="200" Width="300"
VerticalAlignment="Center" HorizontalAlignment="Center"/>
<Image x:Name="imagePreivew" Stretch="Uniform" Grid.Column="1"
VerticalAlignment="Center" HorizontalAlignment="Stretch"/>
<StackPanel Orientation="Horizontal">
<Button Click="InitCameraBtn_Click" Content="Initialize Camera" />
<Button Click="StartPreviewBtn_Click" Content="Start Capture Preview" />
<Button Click="TakePhotoBtn_Click" Content="Capture Photo"/>
</StackPanel>
和我的cs
private Windows.Media.Capture.MediaCapture captureManager;
public MainPage()
{
this.InitializeComponent();
}
private static async Task<DeviceInformation> GetCameraID(Windows.Devices.Enumeration.Panel desiredCamera)
{
// get available devices for capturing pictures
DeviceInformation deviceID = (await DeviceInformation.FindAllAsync(DeviceClass.VideoCapture))
.FirstOrDefault(x => x.EnclosureLocation != null && x.EnclosureLocation.Panel == desiredCamera);
if (deviceID != null) return deviceID;
else throw new Exception(string.Format("Camera of type {0} doesn't exist.", desiredCamera));
}
async private void InitCameraBtn_Click(object sender, RoutedEventArgs e)
{
var cameraID = await GetCameraID(Windows.Devices.Enumeration.Panel.Back);
captureManager = new MediaCapture();
await captureManager.InitializeAsync(new MediaCaptureInitializationSettings
{
StreamingCaptureMode = StreamingCaptureMode.Video,
PhotoCaptureSource = PhotoCaptureSource.VideoPreview,
AudioDeviceId = string.Empty,
VideoDeviceId = cameraID.Id
});
var maxResolution = captureManager.VideoDeviceController.GetAvailableMediaStreamProperties(MediaStreamType.Photo).Aggregate((i1, i2) => (i1 as VideoEncodingProperties).Width > (i2 as VideoEncodingProperties).Width ? i1 : i2);
await captureManager.VideoDeviceController.SetMediaStreamPropertiesAsync(MediaStreamType.Photo, maxResolution);
}
async private void StartPreviewBtn_Click(object sender, RoutedEventArgs e)
{
// rotate to see preview vertically
captureManager.SetPreviewRotation(VideoRotation.Clockwise90Degrees);
capturePreview.Source = captureManager;
await captureManager.StartPreviewAsync();
}
async private void TakePhotoBtn_Click(object sender, RoutedEventArgs e)
{
// create a file
StorageFile photoFile = await ApplicationData.Current.LocalFolder.CreateFileAsync("myFirstPhoto.jpg", CreationCollisionOption.ReplaceExisting);
// take a photo with choosen Encoding
await captureManager.CapturePhotoToStorageFileAsync(ImageEncodingProperties.CreateJpeg(), photoFile);
// we can also take a photo to memory stream
// InMemoryRandomAccessStream memStream = new InMemoryRandomAccessStream();
// await captureManager.CapturePhotoToStreamAsync(ImageEncodingProperties.CreateJpegXR(), memStream);
// show a photo on screen
BitmapImage bitmapToShow = new BitmapImage(new Uri(photoFile.Path));
imagePreivew.Source = bitmapToShow; // show image on screen inside Image control defined in XAML
}
public void Dispose()
{
if (mediaCapture != null)
{
mediaCapture.Dispose();
mediaCapture = null;
}
}
我做错了什么?如果我点击initliaze什么都不会,当我点击其他按钮时我甚至看不到相机我得到一个例外:+
$exception {System.Exception: The GPU device instance has been suspended. Use GetDeviceRemovedReason to determine the appropriate action. (Exception from HRESULT: 0x887A0005)
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.GetResult()
at Save_bills.MainPage.<TakePhotoBtn_Click>d__13.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.AsyncMethodBuilderCore.<ThrowAsync>b__3(Object state)
at System.Threading.WinRTSynchronizationContext.Invoker.InvokeCore()} System.Exception
和
+ $exception {System.Exception: The text associated with this error code could not be found.
The text associated with this error code could not be found.
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.GetResult()
at Save_bills.MainPage.<InitCameraBtn_Click>d__a.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.AsyncMethodBuilderCore.<ThrowAsync>b__3(Object state)
at System.Threading.WinRTSynchronizationContext.Invoker.InvokeCore()} System.Exception
答案 0 :(得分:3)
您正面临此问题,因为您不是处理您的 MediaCapture 对象,这在处理相机时非常重要。我怀疑在你调试你的应用程序并退出它而不进行处理后,你将无法在手机上运行普通的相机应用程序。
您应该做的是在不需要时停止相机预览并处理捕捉元素。什么时候做?这取决于一些事情:
出于测试目的,做一件事 - 在拍摄照片的方法中,最后调用方法停止预览并调用 MediaCapture.Dispose 。执行此操作时,请调试应用程序,拍照,停止调试。如果这样做,您应该能够在下次启动应用程序时初始化相机而不会出现问题。