再次求解C++代码转Delphi(继续100分)感谢各位大牛们

leandzgc 2012-08-22 04:00:21

void CDemoDlg::OnBnClickedBtnDetect()
{
int ret = 0;
unsigned char buffer[IDFP_IMG_WIDTH*IDFP_IMG_HEIGHT] = {0};
unsigned char bufferShow[IDFP_IMG_WIDTH*IDFP_IMG_HEIGHT] = {0};

LIVESCAN_BeginCapture(0);

// Timeout = 3000
if((ret = LIVESCAN_CaptureFPBmpData(0, buffer, 3000)) > 0)
{
for(int i = 0; i < IDFP_IMG_HEIGHT; i++)
{
memcpy(bufferShow+i*IDFP_IMG_WIDTH, buffer+(IDFP_IMG_HEIGHT-1-i)*IDFP_IMG_WIDTH, IDFP_IMG_WIDTH);
}
HBITMAP hb = BuildImage(bufferShow, IDFP_IMG_WIDTH, IDFP_IMG_HEIGHT);

GetDlgItem(IDC_STATIC_IMAGE)->SendMessage(STM_SETIMAGE, IMAGE_BITMAP, (LPARAM)hb);
DeleteObject(hb);
hb = NULL;

SetDlgItemText(IDC_EDT_INFO, "成功");
}
else
{
LIVESCAN_GetErrorInfo(ret, pszErrorInfo);
str.Format("失败, %s", pszErrorInfo);
SetDlgItemText(IDC_EDT_INFO, str);
}
LIVESCAN_EndCapture(0);
}


上面的代码我翻译过来是,不知道有没有错?


procedure TForm1.btn2Click(Sender: TObject);
var
ret, i: Integer;
buffer, bufferShow: PChar;
hb: HBITMAP;
begin
GetMem(buffer, IDFP_IMG_WIDTH * IDFP_IMG_HEIGHT);
GetMem(bufferShow, IDFP_IMG_WIDTH * IDFP_IMG_HEIGHT);
LIVESCAN_BeginCapture(0);

//timeout 3000;
ret := LIVESCAN_CaptureFPBmpData(0, buffer, 3000);
if ret > 0 then
begin
for i := 0 to IDFP_IMG_HEIGHT do
begin
CopyMemory(bufferShow + i * IDFP_IMG_WIDTH, buffer + (IDFP_IMG_HEIGHT - 1 - i) * IDFP_IMG_WIDTH, IDFP_IMG_WIDTH);
// memcpy(bufferShow+i*IDFP_IMG_WIDTH, buffer+(IDFP_IMG_HEIGHT-1-i)*IDFP_IMG_WIDTH, IDFP_IMG_WIDTH);
end;

hb := BuildImage(bufferShow, IDFP_IMG_WIDTH, IDFP_IMG_HEIGHT);
img1.Picture.Bitmap.Handle := hb;
// GetDlgItem(IDC_STATIC_IMAGE) - > SendMessage(STM_SETIMAGE, IMAGE_BITMAP, (LPARAM)hb);
DeleteObject(hb);
hb := NULL;
lst1.Items.Text := '成功';
end;
end;



主要还是请各位大牛们帮我把下面的代码翻译成Delphi的,谢谢!


HBITMAP BuildImage(BYTE *image, int width, int height)
{
static HBITMAP pic;
static CClientDC dc(NULL);

BYTE info[256 * 4 + 40], *colors;
BITMAPINFOHEADER *header = (BITMAPINFOHEADER*)info;

header -> biSize = 40;
header -> biHeight = height;
header -> biWidth = width;
header -> biPlanes = 1;
header -> biCompression = 0;
header -> biSizeImage = width * height;
header -> biXPelsPerMeter = 0;
header -> biYPelsPerMeter = 0;
header -> biClrImportant = 0;
header -> biBitCount = 8;
header -> biClrUsed = 256;

colors = info + 40;
for(int i = 0; i < 256; i++)
{
*colors++ = i;
*colors++ = i;
*colors++ = i;
*colors++ = 0;
}

if(pic)
DeleteObject(pic);

void *ptr;
// CClientDC dc(NULL);
pic = CreateDIBSection(dc.m_hDC, (BITMAPINFO*)info, DIB_RGB_COLORS, &ptr, NULL, 0);
memcpy(ptr, image, width * height);

return pic;
}



下面是我翻译过来的


function BuildImage(image: PChar; width, height: Integer): HBITMAP; stdcall;
var
pic: HBITMAP;
DC: HDC;
info: array of PByte;
colors: PByte;
header: PBITMAPINFOHEADER;
begin
SetLength(info, 256 * 4 + 40);
header := Pointer(info);
header.biSize := 40;
header.biHeight := height;
header.biWidth := width;
header.biPlanes := 1;
header.biCompression := 0;
header.biSizeImage := width * height;
header.biXPelsPerMeter := 0;
header.biYPelsPerMeter := 0;
header.biClrImportant := 0;
header.biBitCount := 8;
header.biClrUsed := 256;
colors := Pointer(info + 40);//在这里报错了,不知道怎么转换colors = info + 40;C++这样写的。


// for i := 0 to 256 do//还有这下面怎么翻译呢?看不懂啊....
// begin
// *colors++ = i;
// *colors++ = i;
// *colors++ = i;
// *colors++ = 0;
// end;

// if (pic)
// DeleteObject(pic);
//
// void * ptr;
//// CClientDC dc(NULL);
// pic = CreateDIBSection(dc.m_hDC, (BITMAPINFO * )info, DIB_RGB_COLORS, &ptr, NULL, 0);
// memcpy(ptr, image, width * height);//一直到这里都看不懂了...
//
result := pic;
end;
...全文
235 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
leandzgc 2012-08-28
  • 打赏
  • 举报
回复
搞定了,感谢,嘿嘿!
[Quote=引用 3 楼 的回复:]

Delphi(Pascal) code
function BuildImage(const image: PByte; width, height: Integer): HBITMAP;
var
i: Integer;
pic: HBITMAP;
dc: HDC;
ptr: Pointer;
info: array [0..(256 * 4 + 40) -1] of Byt……
[/Quote]
Hexpate 2012-08-27
  • 打赏
  • 举报
回复
function BuildImage(const image: PByte; width, height: Integer): HBITMAP;
var
i: Integer;
pic: HBITMAP;
dc: HDC;
ptr: Pointer;
info: array [0..(256 * 4 + 40) -1] of Byte;
header : PBitmapInfoHeader;
colors: PRGBQuad;
begin
header := @info[0];
header.biSize := 40;
header.biHeight := height;
header.biWidth := width;
header.biPlanes := 1;
header.biCompression := 0;
header.biSizeImage := width * height;
header.biXPelsPerMeter := 0;
header.biYPelsPerMeter := 0;
header.biClrImportant := 0;
header.biBitCount := 8;
header.biClrUsed := 256;

colors := @info[40];
for i := 0 to 256 - 1 do
begin
colors.rgbBlue := i;
colors.rgbGreen := i;
colors.rgbRed := i;
colors.rgbReserved := 0;
inc(colors);
end;
dc:= GetDC(0);
pic := CreateDIBSection(dc, PBITMAPINFO(@info[0])^, DIB_RGB_COLORS, ptr, 0, 0);
Move(image^, ptr^, width * height);
Result := pic;
end;


帮你翻译好了, 你先试试吧
Hexpate 2012-08-27
  • 打赏
  • 举报
回复
MemoryStream 如果是想增加头文件, 你开始的时候可以先写入Header 啊, 如果是最后才可以写入Header 那么你可以在之前先保留一个Header的大小, 一般用Position := Sizeof(Header)即可, 到最后的时候 ,再把position移动到开头写入Header
leandzgc 2012-08-27
  • 打赏
  • 举报
回复
好吧,我现在只要Delphi的增加bmp头文件的例子就行了。怎么把PChar里面的内容保存为一个bmp文件?或是一个TMemoryStream如何增加bmp头文件?

1,184

社区成员

发帖
与我相关
我的任务
社区描述
Delphi Windows SDK/API
社区管理员
  • Windows SDK/API社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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