1,183
社区成员
发帖
与我相关
我的任务
分享
type
VideoData=record
buf:array[0..8079] of byte; //压缩后的视频数据
Num:integer;//帧数据过大时,分几个数据包发送,数据包在这一帧中的编号
IsLast:boolean;//是否是这一帧的最后一个数据包
end;
Var
FCV: TCOMPVARS;//帧压缩结构
FInInfo: TBitmapInfo;//压缩时输入结构
FOutInfo: TBitmapInfo;//压缩时输出结构
FoutActSize: DWORD;//压缩后帧数据的大小
Buffer:^byte; //压缩后帧数据地址
Buf: array of Byte;// 压缩后帧数据
procedure TForm1.VideoCap1VideoStream(sender: TObject; lpVhdr: PVIDEOHDR);
var
KeyFrame:boolean;
MyVideo:VideoData;
i:integer;
begin
FOutActSize:=0;
MyVideo.Num:=0;
//压缩帧数据
Buffer:= ICSeqCompressFrame(@FCV, 0, lpVHdr^.lpData, @KeyFrame, @FOutActSize);//buffer等于NIL
SetLength(buf,FOutActSize);
Move(Buffer^, Buf[0], FOutActSize);
//当帧数据太大时,分几个数据包发送
while FOutActSize>8080 do
begin
MyVideo.Num:=MyVideo.Num+1;
MyVideo.IsLast:=false;
for i:=0 to 8079 do
begin
MyVideo.buf[i]:= buf[(MyVideo.Num-1)*8080+i];
end;
FOutActSize:=FOutActSize-8080;
// 向客户端发送数据包
VideoSender.SendBuffer('127.0.0.1',1234,MyVideo,sizeof(MyVideo));
end;
{if FOutActSize<8080 then
begin
MyVideo.Num:=MyVideo.Num+1;
MyVideo.IsLast:=true; //当前帧最后一个数据包
for i:=0 to FOutActSize do
begin
MyVideo.buf[i]:= buf[(MyVideo.Num-1)*8080+i];
end; }
//向客户端发送数据包
VideoSender.SendBuffer('127.0.0.1',1234,MyVideo,sizeof(MyVideo));
//end;
application.ProcessMessages;
end;
//填充BMP头结构
procedure TForm1.FillBitmapStruc;
begin
FillChar(FInInfo.bmiHeader, SizeOf(TBitmapInfoHeader), 0);
with FInInfo.bmiHeader do
begin
biBitCount := 24;
biCompression := BI_RGB;
biHeight := 240;
biPlanes := 1;
biSize := SizeOf(TBitmapInfoHeader);
biWidth := 320;
end;
end;
//初始化压缩引擎
procedure TForm1.InitCompressor;
begin
FillChar(FCV, SizeOf(FCV), 0);
with FCV do
begin
dwFlags := ICMF_COMPVARS_VALID;
cbSize := SizeOf(FCV);
fccHandler := mmioFOURCC('d','i','v','x'); //选择压缩引擎,这里选择divx
fccType := ICTYPE_VIDEO;
hic := ICOpen(ICTYPE_VIDEO,fccHandler, ICMODE_COMPRESS);
lDataRate := 780;
lKey := 15;
lQ :=dword(ICQUALITY_DEFAULT);
if hic <> 0 then
begin
FillChar(FOutInfo, SizeOf(FOutInfo), 0);
ICCompressGetFormat(hic, @FInInfo, @FOutInfo);
FInInfo.bmiHeader.biCompression:=BI_RGB;
FOutInfo.bmiHeader.biCompression:=fccHandler;
ICSeqCompressFrameStart(@FCV, @FInInfo);
end;
end;
end;
//FormOnShow事件
procedure TForm1.FormShow(Sender: TObject);
begin
FillBitmapStruc;
InitCompressor;
//设置VideoCap1的相关属性
VideoCap1.DriverIndex:=0;
VideoCap1.CapToFile:=false;
VideoCap1.DriverOpen:=true;
videocap1.VideoPreview:=true;
end;
DWORD dwCkID;
DWORD dwCompFlags;
DWORD dwQuality;
LONG lNumFrames, lFrameNum;
// Assume dwNumFrames is initialized to the total number of frames.
// Assume dwQuality holds the proper quality value (0-10000).
// Assume lpbiOut, lpOut, lpbiIn, and lpIn are initialized properly.
// If OK to start, compress each frame.
if (ICCompressBegin(hIC, lpbiIn, lpbiOut) == ICERR_OK)
{
for ( lFrameNum = 0; lFrameNum < lNumFrames; lFrameNum++)
{
if (ICCompress(hIC, 0, lpbiOut, lpOut, lpbiIn, lpIn,
&dwCkID, &dwCompFlags, lFrameNum,
0, dwQuality, NULL, NULL) == ICERR_OK)
{
// Write compressed data to the AVI file.
// Set lpIn to the next frame in the sequence.
}
else
{
// Handle compressor error.
}
}
ICCompressEnd(hIC); // terminate compression
}
else
{
// Handle the error identifying the unsupported format.
}
DWORD dwCkID;
DWORD dwCompFlags;
DWORD dwQuality;
LONG lNumFrames, lFrameNum;
// Assume dwNumFrames is initialized to the total number of frames.
// Assume dwQuality holds the proper quality value (0-10000).
// Assume lpbiOut, lpOut, lpbiIn, and lpIn are initialized properly.
// If OK to start, compress each frame.
if (ICCompressBegin(hIC, lpbiIn, lpbiOut) == ICERR_OK)
{
for ( lFrameNum = 0; lFrameNum < lNumFrames; lFrameNum++)
{
if (ICCompress(hIC, 0, lpbiOut, lpOut, lpbiIn, lpIn,
&dwCkID, &dwCompFlags, lFrameNum,
0, dwQuality, NULL, NULL) == ICERR_OK)
{
// Write compressed data to the AVI file.
// Set lpIn to the next frame in the sequence.
}
else
{
// Handle compressor error.
}
}
ICCompressEnd(hIC); // terminate compression
}
else
{
// Handle the error identifying the unsupported format.
}
LPVOID ICSeqCompressFrame(
PCOMPVARS pc,
UINT uiFlags,
LPVOID lpBits,
BOOL * pfKey,
LONG * plSize
);
LPVOID ICSeqCompressFrame(
PCOMPVARS pc,
UINT uiFlags,
LPVOID lpBits,
BOOL * pfKey,
LONG * plSize
);