本文提供工程師分享的一段完整的Delphi代碼,實現(xiàn)利用攝像頭進(jìn)行拍照的功能。注意需要TVideoCap控件支持。
procedure Tfrm1.Button2Click(Sender: TObject);
Var
jpeg: TJPEGImage;
bmp: TBitmap;
MyStm:TMemoryStream;
strfilename,strcard:String;
begin
inherited;
strfilename:=extractfilepath(Application.ExeName);
with DBEdit12.DataSource.DataSet do
begin
if not VideoCap1.SaveAsDIB then
begin
ShowMessage(‘拍攝照片失敗’);
Abort;
end;//
bmp:= TBitmap.Create;
try
bmp.LoadFromFile(strfilename ‘Capture.bmp’ );
MyStm:=TMemoryStream.Create;
jpeg:= TJPEGImage.Create;
try
jpeg.Assign( bmp );
jpeg.compress;
jpeg.SaveTofile( strfilename ‘Capture.jpg’);
strfilename:=strfilename ‘Capture.jpg’;
jpeg.LoadFromFile(strfilename);
jpeg.SaveToStream(MyStm);
MyStm.Position:=0;
OpenQry(query1,‘select count(1) as cnt from tb_photo where card_no=’‘’ strcard ‘’‘’);//先查找是否有此卡號
OpenQry(pubqry,‘select * from tb_photo where card_no=’‘’ strcard ‘’‘’);//打開卡號數(shù)據(jù)表
{OpenQry是自定義打開數(shù)據(jù)表的函數(shù)
OpenQry(qry1:TQuery,ssql:String);
begin
with qry1 do
beign
if active then
close;
sql.clear;
sql.add(ssql);
try
open
catch
on E: Exception do ErrorDialog(E.Message, E.HelpContext);
end;//try
end;
end;
}
with query1 do
begin
if pubqry.Fields[0].AsInteger 》0 then //先判斷此卡號的圖片是否存在,若存在,替換;否則,插入。
Edit
else
Insert;
FieldByName(‘card_no’).AsString :=strcard;
TBlobField(FieldByName(‘photo’)).LoadFromStream(MyStm); 特別注意:本站所有轉(zhuǎn)載文章言論不代表本站觀點,本站所提供的攝影照片,插畫,設(shè)計作品,如需使用,請與原作者聯(lián)系。
Post; //統(tǒng)一提交
end;//with
finally
MyStm.Free;
jpeg.free;
end; //try
finally
bmp.free;
end;//try
end;//with
end;
(審核編輯: 小丸子2)
分享