一个DELPHI操作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 + ;
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摄像头类的更多相关文章
- 安卓 USB摄像头 开源库 UVCCamera 教程
https://github.com/saki4510t/UVCCamera UVCCamera 听名字就知道使用UVC( USB VEDIO CLASS) 协议的通用类库.linux原生支持,基本支 ...
- 自己封装的poi操作Excel工具类
自己封装的poi操作Excel工具类 在上一篇文章<使用poi读写Excel>中分享了一下poi操作Excel的简单示例,这次要分享一下我封装的一个Excel操作的工具类. 该工具类主要完 ...
- 25、写一个USB摄像头驱动程序(有ioctrl分析)
videobuf2-core.h中的vb2_buffer,记录了v4l2_buffer ,驱动可以对vb2_buffer的v4l2_buffer进行操控, vb2_buffer是v4l2框架层的代码, ...
- 摄像头调用代码 笔记本的话,本身有一个摄像头,由于用的usb摄像头,需要把笔记本的摄像头禁用后,再使用
摄像头调用代码 笔记本的话,本身有一个摄像头,由于用的usb摄像头,需要把笔记本的摄像头禁用后,再使用 <!DOCTYPE html> <html lang="en&quo ...
- Linux USB摄像头驱动【转】
本文转载自:http://www.itdadao.com/articles/c15a509940p0.html 在 cortex-a8 中,可接入摄像头的接口通常可以分为两种, CAMERA 接口和 ...
- Linux USB 摄像头驱动
在 cortex-a8 中,可接入摄像头的接口通常可以分为两种, CAMERA 接口和 USB 接口的摄像头.这一章主要是介绍 USB 摄像头的设备驱动程序.在我们印象中,驱动程序都是一个萝卜一个坑, ...
- Delphi操作XML简介
参考:http://www.delphifans.com/InfoView/Article_850.html Delphi 7支持对XML文档的操作,可以通过 TXMLDocument类来实现对XML ...
- USB 设备类协议入门【转】
本文转载自:http://www.cnblogs.com/xidongs/archive/2011/09/26/2191616.html 一.应用场合 USB HID类是比较大的一个类,HID类设备属 ...
- USB CDC类
现代嵌入式系统中,异步串行通信接口往往作为标准外设出现在单片机和嵌入式系统中.但是随着个人计算机通用外围设备越来越少地使用串口,串口正在逐渐从个人计算机特别是便携式电脑上消失.于是嵌入式开发人员常常发 ...
随机推荐
- MyBatis Generator中文文档
MyBatis Generator中文文档 MyBatis Generator中文文档地址: http://mbg.cndocs.tk/ 该中文文档由于尽可能和原文内容一致,所以有些地方如果不熟悉,看 ...
- [Robot Framework] Robot Framework里面的变量怎么知道是在哪里定义的?
看变量在哪里定义的:Ctrl+Alt+Space
- Spring IOC(七)类型推断
Spring IOC(七)类型推断 Spring 系列目录(https://www.cnblogs.com/binarylei/p/10198698.html) Spring 容器中可以根据 bean ...
- swift MD5 加密方法
引入OC类库 md5.h: #import <UIKit/UIKit.h> @interface Md5Controller : UIViewController @end md5.m: ...
- Reduce 和 Transduce 的含义
一.reduce 的用法 reduce是一种数组运算,通常用于将数组的所有成员"累积"为一个值. var arr = [1, 2, 3, 4]; var sum = (a, b) ...
- UVA 10405 Longest Common Subsequence
题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=16&p ...
- Ubuntu服务器如何搭建PPTPD(原创保证可用)
Ubuntu是一款基于linux的操作系统,无需许可和订购的费用,Ubuntu Server可以帮助您高效地扩展您的数据中心.它精简的架构和自动化部署的能力让您只需花费更少的运算能力和资源,便可提供更 ...
- git .gitignore未生效
添加进.gitignore的问题未生效. .gitignore只会忽略在.gitignore编写之后的未跟踪(untrack)文件,而在编写.gitignore之前已经add and commit的文 ...
- 41.App 框架的搭建思路以及代码的规范
本链接 引用别人文章https://www.jianshu.com/p/d553096914ff
- (18)What a planet needs to sustain life
https://www.ted.com/talks/dave_brain_what_a_planet_needs_to_sustain_life/transcript 00:12I'm really ...