Hello, I making a program that connects to a usb webcam using videodev2 that I get from sdpo package.
I have any problem to capture a frame. But I want to save it as jpg format.
repeat
begin
FillChar(vBuf, sizeof(v4l2_buffer), 0);
vbuf._type := V4L2_BUF_TYPE_VIDEO_CAPTURE;
vbuf.memory := V4L2_MEMORY_MMAP;
//Capturar imagen
if DynIoctl(videohandle, VIDIOC_DQBUF, @vbuf) < 0 then
begin
raise Exception.Create('Fallo durante la captura');
end;
end;
until vBuf.bytesused > 0;
VideoBufferIndex := vbuf.index;
FramePtr := VideoBuffer[VideoBufferIndex];
Datos := TMemoryStream.Create;
Datos.SetSize(vHeight * vWidth);
Datos.Position := 0;
size := vHeight * vWidth;
// case PixelFormat of
// uvcpf_YUYV:
//YUYV_to_Gray function converted
for i := 0 to (size div 2) - 1 do
begin
g := FramePtr^ and $FF;
r := g;
b := g;
//dest^ := (r shl 16) or (g shl 8) or (b);
temp := (r shl 16) or (g shl 8) or (b);
Datos.Write(temp, SizeOf(Temp));
g := (FramePtr^ shr 16) and $FF;
r := g;
b := g;
//dest^ := (r shl 16) or (g shl 8) or (b);
temp := (r shl 16) or (g shl 8) or (b);
Datos.Write(temp, SizeOf(Temp));
Inc(FramePtr);
//Inc(dest);
end;
Imagen := TFPMemoryImage.Create(vWidth,vHeight);
Imagen.UsePalette:=true;
MiPaleta.Alpha:=0;
MiPaleta.Blue:=155;
MiPaleta.Green:=75;
MiPaleta.Red:=56;
f := Imagen.Palette.Add(MiPaleta);
DAtos.Position:=0;
For X:= 0 To Imagen.Width-1 do
begin
For Y := 0 To Imagen.Height-1 do
begin
pixel := Datos.ReadByte;
Imagen.Pixels[x,y] := pixel;
end;
end;
// case PixelFormat of
// uvcpf_YUYV:
// YUYV_to_Gray(PLongWord(FramePtr), (pdatos),
// vWidth * vHeight);
// uvcpf_YUV420:
// YUV420_to_Gray(FramePtr, (pdatos), vWidth * vHeight);
// uvcpf_RGB24:
// RGB24_to_TrueColor(PRGB24Pixel(FramePtr), (pdatos), vWidth * vHeight);
// uvcpf_BGR24:
// BGR24_to_TrueColor(PRGB24Pixel(FramePtr), (pdatos), vWidth * vHeight);
// end;
j := TFPWriterJPEG.Create;
j.CompressionQuality:=75;
Imagen.SaveToFile('pepe.jpg',j);
In this code, I got a image from camera, and I convert to YUYV_to_Gray and store into TMemorystream. But I'm lost with palette. I think I should create 255 palettes, which values?. Or there is other way to do the job?.
I attach all code.
Programmed in Linux 20.04 and Lazarus 2.10
Thanks in advance
/BlueIcaro