Пример #1
0
        public async Task CS_W_MediaReader_LumiaEffect()
        {
            using (var mediaReader = await MediaReader.CreateFromPathAsync("ms-appx:///car.mp4", AudioInitialization.Deselected, VideoInitialization.Nv12))
                using (var mediaResult = await mediaReader.VideoStream.ReadAsync())
                {
                    var streamProperties = mediaReader.VideoStream.GetCurrentStreamProperties();
                    int width            = (int)streamProperties.Width;
                    int height           = (int)streamProperties.Height;
                    Assert.AreEqual(320, width);
                    Assert.AreEqual(240, height);

                    var inputSample = (MediaSample2D)mediaResult.Sample;
                    Assert.AreEqual(MediaSample2DFormat.Nv12, inputSample.Format);
                    Assert.AreEqual(320, inputSample.Width);
                    Assert.AreEqual(240, inputSample.Height);

                    using (var outputSample = new MediaSample2D(MediaSample2DFormat.Nv12, width, height))
                    {
                        Assert.AreEqual(MediaSample2DFormat.Nv12, outputSample.Format);
                        Assert.AreEqual(320, outputSample.Width);
                        Assert.AreEqual(240, outputSample.Height);

                        using (var inputBuffer = inputSample.LockBuffer(BufferAccessMode.Read))
                            using (var outputBuffer = outputSample.LockBuffer(BufferAccessMode.Write))
                            {
                                // Wrap MediaBuffer2D in Bitmap
                                var inputBitmap = new Bitmap(
                                    new Size(width, height),
                                    ColorMode.Yuv420Sp,
                                    new uint[] { inputBuffer.Planes[0].Pitch, inputBuffer.Planes[1].Pitch },
                                    new IBuffer[] { inputBuffer.Planes[0].Buffer, inputBuffer.Planes[1].Buffer }
                                    );
                                var outputBitmap = new Bitmap(
                                    new Size(width, height),
                                    ColorMode.Yuv420Sp,
                                    new uint[] { outputBuffer.Planes[0].Pitch, outputBuffer.Planes[1].Pitch },
                                    new IBuffer[] { outputBuffer.Planes[0].Buffer, outputBuffer.Planes[1].Buffer }
                                    );

                                // Apply effect
                                var effect = new FilterEffect();
                                effect.Filters = new IFilter[] { new WatercolorFilter() };
                                effect.Source  = new BitmapImageSource(inputBitmap);
                                var renderer = new BitmapRenderer(effect, outputBitmap);
                                await renderer.RenderAsync();
                            }

                        // Save the file
                        var folder = await KnownFolders.PicturesLibrary.CreateFolderAsync("MediaCaptureReaderTests", CreationCollisionOption.OpenIfExists);

                        var file = await folder.CreateFileAsync("CS_W_MediaReader_TestLumiaEffect.jpg", CreationCollisionOption.ReplaceExisting);

                        await outputSample.SaveToFileAsync(file, ImageCompression.Jpeg);

                        Logger.LogMessage("Saved {0}", file.Path);
                    }
                }
        }
Пример #2
0
        private unsafe void ProcessSample(MediaSample2D sample)
        {
            using (var buffer = sample.LockBuffer(BufferAccessMode.ReadWrite))
            {
                int   width  = buffer.Width;
                int   height = buffer.Height;
                int   pitch  = (int)buffer.Planes[0].Pitch;
                byte *data   = buffer.Planes[0].Buffer.GetData();

                for (int i = 0; i < height; i++)
                {
                    for (int j = 0; j < width; j++)
                    {
                        int pos = i * pitch + 4 * j;
                        data[pos + 0] = (byte)Math.Min(255, 2 * data[pos + 0]); // B
                        data[pos + 1] = (byte)Math.Min(255, 2 * data[pos + 1]); // G
                        data[pos + 2] = (byte)Math.Min(255, 2 * data[pos + 2]); // R
                    }
                }
            }
        }