【发布时间】:2015-03-13 22:51:51
【问题描述】:
我在拍摄照片时遇到问题。我已经尝试解决了 3 天,希望您能帮我解决这个问题。
我的xml:
<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
【问题讨论】:
-
如果您告诉我们您遇到了什么异常,将会有所帮助。
-
在应用程序的功能中(来自清单)您是否启用了相机访问权限? (双击.appxmanifest,点击“功能”选项卡,我相信那里有一个“相机”或“网络摄像头”的复选框)
-
是的,我做到了,它已启用
-
您的目标是 Silverlight 还是 Runtime?重置手机时,相机是第一次闪烁吗?您在哪里处理 mediaCapture 以及在哪里停止预览?
-
我的目标是运行时,是的,当我重置手机时,一切正常。我可以初始化相机、捕捉预览和拍照,但我无法返回捕捉预览。我有处理方法,但我不知道在哪里使用它
标签: c# xaml windows-phone-8.1