最近在使用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 + ;
const WM_CAP_DRIVER_CONNECT = WM_CAP_START + ;
const WM_CAP_DRIVER_DISCONNECT = WM_CAP_START + ;
const WM_CAP_SAVEDIB = WM_CAP_START + ;
const WM_CAP_GRAB_FRAME = WM_CAP_START + ;
const WM_CAP_SEQUENCE = WM_CAP_START + ;
const WM_CAP_FILE_SET_CAPTURE_FILEA = WM_CAP_START + ;
const WM_CAP_SEQUENCE_NOFILE = WM_CAP_START + ;
const WM_CAP_SET_OVERLAY = WM_CAP_START + ;
const WM_CAP_SET_PREVIEW = WM_CAP_START + ;
const WM_CAP_SET_CALLBACK_VIDEOSTREAM = WM_CAP_START + ;
const WM_CAP_SET_CALLBACK_ERROR = WM_CAP_START + ;
const WM_CAP_SET_CALLBACK_STATUSA = WM_CAP_START + ;
const WM_CAP_SET_CALLBACK_FRAME = WM_CAP_START + ;
const WM_CAP_SET_SCALE = WM_CAP_START + ;
const WM_CAP_SET_PREVIEWRATE = WM_CAP_START + ; const WM_CAP_DLG_VIDEODISPLAY = WM_CAP_START + ; //打开视频格式设置对话框,选择数字视频的框架大小和视频图像的色深,以及捕获视频图像的压缩格式。 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, , Length(BmpPath) - ) + 'jpg');
Result := Copy(BmpPath, , Length(BmpPath) - ) + '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, , , bbmp.Width, bbmp.Height, abmp.Canvas.Handle, , , 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 <> then
begin
ForceDirectories(ExtractFilePath(FileName));
if SendMessage(hWndC, WM_CAP_SAVEDIB, , longint(pchar(FileName))) <> then exit; //截图
if FileExists(FileName) then
begin
Image_FitBitmap(FileName, FileName, , );
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, //窗口样式
, //X坐标
, //Y坐标
FPanel.Width, //窗口宽
FPanel.Height, //窗口高
FPanel.Handle, //窗口句柄
); //一般为0
if hWndC <> then
begin
if SendMessage(hWndC, WM_CAP_SET_CALLBACK_VIDEOSTREAM, , ) <> then exit;
//捕捉一个视频流
if SendMessage(hWndC, WM_CAP_SET_CALLBACK_ERROR, , ) <> then exit; //得到一个设备错误
if SendMessage(hWndC, WM_CAP_SET_CALLBACK_STATUSA, , ) <> then exit; //得到一个设备状态
if SendMessage(hWndC, WM_CAP_DRIVER_CONNECT, , ) <> then exit;
//将一个捕捉窗口与一个设备驱动相关联
if SendMessage(hWndC, WM_CAP_SET_SCALE, , ) <> then exit;
if SendMessage(hWndC, WM_CAP_SET_PREVIEWRATE, , ) <> then exit;
SendMessage(hWndC, WM_CAP_SET_OVERLAY, , );
if SendMessage(hWndC, WM_CAP_SET_PREVIEW, , ) <> then exit;
Result := True;
FIsOpen := True;
end;
end; function TUsbCamera.StartRecord(FileName: string): Boolean;
begin
Result := False;
if hWndC <> then
begin
SendMessage(hWndC, WM_CAP_FILE_SET_CAPTURE_FILEA, , Longint(pchar(FileName))); // 录成AVI
Result := SendMessage(hWndC, WM_CAP_SEQUENCE, , ) = ;
end;
end; function TUsbCamera.StopRecord: Boolean;
begin
Result := False;
if hWndC <> then Result := SendMessage(hWndC, WM_CAP_STOP, , ) = ;
end; function TUsbCamera.Stop: boolean;
begin
Result := False;
if hWndC <> then
begin
Result := SendMessage(hWndC, WM_CAP_DRIVER_DISCONNECT, , ) = ; //将捕捉窗同驱动器断开
FIsOpen := False;
end;
end; end.

一个DELPHI操作USB摄像头类的更多相关文章

  1. 安卓 USB摄像头 开源库 UVCCamera 教程

    https://github.com/saki4510t/UVCCamera UVCCamera 听名字就知道使用UVC( USB VEDIO CLASS) 协议的通用类库.linux原生支持,基本支 ...

  2. 自己封装的poi操作Excel工具类

    自己封装的poi操作Excel工具类 在上一篇文章<使用poi读写Excel>中分享了一下poi操作Excel的简单示例,这次要分享一下我封装的一个Excel操作的工具类. 该工具类主要完 ...

  3. 25、写一个USB摄像头驱动程序(有ioctrl分析)

    videobuf2-core.h中的vb2_buffer,记录了v4l2_buffer ,驱动可以对vb2_buffer的v4l2_buffer进行操控, vb2_buffer是v4l2框架层的代码, ...

  4. 摄像头调用代码 笔记本的话,本身有一个摄像头,由于用的usb摄像头,需要把笔记本的摄像头禁用后,再使用

    摄像头调用代码 笔记本的话,本身有一个摄像头,由于用的usb摄像头,需要把笔记本的摄像头禁用后,再使用 <!DOCTYPE html> <html lang="en&quo ...

  5. Linux USB摄像头驱动【转】

    本文转载自:http://www.itdadao.com/articles/c15a509940p0.html 在 cortex-a8 中,可接入摄像头的接口通常可以分为两种, CAMERA 接口和 ...

  6. Linux USB 摄像头驱动

    在 cortex-a8 中,可接入摄像头的接口通常可以分为两种, CAMERA 接口和 USB 接口的摄像头.这一章主要是介绍 USB 摄像头的设备驱动程序.在我们印象中,驱动程序都是一个萝卜一个坑, ...

  7. Delphi操作XML简介

    参考:http://www.delphifans.com/InfoView/Article_850.html Delphi 7支持对XML文档的操作,可以通过 TXMLDocument类来实现对XML ...

  8. USB 设备类协议入门【转】

    本文转载自:http://www.cnblogs.com/xidongs/archive/2011/09/26/2191616.html 一.应用场合 USB HID类是比较大的一个类,HID类设备属 ...

  9. USB CDC类

    现代嵌入式系统中,异步串行通信接口往往作为标准外设出现在单片机和嵌入式系统中.但是随着个人计算机通用外围设备越来越少地使用串口,串口正在逐渐从个人计算机特别是便携式电脑上消失.于是嵌入式开发人员常常发 ...

随机推荐

  1. 11. pt-heartbeat

    pt-heartbeat [OPTIONS] [DSN] --update|--monitor|--check|--stop ------------------------------------- ...

  2. 05. pt-diskstats

    pt-diskstats --devices-regex=sda --interval=1 --iterations=3 --show-timestamps #ts device rd_s rd_av ...

  3. MySQL5.7的安装(CentOS 7 & Ubuntu 16.04)

    CentOS 通过 yum 安装MySQL5.7 Yum Repository 下载地址:https://dev.mysql.com/downloads/repo/yum/ 选择相应的版本进行下载:R ...

  4. JDK8集合类源码解析 - LinkedList

    linkedList主要要注意以下几点: 1构造器 2 添加add(E e) 3 获取get(int index) 4 删除 remove(E e),remove(int index) 5  判断对象 ...

  5. 上传文件 input file

    //-----前端文件------- form id="uploadForm" enctype="multipart/form-data"> <in ...

  6. 【WebService】调用第三方提供的webService服务(七)

    互联网上面有很多的免费webService服务,我们可以调用这些免费的WebService服务,将一些其他网站的内容信息集成到我们的Web应用中显示,下面就以获取天气预报数据和查询国内手机号码归属地为 ...

  7. 【WebService】WebService之CXF的拦截器(五)

    CXF拦截器介绍 CXF拦截器是功能的主要实现单元,也是主要的扩展点,可以在不对核心模块进行修改的情况下,动态添加功能.当服务被调用时,会经过多个拦截器链(Interceptor Chain)处理,拦 ...

  8. java测试ATM自助操作系统

    开学第一周系主任安排了一项测试,测试要求:模拟ATM自助取款机用文件进行存储账户信息,密码等,并进行存款取款,转账,查询记录等操作,而且要进行文件的读取与录入. 这是一个ATM自助取款的操作系统,进行 ...

  9. 2018.11.02 NOIP模拟 优美的序列(数论+单调栈/链表)

    传送门 考虑如果一个区间满足最小值等于最大公约数那么这个区间是合法的. 因此我们对于每一个点维护可以延展到的最左/右端点保证这一段区间的gcdgcdgcd等于这个点的值. 这个可以用之前同类的链表或者 ...

  10. ThinkPHP redirect 传参

    重定向带参 $this->redirect('pay/under_line_success',array('order_id'=>$stuInfo),5,'页面跳转中….'); 第一个参数 ...