写了一个DELPHI操作USB摄像头类分享给大家
2015-05-18 15:55
260 查看
最近在使用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.
相关文章推荐
- 给大家分享一个Delphi开发游戏的控件。
- 一个很震撼的桌面操作演示,拿出来和大家分享一下
- 阿庆SQL智能查询分析器,使用delphi开发的一个数据库查询分析管理工具.分享给大家
- C# PDF Page操作——设置页面切换按钮 C# 添加、读取Word脚注尾注 C#为什么不能像C/C++一样的支持函数只读传参 web 给大家分享一个好玩的东西,也许你那块就用的到
- 为大家分享一个 Ajax Loading —— spin.js
- 为大家分享一个 Ajax Loading —— spin.js(转)
- 太感动了,转载他人让大家也分享 一个妓女死了,学校为她降半旗!(无比崇敬)---希望朋友能转载一下!
- 原创:老司机为教老婆学Python,星期天用Python写了一个俄罗斯方块程序。现在分享给大家,供大家闲暇时娱乐。
- 一个外地人在郑州报考驾校、选择驾校的经历分享给大家。
- 跟大家分享一个非常精简的夏令时转换程序,程序是通用的可以在51、430、PIC、瑞萨系列的单片机上使用
- 今天写了一个webservice的loadrunner脚步和大家分享一下
- Android 给大家分享一个解决ViewPager和Scrollview滑动冲突的工具类
- 有段时间没有更新博客了,最近比较忙,今天和大家分享一个使用Swift实现的新特性功能吧
- 2010年3月4日参加由张应杭老师主讲的关于传统文化的培训-一个有益的PPT分享给大家
- 给大家分享一个在Android中获取验证码的倒数计时器,已经封装好了,直接引用就行。
- 分享一个win7 下串口转换USB的驱动程序
- 一个通用的操作数据库的方法,请大家指点、优化
- 前两天写了一个ini文件解析类,感觉还不错,和大家分享一下:)
- 好东西是要分享的,下了一个优秀的USB PC分析软件,不逊于Bus Hound
- 一个分析很透彻的strust2流程分享给大家