function opencap(vc:TVideoCap):boolean;//设置摄像头的分辩率为640*480,返回摄像头序号
var
// CapParms: TCAPTUREPARMS;
CaptureHandle: THandle;
BmpInfo: TBitmapInfo;
n:integer;
begin
CaptureHandle := 0;
//定义视频输入格式
FillChar(BmpInfo.bmiHeader, SizeOf(TBitmapInfoHeader), 0);
with BmpInfo.bmiHeader do begin
biBitCount := 24;
biClrImportant := 0;
biClrUsed := 0;
biCompression := BI_RGB;
biHeight := 600;//分辨率
biPlanes := 1;
biSize := SizeOf(TBitmapInfoHeader);
biSizeImage := 0;
biWidth := 800;
biXPelsPerMeter := 0;
biYPelsPerMeter := 0;
end;
//创建一个AVICap捕获窗口
CaptureHandle := capCreateCaptureWindow('Capwin',WS_VISIBLE or WS_CHILD, 0, 1, 1, 0,Form1.Handle, 0);
if CaptureHandle = 0 then
begin
ShowMessage('创建窗口失败!');
Exit;
end;
for n:=0 to 5 do
if capDriverConnect(CaptureHandle, n) then //侦查摄像头,0代表第一个摄像头
Break
else
Continue;
Result:=true;
if n=6 then //不太可能同时连6个头
begin
showmessage(' 打开摄像头失败! ');
Result:=false;
exit
end;
capSetVideoFormat(CaptureHandle, @BmpInfo, SizeOf(BmpInfo)); //设置视频格式
capDriverDisconnect(CaptureHandle); //断开连接
DestroyWindow(CaptureHandle);
//vc.DriverOpen:=false;
vc.DriverName:=''; //可以指定名字
vc.DriverIndex:=n;
vc.DriverOpen:=true;
vc.VideoPreview:=true;
end;
procedure TForm1.Button5Click(Sender: TObject);
var
MyJPEG,jp1, jp2, jp : TJPEGImage;
tmp,BMP : TBitmap;
i:integer;
begin
if pz=1 then begin//连接设备
if not opencap(VideoCap1) then
begin
exit
end;
Panel1.SendToBack;
pz:=2;
tbutton(Sender).Caption:='拍照';
exit;
end;
if pz=3 then begin//重来//重启设备
pz:=2;
Panel1.SendToBack;
tbutton(Sender).Caption:='拍照';
exit;
end;
//拍照
pz:=3;
tbutton(Sender).Caption:='重来';
VideoCap1.GrabFrameNoStop;
VideoCap1.SingleImageFile:='fdfd.bmp';
if not VideoCap1.SaveAsDib then begin
showMessage('抓拍不成功!');
exit;
end;
//VideoCap1.DriverName:=''; //停止设备
//合并 ,要将bmp转换为jpg后才合并
VideoCap1.SaveToClipboard;
tmp := TBitmap.Create;
tmp.LoadFromFile('fdfd.bmp'); //刚才拍的图片
tmp.Canvas.Brush.style := bsclear; //透明底色
tmp.canvas.font.color:=clAqua; //颜色
tmp.Canvas.TextOut(5,5,datetimetostr(now));//加字
MyJPEG:=TJPEGImage.Create;
MyJPEG.Assign(tmp);
MyJPEG.CompressionQuality:=30; //压缩比
MyJPEG.Compress;
tmp.Free;
jP1 := TJPEGImage.Create;
jP1.Assign(MyJPEG); //刚才拍的图片
jp2 := TJPEGImage.Create;
if Image1.Picture.Graphic<>nil then jp2.Assign(Image1.Picture.Graphic);//画原来的图
BMP := TBitmap.Create;
bmp.Height := jp1.Height;
bmp.Width := jp1.Width + jp2.Width;//新尺寸
bmp.Canvas.Draw( 0,0, jp2);//加入新图
bmp.Canvas.Draw( jp2.Width,0, jp1);//加入新图
//压缩
MyJPEG.Assign(bmp);
MyJPEG.CompressionQuality:=30; //压缩比
MyJPEG.Compress;
bmp.Free;
if Image1.Picture.Graphic<>nil then Image1.Left:=0-Image1.Width;//便于显示新图片
Image1.Picture.Assign(MyJPEG);
//ADOTable1.Edit;
jp1.Free;
jp2.Free;
MyJPEG.Free;
DeleteFile('fdfd.bmp');
VideoCap1.SendToBack;
end;