最近在使用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. Java中关键字static的使用

    static 关键字 1).static只能修饰成员变量或成员方法,所有非静态是对象相关的,所有静态是类相关的. 2)被static修饰的成员变量成员方法独立于该类的任何对象,它不依赖类的特定的实例, ...

  2. c++11 多线程依次打印ABC

    并发 练习代码 #include <thread> #include <vector> #include <mutex> #include <iostream ...

  3. Two Sum IV - Input is a BST LT653

    Given a Binary Search Tree and a target number, return true if there exist two elements in the BST s ...

  4. java连接数据库以及连接参数格式

    //链接数据库代码部分  下面具有连接的基本参数可以对照修改(参数存放在file下面的database.properties下面) //参数存放在file下面的database.properties下 ...

  5. Win 10 关闭了.net framework 3.5后再开启不成功

    背景: Win10 想要使用IETester来测试下页面的浏览器兼容性.在“控制面板”里关闭了.net framework 3.5,再重新开启. 问题: 在重新开启的时候,提示不成功.错误代码0x80 ...

  6. Git使用基础篇(zz)

    Git使用基础篇 您的评价:          收藏该经验       Git是一个分布式的版本控制工具,本篇文章从介绍Git开始,重点在于介绍Git的基本命令和使用技巧,让你尝试使用Git的同时,体 ...

  7. 数据库之mysql练习

    建表 部门表 #DROP IF EXISTS TABLE DEPT; CREATE TABLE DEPT( DEPTNO int PRIMARY KEY,##部门编号 DNAME VARCHAR(14 ...

  8. python入门之文件处理

    1.读取文件 f=open(file="C:\BiZhi\新建文本文档.txt",mode="r",encoding="utf-8") da ...

  9. 20155312 2016-2017-2 《Java程序设计》第八周学习总结

    20155312 2016-2017-2 <Java程序设计>第八周学习总结 课堂内容总结 学习模式 游乐园模式-荒野求生模式 学习方法 以代码为中心->遇到不会的类和方法(参数等) ...

  10. KM匹配板子

    /* gyt Live up to every day */ #include<cstdio> #include<cmath> #include<iostream> ...