From b36c183e2d3c231209b776f48d41e54b5d1125cd Mon Sep 17 00:00:00 2001 From: lxmou666 <772765102@qq.com> Date: Wed, 30 Dec 2020 23:41:28 +0800 Subject: [PATCH] =?UTF-8?q?=E8=A7=86=E9=A2=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- JianGongYun/JianGongYun.csproj | 8 +++--- JianGongYun/TRTC/LiveClassroom.cs | 19 +++++++------- .../TRTC/Utils/WriteableBitmapToMat.cs | 26 ++++++++++++------- 3 files changed, 29 insertions(+), 24 deletions(-) diff --git a/JianGongYun/JianGongYun.csproj b/JianGongYun/JianGongYun.csproj index aa6d4da..354b709 100644 --- a/JianGongYun/JianGongYun.csproj +++ b/JianGongYun/JianGongYun.csproj @@ -16,12 +16,12 @@ - - + + - + @@ -52,7 +52,7 @@ - 4.5.1.20201226 + 4.5.1.20201229 diff --git a/JianGongYun/TRTC/LiveClassroom.cs b/JianGongYun/TRTC/LiveClassroom.cs index c441623..ed30a0c 100644 --- a/JianGongYun/TRTC/LiveClassroom.cs +++ b/JianGongYun/TRTC/LiveClassroom.cs @@ -272,10 +272,10 @@ namespace JianGongYun.TRTC public static void VedioRecordTask(TXLiteAVVideoView view, TRTCVideoStreamType streamType) { var end = false; - ConcurrentQueue bitmaps = new ConcurrentQueue(); + ConcurrentQueue bitmaps = new ConcurrentQueue(); view.OnRenderVideoFrameHandler += (b) => { - bitmaps.Enqueue(b.ToBitmap()); + bitmaps.Enqueue(b.ToMat()); }; view.OnViewRemove += () => { @@ -288,7 +288,6 @@ namespace JianGongYun.TRTC var imgTemp = Path.Combine(_recoderDir, $"{streamType}.png"); var videoFile = Path.Combine(_recoderDir, $"{Util.TimeStr()}_{streamType}.avi"); VideoWriter vw = new VideoWriter(); - Mat mat = default; bool videoWriterInit = false; while (!end || bitmaps.Count > 0) { @@ -297,26 +296,26 @@ namespace JianGongYun.TRTC Thread.Sleep(100); continue; } - if (bitmaps.TryDequeue(out var bitmap)) + if (bitmaps.TryDequeue(out var mat)) { - bitmap.Save(imgTemp, System.Drawing.Imaging.ImageFormat.Png); - bitmap.Dispose(); - mat = Cv2.ImRead(imgTemp, ImreadModes.AnyColor); - Cv2.ImShow(streamType.ToString(), mat); - Cv2.WaitKey(1); + //mat.SaveImage(imgTemp); + //mat.Dispose(); + //mat = Cv2.ImRead(imgTemp, ImreadModes.AnyColor); + //Cv2.ImShow(streamType.ToString(), mat); + //Cv2.WaitKey(1); if (!videoWriterInit) { vw.Open(videoFile, FourCC.H264, settingWindowViewModel.EncParams.videoFps, mat.Size()); videoWriterInit = true; } vw.Write(mat); + mat.Dispose(); } else { Thread.Sleep(100); } } - mat?.Dispose(); vw?.Release(); vw?.Dispose(); if (File.Exists(imgTemp)) diff --git a/JianGongYun/TRTC/Utils/WriteableBitmapToMat.cs b/JianGongYun/TRTC/Utils/WriteableBitmapToMat.cs index 579e8ce..81b838b 100644 --- a/JianGongYun/TRTC/Utils/WriteableBitmapToMat.cs +++ b/JianGongYun/TRTC/Utils/WriteableBitmapToMat.cs @@ -17,16 +17,10 @@ namespace JianGongYun.TRTC.Utils /// public static Mat ToMat(this WriteableBitmap writeableBitmap) { - using (MemoryStream outStream = new MemoryStream()) + using (var bmp = writeableBitmap.ToBitmap()) { - BitmapEncoder enc = new PngBitmapEncoder(); - 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; - } + var mat = OpenCvSharp.Extensions.BitmapConverter.ToMat(bmp); + return mat; } } /// @@ -38,11 +32,23 @@ namespace JianGongYun.TRTC.Utils { using (MemoryStream outStream = new MemoryStream()) { - BitmapEncoder enc = new PngBitmapEncoder(); + //System.Windows.Media.Imaging.WmpBitmapEncoder + BitmapEncoder enc = new BmpBitmapEncoder(); enc.Frames.Add(BitmapFrame.Create(writeableBitmap)); enc.Save(outStream); return new System.Drawing.Bitmap(outStream); } } + + public static string ToFile(this WriteableBitmap writeableBitmap, string fileName) + { + using (var file = File.Create(fileName)) + { + BitmapEncoder enc = new BmpBitmapEncoder(); + enc.Frames.Add(BitmapFrame.Create(writeableBitmap)); + enc.Save(file); + return fileName; + } + } } }