请教:C++代码转换成Delphi(视频处理)

gggxkj999 2008-03-28 02:29:27
ULONG m_ulSampleDone;
int m_ulRGBMax;
ULONG* pSamplePicture;
m_ulRGBMax = 0;
ULONG VideoLine,VideoCol;
ULONG VideoMode;


SFCG_GetVideoMode( VideoMode, m_uCardNum );
if( VideoMode )
{
VideoLine = 720;
VideoCol = 576;
}
else
{
VideoLine = 720;
VideoCol = 488;
}

m_str.Format(_T(" "));
m_StartEndTime_str.Format(_T(" "));
m_SpendTime_str.Format(_T(" "));

pSamplePicture = new ULONG[VideoLine*VideoCol]; //Sampled data
if( !SFCG_SetVideoSample(m_uCardNum) )
{
MessageBox( _T("Set video sample error!"),_T("Error") );
return;
}
while( !SFCG_GetIfVideoSampleDone( m_ulSampleDone, m_uCardNum ) );

SFCG_GetVideoSampleData( pSamplePicture, m_uCardNum );

CClientDC dc(this);

if (m_hBitmap)
::DeleteObject(m_hBitmap);

//成员变量 位图句柄,用于在视图重绘时
m_hBitmap = ::CreateCompatibleBitmap(dc.GetSafeHdc(),VideoLine,VideoCol);

if(!m_hBitmap) //生成句柄不成功
return;

CBitmap bmp;
bmp.Attach(m_hBitmap);
BITMAP bm;
unsigned int PixelBytes = 0;
unsigned int i;

bmp.GetBitmap(&bm);
BYTE* lpBits = NULL;
BYTE r,g,b;

switch(bm.bmBitsPixel)
{
case 16: // R:G:B = 5:6:5
PixelBytes = 2;
lpBits = new BYTE[bm.bmWidth*bm.bmHeight*PixelBytes];
for(i = 0;i<VideoCol*VideoLine;i++)
{
r = BYTE(pSamplePicture[i]>>16); //Get Red Component
g = BYTE(pSamplePicture[i]>>8); //Get Green Component
b = BYTE(pSamplePicture[i]); //Get Blue Component
lpBits[2*i] = (0xe0&(g<<3))|(b>>3);
lpBits[2*i+1] = (r&0xf8)|(g>>5);
}

break;
case 24:
PixelBytes = 3; // R:G:B = 8:8:8
lpBits = new BYTE[bm.bmWidth*bm.bmHeight*PixelBytes];
for(i = 0;i<VideoCol*VideoLine;i++)
{
r = BYTE(pSamplePicture[i]>>16); //Get Red Component
g = BYTE(pSamplePicture[i]>>8); //Get Green Component
b = BYTE(pSamplePicture[i]); //Get Blue Component
lpBits[3*i] = b;
lpBits[3*i+1] = g;
lpBits[3*i+2] = r;
}
break;
default:
break;
}

if(lpBits) // Not 32 bits true color
bmp.SetBitmapBits(bm.bmWidth*bm.bmHeight*PixelBytes,lpBits);
else // 32 bits true color
bmp.SetBitmapBits(VideoCol*VideoLine*4,pSamplePicture);

// Display sampled picture
CDC mem;
mem.CreateCompatibleDC(&dc);
CBitmap* pOld = mem.SelectObject(&bmp);
dc.BitBlt(0,0,VideoLine,VideoCol,&mem,0,0,SRCCOPY);
mem.SelectObject(pOld);
mem.DeleteDC();
bmp.Detach();

if(lpBits)delete lpBits;
delete pSamplePicture;
...全文
297 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
meiZiNick 2008-05-01
  • 打赏
  • 举报
回复
我也想了解,谢谢LZ.
knowledge_Is_Life 2008-05-01
  • 打赏
  • 举报
回复
都是很好的建议! 值得学习
gggxkj999 2008-04-10
  • 打赏
  • 举报
回复
问题搞定了,一直忘了帖上来与大家一起分享,处理得有一点笨,希望大家给点建议!


//获取视频的制式,VideoMode为1时,工作在PAL制,为0时,工作在NTSC制
SFCG_GetVideoMode(@VideoMode, m_uCardNum - 1);
if Boolean(VideoMode) then
begin
VideoLine := 720;
VideoCol := 576;
end
else
begin
VideoLine := 720;
VideoCol := 488;
end;

bmp := TBitmap.Create;
bmp.PixelFormat := pf24Bit;
bmp.width := VideoLine;
bmp.Height := VideoCol;

i := VideoLine * VideoCol;

GetMem(pSamplePicture, i * 4);

if not SFCG_SetVideoSample(m_uCardNum - 1) then //设置板卡进入采样状态
begin
Application.MessageBox('板卡图像采样错误!', '提示', MB_Ok + MB_ICONINFORMATION);
Exit;
end;

{SFCG_GetIfVideoSampleDone获取采样是否完成, true采样完成, false采样未完成
如果采样未完成,程序将始终在此循环直到采样完成}
while true do
begin
if SFCG_GetIfVideoSampleDone(@m_ulSampleDone, m_uCardNum - 1) then
Break;
end;

{SFCG_GetVideoSampleData得到采样数据,得到的是图像中每个像素点的ARGB格式的采样数据,
高位为A,依次是R,G,B
对PAL制信号,采样的有效行为576行,每行的有效点为720点}
SFCG_GetVideoSampleData(pSamplePicture, m_uCardNum - 1);

{读取每个像素点的RGB值,并按缓存中的排列依次排列在控件}
for j := 0 to i - 1 do
begin
R := Byte(((pULONG(DWORD(pSamplePicture) + j * 4)^) shr 16) and $FF);
G := Byte(((pULONG(DWORD(pSamplePicture) + j * 4)^) shr 8) and $FF);
B := Byte(pULONG(DWORD(pSamplePicture) + j * 4)^ and $FF);

if j <= 719 then
begin
Row := j;
Col := 0;
end
else
begin
Row := j mod 720;
Col := j div 720;
end;

bmp.Canvas.Pixels[Row, Col] := RGB(R, G, B);
Application.ProcessMessages;
end;

Image1.Picture.Bitmap.Assign(bmp);
Image1.Repaint;
Bmp.Free;
FreeMem(pSamplePicture, i * 4);
gggxkj999 2008-04-09
  • 打赏
  • 举报
回复
谢谢大家的帮忙,非常受益了!
Rex_love_Burger 2008-04-07
  • 打赏
  • 举报
回复
还有点时间,作图部分也给你翻译几句先!
var
dc : HDC;
bmp : TBitmap;
m_hBitmap : HBitMap;
lpBits : pbyte;
r,g,b : byte;
PixelBytes,i : Cardinal;
dc:=GetDC(Form1.handle);//这里的handle就是你显示窗口的handle,你用pannel等也一样

if (m_hBitmap<>0) then DeleteObject(m_hBitmap);//这句你自己看是否有必要,如果前面m_hBitmap没创建过就不用这句

//成员变量 位图句柄,用于在视图重绘时
m_hBitmap := CreateCompatibleBitmap(dc,VideoLine,VideoCol);
if(m_hBitmap=nil) then exit; //生成句柄不成功
bmp.Handle:=m_hBitmap;
PixelBytes = 0;


case bmp.Pixelformat of
{
pf16bit: // R:G:B = 5:6:5
begin
PixelBytes := 2;
lpBits := GetMemory(bmp.Width*bmp.Height*PixelBytes];
for i:=0 to VideoCol*VideoLine-1 do
begin
r := BYTE(pSamplePicture[i] shl 16); //Get Red Component
g := BYTE(pSamplePicture[i] shl 8); //Get Green Component
b := BYTE(pSamplePicture[i]); //Get Blue Component
lpBits[2*i] := ($E0 and(g shr 3))or(b shl 3);
lpBits[2*i+1] := (r and $f8)or(g shl 5);
end;
end;

pf24bit:
begin
//...........这就自己看这办
end;
end;

if (lpBits<>nil) then // Not 32 bits true color
//这里什么都不做
else // 32 bits true color
bmp.PixelFormat:=pf32bit;

// Display sampled picture
下面的是创建内存位图然后bitblt到屏幕或位图
然后释放创建的相关资源,如位图、HDC等,你自己应该可以搞定了我就不写了,这下真没时间了


Rex_love_Burger 2008-04-07
  • 打赏
  • 举报
回复
const m_ulRGBMax = 0;

var
m_ulSampleDone:Longword;
m_ulRGBMax:integer;
pSamplePicture:^longword;
VideoLine,VideoCol:Longword;
VideoMode:Longword;
SamplePicture:array [0..VideoLine*VideoCol-1] of Longword; //Sampled data 如果很大的情况,可能堆栈溢出,你可以自己改成它原来哪种动态创建就行了

SFCG_GetVideoMode( VideoMode, m_uCardNum );
if( VideoMode<>0 ) then
bgein
VideoLine := 720;
VideoCol := 576;
end else
begin
VideoLine := 720;
VideoCol := 488;
end;

m_str:='';//这里要注意,delphi的string和vc的CString是不一样的,要注意处理
m_StartEndTime_str:='';
m_SpendTime_str:='';

pSamplePicture:=@(SamplePicture[0]);//Sampled data

if(not SFCG_SetVideoSample(m_uCardNum)) then//要看你函数返回是否bool型,否则应该写成 if(SFCG_SetVideoSample(m_uCardNum)=0) then
begin
application.MessageBox('Set video sample error!','Error',mb_iconError+mb_ok);
exit;
end;

while( not SFCG_GetIfVideoSampleDone( m_ulSampleDone, m_uCardNum ) ) do//同上,应该注意函数返回是否bool型
begin
SFCG_GetVideoSampleData( pSamplePicture, m_uCardNum );
end;

//............下面的作图部分今天没时间回答你了,你可以自己查查资料,不是很复杂,或我空了再给你翻译了,呵呵
AP 2008-04-07
  • 打赏
  • 举报
回复
一般视频采集卡有 delphi 接口的
UndefinedCoder 2008-04-06
  • 打赏
  • 举报
回复
2楼的,卖你妈B不?
gjw310 2008-04-03
  • 打赏
  • 举报
回复
太长了,清明起来试试

1,184

社区成员

发帖
与我相关
我的任务
社区描述
Delphi GAME,图形处理/多媒体
社区管理员
  • GAME,图形处理/多媒体社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧