2017-11-16 64 views
0

UWP 응용 프로그램에 카메라 기능이 있습니다.이 기능 중 하나는 실제로 연결된 카메라 장치가 있는지 여부를 실제로 확인하는 것입니다. 이 예제 세트를 따라이 기능을 개발했습니다. CameraStarterKitUWP 응용 프로그램에서 다른 페이지로 이동했을 때 웹캠을 중지하는 방법

웹캠이나 카메라가없는 컴퓨터에서 코드를 테스트하면 메시지 프롬프트가 표시됩니다. 그러나 나는 응용 프로그램을 최소화하고 다시 열 때 프롬프트가 나타나기 때문에 페이지를 종료 할 때 카메라 장치가 있는지 여부를 확인하는 코드가 여전히 실행되고 있다고 생각합니다.

어쨌든 다른 페이지로 이동할 때 카메라가 연결되어 있는지 확인하기 위해 코드를 중지 할 수 있습니까?

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; 
    } 

답변

0

: SetUpBasedOnStateAsync() 방법

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

코드 : 연결된 카메라가 있으면 여기

은 확인을위한 코드이며 어쨌든 나는 그걸 막을 수 있니? 다른 페이지로 이동할 때 카메라가 있는지 여부를 확인하는 코드가 연결되어 있습니까? 다른 페이지로 이동하면

OnNavigatingFrom 핸들러 메소드가 호출 될 것이다, 당신은 SetUpBasedOnStateAsync 방법은 그것을 호출 할 것이라고 볼 수 있었다. 이 방법에서는 InitializeCameraAsync 메서드가 호출됩니다. 따라서 SetUpBasedOnStateAsync 방법에서 일부 회선 코드 만 변경하면됩니다.

는하지만 생각 나는 응용 프로그램을 최소화하고 다시 그것을 열 때 프롬프트가 표시되고 나는 페이지를 종료 할 때 카메라 장치가 여전히 뒤에 실행이 있는지 여부를 확인하는 코드.

정상적인 동작입니다. 앱을 최소화하고 열면 코드는 if 블록으로 들어갑니다. 이 행동을 원하지 않으면. 공식 코드 샘플을 변경하는 대신 새 프로젝트를 만들 것을 제안합니다. 왜냐하면 당신은 많은 장소를 바꿀 필요가 있기 때문입니다.

+0

안녕하세요, 답장을 보내 주셔서 감사합니다. :) SetUpBasedOnStateAsync 메서드에 대한 코드를 추가했습니다. 어떻게 코드를 변경합니까? 또는 코드에서 무엇을 수정해야합니까? – thalassophile

+0

@thalassophile 다른 페이지로 이동할 때 카메라가 있는지 여부를 코드가 검사 할 것이라고 말한 이유를 알지 못했습니다. MS 공식 코드 샘플을 테스트하면 다른 페이지로 이동할 때 코드가 카메라를 감지하는 대신 카메라를 정리합니다. –

+0

시스템에 감지 된 카메라 장치가 없다는 메시지 대화 상자를 추가했습니다. 웹캠이 연결되지 않은 바탕 화면에서 메시지를 팝업으로 표시하고 명령 단추를 클릭하면 내 홈 페이지로 돌아갔습니다 ... 그러나 응용 프로그램을 최소화하고 다시 열면 메시지 대화 상자에 발견 된 카메라 장치가 없습니다. – thalassophile