http://www.experts-exchange.com/Prog..._23446083.html
La question
Tags:
Delphi WebCam Capture Picture Windows Image Acquisition wiaaut
Using Microsoft Windows Image Acquisition Library v2.0 "wiaaut.dll"
the following code almost works, see code comments;
procedure TForm1.Button1Click(Sender: TObject);
var
Itm: Item;
Img: ImageFile; // Type = IDispatch & IUnKnown
begin
Itm := VideoPreview1.Device.ExecuteCommand(wiaCommandTakePicture);
Img := Itm.Transfer( wiaFormatBMP); // This line returns this error;
// [Error] Unit1.pas(292): Incompatible types: 'OleVariant' and 'IImageFile'
Img.SaveFile('c:\Temp\Temp.bmp'); // This is the final required step.
end;
--------------------------------------
La reponse
// Requires ActiveX in uses clause
function OleVarToIntf(OleVar: OleVariant; IID: TGUID; out Obj): HResult;
var lpVarArg: PVariantArg;
begin
// Clear outbound param
Pointer(Obj):=nil;
// Cast OLE variant to its true type
lpVarArg:=PVariantArg(@OleVar);
// Check for IDispatch
if (lpVarArg^.vt = VT_DISPATCH) and Assigned(lpVarArg^.dispVal) then
// QI for desired interface
result:=IDispatch(lpVarArg^.dispVal).QueryInterface(IID, Obj)
// Check for IUnknown
else if (lpVarArg^.vt = VT_UNKNOWN) and Assigned(lpVarArg^.unkVal) then
// QI for desired interface
result:=IUnknown(lpVarArg^.dispVal).QueryInterface(IID, Obj)
else
// No interface
result:=E_NOINTERFACE;
end;
procedure TForm1.Button1Click(Sender: TObject);
var Itm: Item;
Img: ImageFile;
begin
// Convert the OLE variant to the interface that it is holding
if (OleVarToIntf(VideoPreview1.Device.ExecuteCommand(wiaCommandTakePicture), Item, Itm) = S_OK) then
begin
// Don't have the tlb conversion for this library, but check to see what Transfer(...)
// returns. If it returns an ImageFile interface then nothing else needs to be done.
// If it returns OleVariant then you will need to cast the result as well, eg:
//
// if (OleVarToIntf(Itm.Transfer(wiaFormatBMP), ImageFile, Img) = S_OK) then
// begin
// Img.SaveFile('c:\temp\temp.bmp');
// end;
//
Img:=Itm.Transfer(wiaFormatBMP);
Img.SaveFile('c:\Temp\Temp.bmp');
end;
end;
Partager