私はUWPアプリケーションにカメラ機能を備えていますが、実際に接続されているカメラデバイスがあるかどうかを確認する機能があります。 CameraStarterKitUWPアプリケーション内の別のページにナビゲートしたときにウェブカメラを停止する方法
ウェブカメラやカメラのないマシンでコードをテストしたところ、メッセージプロンプトが表示されます。しかし、アプリケーションを最小化して再び開くと、プロンプトが表示されているので、ページを終了するとカメラデバイスがあるかどうかを確認しているコードが残っていると思います。
とにかく他のページに移動したときにカメラが接続されているかどうかを確認するコードを停止できますか?
private async Task InitializeCameraAsync()
{
Debug.WriteLine("InitializeCameraAsync");
if (_mediaCapture == null)
{
// Attempt to get the back camera if one is available, but use any camera device if not
var cameraDevice = await FindCameraDeviceByPanelAsync(Windows.Devices.Enumeration.Panel.Back);
if (cameraDevice == null)
{
this.LoadProgressRing.IsActive = false;
this.LoadProgressStackPanel.Visibility = Windows.UI.Xaml.Visibility.Collapsed;
MessageDialog cameraError = new MessageDialog("Connection Problem. No camera device found. Please kindly contacct the administrator.");
UICommand YesBtn = new UICommand("Ok", delegate (IUICommand command)
{
idleTimer.Stop();
var rootFrame = (Window.Current.Content as Frame);
rootFrame.Navigate(typeof(HomePage));
rootFrame.BackStack.Clear();
});
cameraError.Commands.Add(YesBtn);
await cameraError.ShowAsync();
Debug.WriteLine("No camera device found!");
return;
}
// Create MediaCapture and its settings
_mediaCapture = new MediaCapture();
var settings = new MediaCaptureInitializationSettings { VideoDeviceId = cameraDevice.Id };
// Initialize MediaCapture
try
{
await _mediaCapture.InitializeAsync(settings);
_isInitialized = true;
}
catch (UnauthorizedAccessException)
{
Debug.WriteLine("The app was denied access to the camera");
}
// If initialization succeeded, start the preview
if (_isInitialized)
{
// Figure out where the camera is located
if (cameraDevice.EnclosureLocation == null || cameraDevice.EnclosureLocation.Panel == Windows.Devices.Enumeration.Panel.Unknown)
{
// No information on the location of the camera, assume it's an external camera, not integrated on the device
_externalCamera = true;
}
else
{
// Camera is fixed on the device
_externalCamera = false;
// Only mirror the preview if the camera is on the front panel
_mirroringPreview = (cameraDevice.EnclosureLocation.Panel == Windows.Devices.Enumeration.Panel.Front);
}
// Initialize rotationHelper
_rotationHelper = new CameraRotationHelper(cameraDevice.EnclosureLocation);
_rotationHelper.OrientationChanged += RotationHelper_OrientationChanged;
await StartPreviewAsync();
UpdateCaptureControls();
}
return;
}
}
EDIT SetUpBasedOnStateAsync(用
コード)方法:
private async Task SetUpBasedOnStateAsync()
{
// Avoid reentrancy: Wait until nobody else is in this function.
while (!_setupTask.IsCompleted)
{
await _setupTask;
}
// We want our UI to be active if
// * We are the current active page.
// * The window is visible.
// * The app is not suspending.
bool wantUIActive = _isActivePage && Window.Current.Visible && !_isSuspending;
if (_isUIActive != wantUIActive)
{
_isUIActive = wantUIActive;
Func<Task> setupAsync = async() =>
{
if (wantUIActive)
{
await SetupUiAsync();
await InitializeCameraAsync();
}
else
{
await CleanupCameraAsync();
await CleanupUiAsync();
}
};
_setupTask = setupAsync();
}
await _setupTask;
}
こんにちは、返信ありがとうございます! :) SetUpBasedOnStateAsyncメソッドのコードを追加しました。どのようにコードを変更するのですか?コードで何を修正する必要がありますか? – thalassophile
@thalassophile別のページに移動したときにカメラがあるかどうかをコードがチェックすると言った理由はわかりませんでした。 MS公式コードサンプルをテストすると、別のページに移動すると、コードはカメラを検出する代わりにカメラをクリーンアップします。 –
私は、システムが検出された場合、カメラデバイスが見つからないことをユーザーに知らせるメッセージダイアログを追加しました。 Webカメラが接続されていないデスクトップで試してみましたが、メッセージがポップアップして、コマンドボタンをクリックしてホームページに戻りました...しかし、アプリケーションを最小化して再び開くと、見つかったカメラデバイスはありません。 – thalassophile