Repository: microsoft/WindowsAppSDK

Issue #1034: WinUi 3 desktop app doesn't open camera. and is there any way that can open default system camera and capture image
Created: 2021-07-03T08:36:55Z
CameraCaptureUI captureUI = new CameraCaptureUI();
        StorageFile photo = await captureUI.CaptureFileAsync(CameraCaptureUIMode.Photo);

        if (photo != null)
        {
            CameraCapture.Source = new BitmapImage(new Uri(photo.Path));
        }


CameraCaptureUI  it doesn't open camera. even  CaptureElement  does not find on UI

Duplicate issue #3241: CameraCaptureUI.CaptureFileAsync() returns null
Created: 2022-04-06T10:12:14Z
Describe the bug
See dotnet/maui#5849.
The MAUI MediaPicker.CapturePhotoAsync() method returns null instantly on Windows. Expect to either see camera UI or an exception indicating a problem (e.g. lack of permissions, incorrect config, no camera available, etc).
Digging deeper, I am able to reproduce in a new, empty WinUI3 project:
var captureUi = new CameraCaptureUI();
var result = await captureUi.CaptureFileAsync(CameraCaptureUIMode.Photo);
// We get here straight away, result == null
...
Whereas the lower-level MediaCapture API works as expected:
var devices = await DeviceInformation.FindAllAsync(DeviceClass.VideoCapture);
DeviceInformation device = devices.FirstOrDefault(); // Finds one device, my webcam

MediaCaptureInitializationSettings settings = new MediaCaptureInitializationSettings();
settings.VideoDeviceId = device.Id;

MediaCapture mediaCapture = new MediaCapture();
await mediaCapture.InitializeAsync(settings);

StorageFolder storageFolder = ApplicationData.Current.LocalCacheFolder;
var result = await storageFolder.CreateFileAsync("test.jpg", CreationCollisionOption.ReplaceExisting);

await mediaCapture
    .CapturePhotoToStorageFileAsync(ImageEncodingProperties.CreateJpeg(), result)
    .AsTask();

// result is not null, JPEG file produced has a valid image inside
Captures a picture and displays the filename as expected.
So there doesn't seem to be a hardware / driver / OS / Windows SDK level issue. Seems to be Windows.Media.Capture.CameraCaptureUI that is misbehaving.
I created a WinUI3 app (no MAUI) and the CameraCaptureUI code still returns null. I tried adding more permissions on top of runFullTrust:
  <Capabilities>
    <rescap:Capability Name="runFullTrust" />
    <uap:Capability Name="videosLibrary"/>
    <uap:Capability Name="picturesLibrary"/>
    <DeviceCapability Name="webcam"/>
    <DeviceCapability Name="microphone"/>
  </Capabilities>
Which seems like all relevant capabilities.
Steps to reproduce the bug

Create a new WinUI3 project using the Visual Studio template ("Blank App, Packaged (WinUI 3 in Desktop)
Change myButton_Click() in the generated MainWindow.xaml.cs to:
private async void myButton_Click(object sender, RoutedEventArgs e)
{
    var captureUi = new CameraCaptureUI();
    var result = await captureUi.CaptureFileAsync(CameraCaptureUIMode.Photo);

    myButton.Content = result == null ? "null result" : (result.DisplayName ?? "null DisplayName");
}

Observe output

expect to see camera UI followed by the UI showing a filename
actually see the UI show null result instantly


Add <DeviceCapability Name="webcam" /> to Package.appxmanifest
Repeat steps 1 - 3
Observe same behaviour

Expected behavior
Expect to see camera UI, followed by the button caption showing a filename.
Screenshots
No response
NuGet package version
WinUI 3 - Windows App SDK 1.0
Windows app type

 UWP
 Win32

Device form factor
Desktop
Windows version
Windows 10 (21H1): Build 19043
Additional context
No response

Closing comment (2022-04-06T12:16:30Z): Belongs in WindowsAppSDK repo and dupe of:
#1034
On productboard:
https://portal.productboard.com/winappsdk/1-windows-app-sdk/c/49-support-cameracaptureui?utm_medium=social&utm_source=portal_share