This commit is contained in:
lxmou666 2020-12-30 23:41:28 +08:00
parent bc46d869a0
commit b36c183e2d
3 changed files with 29 additions and 24 deletions

View File

@ -16,12 +16,12 @@
<PackageReference Include="CommonServiceLocator" Version="2.0.5" />
<PackageReference Include="MvvmLightLibs" Version="5.4.1.1" />
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
<PackageReference Include="OpenCvSharp4.runtime.win" Version="4.5.1.20201226" />
<PackageReference Include="OpenCvSharp4.WpfExtensions" Version="4.5.1.20201226" />
<PackageReference Include="OpenCvSharp4.runtime.win" Version="4.5.1.20201229" />
<PackageReference Include="OpenCvSharp4.WpfExtensions" Version="4.5.1.20201229" />
<PackageReference Include="zlib.net" Version="1.0.4" />
</ItemGroup>
<ItemGroup Condition="'$(TargetFramework)' != 'net45'">
<PackageReference Include="System.Text.Encoding.CodePages" Version="4.7.0" />
<PackageReference Include="System.Text.Encoding.CodePages" Version="5.0.0" />
</ItemGroup>
<ItemGroup>
<Compile Update="Views\Window2.xaml.cs">
@ -52,7 +52,7 @@
</ItemGroup>
<ItemGroup Condition="'$(TargetFramework)' == 'netcoreapp3.1'">
<PackageReference Include="OpenCvSharp4">
<Version>4.5.1.20201226</Version>
<Version>4.5.1.20201229</Version>
</PackageReference>
</ItemGroup>
<Target Name="PostBuild" AfterTargets="PostBuildEvent">

View File

@ -272,10 +272,10 @@ namespace JianGongYun.TRTC
public static void VedioRecordTask(TXLiteAVVideoView view, TRTCVideoStreamType streamType)
{
var end = false;
ConcurrentQueue<Bitmap> bitmaps = new ConcurrentQueue<Bitmap>();
ConcurrentQueue<Mat> bitmaps = new ConcurrentQueue<Mat>();
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))

View File

@ -17,16 +17,10 @@ namespace JianGongYun.TRTC.Utils
/// <returns></returns>
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;
}
}
/// <summary>
@ -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;
}
}
}
}