timelapse/Timelapse/Pages/PreviewWindow.razor

113 lines
3.0 KiB
Plaintext

@page "/preview"
@using SixLabors.ImageSharp
@using SixLabors.ImageSharp.PixelFormats
@inject IJSRuntime JsRuntime;
@using Timelapse.Data;
@inject ExtensionProjectService EPS;
<div class="container">
<video id="videoFeed" width="320" height="240" autoplay></video>
<br>
<button @onclick="RecStopToggle" class="btn btn-primary">@RecStopText</button>
<button @onclick="OneXToggle" class="btn btn-primary">@OneXText</button>
</div>
@code {
public void RecStopToggle()
{
Recording = !Recording;
RecStopText = Recording ? "Stop" : "Record";
}
public void OneXToggle()
{
OneX = !OneX;
OneXText = OneX ? "Disable OneX" : "Enable OneX";
}
public static PreviewWindow pw;
protected override async Task OnInitializedAsync()
{
OneXText="Enable OneX";
RecStopText="Record";
StartTimer();
}
public int Width {get {return EPS.Project.Width;}}
public int Height {get {return EPS.Project.Width;}}
protected override async Task OnAfterRenderAsync (bool firstRender)
{
pw=this;
await JsRuntime.InvokeVoidAsync("startVideo", "videoFeed");
}
[JSInvokable]
public void ProcessImage(string imageString)
{
byte[] imageData = Convert.FromBase64String(imageString.Split(',')[1]);
Image<Rgb24> image = Image.Load<Rgb24>(imageData);
Task.Run(async ()=>{ await EPS.SendImageAsync(image);});
}
public string OneXText {get;set;}
public string RecStopText {get;set;}
public bool Recording=false;
public bool OneX =false;
public async Task TakeFrameAsync()
{
await JsRuntime.InvokeVoidAsync("getFrame", "videoFeed",DotNetObjectReference.Create(this));
}
private static System.Timers.Timer aTimer;
public void StartTimer()
{
aTimer = new System.Timers.Timer(1000);
aTimer.Elapsed += CountDownTimer;
aTimer.Enabled = true;
}
double interval;
int frame=0;
public double GetInterval()
{
if(EPS.IsProjectLoaded)
{
if(EPS.Project.Estimated)
{
double projlen = EPS.Project.EstimatedProjectLength.TotalSeconds;
double vidlen=EPS.Project.EstimatedVideoLength.TotalSeconds;
if(vidlen == 0)
{
return 1.0;
}
return projlen / vidlen;
}else{
return EPS.Project.Interval.TotalSeconds;
}
}
return 1.0;
}
double Frames {get {return OneX ? 1.0 : GetInterval(); }}
public void CountDownTimer(Object source, System.Timers.ElapsedEventArgs e)
{
frame++;
if(frame >= Frames)
{
if(Recording && EPS.IsProjectLoaded)
{
Task.Run(async()=>{ await TakeFrameAsync();});
frame=0;
}
}
}
}