settings for avalonia

libcamera bugfix
This commit is contained in:
EugeneTes
2026-04-01 14:05:37 +02:00
parent 1032a5a28c
commit bb861834fd
26 changed files with 1139 additions and 59 deletions

View File

@@ -8,9 +8,9 @@ namespace Hawkeye.VisionBuilder.UI.Sources.Raspberry.Libcamera
{
public class LibcameraImageSource : IImageSource, IVisionBuilderModule
{
private CaptureSettings _settings;
private LibcameraCaptureSettings _settings;
public LibcameraImageSource(CaptureSettings settings)
public LibcameraImageSource(LibcameraCaptureSettings settings)
{
_settings = settings;
}
@@ -41,72 +41,80 @@ namespace Hawkeye.VisionBuilder.UI.Sources.Raspberry.Libcamera
private void StartLoop()
{
var psi = new ProcessStartInfo
try
{
FileName = "rpicam-vid",
Arguments = _settings.BuildArguments(),
RedirectStandardOutput = true,
UseShellExecute = false
};
var psi = new ProcessStartInfo
{
FileName = "rpicam-vid",
Arguments = _settings.BuildArguments(),
RedirectStandardOutput = true,
UseShellExecute = false
};
using (Process process = Process.Start(psi))
using (BinaryReader br = new BinaryReader(process.StandardOutput.BaseStream))
{
byte[] imageBuffer = new byte[1024 * 1024];
// decode MJPEG buffer
var buff = br.ReadBytes(ChunkSize);
int frameId = 0;
while (true)
using (Process process = Process.Start(psi))
using (BinaryReader br = new BinaryReader(process.StandardOutput.BaseStream))
{
byte[] imageBuffer = new byte[1024 * 1024];
var imageStart = Find(buff, JpegHeader);
if (imageStart != -1)
// decode MJPEG buffer
var buff = br.ReadBytes(ChunkSize);
int frameId = 0;
while (true)
{
var size = buff.Length - imageStart;
Array.Copy(buff, imageStart, imageBuffer, 0, size);
while (true)
var imageStart = Find(buff, JpegHeader);
if (imageStart != -1)
{
buff = br.ReadBytes(ChunkSize);
var imageEnd = Find(buff, JpegFooter);
if (imageEnd != -1)
var size = buff.Length - imageStart;
Array.Copy(buff, imageStart, imageBuffer, 0, size);
while (true)
{
var frame = new byte[size + imageEnd];
Array.Copy(imageBuffer, frame, size);
Array.Copy(buff, 0, frame, size, imageEnd);
// process frame
frameId++;
var decoded = Mat.ImDecode(frame);
buff = br.ReadBytes(ChunkSize);
var imageEnd = Find(buff, JpegFooter);
if (imageEnd != -1)
{
var frame = new byte[size + imageEnd];
Array.Copy(imageBuffer, frame, size);
Array.Copy(buff, 0, frame, size, imageEnd);
// process frame
frameId++;
var decoded = Mat.ImDecode(frame);
_imageChannel.Writer.WriteAsync(decoded, CancellationToken.None);
_imageChannel.Writer.WriteAsync(decoded, CancellationToken.None);
// copy the leftover data to the start
Array.Copy(buff, imageEnd, buff, 0, buff.Length - imageEnd);
// fill the remainder of the buffer with new data and start over
var temp = br.ReadBytes(imageEnd);
Array.Copy(temp, 0, buff, buff.Length - imageEnd, temp.Length);
break;
// copy the leftover data to the start
Array.Copy(buff, imageEnd, buff, 0, buff.Length - imageEnd);
// fill the remainder of the buffer with new data and start over
var temp = br.ReadBytes(imageEnd);
Array.Copy(temp, 0, buff, buff.Length - imageEnd, temp.Length);
break;
}
// copy all of the data to the imageBuffer
Array.Copy(buff, 0, imageBuffer, size, buff.Length);
size += buff.Length;
}
// copy all of the data to the imageBuffer
Array.Copy(buff, 0, imageBuffer, size, buff.Length);
size += buff.Length;
}
else
{
Console.WriteLine("JPEG header not found.");
break;
}
}
else
{
Console.WriteLine("JPEG header not found.");
break;
}
process.Kill();
}
process.Kill();
}
catch (Exception e)
{
Serilog.Log.Error($"Error starting libcamera: {e}");
}
}