usb摄像头类

最近在使用Usb摄像头做了个项目,其中写了一个操作usb摄像头类分享给大家

{}
{ }
{ 操作USB摄像头类 }
{ }
{ 作者:lqen }
{ 日期:2015.05.18 }
{ }
{
}

unit untUsbCamera;

interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ExtCtrls, jpeg;

const WM_CAP_START = WM_USER;
const WM_CAP_STOP = WM_CAP_START + 68;
const WM_CAP_DRIVER_CONNECT = WM_CAP_START + 10;
const WM_CAP_DRIVER_DISCONNECT = WM_CAP_START + 11;
const WM_CAP_SAVEDIB = WM_CAP_START + 25;
const WM_CAP_GRAB_FRAME = WM_CAP_START + 60;
const WM_CAP_SEQUENCE = WM_CAP_START + 62;
const WM_CAP_FILE_SET_CAPTURE_FILEA = WM_CAP_START + 20;
const WM_CAP_SEQUENCE_NOFILE = WM_CAP_START + 63;
const WM_CAP_SET_OVERLAY = WM_CAP_START + 51;
const WM_CAP_SET_PREVIEW = WM_CAP_START + 50;
const WM_CAP_SET_CALLBACK_VIDEOSTREAM = WM_CAP_START + 6;
const WM_CAP_SET_CALLBACK_ERROR = WM_CAP_START + 2;
const WM_CAP_SET_CALLBACK_STATUSA = WM_CAP_START + 3;
const WM_CAP_SET_CALLBACK_FRAME = WM_CAP_START + 5;
const WM_CAP_SET_SCALE = WM_CAP_START + 53;
const WM_CAP_SET_PREVIEWRATE = WM_CAP_START + 52;

const WM_CAP_DLG_VIDEODISPLAY = WM_CAP_START + 41; //打开视频格式设置对话框,选择数字视频的框架大小和视频图像的色深,以及捕获视频图像的压缩格式。

type
TUsbCamera = class
private
FPanel: TPanel;
hWndC: THandle; //定义捕捉窗句柄
FIsOpen: boolean;

function BmpToJpg(BmpPath: string): string;
function Image_FitBitmap(const Source, Dest: string; const x, y: integer): Boolean;

protected

public
constructor Create();
destructor Destroy; override;
function Play(Panel: TPanel): boolean;
function Stop: boolean;
function StartRecord(FileName: string): Boolean;
function StopRecord: Boolean;
function Capture(FileName: string): Boolean;
published
property IsOpen: boolean read FIsOpen write FIsOpen;
end;
function capCreateCaptureWindowA(lpszWindowName: PCHAR; dwStyle: longint; x: integer; y: integer; nWidth: integer; nHeight: integer; ParentWin: HWND; nId: integer): HWND; STDCALL EXTERNAL ‘AVICAP32.DLL’;
implementation

{ TUsbCamera }

function TUsbCamera.BmpToJpg(BmpPath: string): string;
var
Jpg: TJpegImage;
BMP: TBitMap;
begin
Result := ‘’;
BmpPath := Trim(BmpPath);
Jpg := TJpegImage.Create;
BMP := TBitmap.Create;
try
BMP.LoadFromFile(BmpPath);
Jpg.Assign(BMP);
Jpg.SaveToFile(Copy(BmpPath, 1, Length(BmpPath) - 3) + ‘jpg’);
Result := Copy(BmpPath, 1, Length(BmpPath) - 3) + ‘jpg’;
finally
BMP.Free;
Jpg.Free;
BMP := nil;
Jpg := nil;
end;
end;

function TUsbCamera.Image_FitBitmap(const Source, Dest: string; const x, y: integer): Boolean;
var
abmp, bbmp: tbitmap; //定义变量 abmp为源对象变量 bbmp为目的对象变量
begin
abmp := tbitmap.Create; //创建位图资源
bbmp := tbitmap.Create; //创建位图资源
try
abmp.LoadFromFile(Source); //载入源位图资源
bbmp.Width := x; //设置目的位图的宽
bbmp.Height := y; //设置目的位图的高
bbmp.PixelFormat := pfDevice; //设置位图格式为当前设备默认格式
SetStretchBltMode(bbmp.Canvas.Handle, COLORONCOLOR); //设置指位图拉伸模式
StretchBlt(bbmp.Canvas.Handle, 0, 0, bbmp.Width, bbmp.Height, abmp.Canvas.Handle, 0, 0, abmp.Width, abmp.Height, srccopy); //从源矩形中复制一个位图到目标矩形并适当压缩
bbmp.SaveToFile(Dest); //保存转换后的目的图片
finally
abmp.Free; //释放资源
bbmp.Free; //释放资源
end;
end;

function TUsbCamera.Capture(FileName: string): boolean;
begin
Result := False;
if hWndC <> 0 then
begin
ForceDirectories(ExtractFilePath(FileName));
if SendMessage(hWndC, WM_CAP_SAVEDIB, 0, longint(pchar(FileName))) <> 1 then exit; //截图
if FileExists(FileName) then
begin
Image_FitBitmap(FileName, FileName, 400, 400);
FileName := BmpToJpg(FileName);
Result := True;
end;
end;
end;

constructor TUsbCamera.Create();
begin
end;

destructor TUsbCamera.Destroy;
begin
Stop;

inherited;
end;

function TUsbCamera.Play(Panel: TPanel): boolean;
begin
Result := False;
FPanel := Panel;
//使用Tpanel控件来创建捕捉窗口
hWndC := CapCreateCaptureWindowA(‘My Own Capture Window’,
WS_CHILD or WS_VISIBLE, //窗口样式
0, //X坐标
0, //Y坐标
FPanel.Width, //窗口宽
FPanel.Height, //窗口高
FPanel.Handle, //窗口句柄
0); //一般为0
if hWndC <> 0 then
begin
if SendMessage(hWndC, WM_CAP_SET_CALLBACK_VIDEOSTREAM, 0, 0) <> 1 then exit;
//捕捉一个视频流
if SendMessage(hWndC, WM_CAP_SET_CALLBACK_ERROR, 0, 0) <> 1 then exit; //得到一个设备错误
if SendMessage(hWndC, WM_CAP_SET_CALLBACK_STATUSA, 0, 0) <> 1 then exit; //得到一个设备状态
if SendMessage(hWndC, WM_CAP_DRIVER_CONNECT, 0, 0) <> 1 then exit;
//将一个捕捉窗口与一个设备驱动相关联
if SendMessage(hWndC, WM_CAP_SET_SCALE, 1, 0) <> 1 then exit;
if SendMessage(hWndC, WM_CAP_SET_PREVIEWRATE, 66, 0) <> 1 then exit;
SendMessage(hWndC, WM_CAP_SET_OVERLAY, 1, 0);
if SendMessage(hWndC, WM_CAP_SET_PREVIEW, 1, 0) <> 1 then exit;
Result := True;
FIsOpen := True;
end;
end;

function TUsbCamera.StartRecord(FileName: string): Boolean;
begin
Result := False;
if hWndC <> 0 then
begin
SendMessage(hWndC, WM_CAP_FILE_SET_CAPTURE_FILEA, 0, Longint(pchar(FileName))); // 录成AVI
Result := SendMessage(hWndC, WM_CAP_SEQUENCE, 0, 0) = 1;
end;
end;

function TUsbCamera.StopRecord: Boolean;
begin
Result := False;
if hWndC <> 0 then Result := SendMessage(hWndC, WM_CAP_STOP, 0, 0) = 1;
end;

function TUsbCamera.Stop: boolean;
begin
Result := False;
if hWndC <> 0 then
begin
Result := SendMessage(hWndC, WM_CAP_DRIVER_DISCONNECT, 0, 0) = 1; //将捕捉窗同驱动器断开
FIsOpen := False;
end;
end;

end.

发布了20 篇原创文章 · 获赞 1 · 访问量 5435

猜你喜欢

转载自blog.csdn.net/yygyyygy1233/article/details/85258996