unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, OleCtrls, CamCaptureCtlLib_TLB; type TForm1 = class(TForm) CamCaptureCtl1: TCamCaptureCtl; Label1: TLabel; Label2: TLabel; ComboBox1: TComboBox; ComboBox2: TComboBox; Button1: TButton; Button2: TButton; Label3: TLabel; ComboBox3: TComboBox; Button3: TButton; GroupBox1: TGroupBox; RadioButton1: TRadioButton; RadioButton2: TRadioButton; RadioButton3: TRadioButton; procedure FormClose(Sender: TObject; var Action: TCloseAction); procedure FormShow(Sender: TObject); procedure Button1Click(Sender: TObject); procedure Button2Click(Sender: TObject); procedure ComboBox1Change(Sender: TObject); procedure ComboBox2Change(Sender: TObject); procedure ComboBox3Change(Sender: TObject); procedure Button3Click(Sender: TObject); procedure RadioButton1Click(Sender: TObject); procedure RadioButton2Click(Sender: TObject); procedure RadioButton3Click(Sender: TObject); private { Private declarations } public { Public declarations } end; var Form1: TForm1; implementation {$R *.dfm} procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction); begin CamCaptureCtl1.CloseDevice(); end; procedure TForm1.FormShow(Sender: TObject); var i,j,pos,tmpVal:integer; devCount,resCount:integer; devName,resStr :string; strs :TStrings; begin devCount:= CamCaptureCtl1.GetDeviceCount(); if devCount<=0 then Exit; if devCount>0 then begin for i:=0 to devCount-1 do begin devName:=CamCaptureCtl1.GetDeviceName(i); //showmessage( devName); ComboBox1.Items.Add(devName); end; if devCount>1 then begin ComboBox1.ItemIndex:=1; //默认打开副摄像头 end else begin ComboBox1.ItemIndex:=0; end; end; resCount:= CamCaptureCtl1.GetResolutionCount(ComboBox1.ItemIndex); if resCount<=0 then begin showmessage('获取分辨率信息异常!'); Exit; end; pos:=0; tmpVal:=0; for i:=0 to resCount-1 do begin resStr:=CamCaptureCtl1.GetResolution(i); ComboBox2.Items.Add(resStr); strs:= TStringList.Create; strs.Delimiter:='*'; strs.DelimitedText:=resStr; if StrToInt(strs[0])> tmpVal then //选出最大分辨率 begin tmpVal:= StrToInt(strs[0]); pos:=i; end; end; ComboBox2.ItemIndex:= pos; end; //打开设备 procedure TForm1.Button1Click(Sender: TObject); var iRest :integer; width,height: integer; strs :TStrings; begin if ComboBox2.Items.Count>0 then begin strs:= TStringList.Create; strs.Delimiter:='*'; strs.DelimitedText:=ComboBox2.Text; if strs.Count>0 then begin width:=StrToInt(strs[0]); height:= StrToInt(strs[1]); iRest:=CamCaptureCtl1.OpenDevice(ComboBox1.ItemIndex,width,height); if iRest<>0 then showmessage('打开设备失败!'); end; end; end; //关闭设备 procedure TForm1.Button2Click(Sender: TObject); begin CamCaptureCtl1.CloseDevice(); end; //切换摄像头 procedure TForm1.ComboBox1Change(Sender: TObject); var i,j,pos,tmpVal:integer; iRest :integer; resCount:integer; resStr :string; strs :TStrings; begin resCount:= CamCaptureCtl1.GetResolutionCount(ComboBox1.ItemIndex); if resCount<=0 then begin showmessage('获取分辨率信息异常!'); Exit; end; pos:=0; tmpVal:=0; for i:=0 to resCount-1 do begin resStr:=CamCaptureCtl1.GetResolution(i); ComboBox2.Items.Add(resStr); strs:= TStringList.Create; strs.Delimiter:='*'; strs.DelimitedText:=resStr; if StrToInt(strs[0])> tmpVal then //选出最大分辨率 begin tmpVal:= StrToInt(strs[0]); pos:=i; end; end; ComboBox2.ItemIndex:= pos; if ComboBox2.Items.Count>0 then begin strs:= TStringList.Create; strs.Delimiter:='*'; strs.DelimitedText:=ComboBox2.Text; if strs.Count>0 then begin width:=StrToInt(strs[0]); height:= StrToInt(strs[1]); iRest:=CamCaptureCtl1.OpenDevice(ComboBox1.ItemIndex,width,height); if iRest<>0 then showmessage('打开设备失败!'); end; end; end; //切换分辨率 procedure TForm1.ComboBox2Change(Sender: TObject); var i,j,pos,tmpVal:integer; iRest :integer; strs :TStrings; begin if ComboBox2.Items.Count>0 then begin strs:= TStringList.Create; strs.Delimiter:='*'; strs.DelimitedText:=ComboBox2.Text; if strs.Count>0 then begin width:=StrToInt(strs[0]); height:= StrToInt(strs[1]); iRest:=CamCaptureCtl1.OpenDevice(ComboBox1.ItemIndex,width,height); if iRest<>0 then showmessage('切换分辨率失败!'); end; //Form1.Width := 730; //Form1.Height := 563; end; end; //设置DPI procedure TForm1.ComboBox3Change(Sender: TObject); var val:integer; begin val:= StrToInt(ComboBox3.Text); //CamCaptureCtl1.SetDPI(val,val); end; //拍照 procedure TForm1.Button3Click(Sender: TObject); begin CamCaptureCtl1.CaptureImage('D:/test.jpg'); end; procedure TForm1.RadioButton1Click(Sender: TObject); begin if RadioButton1.Checked then begin CamCaptureCtl1.SetCutType(0); end; end; procedure TForm1.RadioButton2Click(Sender: TObject); begin if RadioButton2.Checked then begin CamCaptureCtl1.SetCutType(1); end; end; procedure TForm1.RadioButton3Click(Sender: TObject); begin if RadioButton3.Checked then begin CamCaptureCtl1.SetCutType(2); end; end; end.