using OpenCvSharp;
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Windows.Media.Imaging;

namespace JianGongYun.TRTC.Utils
{
    public static class WriteableBitmapToMat
    {
        /// <summary>
        /// WriteableBitmap转Mat
        /// </summary>
        /// <param name="writeableBitmap"></param>
        /// <returns></returns>
        public static Mat ToMat(this WriteableBitmap writeableBitmap)
        {
            using (MemoryStream outStream = new MemoryStream())
            {
                BitmapEncoder enc = new JpegBitmapEncoder();
                enc.Frames.Add(BitmapFrame.Create(writeableBitmap));
                enc.Save(outStream);
                using (System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(outStream))
                {
                    var mat = OpenCvSharp.Extensions.BitmapConverter.ToMat(bmp);
                    return mat;
                }
            }
        }
    }
}
