Delphi利用avicap32.dll编程控制摄像头实现拍照、录制视频
项目需求:平板电脑(Windows系统)一维/二维码扫描功能;
需求分析:
需要扫描一维/二维码时,分两步实现。
第一步,avicap32.dll或者dspack技术实现静默打开摄像头拍照、保存BMP图片。
第二步,ZXing技术实现对一维/二维码的解析,最终获取到码值。
本篇是利用avicap32.dll技术实现拍照/录制视频的测试Demo。
请看代码:
1 unit uMain; 2 3 interface 4 5 uses 6 Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, 7 Dialogs, ExtCtrls, StdCtrls, RzPanel, RzButton; 8 9 type 10 TFrmMain = class(TForm) 11 gbScreen: TRzGroupBox; 12 gbOperation: TRzGroupBox; 13 btnStart: TRzBitBtn; 14 btnStop: TRzBitBtn; 15 btnSaveBMP: TRzBitBtn; 16 btnStartR: TRzBitBtn; 17 btnSaveAVI: TRzBitBtn; 18 pMain: TPanel; 19 procedure btnStartClick(Sender: TObject); 20 procedure FormClose(Sender: TObject; var Action: TCloseAction); 21 procedure btnStopClick(Sender: TObject); 22 procedure btnSaveBMPClick(Sender: TObject); 23 procedure btnStartRClick(Sender: TObject); 24 procedure btnSaveAVIClick(Sender: TObject); 25 private 26 { Private declarations } 27 hWndC: THandle; 28 public 29 { Public declarations } 30 end; 31 32 var 33 FrmMain: TFrmMain; 34 const WM_CAP_START = WM_USER; 35 const WM_CAP_STOP = WM_CAP_START + 68; 36 const WM_CAP_DRIVER_CONNECT = WM_CAP_START + 10; 37 const WM_CAP_DRIVER_DISCONNECT = WM_CAP_START + 11; 38 const WM_CAP_SAVEDIB = WM_CAP_START + 25; 39 const WM_CAP_GRAB_FRAME = WM_CAP_START + 60; 40 const WM_CAP_SEQUENCE = WM_CAP_START + 62; 41 const WM_CAP_FILE_SET_CAPTURE_FILEA = WM_CAP_START + 20; 42 const WM_CAP_SEQUENCE_NOFILE = WM_CAP_START + 63; 43 const WM_CAP_SET_OVERLAY = WM_CAP_START + 51; 44 const WM_CAP_SET_PREVIEW = WM_CAP_START + 50; 45 const WM_CAP_SET_CALLBACK_VIDEOSTREAM = WM_CAP_START + 6; 46 const WM_CAP_SET_CALLBACK_ERROR = WM_CAP_START + 2; 47 const WM_CAP_SET_CALLBACK_STATUSA = WM_CAP_START + 3; 48 const WM_CAP_SET_CALLBACK_FRAME = WM_CAP_START + 5; 49 const WM_CAP_SET_SCALE = WM_CAP_START + 53; 50 const WM_CAP_SET_PREVIEWRATE = WM_CAP_START + 52; 51 52 function capCreateCaptureWindowA(lpszWindowName: PCHAR; 53 dwStyle: longint; x: integer; y: integer; nWidth: integer; 54 nHeight: integer; ParentWin: HWND; nId: integer): HWND; 55 STDCALL EXTERNAL 'AVICAP32.DLL'; 56 57 implementation 58 59 {$R *.dfm} 60 61 procedure TFrmMain.btnStartClick(Sender: TObject); 62 begin 63 //开始拍摄 64 hWndC := capCreateCaptureWindowA('My Own Capture Window', WS_CHILD or WS_VISIBLE, pMain.Left, pMain.Top, pMain.Width, pMain.Height, FrmMain.Handle, 0); 65 if hWndC <> 0 then 66 begin 67 SendMessage(hWndC, WM_CAP_SET_CALLBACK_VIDEOSTREAM, 0, 0); 68 SendMessage(hWndC, WM_CAP_SET_CALLBACK_ERROR, 0, 0); 69 SendMessage(hWndC, WM_CAP_SET_CALLBACK_STATUSA, 0, 0); 70 SendMessage(hWndC, WM_CAP_DRIVER_CONNECT, 0, 0); 71 SendMessage(hWndC, WM_CAP_SET_SCALE, 1, 0); 72 SendMessage(hWndC, WM_CAP_SET_PREVIEWRATE, 66, 0); 73 SendMessage(hWndC, WM_CAP_SET_OVERLAY, 1, 0); 74 SendMessage(hWndC, WM_CAP_SET_PREVIEW, 1, 0); 75 end; 76 end; 77 78 procedure TFrmMain.FormClose(Sender: TObject; var Action: TCloseAction); 79 begin 80 //程序退出时,自动关闭摄像头 81 if hWndC <> 0 then 82 begin 83 SendMessage(hWndC, WM_CAP_DRIVER_DISCONNECT, 0, 0); 84 end; 85 end; 86 87 procedure TFrmMain.btnStopClick(Sender: TObject); 88 begin 89 //关闭摄像头 90 if hWndC <> 0 then 91 begin 92 SendMessage(hWndC, WM_CAP_DRIVER_DISCONNECT, 0, 0); 93 hWndC := 0; 94 end; 95 end; 96 97 procedure TFrmMain.btnSaveBMPClick(Sender: TObject); 98 begin 99 //保存BMP 100 if hWndC <> 0 then 101 begin 102 SendMessage(hWndC, WM_CAP_SAVEDIB, 0, longint(pchar('D:/1.bmp'))); 103 end; 104 end; 105 106 procedure TFrmMain.btnStartRClick(Sender: TObject); 107 begin 108 //开始录制AVI视频 109 if hWndC <> 0 then 110 begin 111 SendMessage(hWndC, WM_CAP_FILE_SET_CAPTURE_FILEA, 0, Longint(pchar('c:/test.avi'))); 112 SendMessage(hWndC, WM_CAP_SEQUENCE, 0, 0); 113 end; 114 end; 115 116 procedure TFrmMain.btnSaveAVIClick(Sender: TObject); 117 begin 118 //停止录制并保存AVI视频 119 if hWndC <> 0 then 120 begin 121 SendMessage(hWndC, WM_CAP_STOP, 0, 0); 122 end; 123 end; 124 125 end.
运行效果:
作者:Jeremy.Wu
出处:https://www.cnblogs.com/jeremywucnblog/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。