Win10 UWP Custom Video Effect, IBasicVideoEffect

浪子不回头ぞ 提交于 2019-12-08 02:32:16

问题


I am trying to write my own implementation of IBasicVideoEffect to analyze video frames in a Windows 10 UWP application, the end goal is using the Zxing.NET library to scan qr codes.

I cannot add the video effect to the instance of MediaCapture in my code. The line var effect = await _mediaCapture.AddVideoEffectAsync(new VideoEffectDefinition(typeof(MyVideoEffect).FullName), MediaStreamType.VideoPreview); in Win10QR.MainPage.cs throws an exception stating "Class not registered (Exception from HRESULT: 0x80040154 (REGDB_E_CLASSNOTREG))"

MyVideoEffect.cs:

namespace Win10QR
{
    public class MyVideoEffect : IBasicVideoEffect
    {
        public bool IsReadOnly
        {
            get
            {
                return false;
            }
        }

        public IReadOnlyList<VideoEncodingProperties> SupportedEncodingProperties
        {
            get
            {
                var properties = new List<VideoEncodingProperties>();
                properties.Add(VideoEncodingProperties.CreateUncompressed("ARGB32", 640, 480));
                return properties;
            }
        }

        public MediaMemoryTypes SupportedMemoryTypes
        {
            get
            {
                return MediaMemoryTypes.GpuAndCpu;
            }
        }

        public bool TimeIndependent
        {
            get
            {
                return false;
            }
        }

        public void Close(MediaEffectClosedReason reason)
        {

        }

        public void DiscardQueuedFrames()
        {

        }

        public void ProcessFrame(ProcessVideoFrameContext context)
        {
            var resultString = AnalyzeBitmap(context.InputFrame.SoftwareBitmap);

            if (resultString != null)
            {
                Debug.WriteLine(resultString);
            }
        }

        public void SetEncodingProperties(VideoEncodingProperties encodingProperties, IDirect3DDevice device)
        {

        }

        public void SetProperties(IPropertySet configuration)
        {

        }

        private string AnalyzeBitmap(SoftwareBitmap bitmap)
        {
            var reader = new BarcodeReader();
            var writableBitmap = new WriteableBitmap(bitmap.PixelWidth, bitmap.PixelHeight);
            bitmap.CopyToBuffer(writableBitmap.PixelBuffer);

            var result = reader.Decode(writableBitmap);

            if (result != null)
            {
                return result.Text;
            }

            return null;
        }
    }
}

I have tried var effect = await _mediaCapture.AddVideoEffectAsync(new VideoEffectDefinition("MyVideoEffect", MediaStreamType.VideoPreview); and var effect = await _mediaCapture.AddVideoEffectAsync(new VideoEffectDefinition("Win10QR.MyVideoEffect", MediaStreamType.VideoPreview);, both throw the same exception as above.

However, var effect = await _mediaCapture.AddVideoEffectAsync(new VideoEffectDefinition("Windows.Media.VideoStabilizationEffect", MediaStreamType.VideoPreview); seems to work for video stabilization.

Out of curiosity, I tried putting any old class in, ex: var effect = await _mediaCapture.AddVideoEffectAsync(new VideoEffectDefinition("Windows.UI.Xaml.DataTemplate", MediaStreamType.VideoPreview); and a different exception was thrown: "No such interface supported\r\n\r\nFailed to activate video effect", which makes sense. This leads me to believe that my implementation of the interface is not the problem.

Is there something in my Package.appxmanifest or elsewhere that I need to do to get it to find my video effect class? Both classes are in the Win10QR namespace.

Thanks for looking.


回答1:


The leading cause of the registration issue is likely that you have your class file in the same project as your app. Is that the case? Due to the way WinRT activation works (basically COM activation under the hood), effects need to be implemented in a separate WinRT Class Library project (WinRT activates it as an in-proc component). If you create a separate WinRT Class Library, put this effect class in it, and then add a reference to use it from the main app, this problem should go away.

Frustrating I know :). I ran into this myself when we started implementing this work and it's very common for folks to run into this issue when they start working with IBasicVideoEffect. I'm trying to investigate possible tooling or runtime fixes in the future that will eliminate the need for this.

If this doesn't help, let me know and I'll try to figure out other possible reasons :).



来源:https://stackoverflow.com/questions/32079858/win10-uwp-custom-video-effect-ibasicvideoeffect

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!