Search code examples
encodingcamerauwppreview

UWP Camera preview: "No suitable transform was found to encode or decode"


I'm previewing a camera with a CaptureElement in UWP. But why do i get the error: "No suitable transform was found to encode or decode" when using certain resolutions? It happens when using a webcam

I don't get the error when i use this method: mediacapture.VideoDeviceController.SetMediaStreamPropertiesAsync(...). but if i use this method, the captureelement doesn't get scaled to the resolution i want (capture element has it's strech attribute set to none because i don't want quality loss)

        try
        {
            MediaCaptureInitializationSettings mediacapturesettings = new MediaCaptureInitializationSettings { VideoDeviceId = DeviceInfo.Id };
            await mediacapture.InitializeAsync(mediacapturesettings);
        }
        catch (Exception exception)
        {
            var dialog = new MessageDialog(exception.Message);
            await dialog.ShowAsync();
        }


        captureelement.Source = mediacapture;
        captureelement.FlowDirection = Windows.UI.Xaml.FlowDirection.LeftToRight;

        await mediacapture.StartPreviewAsync();
        // await mediacapture.VideoDeviceController.SetMediaStreamPropertiesAsync(MediaStreamType.VideoPreview, Resolution.EncodingProperties);
        await mediacapture.SetEncodingPropertiesAsync(MediaStreamType.VideoPreview, Resolution.EncodingProperties,null);

        VideoRotation currentrotation = Rotation;
        mediacapture.SetPreviewRotation(currentrotation);

Solution

  • I don't get the error when i use this method: mediacapture.VideoDeviceController.SetMediaStreamPropertiesAsync(...). but if i use this method, the captureelement doesn't get scaled to the resolution i want .

    It only happens the first time i callstartpreview().

    I've consulted internally and got a response. This problem is probably graphic driver related. Your graphic driver may require your camera device preview to be in running state before you call MediaCapture.SetEncodingPropertiesAsync, so you got this error and only got it at the first time.

    i want to know the difference between the two methods.

    Difference:

    1. MediaCapture.SetEncodingPropertiesAsync set the property on the camera sink. You can set resolutions and rotation here that the camera does not support and then an encoder will do work to convert to desired format.
    2. MediaCapture.VideoDeviceController.SetMediaStreamPropertiesAsync set the properties on the source. You can only set the configurations that the camera supports.

    So the size of preview window is controlled by the Xaml CaptureElement. If you are using VideoDeviceController.SetMediaStreamPropertiesAsync and want to resize the preview window, you can only set the Stretch="UniformToFill" to let the preview window fill the parent element.